diff --git a/core/device/device.go b/core/device/device.go index 19e4f0a1..540d3f08 100644 --- a/core/device/device.go +++ b/core/device/device.go @@ -9,9 +9,6 @@ import ( type Device interface { stack.LinkEndpoint - // Close stops and closes the device. - Close() error - // Name returns the current name of the device. Name() string diff --git a/core/device/fdbased/fd_unix.go b/core/device/fdbased/fd_unix.go index 56f3bc80..b45d3595 100644 --- a/core/device/fdbased/fd_unix.go +++ b/core/device/fdbased/fd_unix.go @@ -1,4 +1,4 @@ -//go:build !windows +//go:build unix package fdbased @@ -40,8 +40,9 @@ func (f *FD) Name() string { return strconv.Itoa(f.fd) } -func (f *FD) Close() error { - return unix.Close(f.fd) +func (f *FD) Close() { + defer f.LinkEndpoint.Close() + _ = unix.Close(f.fd) } var _ device.Device = (*FD)(nil) diff --git a/core/device/fdbased/open_linux.go b/core/device/fdbased/open_linux.go index eccf6ca2..b733af46 100644 --- a/core/device/fdbased/open_linux.go +++ b/core/device/fdbased/open_linux.go @@ -1,5 +1,3 @@ -//go:build (linux && amd64) || (linux && arm64) - package fdbased import ( diff --git a/core/device/fdbased/open_others.go b/core/device/fdbased/open_unix.go similarity index 87% rename from core/device/fdbased/open_others.go rename to core/device/fdbased/open_unix.go index 1c1535fb..60958850 100644 --- a/core/device/fdbased/open_others.go +++ b/core/device/fdbased/open_unix.go @@ -1,4 +1,4 @@ -//go:build !(linux && amd64) && !(linux && arm64) && !windows +//go:build unix && !linux package fdbased diff --git a/core/device/tun/tun_netstack.go b/core/device/tun/tun_netstack.go index 08ce429b..5de27daf 100644 --- a/core/device/tun/tun_netstack.go +++ b/core/device/tun/tun_netstack.go @@ -1,4 +1,4 @@ -//go:build (linux && amd64) || (linux && arm64) +//go:build linux package tun @@ -6,8 +6,8 @@ import ( "fmt" "golang.org/x/sys/unix" + "gvisor.dev/gvisor/pkg/rawfile" "gvisor.dev/gvisor/pkg/tcpip/link/fdbased" - "gvisor.dev/gvisor/pkg/tcpip/link/rawfile" "gvisor.dev/gvisor/pkg/tcpip/link/tun" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -76,8 +76,9 @@ func (t *TUN) Name() string { return t.name } -func (t *TUN) Close() error { - return unix.Close(t.fd) +func (t *TUN) Close() { + defer t.LinkEndpoint.Close() + _ = unix.Close(t.fd) } func setMTU(name string, n uint32) error { diff --git a/core/device/tun/tun_wireguard.go b/core/device/tun/tun_wireguard.go index e6ae398c..7e49505e 100644 --- a/core/device/tun/tun_wireguard.go +++ b/core/device/tun/tun_wireguard.go @@ -1,4 +1,4 @@ -//go:build !(linux && amd64) && !(linux && arm64) +//go:build !linux package tun @@ -89,7 +89,7 @@ func (t *TUN) Name() string { return name } -func (t *TUN) Close() error { +func (t *TUN) Close() { defer t.Endpoint.Close() - return t.nt.Close() + _ = t.nt.Close() } diff --git a/core/device/tun/tun_wireguard_linux.go b/core/device/tun/tun_wireguard_linux.go deleted file mode 100644 index 45cc6e4f..00000000 --- a/core/device/tun/tun_wireguard_linux.go +++ /dev/null @@ -1,26 +0,0 @@ -//go:build linux && !(amd64 || arm64) - -package tun - -import ( - "fmt" - "os" - - "golang.zx2c4.com/wireguard/tun" - gun "gvisor.dev/gvisor/pkg/tcpip/link/tun" -) - -const ( - offset = 0 /* IFF_NO_PI */ - defaultMTU = 1500 -) - -func createTUN(name string, mtu int) (tun.Device, error) { - nfd, err := gun.Open(name) - if err != nil { - return nil, fmt.Errorf("create tun: %w", err) - } - - fd := os.NewFile(uintptr(nfd), "/dev/net/tun") - return tun.CreateTUNFromFile(fd, mtu) -} diff --git a/core/stack.go b/core/stack.go index 64e8b795..8c7eaa4f 100644 --- a/core/stack.go +++ b/core/stack.go @@ -3,7 +3,6 @@ package core import ( "net" - "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" "gvisor.dev/gvisor/pkg/tcpip/network/ipv6" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -55,7 +54,7 @@ func CreateStack(cfg *Config) (*stack.Stack, error) { }) // Generate unique NIC id. - nicID := tcpip.NICID(s.UniqueID()) + nicID := s.NextNICID() opts = append(opts, // Important: We must initiate transport protocol handlers diff --git a/engine/engine.go b/engine/engine.go index 52c2a938..efabc870 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -82,14 +82,14 @@ func start() error { func stop() (err error) { _engineMu.Lock() if _defaultDevice != nil { - err = _defaultDevice.Close() + _defaultDevice.Close() } if _defaultStack != nil { _defaultStack.Close() _defaultStack.Wait() } _engineMu.Unlock() - return err + return nil } func execCommand(cmd string) error { diff --git a/go.mod b/go.mod index bc18e77c..56ab338a 100644 --- a/go.mod +++ b/go.mod @@ -1,27 +1,29 @@ module github.com/xjasonlyu/tun2socks/v2 -go 1.22 +go 1.22.0 + +toolchain go1.22.5 require ( github.com/docker/go-units v0.5.0 - github.com/go-chi/chi/v5 v5.0.12 + github.com/go-chi/chi/v5 v5.1.0 github.com/go-chi/cors v1.2.1 github.com/go-chi/render v1.0.3 github.com/go-gost/relay v0.5.0 github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/google/uuid v1.6.0 github.com/gorilla/schema v1.4.1 - github.com/gorilla/websocket v1.5.1 + github.com/gorilla/websocket v1.5.3 github.com/sirupsen/logrus v1.9.3 - github.com/stretchr/testify v1.8.4 + github.com/stretchr/testify v1.9.0 go.uber.org/atomic v1.11.0 go.uber.org/automaxprocs v1.5.3 - golang.org/x/crypto v0.22.0 - golang.org/x/sys v0.19.0 + golang.org/x/crypto v0.25.0 + golang.org/x/sys v0.22.0 golang.org/x/time v0.5.0 golang.zx2c4.com/wireguard v0.0.0-20231211153847-12269c276173 gopkg.in/yaml.v3 v3.0.1 - gvisor.dev/gvisor v0.0.0-20240405191320-0878b34101b5 + gvisor.dev/gvisor v0.0.0-20240713103206-39d6c232e61d ) require ( @@ -30,6 +32,6 @@ require ( github.com/google/btree v1.1.2 // indirect github.com/kr/text v0.2.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/net v0.24.0 // indirect + golang.org/x/net v0.27.0 // indirect golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect ) diff --git a/go.sum b/go.sum index 95e27fdb..f60ab0d1 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= -github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s= -github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= +github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4= @@ -22,8 +22,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/schema v1.4.1 h1:jUg5hUjCSDZpNGLuXQOgIWGdlgrIdYvgQ0wZtdK1M3E= github.com/gorilla/schema v1.4.1/go.mod h1:Dg5SSm5PV60mhF2NFaTV1xuYYj8tV8NOPRo4FggUMnM= -github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= -github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -36,19 +36,19 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8= go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0= -golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30= -golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M= -golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= -golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= +golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= +golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= -golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 h1:B82qJJgjvYKsXS9jeunTOisW56dUokqW/FOteYJJ/yg= @@ -61,5 +61,5 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gvisor.dev/gvisor v0.0.0-20240405191320-0878b34101b5 h1:DOUDfNS+CFMM46k18FRF5k/0yz5NhZYMiUQxf4xglIU= -gvisor.dev/gvisor v0.0.0-20240405191320-0878b34101b5/go.mod h1:NQHVAzMwvZ+Qe3ElSiHmq9RUm1MdNHpUZ52fiEqvn+0= +gvisor.dev/gvisor v0.0.0-20240713103206-39d6c232e61d h1:dFTIljP/5ReqgM7nMR4DauApFatUaSP8r9btX0sd8a8= +gvisor.dev/gvisor v0.0.0-20240713103206-39d6c232e61d/go.mod h1:sxc3Uvk/vHcd3tj7/DHVBoR5wvWT/MmRq2pj7HRJnwU=