From 0032242c9617c4c7fe516513d531a6716e21acc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Fri, 16 Sep 2022 15:34:59 +0800 Subject: [PATCH] Skip forward for cast addresses --- system.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/system.go b/system.go index 3074f97..1ed56fd 100644 --- a/system.go +++ b/system.go @@ -246,7 +246,9 @@ func (s *System) processIPv6(packet clashtcpip.IPv6Packet) error { func (s *System) processIPv4TCP(packet clashtcpip.IPv4Packet, header clashtcpip.TCPPacket) error { source := netip.AddrPortFrom(packet.SourceIP(), header.SourcePort()) destination := netip.AddrPortFrom(packet.DestinationIP(), header.DestinationPort()) - if source.Addr() == s.inet4ServerAddress && source.Port() == s.tcpPort { + if !destination.Addr().IsGlobalUnicast() { + return common.Error(s.tun.Write(packet)) + } else if source.Addr() == s.inet4ServerAddress && source.Port() == s.tcpPort { session := s.tcpNat.LookupBack(destination.Port()) if session == nil { return E.New("ipv4: tcp: session not found: ", destination.Port()) @@ -270,7 +272,9 @@ func (s *System) processIPv4TCP(packet clashtcpip.IPv4Packet, header clashtcpip. func (s *System) processIPv6TCP(packet clashtcpip.IPv6Packet, header clashtcpip.TCPPacket) error { source := netip.AddrPortFrom(packet.SourceIP(), header.SourcePort()) destination := netip.AddrPortFrom(packet.DestinationIP(), header.DestinationPort()) - if source.Addr() == s.inet6ServerAddress && source.Port() == s.tcpPort6 { + if !destination.Addr().IsGlobalUnicast() { + return common.Error(s.tun.Write(packet)) + } else if source.Addr() == s.inet6ServerAddress && source.Port() == s.tcpPort6 { session := s.tcpNat.LookupBack(destination.Port()) if session == nil { return E.New("ipv6: tcp: session not found: ", destination.Port()) @@ -298,13 +302,13 @@ func (s *System) processIPv4UDP(packet clashtcpip.IPv4Packet, header clashtcpip. if packet.FragmentOffset() != 0 { return E.New("ipv4: udp: fragment dropped") } - data := buf.As(header.Payload()) - if data.Len() == 0 { - return nil - } source := netip.AddrPortFrom(packet.SourceIP(), header.SourcePort()) destination := netip.AddrPortFrom(packet.DestinationIP(), header.DestinationPort()) - if !destination.Addr().IsGlobalUnicast() || destination.Addr().IsMulticast() { + if !destination.Addr().IsGlobalUnicast() { + return common.Error(s.tun.Write(packet)) + } + data := buf.As(header.Payload()) + if data.Len() == 0 { return nil } metadata := M.Metadata{ @@ -321,8 +325,8 @@ func (s *System) processIPv4UDP(packet clashtcpip.IPv4Packet, header clashtcpip. func (s *System) processIPv6UDP(packet clashtcpip.IPv6Packet, header clashtcpip.UDPPacket) error { source := netip.AddrPortFrom(packet.SourceIP(), header.SourcePort()) destination := netip.AddrPortFrom(packet.DestinationIP(), header.DestinationPort()) - if !destination.Addr().IsGlobalUnicast() || destination.Addr().IsMulticast() { - return nil + if !destination.Addr().IsGlobalUnicast() { + return common.Error(s.tun.Write(packet)) } data := buf.As(header.Payload()) if data.Len() == 0 {