Browse Source

Feature: support fd device

Experimental support for file descriptor based device, may be used like `fd://3`
pull/76/head
xjasonlyu 3 years ago
parent
commit
f6ba31f121
  1. 9
      core/device/fd/fd.go
  2. 2
      core/device/fd/open_linux.go
  3. 2
      core/device/fd/open_others.go
  4. 3
      engine/parse.go

9
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
}

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

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

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

Loading…
Cancel
Save