Files
tun2socks/tun/tun_linux.go
2019-11-06 10:45:07 +08:00

28 lines
484 B
Go

package tun
import (
"errors"
"io"
"net"
"github.com/songgao/water"
)
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
}
name = tunDev.Name()
ip := net.ParseIP(addr)
if ip == nil {
return nil, errors.New("invalid IP address")
}
return tunDev, nil
}