Feature: support fd device

Experimental support for file descriptor based device, may be used like `fd://3`
This commit is contained in:
xjasonlyu
2021-10-17 14:21:37 +08:00
parent bf3f4599cc
commit f6ba31f121
4 changed files with 14 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
package fd package fd
import ( import (
"fmt"
"strconv" "strconv"
"github.com/xjasonlyu/tun2socks/core/device" "github.com/xjasonlyu/tun2socks/core/device"
@@ -17,6 +18,14 @@ type FD struct {
mtu uint32 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 { func (f *FD) Type() string {
return Driver return Driver
} }

View File

@@ -7,7 +7,7 @@ import (
"gvisor.dev/gvisor/pkg/tcpip/link/fdbased" "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} f := &FD{fd: fd, mtu: mtu}
ep, err := fdbased.New(&fdbased.Options{ ep, err := fdbased.New(&fdbased.Options{

View File

@@ -10,7 +10,7 @@ import (
"github.com/xjasonlyu/tun2socks/core/device/rwbased" "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} f := &FD{fd: fd, mtu: mtu}
ep, err := rwbased.New(os.NewFile(uintptr(fd), f.Name()), mtu) ep, err := rwbased.New(os.NewFile(uintptr(fd), f.Name()), mtu)

View File

@@ -7,6 +7,7 @@ import (
"strings" "strings"
"github.com/xjasonlyu/tun2socks/core/device" "github.com/xjasonlyu/tun2socks/core/device"
"github.com/xjasonlyu/tun2socks/core/device/fd"
"github.com/xjasonlyu/tun2socks/core/device/tun" "github.com/xjasonlyu/tun2socks/core/device/tun"
"github.com/xjasonlyu/tun2socks/proxy" "github.com/xjasonlyu/tun2socks/proxy"
"github.com/xjasonlyu/tun2socks/proxy/proto" "github.com/xjasonlyu/tun2socks/proxy/proto"
@@ -28,6 +29,8 @@ func parseDevice(s string, mtu uint32) (device.Device, error) {
switch driver { switch driver {
case tun.Driver: case tun.Driver:
return tun.Open(name, mtu) return tun.Open(name, mtu)
case fd.Driver:
return fd.Open(name, mtu)
default: default:
return nil, fmt.Errorf("unsupported driver: %s", driver) return nil, fmt.Errorf("unsupported driver: %s", driver)
} }