diff --git a/format.go b/format.go deleted file mode 100644 index cb9e463..0000000 --- a/format.go +++ /dev/null @@ -1,7 +0,0 @@ -package tun - -//go:generate go install -v mvdan.cc/gofumpt@latest -//go:generate go install -v github.com/daixiang0/gci@v0.4.0 -//go:generate gofumpt -l -w . -//go:generate gofmt -s -w . -//go:generate gci write -s "standard,prefix(github.com/sagernet/),default" . diff --git a/gvisor_udp.go b/gvisor_udp.go index 82adce8..3708b9c 100644 --- a/gvisor_udp.go +++ b/gvisor_udp.go @@ -39,40 +39,45 @@ func (f *UDPForwarder) HandlePacket(id stack.TransportEndpointID, pkt *stack.Pac var upstreamMetadata M.Metadata upstreamMetadata.Source = M.SocksaddrFrom(M.AddrFromIP(net.IP(id.RemoteAddress)), id.RemotePort) upstreamMetadata.Destination = M.SocksaddrFrom(M.AddrFromIP(net.IP(id.LocalAddress)), id.LocalPort) - + var netProto tcpip.NetworkProtocolNumber + if upstreamMetadata.Source.IsIPv4() { + netProto = header.IPv4ProtocolNumber + } else { + netProto = header.IPv6ProtocolNumber + } f.udpNat.NewPacket( f.ctx, upstreamMetadata.Source.AddrPort(), buf.As(pkt.Data().AsRange().ToSlice()), upstreamMetadata, func(natConn N.PacketConn) N.PacketWriter { - return &UDPBackWriter{f.stack, id.RemoteAddress, id.RemotePort} + return &UDPBackWriter{f.stack, id.RemoteAddress, id.RemotePort, netProto} }, ) return true } type UDPBackWriter struct { - stack *stack.Stack - source tcpip.Address - sourcePort uint16 + stack *stack.Stack + source tcpip.Address + sourcePort uint16 + sourceNetwork tcpip.NetworkProtocolNumber } func (w *UDPBackWriter) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error { defer buffer.Release() - var netProto tcpip.NetworkProtocolNumber - if destination.IsIPv4() { - netProto = header.IPv4ProtocolNumber - } else { - netProto = header.IPv6ProtocolNumber + if w.sourceNetwork == header.IPv4ProtocolNumber && destination.Addr.Is4In6() { + destination = destination.Unwrap() + } else if w.sourceNetwork == header.IPv6ProtocolNumber && destination.Addr.Is4() { + destination = M.SocksaddrFrom(netip.AddrFrom16(destination.Addr.As16()), destination.Port) } route, err := w.stack.FindRoute( defaultNIC, tcpip.Address(destination.Addr.AsSlice()), w.source, - netProto, + w.sourceNetwork, false, ) if err != nil { @@ -95,7 +100,7 @@ func (w *UDPBackWriter) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) Length: pLen, }) - if route.RequiresTXTransportChecksum() && netProto == header.IPv6ProtocolNumber { + if route.RequiresTXTransportChecksum() && w.sourceNetwork == header.IPv6ProtocolNumber { xsum := udpHdr.CalculateChecksum(header.ChecksumCombine( route.PseudoHeaderChecksum(header.UDPProtocolNumber, pLen), packet.Data().AsRange().Checksum(), diff --git a/monitor_shared.go b/monitor_shared.go index 6980de5..cf451b4 100644 --- a/monitor_shared.go +++ b/monitor_shared.go @@ -5,9 +5,9 @@ package tun import ( "context" "sync" + "time" "github.com/sagernet/sing/common/x/list" - "time" ) func (m *networkUpdateMonitor) RegisterCallback(callback NetworkUpdateCallback) *list.Element[NetworkUpdateCallback] { diff --git a/packages_android.go b/packages_android.go index 2206123..ddf5480 100644 --- a/packages_android.go +++ b/packages_android.go @@ -8,11 +8,11 @@ import ( "os" "strconv" + "github.com/sagernet/abx-go" "github.com/sagernet/sing/common" E "github.com/sagernet/sing/common/exceptions" "github.com/fsnotify/fsnotify" - "github.com/sagernet/abx-go" ) type packageManager struct {