Skip to content

Commit 0cdb0ee

Browse files
committed
Add create tun from file descriptor
1 parent 8cb63db commit 0cdb0ee

File tree

8 files changed

+66
-48
lines changed

8 files changed

+66
-48
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
fmt:
22
@gofumpt -l -w .
33
@gofmt -s -w .
4-
@gci write -s "standard,prefix(github.com/sagernet/),default" .
4+
@gci write --custom-order -s "standard,prefix(github.com/sagernet/),default" .
55

66
fmt_install:
77
go install -v mvdan.cc/gofumpt@latest
8-
go install -v github.com/daixiang0/gci@v0.4.0
8+
go install -v github.com/daixiang0/gci@latest
99

1010
lint:
1111
GOOS=linux golangci-lint run .

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ require (
66
github.com/fsnotify/fsnotify v1.6.0
77
github.com/sagernet/go-tun2socks v1.16.12-0.20220818015926-16cb67876a61
88
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97
9-
github.com/sagernet/sing v0.1.2
10-
golang.org/x/net v0.4.0
11-
golang.org/x/sys v0.3.0
9+
github.com/sagernet/sing v0.1.7
10+
golang.org/x/net v0.7.0
11+
golang.org/x/sys v0.5.0
1212
gvisor.dev/gvisor v0.0.0-20220901235040-6ca97ef2ce1c
1313
)
1414

go.sum

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@ github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4
22
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
33
github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4=
44
github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA=
5-
github.com/sagernet/abx-go v0.0.0-20220819185957-dba1257d738e h1:5CFRo8FJbCuf5s/eTBdZpmMbn8Fe2eSMLNAYfKanA34=
6-
github.com/sagernet/abx-go v0.0.0-20220819185957-dba1257d738e/go.mod h1:qbt0dWObotCfcjAJJ9AxtFPNSDUfZF+6dCpgKEOBn/g=
75
github.com/sagernet/go-tun2socks v1.16.12-0.20220818015926-16cb67876a61 h1:5+m7c6AkmAylhauulqN/c5dnh8/KssrE9c93TQrXldA=
86
github.com/sagernet/go-tun2socks v1.16.12-0.20220818015926-16cb67876a61/go.mod h1:QUQ4RRHD6hGGHdFMEtR8T2P6GS6R3D/CXKdaYHKKXms=
97
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 h1:iL5gZI3uFp0X6EslacyapiRz7LLSJyr4RajF/BhMVyE=
108
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
119
github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
12-
github.com/sagernet/sing v0.1.2 h1:rp5AqY23P0klk2IaLEI0/WJsD8FTVlv9TaI2QSL6TDA=
13-
github.com/sagernet/sing v0.1.2/go.mod h1:bvmen56QnVbMrWy+nr5nsbz7U5MUPuY0L0S/XfhCsTs=
10+
github.com/sagernet/sing v0.1.7 h1:g4vjr3q8SUlBZSx97Emz5OBfSMBxxW5Q8C2PfdoSo08=
11+
github.com/sagernet/sing v0.1.7/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk=
1412
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 h1:gga7acRE695APm9hlsSMoOoE65U4/TcqNj90mc69Rlg=
1513
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
16-
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
17-
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
14+
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
15+
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
1816
golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1917
golang.org/x/sys v0.0.0-20220731174439-a90be440212d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2018
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
21-
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
22-
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
19+
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
20+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2321
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs=
2422
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
2523
gvisor.dev/gvisor v0.0.0-20220901235040-6ca97ef2ce1c h1:m5lcgWnL3OElQNVyp3qcncItJ2c0sQlSGjYK2+nJTA4=

tun.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type Options struct {
4646
ExcludePackage []string
4747
InterfaceMonitor DefaultInterfaceMonitor
4848
TableIndex int
49+
FileDescriptor int
4950
}
5051

5152
func CalculateInterfaceName(name string) (tunName string) {

tun_darwin.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,29 @@ type NativeTun struct {
2828
inet6Address string
2929
}
3030

31-
func Open(options Options) (Tun, error) {
32-
ifIndex := -1
33-
_, err := fmt.Sscanf(options.Name, "utun%d", &ifIndex)
34-
if err != nil {
35-
return nil, E.New("bad tun name: ", options.Name)
36-
}
31+
func New(options Options) (Tun, error) {
32+
var tunFd int
33+
if options.FileDescriptor == 0 {
34+
ifIndex := -1
35+
_, err := fmt.Sscanf(options.Name, "utun%d", &ifIndex)
36+
if err != nil {
37+
return nil, E.New("bad tun name: ", options.Name)
38+
}
3739

38-
tunFd, err := unix.Socket(unix.AF_SYSTEM, unix.SOCK_DGRAM, 2)
39-
if err != nil {
40-
return nil, err
41-
}
40+
tunFd, err = unix.Socket(unix.AF_SYSTEM, unix.SOCK_DGRAM, 2)
41+
if err != nil {
42+
return nil, err
43+
}
4244

43-
err = configure(tunFd, ifIndex, options.Name, options)
44-
if err != nil {
45-
unix.Close(tunFd)
46-
return nil, err
45+
err = configure(tunFd, ifIndex, options.Name, options)
46+
if err != nil {
47+
unix.Close(tunFd)
48+
return nil, err
49+
}
50+
} else {
51+
tunFd = options.FileDescriptor
4752
}
53+
4854
nativeTun := &NativeTun{
4955
tunFile: os.NewFile(uintptr(tunFd), "utun"),
5056
mtu: options.MTU,

tun_linux.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,36 @@ type NativeTun struct {
2525
ruleIndex6 []int
2626
}
2727

28-
func Open(options Options) (Tun, error) {
29-
tunFd, err := open(options.Name)
30-
if err != nil {
31-
return nil, err
32-
}
33-
tunLink, err := netlink.LinkByName(options.Name)
34-
if err != nil {
35-
return nil, E.Errors(err, unix.Close(tunFd))
36-
}
37-
nativeTun := &NativeTun{
38-
tunFd: tunFd,
39-
tunFile: os.NewFile(uintptr(tunFd), "tun"),
40-
options: options,
41-
}
42-
runtime.SetFinalizer(nativeTun.tunFile, nil)
43-
err = nativeTun.configure(tunLink)
44-
if err != nil {
45-
return nil, E.Errors(err, unix.Close(tunFd))
28+
func New(options Options) (Tun, error) {
29+
if options.FileDescriptor == 0 {
30+
tunFd, err := open(options.Name)
31+
if err != nil {
32+
return nil, err
33+
}
34+
tunLink, err := netlink.LinkByName(options.Name)
35+
if err != nil {
36+
return nil, E.Errors(err, unix.Close(tunFd))
37+
}
38+
nativeTun := &NativeTun{
39+
tunFd: tunFd,
40+
tunFile: os.NewFile(uintptr(tunFd), "tun"),
41+
options: options,
42+
}
43+
runtime.SetFinalizer(nativeTun.tunFile, nil)
44+
err = nativeTun.configure(tunLink)
45+
if err != nil {
46+
return nil, E.Errors(err, unix.Close(tunFd))
47+
}
48+
return nativeTun, nil
49+
} else {
50+
nativeTun := &NativeTun{
51+
tunFd: options.FileDescriptor,
52+
tunFile: os.NewFile(uintptr(options.FileDescriptor), "tun"),
53+
options: options,
54+
}
55+
runtime.SetFinalizer(nativeTun.tunFile, nil)
56+
return nativeTun, nil
4657
}
47-
return nativeTun, nil
4858
}
4959

5060
func (t *NativeTun) Read(p []byte) (n int, err error) {

tun_other.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import (
66
"os"
77
)
88

9-
func Open(config Options) (Tun, error) {
9+
func New(config Options) (Tun, error) {
1010
return nil, os.ErrInvalid
1111
}

tun_windows.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ type NativeTun struct {
3636
fwpmSession uintptr
3737
}
3838

39-
func Open(options Options) (WinTun, error) {
39+
func New(options Options) (WinTun, error) {
40+
if options.FileDescriptor != 0 {
41+
return nil, os.ErrInvalid
42+
}
4043
adapter, err := wintun.CreateAdapter(options.Name, TunnelType, generateGUIDByDeviceName(options.Name))
4144
if err != nil {
4245
return nil, err

0 commit comments

Comments
 (0)