Skip to content

Commit

Permalink
chore: cleanup natTable's api
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Feb 18, 2023
1 parent 59cd89a commit fc50392
Show file tree
Hide file tree
Showing 24 changed files with 47 additions and 85 deletions.
4 changes: 0 additions & 4 deletions constant/adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion constant/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions hub/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions hub/route/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion listener/inbound/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion listener/inbound/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion listener/inbound/mixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion listener/inbound/redir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion listener/inbound/shadowsocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion listener/inbound/socks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions listener/inbound/tproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ 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 {
return err
}
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
}
Expand Down
2 changes: 1 addition & 1 deletion listener/inbound/tuic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion listener/inbound/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion listener/inbound/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion listener/inbound/vmess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()

Expand All @@ -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
}
Expand Down
8 changes: 0 additions & 8 deletions listener/shadowsocks/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 0 additions & 9 deletions listener/socks/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net"

"github.com/Dreamacro/clash/common/pool"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/transport/socks5"
)

Expand Down Expand Up @@ -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
}
20 changes: 6 additions & 14 deletions listener/tproxy/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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(""),
Expand All @@ -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
Expand Down
14 changes: 8 additions & 6 deletions listener/tproxy/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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...):
Expand Down
9 changes: 0 additions & 9 deletions listener/tunnel/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net"

"github.com/Dreamacro/clash/common/pool"
C "github.com/Dreamacro/clash/constant"
)

type packet struct {
Expand Down Expand Up @@ -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
}
Loading

0 comments on commit fc50392

Please sign in to comment.