Commit 7eea9b3a authored by ginuerzh's avatar ginuerzh

tuntap: code refactor

parent 47d5b72b
......@@ -395,17 +395,23 @@ func (r *route) GenRouters() ([]router, error) {
case "ohttp":
ln, err = gost.ObfsHTTPListener(node.Addr)
case "tun":
ln, err = gost.TunListener(node.Addr,
gost.TunListenConfig{
TCP: node.GetBool("tcp"),
RemoteAddr: node.Remote,
})
cfg := gost.TunConfig{
Name: node.Get("name"),
Addr: node.Get("net"),
MTU: node.GetInt("mtu"),
Routes: strings.Split(node.Get("route"), ","),
Gateway: node.Get("gw"),
}
ln, err = gost.TunListener(cfg)
case "tap":
ln, err = gost.TapListener(node.Addr,
gost.TapListenConfig{
TCP: node.GetBool("tcp"),
RemoteAddr: node.Remote,
})
cfg := gost.TapConfig{
Name: node.Get("name"),
Addr: node.Get("net"),
MTU: node.GetInt("mtu"),
Routes: strings.Split(node.Get("route"), ","),
Gateway: node.Get("gw"),
}
ln, err = gost.TapListener(cfg)
default:
ln, err = gost.TCPListener(node.Addr)
}
......@@ -444,23 +450,9 @@ func (r *route) GenRouters() ([]router, error) {
case "sni":
handler = gost.SNIHandler()
case "tun":
cfg := gost.TunConfig{
Name: node.Get("name"),
Addr: node.Get("net"),
MTU: node.GetInt("mtu"),
Routes: strings.Split(node.Get("route"), ","),
Gateway: node.Get("gw"),
}
handler = gost.TunHandler(node.Remote, gost.TunConfigHandlerOption(cfg))
handler = gost.TunHandler()
case "tap":
cfg := gost.TapConfig{
Name: node.Get("name"),
Addr: node.Get("net"),
MTU: node.GetInt("mtu"),
Routes: strings.Split(node.Get("route"), ","),
Gateway: node.Get("gw"),
}
handler = gost.TapHandler(node.Remote, gost.TapConfigHandlerOption(cfg))
handler = gost.TapHandler()
default:
// start from 2.5, if remote is not empty, then we assume that it is a forward tunnel.
if node.Remote != "" {
......@@ -507,6 +499,7 @@ func (r *route) GenRouters() ([]router, error) {
gost.KnockingHandlerOption(node.Get("knock")),
gost.NodeHandlerOption(node),
gost.IPsHandlerOption(ips),
gost.TCPModeHandlerOption(node.GetBool("tcp")),
)
rt := router{
......
......@@ -40,8 +40,7 @@ type HandlerOptions struct {
Node Node
Host string
IPs []string
TunConfig TunConfig
TapConfig TapConfig
TCPMode bool
}
// HandlerOption allows a common way to set handler options.
......@@ -197,17 +196,10 @@ func IPsHandlerOption(ips []string) HandlerOption {
}
}
// TunConfigHandlerOption sets the config for tun device.
func TunConfigHandlerOption(cfg TunConfig) HandlerOption {
// TCPModeHandlerOption sets the tcp mode for tun/tap device.
func TCPModeHandlerOption(b bool) HandlerOption {
return func(opts *HandlerOptions) {
opts.TunConfig = cfg
}
}
// TapConfigHandlerOption sets the config for tap device.
func TapConfigHandlerOption(cfg TapConfig) HandlerOption {
return func(opts *HandlerOptions) {
opts.TapConfig = cfg
opts.TCPMode = b
}
}
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment