Feature: add UDP TPROXY support on Linux (#562)

This commit is contained in:
duama
2020-03-08 21:58:49 +08:00
committed by GitHub
parent b2c9cbb43e
commit f7f30d3406
7 changed files with 327 additions and 0 deletions

41
proxy/redir/utils.go Normal file
View File

@ -0,0 +1,41 @@
package redir
import (
"net"
"github.com/Dreamacro/clash/common/pool"
)
type fakeConn struct {
net.PacketConn
origDst net.Addr
rAddr net.Addr
buf []byte
}
func (c *fakeConn) Data() []byte {
return c.buf
}
// WriteBack opens a new socket binding `origDst` to wirte UDP packet back
func (c *fakeConn) WriteBack(b []byte, addr net.Addr) (n int, err error) {
tc, err := dialUDP("udp", c.origDst.(*net.UDPAddr), c.rAddr.(*net.UDPAddr))
if err != nil {
n = 0
return
}
n, err = tc.Write(b)
tc.Close()
return
}
// LocalAddr returns the source IP/Port of UDP Packet
func (c *fakeConn) LocalAddr() net.Addr {
return c.rAddr
}
func (c *fakeConn) Close() error {
err := c.PacketConn.Close()
pool.BufPool.Put(c.buf[:cap(c.buf)])
return err
}