diff --git a/go.mod b/go.mod index f4903a2..fef2e4c 100644 --- a/go.mod +++ b/go.mod @@ -7,10 +7,10 @@ require ( github.com/sagernet/abx-go v0.0.0-20220819185957-dba1257d738e github.com/sagernet/go-tun2socks v1.16.12-0.20220818015926-16cb67876a61 github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 - github.com/sagernet/sing v0.0.0-20220819003212-2424b1e2fac1 - golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b - golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 - gvisor.dev/gvisor v0.0.0-20220819163037-ba6e795b139a + github.com/sagernet/sing v0.0.0-20220905164441-f3d346256d4a + golang.org/x/net v0.0.0-20220907135653-1e95f45603a7 + golang.org/x/sys v0.0.0-20220908164124-27713097b956 + gvisor.dev/gvisor v0.0.0-20220901235040-6ca97ef2ce1c ) require ( diff --git a/go.sum b/go.sum index f9a2ae7..49e4c44 100644 --- a/go.sum +++ b/go.sum @@ -9,18 +9,19 @@ github.com/sagernet/go-tun2socks v1.16.12-0.20220818015926-16cb67876a61/go.mod h github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 h1:iL5gZI3uFp0X6EslacyapiRz7LLSJyr4RajF/BhMVyE= github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM= github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= -github.com/sagernet/sing v0.0.0-20220819003212-2424b1e2fac1 h1:+YC0/ygsJc4Z8qhd7ypsbWgMSm+UWN+QK+PW7I19K4Q= -github.com/sagernet/sing v0.0.0-20220819003212-2424b1e2fac1/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= +github.com/sagernet/sing v0.0.0-20220905164441-f3d346256d4a h1:Bqt+eYP7vJocAgAVAXC0B0ZN0uMr6g6exAoF3Ado2pg= +github.com/sagernet/sing v0.0.0-20220905164441-f3d346256d4a/go.mod h1:kZvzh1VDa/Dg/Bt5WaYKU0jl5ept8KKDpl3Ay4gRtRQ= 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-20220826154423-83b083e8dc8b h1:ZmngSVLe/wycRns9MKikG9OWIEjGcGAkacif7oYQaUY= -golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/net v0.0.0-20220907135653-1e95f45603a7 h1:1WGATo9HAhkWMbfyuVU0tEFP88OIkUvwaHFveQPvzCQ= +golang.org/x/net v0.0.0-20220907135653-1e95f45603a7/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-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220731174439-a90be440212d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 h1:UiNENfZ8gDvpiWw7IpOMQ27spWmThO1RwwdQVbJahJM= -golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220908164124-27713097b956 h1:XeJjHH1KiLpKGb6lvMiksZ9l0fVUh+AmGcm0nOMEBOY= +golang.org/x/sys v0.0.0-20220908164124-27713097b956/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-20220819163037-ba6e795b139a h1:W1h3JsEzYWg7eD4908iHv49p7AOx7JPKsoh/fsxgylM= gvisor.dev/gvisor v0.0.0-20220819163037-ba6e795b139a/go.mod h1:TIvkJD0sxe8pIob3p6T8IzxXunlp6yfgktvTNp+DGNM= +gvisor.dev/gvisor v0.0.0-20220901235040-6ca97ef2ce1c/go.mod h1:TIvkJD0sxe8pIob3p6T8IzxXunlp6yfgktvTNp+DGNM= diff --git a/gvisor_udp.go b/gvisor_udp.go index 3708b9c..87837ff 100644 --- a/gvisor_udp.go +++ b/gvisor_udp.go @@ -20,18 +20,16 @@ import ( ) type UDPForwarder struct { - ctx context.Context - stack *stack.Stack - handler Handler - udpNat *udpnat.Service[netip.AddrPort] + ctx context.Context + stack *stack.Stack + udpNat *udpnat.Service[netip.AddrPort] } func NewUDPForwarder(ctx context.Context, stack *stack.Stack, handler Handler, udpTimeout int64) *UDPForwarder { return &UDPForwarder{ - ctx: ctx, - stack: stack, - handler: handler, - udpNat: udpnat.New[netip.AddrPort](udpTimeout, handler), + ctx: ctx, + stack: stack, + udpNat: udpnat.New[netip.AddrPort](udpTimeout, handler), } } diff --git a/stack.go b/stack.go index 996d010..e92b838 100644 --- a/stack.go +++ b/stack.go @@ -5,6 +5,7 @@ import ( "net/netip" E "github.com/sagernet/sing/common/exceptions" + "github.com/sagernet/sing/common/logger" ) var ( @@ -28,6 +29,7 @@ type StackOptions struct { EndpointIndependentNat bool UDPTimeout int64 Handler Handler + Logger logger.Logger } func NewStack( diff --git a/system.go b/system.go index daa7ae2..4ffda39 100644 --- a/system.go +++ b/system.go @@ -10,6 +10,7 @@ import ( "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/buf" E "github.com/sagernet/sing/common/exceptions" + "github.com/sagernet/sing/common/logger" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" "github.com/sagernet/sing/common/udpnat" @@ -20,6 +21,7 @@ type System struct { tun Tun mtu uint32 handler Handler + logger logger.Logger inet4Prefixes []netip.Prefix inet6Prefixes []netip.Prefix inet4ServerAddress netip.Addr @@ -123,11 +125,16 @@ func (s *System) tunLoop() { continue } packet := packetSlice[PacketOffset:n] - switch packet[0] >> 4 { + switch ipVersion := packet[0] >> 4; ipVersion { case 4: - s.processIPv4(packet) + err = s.processIPv4(packet) case 6: - s.processIPv6(packet) + err = s.processIPv6(packet) + default: + err = E.New("ip: unknown version: ", ipVersion) + } + if err != nil { + s.logger.Trace(err) } } } @@ -161,7 +168,7 @@ func (s *System) acceptLoop(listener net.Listener) { connPort := M.SocksaddrFromNet(conn.RemoteAddr()).Port session := s.tcpNat.LookupBack(connPort) if session == nil { - s.handler.NewError(context.Background(), E.New("unknown session with port ", connPort)) + s.logger.Trace(E.New("unknown session with port ", connPort)) continue } destination := M.SocksaddrFromNetIP(session.Destination) @@ -192,10 +199,6 @@ func (s *System) acceptLoop(listener net.Listener) { } } -func (s *System) NewError(ctx context.Context, err error) { - s.handler.NewError(ctx, err) -} - func (s *System) processIPv4(packet clashtcpip.IPv4Packet) error { if !packet.Valid() { return E.New("ipv4: invalid packet") @@ -211,7 +214,7 @@ func (s *System) processIPv4(packet clashtcpip.IPv4Packet) error { case clashtcpip.ICMP: return s.processIPv4ICMP(packet, packet.Payload()) default: - return nil + return common.Error(s.tun.Write(packet)) } } @@ -230,7 +233,7 @@ func (s *System) processIPv6(packet clashtcpip.IPv6Packet) error { case clashtcpip.ICMPv6: return s.processIPv6ICMP(packet, packet.Payload()) default: - return nil + return common.Error(s.tun.Write(packet)) } } @@ -240,7 +243,7 @@ func (s *System) processIPv4TCP(packet clashtcpip.IPv4Packet, header clashtcpip. if source.Addr() == s.inet4ServerAddress && source.Port() == s.tcpPort { session := s.tcpNat.LookupBack(destination.Port()) if session == nil { - return E.New("session not found: ", destination.Port()) + return E.New("ipv4: tcp: session not found: ", destination.Port()) } packet.SetSourceIP(session.Destination.Addr()) header.SetSourcePort(session.Destination.Port()) @@ -264,7 +267,7 @@ func (s *System) processIPv6TCP(packet clashtcpip.IPv6Packet, header clashtcpip. if source.Addr() == s.inet6ServerAddress && source.Port() == s.tcpPort6 { session := s.tcpNat.LookupBack(destination.Port()) if session == nil { - return E.New("session not found: ", destination.Port()) + return E.New("ipv6: tcp: session not found: ", destination.Port()) } packet.SetSourceIP(session.Destination.Addr()) header.SetSourcePort(session.Destination.Port()) @@ -287,7 +290,7 @@ func (s *System) processIPv4UDP(packet clashtcpip.IPv4Packet, header clashtcpip. return E.New("ipv4: fragment dropped") } if packet.FragmentOffset() != 0 { - return E.New("ipv4: fragment dropped") + return E.New("ipv4: udp: fragment dropped") } source := netip.AddrPortFrom(packet.SourceIP(), header.SourcePort()) destination := netip.AddrPortFrom(packet.DestinationIP(), header.DestinationPort()) diff --git a/tun.go b/tun.go index 22ea88c..f070795 100644 --- a/tun.go +++ b/tun.go @@ -47,10 +47,10 @@ type Options struct { } func CalculateInterfaceName(name string) (tunName string) { - if name != "" { - tunName = name - } else if runtime.GOOS == "darwin" { + if runtime.GOOS == "darwin" { tunName = "utun" + } else if name != "" { + tunName = name } else { tunName = "tun" } diff --git a/tun_darwin.go b/tun_darwin.go index 7a0582d..36498aa 100644 --- a/tun_darwin.go +++ b/tun_darwin.go @@ -14,7 +14,6 @@ import ( E "github.com/sagernet/sing/common/exceptions" N "github.com/sagernet/sing/common/network" - "golang.org/x/net/ipv4" "golang.org/x/net/route" "golang.org/x/sys/unix" ) @@ -83,7 +82,7 @@ var ( func (t *NativeTun) Write(p []byte) (n int, err error) { var packetHeader []byte - if p[0]>>4 == ipv4.Version { + if p[0]>>4 == 4 { packetHeader = packetHeader4[:] } else { packetHeader = packetHeader6[:]