Browse Source

Linux: add option to persist TUN device

pull/15/head
Jason 5 years ago
parent
commit
43a90e00d3
  1. 18
      main.go
  2. 2
      tun/tun_darwin.go
  3. 3
      tun/tun_linux.go
  4. 2
      tun/tun_windows.go

18
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)
}

2
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,

3
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

2
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)

Loading…
Cancel
Save