mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-07 01:33:15 +08:00
Feature: support fd device
Experimental support for file descriptor based device, may be used like `fd://3`
This commit is contained in:
@@ -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
|
||||||
}
|
}
|
||||||
|
@@ -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{
|
||||||
|
@@ -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)
|
||||||
|
@@ -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)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user