From ed74dff7584d2518416ece985e39beee3dcdd13e Mon Sep 17 00:00:00 2001 From: arm64v8a <48624112+arm64v8a@users.noreply.github.com> Date: Sat, 11 Feb 2023 20:32:26 +0900 Subject: [PATCH] libcore: rm gvisor --- libcore/go.mod | 5 - libcore/go.sum | 6 - libcore/tun/gvisor/dispatchers.go | 162 -------------- libcore/tun/gvisor/endpoint.go | 136 ------------ libcore/tun/gvisor/errors.generated.go | 17 -- libcore/tun/gvisor/gvisor.go | 150 ------------- libcore/tun/gvisor/tcp.go | 56 ----- libcore/tun/gvisor/udp.go | 143 ------------- libcore/tun/system/errors.generated.go | 17 -- libcore/tun/system/nat.go | 133 ------------ libcore/tun/system/peer.go | 15 -- libcore/tun/system/tcp.go | 200 ------------------ libcore/tun/system/udp.go | 157 -------------- .../tuns/{tuns_android.go => tuns_linux.go} | 11 +- libcore/tun/tuns/tuns_stub.go | 2 +- 15 files changed, 6 insertions(+), 1204 deletions(-) delete mode 100644 libcore/tun/gvisor/dispatchers.go delete mode 100755 libcore/tun/gvisor/endpoint.go delete mode 100644 libcore/tun/gvisor/errors.generated.go delete mode 100644 libcore/tun/gvisor/gvisor.go delete mode 100644 libcore/tun/gvisor/tcp.go delete mode 100644 libcore/tun/gvisor/udp.go delete mode 100644 libcore/tun/system/errors.generated.go delete mode 100644 libcore/tun/system/nat.go delete mode 100644 libcore/tun/system/peer.go delete mode 100644 libcore/tun/system/tcp.go delete mode 100644 libcore/tun/system/udp.go rename libcore/tun/tuns/{tuns_android.go => tuns_linux.go} (64%) diff --git a/libcore/go.mod b/libcore/go.mod index 7fb286b6a..f7a07078b 100644 --- a/libcore/go.mod +++ b/libcore/go.mod @@ -12,11 +12,8 @@ require ( go.uber.org/automaxprocs v1.4.0 golang.org/x/mobile v0.0.0-20220722155234-aaac322e2105 golang.org/x/sys v0.3.0 - gvisor.dev/gvisor v0.0.0 ) -replace gvisor.dev/gvisor => github.com/sagernet/gvisor v0.0.0-20220402114650-763d12dc953e - replace github.com/v2fly/v2ray-core/v5 v5.0.0 => ../../v2ray-core require ( @@ -29,7 +26,6 @@ require ( github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect github.com/golang/mock v1.6.0 // indirect github.com/golang/protobuf v1.5.2 // indirect - github.com/google/btree v1.0.1 // indirect github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/jhump/protoreflect v1.14.0 // indirect @@ -55,7 +51,6 @@ require ( golang.org/x/mod v0.6.0 // indirect golang.org/x/net v0.4.0 // indirect golang.org/x/text v0.5.0 // indirect - golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect golang.org/x/tools v0.2.0 // indirect google.golang.org/genproto v0.0.0-20211223182754-3ac035c7e7cb // indirect google.golang.org/grpc v1.51.0 // indirect diff --git a/libcore/go.sum b/libcore/go.sum index 44db3bba3..870d048de 100644 --- a/libcore/go.sum +++ b/libcore/go.sum @@ -103,8 +103,6 @@ github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= -github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4= -github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= @@ -238,8 +236,6 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= -github.com/sagernet/gvisor v0.0.0-20220402114650-763d12dc953e h1:Y4avBAtZ59OWvLl6zP9sF62jtMEVRPIH78IQctq9aXQ= -github.com/sagernet/gvisor v0.0.0-20220402114650-763d12dc953e/go.mod h1:tWwEcFvJavs154OdjFCw78axNrsDlz4Zh8jvPqwcpGI= github.com/sagernet/libping v0.1.1 h1:uNMN/02fQmRbsgJ0EuxuM/Upq8FrVP4Xj2+LlYviIOs= github.com/sagernet/libping v0.1.1/go.mod h1:FhmyYM8L32JaKI08noqoS5cK+Gw/Q+4VDnI9WvP6Sp8= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= @@ -418,8 +414,6 @@ golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 h1:GZokNIeuVkl3aZHJchRrr13WCsols02MLUcz1U9is6M= -golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/libcore/tun/gvisor/dispatchers.go b/libcore/tun/gvisor/dispatchers.go deleted file mode 100644 index 87ca7ca2f..000000000 --- a/libcore/tun/gvisor/dispatchers.go +++ /dev/null @@ -1,162 +0,0 @@ -package gvisor - -import ( - "fmt" - - "golang.org/x/sys/unix" - "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" - "gvisor.dev/gvisor/pkg/tcpip/header" - "gvisor.dev/gvisor/pkg/tcpip/link/rawfile" - "gvisor.dev/gvisor/pkg/tcpip/stack" -) - -// bufConfig defines the shape of the vectorised view used to read packets from the NIC. -var bufConfig = []int{128, 256, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768} - -type iovecBuffer struct { - // views are the actual buffers that hold the packet contents. - views []buffer.View - - // iovecs are initialized with base pointers/len of the corresponding - // entries in the views defined above, except when GSO is enabled - // (skipsVnetHdr) then the first iovec points to a buffer for the vnet header - // which is stripped before the views are passed up the stack for further - // processing. - iovecs []unix.Iovec - - // sizes is an array of buffer sizes for the underlying views. sizes is - // immutable. - sizes []int -} - -func newIovecBuffer(sizes []int) *iovecBuffer { - b := &iovecBuffer{ - views: make([]buffer.View, len(sizes)), - sizes: sizes, - } - b.iovecs = make([]unix.Iovec, len(b.views)) - return b -} - -func (b *iovecBuffer) nextIovecs() []unix.Iovec { - vnetHdrOff := 0 - for i := range b.views { - if b.views[i] != nil { - break - } - v := buffer.NewView(b.sizes[i]) - b.views[i] = v - b.iovecs[i+vnetHdrOff] = unix.Iovec{Base: &v[0]} - b.iovecs[i+vnetHdrOff].SetLen(len(v)) - } - return b.iovecs -} - -func (b *iovecBuffer) pullViews(n int) buffer.VectorisedView { - var views []buffer.View - c := 0 - for i, v := range b.views { - c += len(v) - if c >= n { - b.views[i].CapLength(len(v) - (c - n)) - views = append([]buffer.View(nil), b.views[:i+1]...) - break - } - } - // Remove the first len(views) used views from the state. - for i := range views { - b.views[i] = nil - } - return buffer.NewVectorisedView(n, views) -} - -// stopFd is an eventfd used to signal the stop of a dispatcher. -type stopFd struct { - efd int -} - -func newStopFd() (stopFd, error) { - efd, err := unix.Eventfd(0, unix.EFD_NONBLOCK) - if err != nil { - return stopFd{efd: -1}, fmt.Errorf("failed to create eventfd: %w", err) - } - return stopFd{efd: efd}, nil -} - -// stop writes to the eventfd and notifies the dispatcher to stop. It does not -// block. -func (s *stopFd) stop() { - increment := []byte{1, 0, 0, 0, 0, 0, 0, 0} - if n, err := unix.Write(s.efd, increment); n != len(increment) || err != nil { - // There are two possible errors documented in eventfd(2) for writing: - // 1. We are writing 8 bytes and not 0xffffffffffffff, thus no EINVAL. - // 2. stop is only supposed to be called once, it can't reach the limit, - // thus no EAGAIN. - panic(fmt.Sprintf("write(efd) = (%d, %s), want (%d, nil)", n, err, len(increment))) - } -} - -// readVDispatcher uses readv() system call to read inbound packets and -// dispatches them. -type readVDispatcher struct { - stopFd - // fd is the file descriptor used to send and receive packets. - fd int - - // e is the endpoint this dispatcher is attached to. - e *rwEndpoint - - // buf is the iovec buffer that contains the packet contents. - buf *iovecBuffer -} - -func newReadVDispatcher(fd int, e *rwEndpoint) (*readVDispatcher, error) { - stopFd, err := newStopFd() - if err != nil { - return nil, err - } - d := &readVDispatcher{ - stopFd: stopFd, - fd: fd, - e: e, - } - d.buf = newIovecBuffer(bufConfig) - return d, nil -} - -// dispatch reads one packet from the file descriptor and dispatches it. -func (d *readVDispatcher) dispatch() (bool, tcpip.Error) { - n, err := rawfile.BlockingReadvUntilStopped(d.efd, d.fd, d.buf.nextIovecs()) - if n <= 0 || err != nil { - return false, err - } - - pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: d.buf.pullViews(n), - IsForwardedPacket: true, - }) - defer pkt.DecRef() - - var p tcpip.NetworkProtocolNumber - - // We don't get any indication of what the packet is, so try to guess - // if it's an IPv4 or IPv6 packet. - // IP version information is at the first octet, so pulling up 1 byte. - h, ok := pkt.Data().PullUp(1) - if !ok { - return true, nil - } - switch header.IPVersion(h) { - case header.IPv4Version: - p = header.IPv4ProtocolNumber - case header.IPv6Version: - p = header.IPv6ProtocolNumber - default: - return true, nil - } - - d.e.dispatcher.DeliverNetworkPacket(p, pkt) - - return true, nil -} diff --git a/libcore/tun/gvisor/endpoint.go b/libcore/tun/gvisor/endpoint.go deleted file mode 100755 index 116819782..000000000 --- a/libcore/tun/gvisor/endpoint.go +++ /dev/null @@ -1,136 +0,0 @@ -package gvisor - -import ( - "sync" - - "golang.org/x/sys/unix" - "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/header" - "gvisor.dev/gvisor/pkg/tcpip/link/rawfile" - "gvisor.dev/gvisor/pkg/tcpip/stack" -) - -var _ stack.InjectableLinkEndpoint = (*rwEndpoint)(nil) - -// rwEndpoint implements the interface of stack.LinkEndpoint from io.ReadWriter. -type rwEndpoint struct { - fd int - - // mtu (maximum transmission unit) is the maximum size of a packet. - mtu uint32 - wg sync.WaitGroup - - inbound *readVDispatcher - dispatcher stack.NetworkDispatcher -} - -func newRwEndpoint(dev int32, mtu int32) (*rwEndpoint, error) { - e := &rwEndpoint{ - fd: int(dev), - mtu: uint32(mtu), - } - i, err := newReadVDispatcher(e.fd, e) - if err != nil { - return nil, err - } - e.inbound = i - return e, nil -} - -func (e *rwEndpoint) InjectInbound(networkProtocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) { - go e.dispatcher.DeliverNetworkPacket(networkProtocol, pkt) -} - -func (e *rwEndpoint) InjectOutbound(dest tcpip.Address, packet []byte) tcpip.Error { - return rawfile.NonBlockingWrite(e.fd, packet) -} - -// Attach launches the goroutine that reads packets from io.ReadWriter and -// dispatches them via the provided dispatcher. -func (e *rwEndpoint) Attach(dispatcher stack.NetworkDispatcher) { - if dispatcher == nil && e.dispatcher != nil { - e.inbound.stop() - e.Wait() - e.dispatcher = nil - return - } - if dispatcher != nil && e.dispatcher == nil { - e.dispatcher = dispatcher - e.wg.Add(1) - go func() { - e.dispatchLoop(e.inbound) - e.wg.Done() - }() - } -} - -// IsAttached implements stack.LinkEndpoint.IsAttached. -func (e *rwEndpoint) IsAttached() bool { - return e.dispatcher != nil -} - -// dispatchLoop reads packets from the file descriptor in a loop and dispatches -// them to the network stack. -func (e *rwEndpoint) dispatchLoop(inboundDispatcher *readVDispatcher) tcpip.Error { - for { - cont, err := inboundDispatcher.dispatch() - if err != nil || !cont { - return err - } - } -} - -// WritePackets writes packets back into io.ReadWriter. -func (e *rwEndpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) { - // Preallocate to avoid repeated reallocation as we append to batch. - // batchSz is 47 because when SWGSO is in use then a single 65KB TCP - // segment can get split into 46 segments of 1420 bytes and a single 216 - // byte segment. - const batchSz = 47 - batch := make([]unix.Iovec, 0, batchSz) - for pkt := pkts.Front(); pkt != nil; pkt = pkt.Next() { - views := pkt.Views() - for _, v := range views { - batch = rawfile.AppendIovecFromBytes(batch, v, len(views)) - } - } - err := rawfile.NonBlockingWriteIovec(e.fd, batch) - if err != nil { - return 0, err - } - return pkts.Len(), nil -} - -// MTU implements stack.LinkEndpoint.MTU. -func (e *rwEndpoint) MTU() uint32 { - return e.mtu -} - -// Capabilities implements stack.LinkEndpoint.Capabilities. -func (e *rwEndpoint) Capabilities() stack.LinkEndpointCapabilities { - return stack.CapabilityNone -} - -// MaxHeaderLength returns the maximum size of the link layer header. Given it -// doesn't have a header, it just returns 0. -func (*rwEndpoint) MaxHeaderLength() uint16 { - return 0 -} - -// LinkAddress returns the link address of this endpoint. -func (*rwEndpoint) LinkAddress() tcpip.LinkAddress { - return "" -} - -// ARPHardwareType implements stack.LinkEndpoint.ARPHardwareType. -func (*rwEndpoint) ARPHardwareType() header.ARPHardwareType { - return header.ARPHardwareNone -} - -func (e *rwEndpoint) AddHeader(*stack.PacketBuffer) { -} - -// Wait implements stack.LinkEndpoint.Wait. -func (e *rwEndpoint) Wait() { - e.wg.Wait() -} diff --git a/libcore/tun/gvisor/errors.generated.go b/libcore/tun/gvisor/errors.generated.go deleted file mode 100644 index b8246e1e8..000000000 --- a/libcore/tun/gvisor/errors.generated.go +++ /dev/null @@ -1,17 +0,0 @@ -package gvisor - -import ( - "fmt" - - "github.com/v2fly/v2ray-core/v5/common/errors" -) - -type errPathObjHolder struct{} - -func newError(values ...interface{}) *errors.Error { - return errors.New(values...).WithPathObj(errPathObjHolder{}) -} - -func newErrorf(format string, a ...interface{}) *errors.Error { - return errors.New(fmt.Sprintf(format, a)).WithPathObj(errPathObjHolder{}) -} diff --git a/libcore/tun/gvisor/gvisor.go b/libcore/tun/gvisor/gvisor.go deleted file mode 100644 index 00c12a9e8..000000000 --- a/libcore/tun/gvisor/gvisor.go +++ /dev/null @@ -1,150 +0,0 @@ -package gvisor - -import ( - "errors" - "io" - "os" - - "libcore/comm" - "libcore/tun" - - "github.com/sirupsen/logrus" - "github.com/v2fly/v2ray-core/v5/common/buf" - "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/header" - "gvisor.dev/gvisor/pkg/tcpip/link/sniffer" - "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" - "gvisor.dev/gvisor/pkg/tcpip/network/ipv6" - "gvisor.dev/gvisor/pkg/tcpip/stack" - "gvisor.dev/gvisor/pkg/tcpip/transport/icmp" - "gvisor.dev/gvisor/pkg/tcpip/transport/tcp" - "gvisor.dev/gvisor/pkg/tcpip/transport/udp" -) - -//go:generate go run ../errorgen - -var _ tun.Tun = (*GVisor)(nil) - -type GVisor struct { - Endpoint stack.LinkEndpoint - PcapFile *os.File - Stack *stack.Stack -} - -func (t *GVisor) Stop() { - t.Stack.Close() - if t.PcapFile != nil { - _ = t.PcapFile.Close() - } -} - -const DefaultNIC tcpip.NICID = 0x01 - -func New(dev int32, mtu int32, handler tun.Handler, nicId tcpip.NICID, pcap bool, pcapFile *os.File, snapLen uint32, ipv6Mode int32) (*GVisor, error) { - var endpoint stack.LinkEndpoint - endpoint, _ = newRwEndpoint(dev, mtu) - if pcap { - pcapEndpoint, err := sniffer.NewWithWriter(endpoint, &pcapFileWrapper{pcapFile}, snapLen) - if err != nil { - return nil, err - } - endpoint = pcapEndpoint - } - var o stack.Options - switch ipv6Mode { - case comm.IPv6Disable: - o = stack.Options{ - NetworkProtocols: []stack.NetworkProtocolFactory{ - ipv4.NewProtocol, - }, - TransportProtocols: []stack.TransportProtocolFactory{ - tcp.NewProtocol, - udp.NewProtocol, - icmp.NewProtocol4, - }, - } - case comm.IPv6Only: - o = stack.Options{ - NetworkProtocols: []stack.NetworkProtocolFactory{ - ipv6.NewProtocol, - }, - TransportProtocols: []stack.TransportProtocolFactory{ - tcp.NewProtocol, - udp.NewProtocol, - icmp.NewProtocol6, - }, - } - default: - o = stack.Options{ - NetworkProtocols: []stack.NetworkProtocolFactory{ - ipv4.NewProtocol, - ipv6.NewProtocol, - }, - TransportProtocols: []stack.TransportProtocolFactory{ - tcp.NewProtocol, - udp.NewProtocol, - icmp.NewProtocol4, - icmp.NewProtocol6, - }, - } - } - s := stack.New(o) - s.SetRouteTable([]tcpip.Route{ - { - Destination: header.IPv4EmptySubnet, - NIC: nicId, - }, - { - Destination: header.IPv6EmptySubnet, - NIC: nicId, - }, - }) - - bufSize := buf.Size - s.SetTransportProtocolOption(tcp.ProtocolNumber, &tcpip.TCPReceiveBufferSizeRangeOption{ - Min: 1, - Default: bufSize, - Max: bufSize, - }) - s.SetTransportProtocolOption(tcp.ProtocolNumber, &tcpip.TCPSendBufferSizeRangeOption{ - Min: 1, - Default: bufSize, - Max: bufSize, - }) - - sOpt := tcpip.TCPSACKEnabled(true) - s.SetTransportProtocolOption(tcp.ProtocolNumber, &sOpt) - - mOpt := tcpip.TCPModerateReceiveBufferOption(true) - s.SetTransportProtocolOption(tcp.ProtocolNumber, &mOpt) - - gTcpHandler(s, handler) - gUdpHandler(s, handler) - gMust(s.CreateNIC(nicId, endpoint)) - gMust(s.SetSpoofing(nicId, true)) - gMust(s.SetPromiscuousMode(nicId, true)) - - return &GVisor{endpoint, pcapFile, s}, nil -} - -type pcapFileWrapper struct { - io.Writer -} - -func (w *pcapFileWrapper) Write(p []byte) (n int, err error) { - n, err = w.Writer.Write(p) - if err != nil { - logrus.Debug("write pcap file failed: ", err) - } - return n, nil -} - -func gMust(err tcpip.Error) { - if err != nil { - logrus.Panicln(err.String()) - } -} - -func tcpipErr(err tcpip.Error) error { - return errors.New(err.String()) -} diff --git a/libcore/tun/gvisor/tcp.go b/libcore/tun/gvisor/tcp.go deleted file mode 100644 index a41404b75..000000000 --- a/libcore/tun/gvisor/tcp.go +++ /dev/null @@ -1,56 +0,0 @@ -package gvisor - -import ( - "fmt" - "net" - "strconv" - "time" - - v2rayNet "github.com/v2fly/v2ray-core/v5/common/net" - "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/adapters/gonet" - "gvisor.dev/gvisor/pkg/tcpip/stack" - "gvisor.dev/gvisor/pkg/tcpip/transport/tcp" - "gvisor.dev/gvisor/pkg/waiter" - "libcore/tun" -) - -func gTcpHandler(s *stack.Stack, handler tun.Handler) { - forwarder := tcp.NewForwarder(s, 0, 1024, func(request *tcp.ForwarderRequest) { - id := request.ID() - waitQueue := new(waiter.Queue) - endpoint, errT := request.CreateEndpoint(waitQueue) - if errT != nil { - newError("failed to create TCP connection").Base(tcpipErr(errT)).WriteToLog() - // prevent potential half-open TCP connection leak. - request.Complete(true) - return - } - request.Complete(false) - srcAddr := net.JoinHostPort(id.RemoteAddress.String(), strconv.Itoa(int(id.RemotePort))) - src, err := v2rayNet.ParseDestination(fmt.Sprint("tcp:", srcAddr)) - if err != nil { - newError("[TCP] parse source address ", srcAddr, " failed: ", err).AtWarning().WriteToLog() - return - } - dstAddr := net.JoinHostPort(id.LocalAddress.String(), strconv.Itoa(int(id.LocalPort))) - dst, err := v2rayNet.ParseDestination(fmt.Sprint("tcp:", dstAddr)) - if err != nil { - newError("[TCP] parse destination address ", dstAddr, " failed: ", err).AtWarning().WriteToLog() - return - } - go handler.NewConnection(src, dst, gTcpConn{endpoint, gonet.NewTCPConn(waitQueue, endpoint)}) - }) - s.SetTransportProtocolHandler(tcp.ProtocolNumber, forwarder.HandlePacket) -} - -type gTcpConn struct { - ep tcpip.Endpoint - *gonet.TCPConn -} - -func (g gTcpConn) Close() error { - g.ep.Close() - g.TCPConn.SetDeadline(time.Now().Add(-1)) - return g.TCPConn.Close() -} diff --git a/libcore/tun/gvisor/udp.go b/libcore/tun/gvisor/udp.go deleted file mode 100644 index b1f93a360..000000000 --- a/libcore/tun/gvisor/udp.go +++ /dev/null @@ -1,143 +0,0 @@ -package gvisor - -import ( - "fmt" - "net" - - "libcore/tun" - - "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" - "gvisor.dev/gvisor/pkg/tcpip/header" - "gvisor.dev/gvisor/pkg/tcpip/stack" - "gvisor.dev/gvisor/pkg/tcpip/transport/udp" -) - -func gUdpHandler(s *stack.Stack, handler tun.Handler) { - s.SetTransportProtocolHandler(udp.ProtocolNumber, func(id stack.TransportEndpointID, buffer *stack.PacketBuffer) bool { - // Ref: gVisor pkg/tcpip/transport/udp/endpoint.go HandlePacket - udpHdr := header.UDP(buffer.TransportHeader().View()) - if int(udpHdr.Length()) > buffer.Data().Size()+header.UDPMinimumSize { - // Malformed packet. - return true - } - - srcAddr := &net.UDPAddr{ - IP: net.ParseIP(id.RemoteAddress.String()), - Port: int(id.RemotePort), - } - dstAddr := &net.UDPAddr{ - IP: net.ParseIP(id.LocalAddress.String()), - Port: int(id.LocalPort), - } - - data := buffer.Data().ExtractVV() - packet := &gUdpPacket{ - s: s, - id: &id, - nicID: buffer.NICID, - netHdr: buffer.Network(), - netProto: buffer.NetworkProtocolNumber, - } - - handler.HandlePacket(&tun.UDPPacket{ - Src: srcAddr, - Dst: dstAddr, - Data: data.ToView(), - Put: nil, // DecRef by dispatcher - WriteBack: func(bytes []byte, addr *net.UDPAddr) (int, error) { - return packet.WriteBack(bytes, addr) - }, - }) - return true - }) -} - -type gUdpPacket struct { - s *stack.Stack - id *stack.TransportEndpointID - nicID tcpip.NICID - netHdr header.Network - netProto tcpip.NetworkProtocolNumber -} - -func (p *gUdpPacket) WriteBack(b []byte, addr *net.UDPAddr) (int, error) { - v := buffer.View(b) - if len(v) > header.UDPMaximumPacketSize { - // Payload can't possibly fit in a packet. - return 0, fmt.Errorf("%s", &tcpip.ErrMessageTooLong{}) - } - - var ( - localAddress tcpip.Address - localPort uint16 - ) - - if addr == nil { - localAddress = p.netHdr.DestinationAddress() - localPort = p.id.LocalPort - } else { - localAddress = tcpip.Address(addr.IP) - localPort = uint16(addr.Port) - } - - route, err := p.s.FindRoute(p.nicID, localAddress, p.netHdr.SourceAddress(), p.netProto, false /* multicastLoop */) - if err != nil { - return 0, fmt.Errorf("%#v find route: %s", p.id, err) - } - defer route.Release() - - data := v.ToVectorisedView() - if err = gSendUDP(route, data, localPort, p.id.RemotePort); err != nil { - return 0, fmt.Errorf("%v", err) - } - return data.Size(), nil -} - -// gSendUDP sends a UDP segment via the provided network endpoint and under the -// provided identity. -func gSendUDP(r *stack.Route, data buffer.VectorisedView, localPort, remotePort uint16) tcpip.Error { - pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - ReserveHeaderBytes: header.UDPMinimumSize + int(r.MaxHeaderLength()), - Data: data, - }) - defer pkt.DecRef() - - // Initialize the UDP header. - udpHdr := header.UDP(pkt.TransportHeader().Push(header.UDPMinimumSize)) - pkt.TransportProtocolNumber = udp.ProtocolNumber - - length := uint16(pkt.Size()) - udpHdr.Encode(&header.UDPFields{ - SrcPort: localPort, - DstPort: remotePort, - Length: length, - }) - - // Set the checksum field unless TX checksum offload is enabled. - // On IPv4, UDP checksum is optional, and a zero value indicates the - // transmitter skipped the checksum generation (RFC768). - // On IPv6, UDP checksum is not optional (RFC2460 Section 8.1). - if r.RequiresTXTransportChecksum() && r.NetProto() == header.IPv6ProtocolNumber { - xsum := r.PseudoHeaderChecksum(udp.ProtocolNumber, length) - for _, v := range data.Views() { - xsum = header.Checksum(v, xsum) - } - udpHdr.SetChecksum(^udpHdr.CalculateChecksum(xsum)) - } - - ttl := r.DefaultTTL() - - if err := r.WritePacket(stack.NetworkHeaderParams{ - Protocol: udp.ProtocolNumber, - TTL: ttl, - TOS: 0, /* default */ - }, pkt); err != nil { - r.Stats().UDP.PacketSendErrors.Increment() - return err - } - - // Track count of packets sent. - r.Stats().UDP.PacketsSent.Increment() - return nil -} diff --git a/libcore/tun/system/errors.generated.go b/libcore/tun/system/errors.generated.go deleted file mode 100644 index 395d11e78..000000000 --- a/libcore/tun/system/errors.generated.go +++ /dev/null @@ -1,17 +0,0 @@ -package system - -import ( - "fmt" - - "github.com/v2fly/v2ray-core/v5/common/errors" -) - -type errPathObjHolder struct{} - -func newError(values ...interface{}) *errors.Error { - return errors.New(values...).WithPathObj(errPathObjHolder{}) -} - -func newErrorf(format string, a ...interface{}) *errors.Error { - return errors.New(fmt.Sprintf(format, a)).WithPathObj(errPathObjHolder{}) -} diff --git a/libcore/tun/system/nat.go b/libcore/tun/system/nat.go deleted file mode 100644 index a9b9421ea..000000000 --- a/libcore/tun/system/nat.go +++ /dev/null @@ -1,133 +0,0 @@ -package system - -import ( - "os" - - "libcore/tun" - - "github.com/v2fly/v2ray-core/v5/common/buf" - "golang.org/x/sys/unix" - "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" - "gvisor.dev/gvisor/pkg/tcpip/header" - "gvisor.dev/gvisor/pkg/tcpip/header/parse" - "gvisor.dev/gvisor/pkg/tcpip/link/rawfile" - "gvisor.dev/gvisor/pkg/tcpip/stack" -) - -//go:generate go run ../errorgen - -var _ tun.Tun = (*SystemTun)(nil) - -var ( - vlanClient4 = tcpip.Address([]uint8{172, 19, 0, 1}) - vlanClient6 = tcpip.Address([]uint8{0xfd, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1}) -) - -type SystemTun struct { - dev int - mtu int - handler tun.Handler - ipv6Mode int32 - tcpForwarder *tcpForwarder - errorHandler func(err string) -} - -func New(dev int32, mtu int32, handler tun.Handler, ipv6Mode int32, errorHandler func(err string)) (*SystemTun, error) { - t := &SystemTun{ - dev: int(dev), - mtu: int(mtu), - handler: handler, - ipv6Mode: ipv6Mode, - errorHandler: errorHandler, - } - tcpServer, err := newTcpForwarder(t) - if err != nil { - return nil, err - } - go tcpServer.dispatchLoop() - t.tcpForwarder = tcpServer - - go t.dispatchLoop() - return t, nil -} - -func (t *SystemTun) dispatchLoop() { - cache := buf.New() - defer cache.Release() - data := cache.Extend(buf.Size) - - device := os.NewFile(uintptr(t.dev), "tun") - - for { - n, err := device.Read(data) - if err != nil { - break - } - cache.Clear() - cache.Resize(0, int32(n)) - packet := data[:n] - if t.deliverPacket(cache, packet) { - cache = buf.New() - data = cache.Extend(buf.Size) - } - } -} - -func (t *SystemTun) writeRawPacket(vv buffer.VectorisedView) tcpip.Error { - views := vv.Views() - iovecs := make([]unix.Iovec, len(views)) - for i, v := range views { - iovecs[i] = rawfile.IovecFromBytes(v) - } - return rawfile.NonBlockingWriteIovec(t.dev, iovecs) -} - -func (t *SystemTun) writePacket(pkt *stack.PacketBuffer) tcpip.Error { - views := pkt.Views() - iovecs := make([]unix.Iovec, len(views)) - for i, v := range views { - iovecs[i] = rawfile.IovecFromBytes(v) - } - return rawfile.NonBlockingWriteIovec(t.dev, iovecs) -} - -func (t *SystemTun) writeBuffer(bytes []byte) tcpip.Error { - return rawfile.NonBlockingWrite(t.dev, bytes) -} - -func (t *SystemTun) deliverPacket(cache *buf.Buffer, packet []byte) bool { - switch header.IPVersion(packet) { - case header.IPv4Version: - ipHdr := header.IPv4(packet) - switch ipHdr.TransportProtocol() { - case header.TCPProtocolNumber: - t.tcpForwarder.processIPv4(ipHdr, ipHdr.Payload()) - case header.UDPProtocolNumber: - t.processIPv4UDP(cache, ipHdr, ipHdr.Payload()) - return true - } - case header.IPv6Version: - pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Data: buffer.View(packet).ToVectorisedView(), - }) - proto, _, _, _, ok := parse.IPv6(pkt) - pkt.DecRef() - if !ok { - return false - } - ipHdr := header.IPv6(packet) - switch proto { - case header.TCPProtocolNumber: - t.tcpForwarder.processIPv6(ipHdr, ipHdr.Payload()) - case header.UDPProtocolNumber: - t.processIPv6UDP(cache, ipHdr, ipHdr.Payload()) - return true - } - } - return false -} - -func (t *SystemTun) Stop() { - t.tcpForwarder.Close() -} diff --git a/libcore/tun/system/peer.go b/libcore/tun/system/peer.go deleted file mode 100644 index 4c2528ef3..000000000 --- a/libcore/tun/system/peer.go +++ /dev/null @@ -1,15 +0,0 @@ -package system - -import ( - "gvisor.dev/gvisor/pkg/tcpip" -) - -type peerKey struct { - destinationAddress tcpip.Address - sourcePort uint16 -} - -type peerValue struct { - sourceAddress tcpip.Address - destinationPort uint16 -} diff --git a/libcore/tun/system/tcp.go b/libcore/tun/system/tcp.go deleted file mode 100644 index c60e4c839..000000000 --- a/libcore/tun/system/tcp.go +++ /dev/null @@ -1,200 +0,0 @@ -package system - -import ( - "errors" - "net" - "time" - - "libcore/comm" - - "github.com/Dreamacro/clash/common/cache" - "github.com/sirupsen/logrus" - v2rayNet "github.com/v2fly/v2ray-core/v5/common/net" - "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/header" -) - -type tcpForwarder struct { - tun *SystemTun - port uint16 - listener *net.TCPListener - sessions *cache.LruCache -} - -func newTcpForwarder(tun *SystemTun) (*tcpForwarder, error) { - var network string - address := &net.TCPAddr{} - if tun.ipv6Mode == comm.IPv6Disable { - network = "tcp4" - address.IP = net.IP(vlanClient4) - } else { - network = "tcp" - address.IP = net.IPv6zero - } - listener, err := net.ListenTCP(network, address) - if err != nil { - return nil, newError("failed to create tcp forwarder at ", address.IP).Base(err) - } - addr := listener.Addr().(*net.TCPAddr) - port := uint16(addr.Port) - newError("tcp forwarder started at ", addr).AtDebug().WriteToLog() - return &tcpForwarder{tun, port, listener, cache.NewLRUCache( - cache.WithAge(300), - cache.WithUpdateAgeOnGet(), - )}, nil -} - -func (t *tcpForwarder) dispatch() (bool, error) { - conn, err := t.listener.AcceptTCP() - if err != nil { - return true, err - } - addr := conn.RemoteAddr().(*net.TCPAddr) - if ip4 := addr.IP.To4(); ip4 != nil { - addr.IP = ip4 - } - key := peerKey{tcpip.Address(addr.IP), uint16(addr.Port)} - var session *peerValue - iSession, ok := t.sessions.Get(peerKey{key.destinationAddress, key.sourcePort}) - if ok { - session = iSession.(*peerValue) - } else { - conn.Close() - return false, newError("dropped unknown tcp session with source port ", key.sourcePort, " to destination address ", key.destinationAddress) - } - - source := v2rayNet.Destination{ - Address: v2rayNet.IPAddress([]byte(session.sourceAddress)), - Port: v2rayNet.Port(key.sourcePort), - Network: v2rayNet.Network_TCP, - } - destination := v2rayNet.Destination{ - Address: v2rayNet.IPAddress([]byte(key.destinationAddress)), - Port: v2rayNet.Port(session.destinationPort), - Network: v2rayNet.Network_TCP, - } - - go func() { - t.tun.handler.NewConnection(source, destination, conn) - time.Sleep(time.Second * 5) - t.sessions.Delete(key) - }() - - return false, nil -} - -func (t *tcpForwarder) dispatchLoop() { - for { - stop, err := t.dispatch() - if err != nil { - e := newError("dispatch tcp conn failed").Base(err) - e.WriteToLog() - if stop { - if !errors.Is(err, net.ErrClosed) { - t.Close() - t.tun.errorHandler(e.String()) - } - return - } - } - } -} - -func (t *tcpForwarder) processIPv4(ipHdr header.IPv4, tcpHdr header.TCP) { - sourceAddress := ipHdr.SourceAddress() - destinationAddress := ipHdr.DestinationAddress() - sourcePort := tcpHdr.SourcePort() - destinationPort := tcpHdr.DestinationPort() - - var session *peerValue - - if sourcePort != t.port { - - key := peerKey{destinationAddress, sourcePort} - iSession, ok := t.sessions.Get(key) - if ok { - session = iSession.(*peerValue) - } else { - session = &peerValue{sourceAddress, destinationPort} - t.sessions.Set(key, session) - } - - ipHdr.SetSourceAddress(destinationAddress) - ipHdr.SetDestinationAddress(vlanClient4) - tcpHdr.SetDestinationPort(t.port) - - } else { - - iSession, ok := t.sessions.Get(peerKey{destinationAddress, destinationPort}) - if ok { - session = iSession.(*peerValue) - } else { - logrus.Warn("unknown tcp session with source port ", destinationPort, " to destination address ", destinationAddress) - return - } - ipHdr.SetSourceAddress(destinationAddress) - tcpHdr.SetSourcePort(session.destinationPort) - ipHdr.SetDestinationAddress(session.sourceAddress) - } - - ipHdr.SetChecksum(0) - ipHdr.SetChecksum(^ipHdr.CalculateChecksum()) - tcpHdr.SetChecksum(0) - tcpHdr.SetChecksum(^tcpHdr.CalculateChecksum(header.ChecksumCombine( - header.PseudoHeaderChecksum(header.TCPProtocolNumber, ipHdr.SourceAddress(), ipHdr.DestinationAddress(), uint16(len(tcpHdr))), - header.Checksum(tcpHdr.Payload(), 0), - ))) - - t.tun.writeBuffer(ipHdr) -} - -func (t *tcpForwarder) processIPv6(ipHdr header.IPv6, tcpHdr header.TCP) { - sourceAddress := ipHdr.SourceAddress() - destinationAddress := ipHdr.DestinationAddress() - sourcePort := tcpHdr.SourcePort() - destinationPort := tcpHdr.DestinationPort() - - var session *peerValue - - if sourcePort != t.port { - - key := peerKey{destinationAddress, sourcePort} - iSession, ok := t.sessions.Get(key) - if ok { - session = iSession.(*peerValue) - } else { - session = &peerValue{sourceAddress, destinationPort} - t.sessions.Set(key, session) - } - - ipHdr.SetSourceAddress(destinationAddress) - ipHdr.SetDestinationAddress(vlanClient6) - tcpHdr.SetDestinationPort(t.port) - - } else { - - iSession, ok := t.sessions.Get(peerKey{destinationAddress, destinationPort}) - if ok { - session = iSession.(*peerValue) - } else { - logrus.Warn("unknown tcp session with source port ", destinationPort, " to destination address ", destinationAddress) - return - } - - ipHdr.SetSourceAddress(destinationAddress) - tcpHdr.SetSourcePort(session.destinationPort) - ipHdr.SetDestinationAddress(session.sourceAddress) - } - - tcpHdr.SetChecksum(0) - tcpHdr.SetChecksum(^tcpHdr.CalculateChecksum(header.ChecksumCombine( - header.PseudoHeaderChecksum(header.TCPProtocolNumber, ipHdr.SourceAddress(), ipHdr.DestinationAddress(), uint16(len(tcpHdr))), - header.Checksum(tcpHdr.Payload(), 0), - ))) - - t.tun.writeBuffer(ipHdr) -} - -func (t *tcpForwarder) Close() error { - return t.listener.Close() -} diff --git a/libcore/tun/system/udp.go b/libcore/tun/system/udp.go deleted file mode 100644 index a5c06c573..000000000 --- a/libcore/tun/system/udp.go +++ /dev/null @@ -1,157 +0,0 @@ -package system - -import ( - "libcore/tun" - "net" - - "github.com/v2fly/v2ray-core/v5/common/buf" - "gvisor.dev/gvisor/pkg/tcpip" - "gvisor.dev/gvisor/pkg/tcpip/buffer" - "gvisor.dev/gvisor/pkg/tcpip/header" -) - -func (t *SystemTun) processIPv4UDP(cache *buf.Buffer, ipHdr header.IPv4, hdr header.UDP) { - sourceAddress := ipHdr.SourceAddress() - destinationAddress := ipHdr.DestinationAddress() - sourcePort := hdr.SourcePort() - destinationPort := hdr.DestinationPort() - - source := &net.UDPAddr{ - IP: []byte(sourceAddress), - Port: int(sourcePort), - } - destination := &net.UDPAddr{ - IP: []byte(destinationAddress), - Port: int(destinationPort), - } - - ipHdr.SetDestinationAddress(sourceAddress) - hdr.SetDestinationPort(sourcePort) - - headerLength := ipHdr.HeaderLength() - headerCache := buf.New() - headerCache.Write(ipHdr[:headerLength+header.UDPMinimumSize]) - - cache.Advance(int32(headerLength + header.UDPMinimumSize)) - t.handler.HandlePacket(&tun.UDPPacket{ - Src: source, - Dst: destination, - Data: cache.Bytes(), - PutHeader: headerCache.Release, - WriteBack: func(bytes []byte, addr *net.UDPAddr) (int, error) { - index := headerCache.Len() - newHeader := headerCache.ExtendCopy(headerCache.Bytes()) - headerCache.Advance(index) - - defer func() { - headerCache.Clear() - headerCache.Resize(0, index) - }() - - var newSourceAddress tcpip.Address - var newSourcePort uint16 - - if addr != nil { - newSourceAddress = tcpip.Address(addr.IP) - newSourcePort = uint16(addr.Port) - } else { - newSourceAddress = destinationAddress - newSourcePort = destinationPort - } - - newIpHdr := header.IPv4(newHeader) - newIpHdr.SetSourceAddress(newSourceAddress) - newIpHdr.SetTotalLength(uint16(int(headerCache.Len()) + len(bytes))) - newIpHdr.SetChecksum(0) - newIpHdr.SetChecksum(^newIpHdr.CalculateChecksum()) - - udpHdr := header.UDP(headerCache.BytesFrom(headerCache.Len() - header.UDPMinimumSize)) - udpHdr.SetSourcePort(newSourcePort) - udpHdr.SetLength(uint16(header.UDPMinimumSize + len(bytes))) - udpHdr.SetChecksum(0) - udpHdr.SetChecksum(^udpHdr.CalculateChecksum(header.Checksum(bytes, header.PseudoHeaderChecksum(header.UDPProtocolNumber, newSourceAddress, sourceAddress, uint16(header.UDPMinimumSize+len(bytes)))))) - - replyVV := buffer.VectorisedView{} - replyVV.AppendView(newHeader) - replyVV.AppendView(bytes) - - if err := t.writeRawPacket(replyVV); err != nil { - return 0, newError(err.String()) - } - - return len(bytes), nil - }, - }) -} - -func (t *SystemTun) processIPv6UDP(cache *buf.Buffer, ipHdr header.IPv6, hdr header.UDP) { - sourceAddress := ipHdr.SourceAddress() - destinationAddress := ipHdr.DestinationAddress() - sourcePort := hdr.SourcePort() - destinationPort := hdr.DestinationPort() - - source := &net.UDPAddr{ - IP: []byte(sourceAddress), - Port: int(sourcePort), - } - destination := &net.UDPAddr{ - IP: []byte(destinationAddress), - Port: int(destinationPort), - } - - ipHdr.SetDestinationAddress(sourceAddress) - hdr.SetDestinationPort(sourcePort) - - headerLength := uint16(len(ipHdr)) - ipHdr.PayloadLength() - headerCache := buf.New() - headerCache.Write(ipHdr[:headerLength+header.UDPMinimumSize]) - - cache.Advance(int32(headerLength + header.UDPMinimumSize)) - t.handler.HandlePacket(&tun.UDPPacket{ - Src: source, - Dst: destination, - Data: cache.Bytes(), - PutHeader: headerCache.Release, - WriteBack: func(bytes []byte, addr *net.UDPAddr) (int, error) { - index := headerCache.Len() - newHeader := headerCache.ExtendCopy(headerCache.Bytes()) - headerCache.Advance(index) - - defer func() { - headerCache.Clear() - headerCache.Resize(0, index) - }() - - var newSourceAddress tcpip.Address - var newSourcePort uint16 - - if addr != nil { - newSourceAddress = tcpip.Address(addr.IP) - newSourcePort = uint16(addr.Port) - } else { - newSourceAddress = destinationAddress - newSourcePort = destinationPort - } - - newIpHdr := header.IPv6(newHeader) - newIpHdr.SetSourceAddress(newSourceAddress) - newIpHdr.SetPayloadLength(uint16(header.UDPMinimumSize + len(bytes))) - - udpHdr := header.UDP(headerCache.BytesFrom(headerCache.Len() - header.UDPMinimumSize)) - udpHdr.SetSourcePort(newSourcePort) - udpHdr.SetLength(uint16(header.UDPMinimumSize + len(bytes))) - udpHdr.SetChecksum(0) - udpHdr.SetChecksum(^udpHdr.CalculateChecksum(header.Checksum(bytes, header.PseudoHeaderChecksum(header.UDPProtocolNumber, newSourceAddress, sourceAddress, uint16(header.UDPMinimumSize+len(bytes)))))) - - replyVV := buffer.VectorisedView{} - replyVV.AppendView(headerCache.Bytes()) - replyVV.AppendView(bytes) - - if err := t.writeRawPacket(replyVV); err != nil { - return 0, newError(err.String()) - } - - return len(bytes), nil - }, - }) -} diff --git a/libcore/tun/tuns/tuns_android.go b/libcore/tun/tuns/tuns_linux.go similarity index 64% rename from libcore/tun/tuns/tuns_android.go rename to libcore/tun/tuns/tuns_linux.go index 53e14dfa9..5f89de0b7 100644 --- a/libcore/tun/tuns/tuns_android.go +++ b/libcore/tun/tuns/tuns_linux.go @@ -1,21 +1,20 @@ package tuns import ( + "errors" "libcore/tun" - "libcore/tun/gvisor" - "libcore/tun/system" "libcore/tun/tun2socket" "os" - - "gvisor.dev/gvisor/pkg/tcpip" ) func NewGvisor(dev int32, mtu int32, handler tun.Handler, nicId int32, pcap bool, pcapFile *os.File, snapLen uint32, ipv6Mode int32) (tun.Tun, error) { - return gvisor.New(dev, mtu, handler, tcpip.NICID(nicId), pcap, pcapFile, snapLen, ipv6Mode) + // return gvisor.New(dev, mtu, handler, tcpip.NICID(nicId), pcap, pcapFile, snapLen, ipv6Mode) + return nil, errors.New("not available") } func NewSystem(dev int32, mtu int32, handler tun.Handler, ipv6Mode int32, errorHandler func(err string)) (tun.Tun, error) { - return system.New(dev, mtu, handler, ipv6Mode, errorHandler) + // return system.New(dev, mtu, handler, ipv6Mode, errorHandler) + return nil, errors.New("not available") } func NewTun2Socket(fd int32, handler tun.Handler) (tun.Tun, error) { diff --git a/libcore/tun/tuns/tuns_stub.go b/libcore/tun/tuns/tuns_stub.go index 9fc078986..44affa8ed 100644 --- a/libcore/tun/tuns/tuns_stub.go +++ b/libcore/tun/tuns/tuns_stub.go @@ -1,4 +1,4 @@ -//go:build !android +//go:build !linux package tuns