diff --git a/core/device/fd/fd.go b/core/device/fd/fd.go index 4d983a7..631c671 100644 --- a/core/device/fd/fd.go +++ b/core/device/fd/fd.go @@ -1,6 +1,7 @@ package fd import ( + "fmt" "strconv" "github.com/xjasonlyu/tun2socks/core/device" @@ -17,6 +18,14 @@ type FD struct { mtu uint32 } +func Open(name string, mtu uint32) (device.Device, error) { + fd, err := strconv.Atoi(name) + if err != nil { + return nil, fmt.Errorf("cannot open fd: %s", name) + } + return open(fd, mtu) +} + func (f *FD) Type() string { return Driver } diff --git a/core/device/fd/open_linux.go b/core/device/fd/open_linux.go index d14d657..b8455e5 100644 --- a/core/device/fd/open_linux.go +++ b/core/device/fd/open_linux.go @@ -7,7 +7,7 @@ import ( "gvisor.dev/gvisor/pkg/tcpip/link/fdbased" ) -func Open(fd int, mtu uint32) (device.Device, error) { +func open(fd int, mtu uint32) (device.Device, error) { f := &FD{fd: fd, mtu: mtu} ep, err := fdbased.New(&fdbased.Options{ diff --git a/core/device/fd/open_others.go b/core/device/fd/open_others.go index c865030..0bd8e24 100644 --- a/core/device/fd/open_others.go +++ b/core/device/fd/open_others.go @@ -10,7 +10,7 @@ import ( "github.com/xjasonlyu/tun2socks/core/device/rwbased" ) -func Open(fd int, mtu uint32) (device.Device, error) { +func open(fd int, mtu uint32) (device.Device, error) { f := &FD{fd: fd, mtu: mtu} ep, err := rwbased.New(os.NewFile(uintptr(fd), f.Name()), mtu) diff --git a/engine/parse.go b/engine/parse.go index bef45ae..cb33b5d 100644 --- a/engine/parse.go +++ b/engine/parse.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/xjasonlyu/tun2socks/core/device" + "github.com/xjasonlyu/tun2socks/core/device/fd" "github.com/xjasonlyu/tun2socks/core/device/tun" "github.com/xjasonlyu/tun2socks/proxy" "github.com/xjasonlyu/tun2socks/proxy/proto" @@ -28,6 +29,8 @@ func parseDevice(s string, mtu uint32) (device.Device, error) { switch driver { case tun.Driver: return tun.Open(name, mtu) + case fd.Driver: + return fd.Open(name, mtu) default: return nil, fmt.Errorf("unsupported driver: %s", driver) }