diff --git a/go.mod b/go.mod index 27d1779..141bf10 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,9 @@ go 1.18 require ( github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 github.com/sagernet/sing v0.0.0-20220801112236-1bb95f9661fc - golang.org/x/net v0.0.0-20220802222814-0bcc04d9c69b - golang.org/x/sys v0.0.0-20220803195053-6e608f9ce704 - gvisor.dev/gvisor v0.0.0-20220711011657-cecae2f4234d + golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48 + golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 + gvisor.dev/gvisor v0.0.0-20220801010827-addd1f7b3e97 ) require ( diff --git a/go.sum b/go.sum index ece4b5d..082aa37 100644 --- a/go.sum +++ b/go.sum @@ -6,12 +6,12 @@ github.com/sagernet/sing v0.0.0-20220801112236-1bb95f9661fc h1:x7H64IiqyrpxPWl/K github.com/sagernet/sing v0.0.0-20220801112236-1bb95f9661fc/go.mod h1:GbtQfZSpmtD3cXeD1qX2LCMwY8dH+bnnInDTqd92IsM= github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 h1:gga7acRE695APm9hlsSMoOoE65U4/TcqNj90mc69Rlg= github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= -golang.org/x/net v0.0.0-20220802222814-0bcc04d9c69b h1:3ogNYyK4oIQdIKzTu68hQrr4iuVxF3AxKl9Aj/eDrw0= -golang.org/x/net v0.0.0-20220802222814-0bcc04d9c69b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48 h1:N9Vc/rorQUDes6B9CNdIxAn5jODGj2wzfrei2x4wNj4= +golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/sys v0.0.0-20200217220822-9197077df867/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20220803195053-6e608f9ce704 h1:Y7NOhdqIOU8kYI7BxsgL38d0ot0raxvcW+EMQU2QrT4= -golang.org/x/sys v0.0.0-20220803195053-6e608f9ce704/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 h1:9vYwv7OjYaky/tlAeD7C4oC9EsPTlaFl1H2jS++V+ME= +golang.org/x/sys v0.0.0-20220804214406-8e32c043e418/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -gvisor.dev/gvisor v0.0.0-20220711011657-cecae2f4234d h1:KjI6i6P1ib9DiNdNIN8pb2TXfBewpKHf3O58cjj9vw4= -gvisor.dev/gvisor v0.0.0-20220711011657-cecae2f4234d/go.mod h1:TIvkJD0sxe8pIob3p6T8IzxXunlp6yfgktvTNp+DGNM= +gvisor.dev/gvisor v0.0.0-20220801010827-addd1f7b3e97 h1:zncudP85ZlJelPsgxZXN00Rl5M5j7QuDK27L35Ez01M= +gvisor.dev/gvisor v0.0.0-20220801010827-addd1f7b3e97/go.mod h1:TIvkJD0sxe8pIob3p6T8IzxXunlp6yfgktvTNp+DGNM= diff --git a/gvisor_udp.go b/gvisor_udp.go index 2c831a1..14b5875 100644 --- a/gvisor_udp.go +++ b/gvisor_udp.go @@ -13,7 +13,7 @@ import ( N "github.com/sagernet/sing/common/network" "github.com/sagernet/sing/common/udpnat" - gBuffer "gvisor.dev/gvisor/pkg/buffer" + "gvisor.dev/gvisor/pkg/bufferv2" "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -43,7 +43,7 @@ func (f *UDPForwarder) HandlePacket(id stack.TransportEndpointID, pkt *stack.Pac f.udpNat.NewPacket( f.ctx, upstreamMetadata.Source.AddrPort(), - buf.As(pkt.Data().AsRange().AsView()), + buf.As(pkt.Data().AsRange().ToSlice()), upstreamMetadata, func(natConn N.PacketConn) N.PacketWriter { return &UDPBackWriter{f.stack, id.RemoteAddress, id.RemotePort} @@ -58,9 +58,6 @@ type UDPBackWriter struct { sourcePort uint16 } -func (w *UDPBackWriter) WriteIsThreadUnsafe() { -} - func (w *UDPBackWriter) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error { defer buffer.Release() @@ -85,8 +82,7 @@ func (w *UDPBackWriter) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) packet := stack.NewPacketBuffer(stack.PacketBufferOptions{ ReserveHeaderBytes: header.UDPMinimumSize + int(route.MaxHeaderLength()), - Payload: gBuffer.NewWithData(buffer.Bytes()), - OnRelease: buffer.Release, + Payload: bufferv2.MakeWithData(buffer.Bytes()), }) defer packet.DecRef() diff --git a/tun_darwin.go b/tun_darwin.go index 3aa4c40..891181c 100644 --- a/tun_darwin.go +++ b/tun_darwin.go @@ -15,7 +15,7 @@ import ( "golang.org/x/net/route" "golang.org/x/sys/unix" - gBuffer "gvisor.dev/gvisor/pkg/buffer" + "gvisor.dev/gvisor/pkg/bufferv2" "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -109,42 +109,36 @@ func (e *DarwinEndpoint) dispatchLoop() { if err != nil { break } - packet := buf.NewSize(n - 4) - common.Must1(packet.Write(data[4:n])) - pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Payload: gBuffer.NewWithData(packet.Bytes()), - IsForwardedPacket: true, - OnRelease: packet.Release, - }) - var p tcpip.NetworkProtocolNumber - ipHeader, ok := pkt.Data().PullUp(1) - if !ok { - pkt.DecRef() - continue - } - switch header.IPVersion(ipHeader) { + packet := data[4:n] + var networkProtocol tcpip.NetworkProtocolNumber + switch header.IPVersion(packet) { case header.IPv4Version: - p = header.IPv4ProtocolNumber - if header.IPv4(packet.Bytes()).DestinationAddress() == e.tun.inet4Address { - _, err = e.tun.tunFile.Write(data[:n]) + networkProtocol = header.IPv4ProtocolNumber + if header.IPv4(packet).DestinationAddress() == e.tun.inet4Address { + e.tun.tunFile.Write(data[:n]) continue } case header.IPv6Version: - p = header.IPv6ProtocolNumber - if header.IPv6(packet.Bytes()).DestinationAddress() == e.tun.inet6Address { - _, err = e.tun.tunFile.Write(data[:n]) + networkProtocol = header.IPv6ProtocolNumber + if header.IPv6(packet).DestinationAddress() == e.tun.inet6Address { + e.tun.tunFile.Write(data[:n]) continue } default: + e.tun.tunFile.Write(data[:n]) continue } - + pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ + Payload: bufferv2.MakeWithData(data[4:n]), + IsForwardedPacket: true, + }) + pkt.NetworkProtocolNumber = networkProtocol dispatcher := e.dispatcher if dispatcher == nil { pkt.DecRef() return } - dispatcher.DeliverNetworkPacket(p, pkt) + dispatcher.DeliverNetworkPacket(networkProtocol, pkt) pkt.DecRef() } } @@ -163,22 +157,22 @@ func (e *DarwinEndpoint) ARPHardwareType() header.ARPHardwareType { func (e *DarwinEndpoint) AddHeader(buffer *stack.PacketBuffer) { } +var ( + packetHeader4 = [4]byte{0x00, 0x00, 0x00, unix.AF_INET} + packetHeader6 = [4]byte{0x00, 0x00, 0x00, unix.AF_INET6} +) + func (e *DarwinEndpoint) WritePackets(packetBufferList stack.PacketBufferList) (int, tcpip.Error) { - _packetHeader := buf.StackNewSize(4) - defer common.KeepAlive(_packetHeader) - packetHeader := common.Dup(_packetHeader) - defer packetHeader.Release() var n int for _, packet := range packetBufferList.AsSlice() { - packetHeader.FullReset() - packetHeader.WriteZeroN(3) + var packetHeader []byte switch packet.NetworkProtocolNumber { case header.IPv4ProtocolNumber: - packetHeader.WriteByte(unix.AF_INET) + packetHeader = packetHeader4[:] case header.IPv6ProtocolNumber: - packetHeader.WriteByte(unix.AF_INET6) + packetHeader = packetHeader6[:] } - _, err := rw.WriteV(e.tun.tunFd, append([][]byte{packetHeader.Bytes()}, packet.Slices()...)) + _, err := rw.WriteV(e.tun.tunFd, append([][]byte{packetHeader}, packet.AsSlices()...)) if err != nil { return n, &tcpip.ErrAborted{} } diff --git a/tun_windows.go b/tun_windows.go index ef22216..ace396f 100644 --- a/tun_windows.go +++ b/tun_windows.go @@ -20,7 +20,7 @@ import ( E "github.com/sagernet/sing/common/exceptions" "golang.org/x/sys/windows" - gBuffer "gvisor.dev/gvisor/pkg/buffer" + "gvisor.dev/gvisor/pkg/bufferv2" "gvisor.dev/gvisor/pkg/tcpip" "gvisor.dev/gvisor/pkg/tcpip/header" "gvisor.dev/gvisor/pkg/tcpip/stack" @@ -318,33 +318,27 @@ func (e *WintunEndpoint) dispatchLoop() { if err != nil { break } - packet := buf.NewSize(n) - common.Must1(packet.Write(data[:n])) - pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ - Payload: gBuffer.NewWithData(packet.Bytes()), - IsForwardedPacket: true, - OnRelease: packet.Release, - }) - var p tcpip.NetworkProtocolNumber - ipHeader, ok := pkt.Data().PullUp(1) - if !ok { - pkt.DecRef() - continue - } - switch header.IPVersion(ipHeader) { + packet := data[:n] + var networkProtocol tcpip.NetworkProtocolNumber + switch header.IPVersion(packet) { case header.IPv4Version: - p = header.IPv4ProtocolNumber + networkProtocol = header.IPv4ProtocolNumber case header.IPv6Version: - p = header.IPv6ProtocolNumber + networkProtocol = header.IPv6ProtocolNumber default: + e.tun.Write([][]byte{packet}) continue } + pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ + Payload: bufferv2.MakeWithData(packet), + IsForwardedPacket: true, + }) dispatcher := e.dispatcher if dispatcher == nil { pkt.DecRef() return } - dispatcher.DeliverNetworkPacket(p, pkt) + dispatcher.DeliverNetworkPacket(networkProtocol, pkt) pkt.DecRef() } } @@ -366,7 +360,7 @@ func (e *WintunEndpoint) AddHeader(buffer *stack.PacketBuffer) { func (e *WintunEndpoint) WritePackets(packetBufferList stack.PacketBufferList) (int, tcpip.Error) { var n int for _, packet := range packetBufferList.AsSlice() { - _, err := e.tun.Write(packet.Slices()) + _, err := e.tun.Write(packet.AsSlices()) if err != nil { return n, &tcpip.ErrAborted{} }