feat: introduce a new robust approach to handle tproxy udp. (#389)

This commit is contained in:
Ovear
2023-02-17 16:31:15 +08:00
committed by GitHub
parent b2d1cea759
commit 8e4dfbd10d
10 changed files with 246 additions and 12 deletions

View File

@ -2,6 +2,7 @@ package tunnel
import (
"errors"
"github.com/Dreamacro/clash/log"
"net"
"net/netip"
"time"
@ -32,6 +33,7 @@ func handleUDPToLocal(packet C.UDPPacket, pc net.PacketConn, key string, oAddr,
buf := pool.Get(pool.UDPBufferSize)
defer func() {
_ = pc.Close()
closeAllLocalCoon(key)
natTable.Delete(key)
_ = pool.Put(buf)
}()
@ -60,6 +62,19 @@ func handleUDPToLocal(packet C.UDPPacket, pc net.PacketConn, key string, oAddr,
}
}
func closeAllLocalCoon(lAddr string) {
natTable.RangeLocalConn(lAddr, func(key, value any) bool {
conn, ok := value.(*net.UDPConn)
if !ok || conn == nil {
log.Debugln("Value %#v unknown value when closing TProxy local conn...", conn)
return true
}
conn.Close()
log.Debugln("Closing TProxy local conn... lAddr=%s rAddr=%s", lAddr, key)
return true
})
}
func handleSocket(ctx C.ConnContext, outbound net.Conn) {
N.Relay(ctx.Conn(), outbound)
}

View File

@ -337,9 +337,12 @@ 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)
natTable.Set(key, pc)
handle()
}()
}