feat: introduce a new robust approach to handle tproxy udp. (#389)
This commit is contained in:
@ -7,6 +7,7 @@ import (
|
||||
"net/url"
|
||||
|
||||
"github.com/Dreamacro/clash/common/pool"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/transport/socks5"
|
||||
)
|
||||
|
||||
@ -44,6 +45,13 @@ 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 {
|
||||
|
@ -166,3 +166,11 @@ func (c *packet) Drop() {
|
||||
func (c *packet) InAddr() net.Addr {
|
||||
return c.lAddr
|
||||
}
|
||||
|
||||
func (c *packet) SetNatTable(natTable C.NatTable) {
|
||||
// no need
|
||||
}
|
||||
|
||||
func (c *packet) SetUdpInChan(in chan<- C.PacketAdapter) {
|
||||
// no need
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"net"
|
||||
|
||||
"github.com/Dreamacro/clash/common/pool"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/transport/socks5"
|
||||
)
|
||||
|
||||
@ -39,3 +40,11 @@ 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
|
||||
}
|
||||
|
@ -1,16 +1,22 @@
|
||||
package tproxy
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Dreamacro/clash/adapter/inbound"
|
||||
"github.com/Dreamacro/clash/common/pool"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
"net"
|
||||
"net/netip"
|
||||
|
||||
"github.com/Dreamacro/clash/common/pool"
|
||||
)
|
||||
|
||||
type packet struct {
|
||||
pc net.PacketConn
|
||||
lAddr netip.AddrPort
|
||||
buf []byte
|
||||
pc net.PacketConn
|
||||
lAddr netip.AddrPort
|
||||
buf []byte
|
||||
natTable C.NatTable
|
||||
in chan<- C.PacketAdapter
|
||||
}
|
||||
|
||||
func (c *packet) Data() []byte {
|
||||
@ -19,13 +25,12 @@ 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 := dialUDP("udp", addr.(*net.UDPAddr).AddrPort(), c.lAddr)
|
||||
tc, err := createOrGetLocalConn(addr, c.LocalAddr(), c.natTable, c.in)
|
||||
if err != nil {
|
||||
n = 0
|
||||
return
|
||||
}
|
||||
n, err = tc.Write(b)
|
||||
tc.Close()
|
||||
return
|
||||
}
|
||||
|
||||
@ -41,3 +46,82 @@ func (c *packet) Drop() {
|
||||
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) {
|
||||
remote := rAddr.String()
|
||||
local := lAddr.String()
|
||||
localConn := natTable.GetLocalConn(local, remote)
|
||||
// localConn not exist
|
||||
if localConn == nil {
|
||||
lockKey := remote + "-lock"
|
||||
cond, loaded := natTable.GetOrCreateLockForLocalConn(local, lockKey)
|
||||
if loaded {
|
||||
cond.L.Lock()
|
||||
cond.Wait()
|
||||
// we should get localConn here
|
||||
localConn = natTable.GetLocalConn(local, remote)
|
||||
if localConn == nil {
|
||||
return nil, fmt.Errorf("localConn is nil, nat entry not exist")
|
||||
}
|
||||
cond.L.Unlock()
|
||||
} else {
|
||||
if cond == nil {
|
||||
return nil, fmt.Errorf("cond is nil, nat entry not exist")
|
||||
}
|
||||
defer func() {
|
||||
natTable.DeleteLocalConnMap(local, lockKey)
|
||||
cond.Broadcast()
|
||||
}()
|
||||
conn, err := listenLocalConn(rAddr, lAddr, in)
|
||||
if err != nil {
|
||||
log.Errorln("listenLocalConn failed with error: %s, packet loss", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
natTable.AddLocalConn(local, remote, conn)
|
||||
localConn = conn
|
||||
}
|
||||
}
|
||||
return localConn, nil
|
||||
}
|
||||
|
||||
// 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) {
|
||||
additions := []inbound.Addition{
|
||||
inbound.WithInName("DEFAULT-TPROXY"),
|
||||
inbound.WithSpecialRules(""),
|
||||
}
|
||||
lc, err := dialUDP("udp", rAddr.(*net.UDPAddr).AddrPort(), lAddr.(*net.UDPAddr).AddrPort())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
go func() {
|
||||
log.Debugln("TProxy listenLocalConn rAddr=%s lAddr=%s", rAddr.String(), lAddr.String())
|
||||
for {
|
||||
buf := pool.Get(pool.UDPBufferSize)
|
||||
br, err := lc.Read(buf)
|
||||
if err != nil {
|
||||
pool.Put(buf)
|
||||
if errors.Is(err, net.ErrClosed) {
|
||||
log.Debugln("TProxy local conn listener exit.. rAddr=%s lAddr=%s", rAddr.String(), lAddr.String())
|
||||
return
|
||||
}
|
||||
}
|
||||
// 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...)
|
||||
}
|
||||
}()
|
||||
return lc, nil
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"net"
|
||||
|
||||
"github.com/Dreamacro/clash/common/pool"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
)
|
||||
|
||||
type packet struct {
|
||||
@ -33,3 +34,11 @@ 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
|
||||
}
|
||||
|
Reference in New Issue
Block a user