diff --git a/constant/adapters.go b/constant/adapters.go index 879ee6d7..bf5f7fdb 100644 --- a/constant/adapters.go +++ b/constant/adapters.go @@ -217,10 +217,6 @@ type UDPPacket interface { // LocalAddr returns the source IP/Port of packet LocalAddr() net.Addr - - SetNatTable(natTable NatTable) - - SetUdpInChan(in chan<- PacketAdapter) } type UDPPacketInAddr interface { diff --git a/constant/listener.go b/constant/listener.go index a52c1946..6f9f169b 100644 --- a/constant/listener.go +++ b/constant/listener.go @@ -16,7 +16,7 @@ type MultiAddrListener interface { type InboundListener interface { Name() string - Listen(tcpIn chan<- ConnContext, udpIn chan<- PacketAdapter) error + Listen(tcpIn chan<- ConnContext, udpIn chan<- PacketAdapter, natTable NatTable) error Close() error Address() string RawAddress() string diff --git a/hub/executor/executor.go b/hub/executor/executor.go index d88e91dc..b3e33f98 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -137,8 +137,9 @@ func GetGeneral() *config.General { func updateListeners(listeners map[string]C.InboundListener) { tcpIn := tunnel.TCPIn() udpIn := tunnel.UDPIn() + natTable := tunnel.NatTable() - listener.PatchInboundListeners(listeners, tcpIn, udpIn, true) + listener.PatchInboundListeners(listeners, tcpIn, udpIn, natTable, true) } func updateExperimental(c *config.Config) { @@ -348,12 +349,13 @@ func updateGeneral(general *config.General, force bool) { tcpIn := tunnel.TCPIn() udpIn := tunnel.UDPIn() + natTable := tunnel.NatTable() listener.ReCreateHTTP(general.Port, tcpIn) listener.ReCreateSocks(general.SocksPort, tcpIn, udpIn) - listener.ReCreateRedir(general.RedirPort, tcpIn, udpIn) + listener.ReCreateRedir(general.RedirPort, tcpIn, udpIn, natTable) listener.ReCreateAutoRedir(general.EBpf.AutoRedir, tcpIn, udpIn) - listener.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn) + listener.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn, natTable) listener.ReCreateMixed(general.MixedPort, tcpIn, udpIn) listener.ReCreateShadowSocks(general.ShadowSocksConfig, tcpIn, udpIn) listener.ReCreateVmess(general.VmessConfig, tcpIn, udpIn) diff --git a/hub/route/configs.go b/hub/route/configs.go index 5047c6d6..9e630b29 100644 --- a/hub/route/configs.go +++ b/hub/route/configs.go @@ -239,11 +239,12 @@ func patchConfigs(w http.ResponseWriter, r *http.Request) { tcpIn := tunnel.TCPIn() udpIn := tunnel.UDPIn() + natTable := tunnel.NatTable() P.ReCreateHTTP(pointerOrDefault(general.Port, ports.Port), tcpIn) P.ReCreateSocks(pointerOrDefault(general.SocksPort, ports.SocksPort), tcpIn, udpIn) - P.ReCreateRedir(pointerOrDefault(general.RedirPort, ports.RedirPort), tcpIn, udpIn) - P.ReCreateTProxy(pointerOrDefault(general.TProxyPort, ports.TProxyPort), tcpIn, udpIn) + P.ReCreateRedir(pointerOrDefault(general.RedirPort, ports.RedirPort), tcpIn, udpIn, natTable) + P.ReCreateTProxy(pointerOrDefault(general.TProxyPort, ports.TProxyPort), tcpIn, udpIn, natTable) P.ReCreateMixed(pointerOrDefault(general.MixedPort, ports.MixedPort), tcpIn, udpIn) P.ReCreateTun(pointerOrDefaultTun(general.Tun, P.LastTunConf), tcpIn, udpIn) P.ReCreateShadowSocks(pointerOrDefaultString(general.ShadowSocksConfig, ports.ShadowSocksConfig), tcpIn, udpIn) diff --git a/listener/inbound/base.go b/listener/inbound/base.go index 41be5b10..b132ac6c 100644 --- a/listener/inbound/base.go +++ b/listener/inbound/base.go @@ -61,7 +61,7 @@ func (b *Base) RawAddress() string { } // Listen implements constant.InboundListener -func (*Base) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (*Base) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error { return nil } diff --git a/listener/inbound/http.go b/listener/inbound/http.go index b19f0154..a93f9684 100644 --- a/listener/inbound/http.go +++ b/listener/inbound/http.go @@ -42,7 +42,7 @@ func (h *HTTP) Address() string { } // Listen implements constant.InboundListener -func (h *HTTP) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (h *HTTP) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error { var err error h.l, err = http.New(h.RawAddress(), tcpIn, h.Additions()...) if err != nil { diff --git a/listener/inbound/mixed.go b/listener/inbound/mixed.go index a2920c69..dbba264c 100644 --- a/listener/inbound/mixed.go +++ b/listener/inbound/mixed.go @@ -50,7 +50,7 @@ func (m *Mixed) Address() string { } // Listen implements constant.InboundListener -func (m *Mixed) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (m *Mixed) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error { var err error m.l, err = mixed.New(m.RawAddress(), tcpIn, m.Additions()...) if err != nil { diff --git a/listener/inbound/redir.go b/listener/inbound/redir.go index 7a1685ba..4b88d895 100644 --- a/listener/inbound/redir.go +++ b/listener/inbound/redir.go @@ -42,7 +42,7 @@ func (r *Redir) Address() string { } // Listen implements constant.InboundListener -func (r *Redir) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (r *Redir) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error { var err error r.l, err = redir.New(r.RawAddress(), tcpIn, r.Additions()...) if err != nil { diff --git a/listener/inbound/shadowsocks.go b/listener/inbound/shadowsocks.go index e6baa80c..40907485 100644 --- a/listener/inbound/shadowsocks.go +++ b/listener/inbound/shadowsocks.go @@ -57,7 +57,7 @@ func (s *ShadowSocks) Address() string { } // Listen implements constant.InboundListener -func (s *ShadowSocks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (s *ShadowSocks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error { var err error s.l, err = sing_shadowsocks.New(s.ss, tcpIn, udpIn, s.Additions()...) if err != nil { diff --git a/listener/inbound/socks.go b/listener/inbound/socks.go index 010d08f9..aac2ee23 100644 --- a/listener/inbound/socks.go +++ b/listener/inbound/socks.go @@ -68,7 +68,7 @@ func (s *Socks) Address() string { } // Listen implements constant.InboundListener -func (s *Socks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (s *Socks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error { var err error if s.stl, err = socks.New(s.RawAddress(), tcpIn, s.Additions()...); err != nil { return err diff --git a/listener/inbound/tproxy.go b/listener/inbound/tproxy.go index 7aa8af8d..fa458d2c 100644 --- a/listener/inbound/tproxy.go +++ b/listener/inbound/tproxy.go @@ -49,7 +49,7 @@ func (t *TProxy) Address() string { } // Listen implements constant.InboundListener -func (t *TProxy) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (t *TProxy) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error { var err error t.lTCP, err = tproxy.New(t.RawAddress(), tcpIn, t.Additions()...) if err != nil { @@ -57,7 +57,7 @@ func (t *TProxy) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter } if t.udp { if t.lUDP != nil { - t.lUDP, err = tproxy.NewUDP(t.RawAddress(), udpIn, t.Additions()...) + t.lUDP, err = tproxy.NewUDP(t.RawAddress(), udpIn, natTable, t.Additions()...) if err != nil { return err } diff --git a/listener/inbound/tuic.go b/listener/inbound/tuic.go index c74f73f6..f6641500 100644 --- a/listener/inbound/tuic.go +++ b/listener/inbound/tuic.go @@ -69,7 +69,7 @@ func (t *Tuic) Address() string { } // Listen implements constant.InboundListener -func (t *Tuic) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (t *Tuic) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error { var err error t.l, err = tuic.New(t.ts, tcpIn, udpIn, t.Additions()...) if err != nil { diff --git a/listener/inbound/tun.go b/listener/inbound/tun.go index 997164c2..ad215989 100644 --- a/listener/inbound/tun.go +++ b/listener/inbound/tun.go @@ -111,7 +111,7 @@ func (t *Tun) Address() string { } // Listen implements constant.InboundListener -func (t *Tun) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (t *Tun) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error { var err error t.l, err = sing_tun.New(t.tun, tcpIn, udpIn, t.Additions()...) if err != nil { diff --git a/listener/inbound/tunnel.go b/listener/inbound/tunnel.go index 221f4cd6..41d024ef 100644 --- a/listener/inbound/tunnel.go +++ b/listener/inbound/tunnel.go @@ -74,7 +74,7 @@ func (t *Tunnel) Address() string { } // Listen implements constant.InboundListener -func (t *Tunnel) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (t *Tunnel) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error { var err error for _, network := range t.config.Network { switch network { diff --git a/listener/inbound/vmess.go b/listener/inbound/vmess.go index 130e17c5..70e840a5 100644 --- a/listener/inbound/vmess.go +++ b/listener/inbound/vmess.go @@ -69,7 +69,7 @@ func (v *Vmess) Address() string { } // Listen implements constant.InboundListener -func (v *Vmess) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error { +func (v *Vmess) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) error { var err error users := make([]LC.VmessUser, len(v.config.Users)) for i, v := range v.config.Users { diff --git a/listener/listener.go b/listener/listener.go index d747d5f5..d8eb5c0c 100644 --- a/listener/listener.go +++ b/listener/listener.go @@ -207,7 +207,7 @@ func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAd log.Infoln("SOCKS proxy listening at: %s", socksListener.Address()) } -func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) { +func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) { redirMux.Lock() defer redirMux.Unlock() @@ -245,7 +245,7 @@ func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAd return } - redirUDPListener, err = tproxy.NewUDP(addr, udpIn) + redirUDPListener, err = tproxy.NewUDP(addr, udpIn, natTable) if err != nil { log.Warnln("Failed to start Redir UDP Listener: %s", err) } @@ -403,7 +403,7 @@ func ReCreateTuic(config LC.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- return } -func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) { +func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) { tproxyMux.Lock() defer tproxyMux.Unlock() @@ -441,7 +441,7 @@ func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketA return } - tproxyUDPListener, err = tproxy.NewUDP(addr, udpIn) + tproxyUDPListener, err = tproxy.NewUDP(addr, udpIn, natTable) if err != nil { log.Warnln("Failed to start TProxy UDP Listener: %s", err) } @@ -719,7 +719,7 @@ func PatchTunnel(tunnels []LC.Tunnel, tcpIn chan<- C.ConnContext, udpIn chan<- C } } -func PatchInboundListeners(newListenerMap map[string]C.InboundListener, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, dropOld bool) { +func PatchInboundListeners(newListenerMap map[string]C.InboundListener, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable, dropOld bool) { inboundMux.Lock() defer inboundMux.Unlock() @@ -731,7 +731,7 @@ func PatchInboundListeners(newListenerMap map[string]C.InboundListener, tcpIn ch continue } } - if err := newListener.Listen(tcpIn, udpIn); err != nil { + if err := newListener.Listen(tcpIn, udpIn, natTable); err != nil { log.Errorln("Listener %s listen err: %s", name, err.Error()) continue } diff --git a/listener/shadowsocks/utils.go b/listener/shadowsocks/utils.go index eee5660a..2e9fd003 100644 --- a/listener/shadowsocks/utils.go +++ b/listener/shadowsocks/utils.go @@ -7,7 +7,6 @@ import ( "net/url" "github.com/Dreamacro/clash/common/pool" - C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/transport/socks5" ) @@ -45,13 +44,6 @@ func (c *packet) InAddr() net.Addr { return c.pc.LocalAddr() } -func (c *packet) SetNatTable(natTable C.NatTable) { - // no need -} - -func (c *packet) SetUdpInChan(in chan<- C.PacketAdapter) { - // no need -} func ParseSSURL(s string) (addr, cipher, password string, err error) { u, err := url.Parse(s) if err != nil { diff --git a/listener/socks/utils.go b/listener/socks/utils.go index 29898fda..4c53b9e5 100644 --- a/listener/socks/utils.go +++ b/listener/socks/utils.go @@ -4,7 +4,6 @@ import ( "net" "github.com/Dreamacro/clash/common/pool" - C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/transport/socks5" ) @@ -40,11 +39,3 @@ func (c *packet) Drop() { func (c *packet) InAddr() net.Addr { return c.pc.LocalAddr() } - -func (c *packet) SetNatTable(natTable C.NatTable) { - // no need -} - -func (c *packet) SetUdpInChan(in chan<- C.PacketAdapter) { - // no need -} diff --git a/listener/tproxy/packet.go b/listener/tproxy/packet.go index d66fac51..e3a20414 100644 --- a/listener/tproxy/packet.go +++ b/listener/tproxy/packet.go @@ -15,8 +15,8 @@ type packet struct { pc net.PacketConn lAddr netip.AddrPort buf []byte - natTable C.NatTable in chan<- C.PacketAdapter + natTable C.NatTable } func (c *packet) Data() []byte { @@ -25,7 +25,7 @@ func (c *packet) Data() []byte { // WriteBack opens a new socket binding `addr` to write UDP packet back func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) { - tc, err := createOrGetLocalConn(addr, c.LocalAddr(), c.natTable, c.in) + tc, err := createOrGetLocalConn(addr, c.LocalAddr(), c.in, c.natTable) if err != nil { n = 0 return @@ -47,18 +47,10 @@ func (c *packet) InAddr() net.Addr { return c.pc.LocalAddr() } -func (c *packet) SetNatTable(natTable C.NatTable) { - c.natTable = natTable -} - -func (c *packet) SetUdpInChan(in chan<- C.PacketAdapter) { - c.in = in -} - // this function listen at rAddr and write to lAddr // for here, rAddr is the ip/port client want to access // lAddr is the ip/port client opened -func createOrGetLocalConn(rAddr, lAddr net.Addr, natTable C.NatTable, in chan<- C.PacketAdapter) (*net.UDPConn, error) { +func createOrGetLocalConn(rAddr, lAddr net.Addr, in chan<- C.PacketAdapter, natTable C.NatTable) (*net.UDPConn, error) { remote := rAddr.String() local := lAddr.String() localConn := natTable.GetLocalConn(local, remote) @@ -83,7 +75,7 @@ func createOrGetLocalConn(rAddr, lAddr net.Addr, natTable C.NatTable, in chan<- natTable.DeleteLocalConnMap(local, lockKey) cond.Broadcast() }() - conn, err := listenLocalConn(rAddr, lAddr, in) + conn, err := listenLocalConn(rAddr, lAddr, in, natTable) if err != nil { log.Errorln("listenLocalConn failed with error: %s, packet loss", err.Error()) return nil, err @@ -97,7 +89,7 @@ func createOrGetLocalConn(rAddr, lAddr net.Addr, natTable C.NatTable, in chan<- // this function listen at rAddr // and send what received to program itself, then send to real remote -func listenLocalConn(rAddr, lAddr net.Addr, in chan<- C.PacketAdapter) (*net.UDPConn, error) { +func listenLocalConn(rAddr, lAddr net.Addr, in chan<- C.PacketAdapter, natTable C.NatTable) (*net.UDPConn, error) { additions := []inbound.Addition{ inbound.WithInName("DEFAULT-TPROXY"), inbound.WithSpecialRules(""), @@ -120,7 +112,7 @@ func listenLocalConn(rAddr, lAddr net.Addr, in chan<- C.PacketAdapter) (*net.UDP } // since following localPackets are pass through this socket which listen rAddr // I choose current listener as packet's packet conn - handlePacketConn(lc, in, buf[:br], lAddr.(*net.UDPAddr).AddrPort(), rAddr.(*net.UDPAddr).AddrPort(), additions...) + handlePacketConn(lc, in, natTable, buf[:br], lAddr.(*net.UDPAddr).AddrPort(), rAddr.(*net.UDPAddr).AddrPort(), additions...) } }() return lc, nil diff --git a/listener/tproxy/udp.go b/listener/tproxy/udp.go index f85c9ea9..d3727180 100644 --- a/listener/tproxy/udp.go +++ b/listener/tproxy/udp.go @@ -32,7 +32,7 @@ func (l *UDPListener) Close() error { return l.packetConn.Close() } -func NewUDP(addr string, in chan<- C.PacketAdapter, additions ...inbound.Addition) (*UDPListener, error) { +func NewUDP(addr string, in chan<- C.PacketAdapter, natTable C.NatTable, additions ...inbound.Addition) (*UDPListener, error) { if len(additions) == 0 { additions = []inbound.Addition{ inbound.WithInName("DEFAULT-TPROXY"), @@ -83,19 +83,21 @@ func NewUDP(addr string, in chan<- C.PacketAdapter, additions ...inbound.Additio // try to unmap 4in6 address lAddr = netip.AddrPortFrom(lAddr.Addr().Unmap(), lAddr.Port()) } - handlePacketConn(l, in, buf[:n], lAddr, rAddr, additions...) + handlePacketConn(l, in, natTable, buf[:n], lAddr, rAddr, additions...) } }() return rl, nil } -func handlePacketConn(pc net.PacketConn, in chan<- C.PacketAdapter, buf []byte, lAddr, rAddr netip.AddrPort, additions ...inbound.Addition) { +func handlePacketConn(pc net.PacketConn, in chan<- C.PacketAdapter, natTable C.NatTable, buf []byte, lAddr, rAddr netip.AddrPort, additions ...inbound.Addition) { target := socks5.AddrFromStdAddrPort(rAddr) pkt := &packet{ - pc: pc, - lAddr: lAddr, - buf: buf, + pc: pc, + lAddr: lAddr, + buf: buf, + in: in, + natTable: natTable, } select { case in <- inbound.NewPacket(target, pkt, C.TPROXY, additions...): diff --git a/listener/tunnel/packet.go b/listener/tunnel/packet.go index fa85879f..602f7675 100644 --- a/listener/tunnel/packet.go +++ b/listener/tunnel/packet.go @@ -4,7 +4,6 @@ import ( "net" "github.com/Dreamacro/clash/common/pool" - C "github.com/Dreamacro/clash/constant" ) type packet struct { @@ -34,11 +33,3 @@ func (c *packet) Drop() { func (c *packet) InAddr() net.Addr { return c.pc.LocalAddr() } - -func (c *packet) SetNatTable(natTable C.NatTable) { - // no need -} - -func (c *packet) SetUdpInChan(in chan<- C.PacketAdapter) { - // no need -} diff --git a/transport/tuic/server.go b/transport/tuic/server.go index fdea899d..2830b324 100644 --- a/transport/tuic/server.go +++ b/transport/tuic/server.go @@ -316,13 +316,5 @@ func (s *serverUDPPacket) Drop() { s.packet.DATA = nil } -func (s *serverUDPPacket) SetNatTable(natTable C.NatTable) { - // no need -} - -func (s *serverUDPPacket) SetUdpInChan(in chan<- C.PacketAdapter) { - // no need -} - var _ C.UDPPacket = &serverUDPPacket{} var _ C.UDPPacketInAddr = &serverUDPPacket{} diff --git a/tunnel/connection.go b/tunnel/connection.go index 687b2887..bd8d1b63 100644 --- a/tunnel/connection.go +++ b/tunnel/connection.go @@ -2,7 +2,6 @@ package tunnel import ( "errors" - "github.com/Dreamacro/clash/log" "net" "net/netip" "time" @@ -10,6 +9,7 @@ import ( N "github.com/Dreamacro/clash/common/net" "github.com/Dreamacro/clash/common/pool" C "github.com/Dreamacro/clash/constant" + "github.com/Dreamacro/clash/log" ) func handleUDPToRemote(packet C.UDPPacket, pc C.PacketConn, metadata *C.Metadata) error { diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index 5c3814bc..695f2945 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -82,6 +82,11 @@ func UDPIn() chan<- C.PacketAdapter { return udpQueue } +// NatTable return nat table +func NatTable() C.NatTable { + return natTable +} + // Rules return all rules func Rules() []C.Rule { return rules @@ -338,8 +343,6 @@ func handleUDPConn(packet C.PacketAdapter) { oAddr := metadata.DstIP natTable.Set(key, pc) - packet.SetNatTable(natTable) - packet.SetUdpInChan(udpQueue) go handleUDPToLocal(packet, pc, key, oAddr, fAddr)