diff --git a/main.go b/main.go index 6f56904..03d57ae 100644 --- a/main.go +++ b/main.go @@ -37,13 +37,14 @@ var ( type CmdArgs struct { // Main - Version *bool - TunName *string - TunAddr *string - TunGw *string - TunMask *string - TunDNS *string - LogLevel *string + Version *bool + TunName *string + TunAddr *string + TunGw *string + TunMask *string + TunDNS *string + TunPersist *bool + LogLevel *string // Proxy ProxyServer *string @@ -74,6 +75,7 @@ func init() { args.TunGw = flag.String("tunGw", "240.0.0.1", "TUN interface gateway") args.TunMask = flag.String("tunMask", "255.255.255.0", "TUN interface netmask") args.TunDNS = flag.String("tunDNS", "8.8.8.8,8.8.4.4", "DNS resolvers for TUN interface (Windows Only)") + args.TunPersist = flag.Bool("tunPersist", false, "Persist TUN interface after the program exits or the last open file descriptor is closed (Linux only)") // Proxy args.ProxyServer = flag.String("proxyServer", "", "Proxy server address") @@ -128,7 +130,7 @@ func main() { // Open the tun device dnsServers := strings.Split(*args.TunDNS, ",") - tunDev, err := tun.OpenTunDevice(*args.TunName, *args.TunAddr, *args.TunGw, *args.TunMask, dnsServers) + tunDev, err := tun.OpenTunDevice(*args.TunName, *args.TunAddr, *args.TunGw, *args.TunMask, dnsServers, *args.TunPersist) if err != nil { log.Fatalf("failed to open tun device: %v", err) } diff --git a/tun/tun_darwin.go b/tun/tun_darwin.go index d4cf969..2a9827b 100644 --- a/tun/tun_darwin.go +++ b/tun/tun_darwin.go @@ -30,7 +30,7 @@ func isIPv6(ip net.IP) bool { return false } -func OpenTunDevice(name, addr, gw, mask string, dnsServers []string) (io.ReadWriteCloser, error) { +func OpenTunDevice(name, addr, gw, mask string, dnsServers []string, persist bool) (io.ReadWriteCloser, error) { _ = dnsServers tunDev, err := water.New(water.Config{ DeviceType: water.TUN, diff --git a/tun/tun_linux.go b/tun/tun_linux.go index a2e64b6..9154395 100644 --- a/tun/tun_linux.go +++ b/tun/tun_linux.go @@ -8,11 +8,12 @@ import ( "github.com/songgao/water" ) -func OpenTunDevice(name, addr, gw, mask string, dnsServers []string) (io.ReadWriteCloser, error) { +func OpenTunDevice(name, addr, gw, mask string, dnsServers []string, persist bool) (io.ReadWriteCloser, error) { cfg := water.Config{ DeviceType: water.TUN, } cfg.Name = name + cfg.Persist = persist tunDev, err := water.New(cfg) if err != nil { return nil, err diff --git a/tun/tun_windows.go b/tun/tun_windows.go index 0e5f43b..97d640b 100644 --- a/tun/tun_windows.go +++ b/tun/tun_windows.go @@ -157,7 +157,7 @@ func getTuntapComponentId(ifaceName string) (string, string, error) { return "", "", errors.New("not found component id") } -func OpenTunDevice(name, addr, gw, mask string, dns []string) (io.ReadWriteCloser, error) { +func OpenTunDevice(name, addr, gw, mask string, dns []string, persist bool) (io.ReadWriteCloser, error) { componentId, devName, err := getTuntapComponentId(name) if err != nil { return nil, fmt.Errorf("failed to get component ID: %v", err)