Commit 7eea9b3a authored by ginuerzh's avatar ginuerzh

tuntap: code refactor

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