Fix: TPROXY fakeip (#572)

This commit is contained in:
gVisor bot 2020-03-10 20:36:24 +08:00
parent 2193e571e1
commit ad551c1e30
4 changed files with 23 additions and 17 deletions

View File

@ -35,8 +35,7 @@ func NewRedirUDPProxy(addr string) (*RedirUDPListener, error) {
oob := make([]byte, 1024) oob := make([]byte, 1024)
for { for {
buf := pool.BufPool.Get().([]byte) buf := pool.BufPool.Get().([]byte)
n, oobn, _, lAddr, err := c.ReadMsgUDP(buf, oob)
n, oobn, _, remoteAddr, err := c.ReadMsgUDP(buf, oob)
if err != nil { if err != nil {
pool.BufPool.Put(buf[:cap(buf)]) pool.BufPool.Put(buf[:cap(buf)])
if rl.closed { if rl.closed {
@ -45,11 +44,11 @@ func NewRedirUDPProxy(addr string) (*RedirUDPListener, error) {
continue continue
} }
origDst, err := getOrigDst(oob, oobn) rAddr, err := getOrigDst(oob, oobn)
if err != nil { if err != nil {
continue continue
} }
handleRedirUDP(l, buf[:n], remoteAddr, origDst) handleRedirUDP(l, buf[:n], lAddr, rAddr)
} }
}() }()
@ -65,13 +64,11 @@ func (l *RedirUDPListener) Address() string {
return l.address return l.address
} }
func handleRedirUDP(pc net.PacketConn, buf []byte, addr *net.UDPAddr, origDst *net.UDPAddr) { func handleRedirUDP(pc net.PacketConn, buf []byte, lAddr *net.UDPAddr, rAddr *net.UDPAddr) {
target := socks5.ParseAddrToSocksAddr(origDst) target := socks5.ParseAddrToSocksAddr(rAddr)
packet := &fakeConn{ packet := &fakeConn{
PacketConn: pc, PacketConn: pc,
origDst: origDst, lAddr: lAddr,
rAddr: addr,
buf: buf, buf: buf,
} }
tunnel.AddPacket(adapters.NewPacket(target, packet, C.REDIR)) tunnel.AddPacket(adapters.NewPacket(target, packet, C.REDIR))

View File

@ -8,18 +8,17 @@ import (
type fakeConn struct { type fakeConn struct {
net.PacketConn net.PacketConn
origDst net.Addr lAddr *net.UDPAddr
rAddr net.Addr buf []byte
buf []byte
} }
func (c *fakeConn) Data() []byte { func (c *fakeConn) Data() []byte {
return c.buf return c.buf
} }
// WriteBack opens a new socket binding `origDst` to wirte UDP packet back // WriteBack opens a new socket binding `addr` to wirte UDP packet back
func (c *fakeConn) WriteBack(b []byte, addr net.Addr) (n int, err error) { 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)) tc, err := dialUDP("udp", addr.(*net.UDPAddr), c.lAddr)
if err != nil { if err != nil {
n = 0 n = 0
return return
@ -31,7 +30,7 @@ func (c *fakeConn) WriteBack(b []byte, addr net.Addr) (n int, err error) {
// LocalAddr returns the source IP/Port of UDP Packet // LocalAddr returns the source IP/Port of UDP Packet
func (c *fakeConn) LocalAddr() net.Addr { func (c *fakeConn) LocalAddr() net.Addr {
return c.rAddr return c.lAddr
} }
func (c *fakeConn) Close() error { func (c *fakeConn) Close() error {

View File

@ -88,7 +88,7 @@ func handleUDPToRemote(packet C.UDPPacket, pc C.PacketConn, metadata *C.Metadata
DefaultManager.Upload() <- int64(len(packet.Data())) DefaultManager.Upload() <- int64(len(packet.Data()))
} }
func handleUDPToLocal(packet C.UDPPacket, pc net.PacketConn, key string) { func handleUDPToLocal(packet C.UDPPacket, pc net.PacketConn, key string, fAddr net.Addr) {
buf := pool.BufPool.Get().([]byte) buf := pool.BufPool.Get().([]byte)
defer pool.BufPool.Put(buf[:cap(buf)]) defer pool.BufPool.Put(buf[:cap(buf)])
defer natTable.Delete(key) defer natTable.Delete(key)
@ -101,6 +101,10 @@ func handleUDPToLocal(packet C.UDPPacket, pc net.PacketConn, key string) {
return return
} }
if fAddr != nil {
from = fAddr
}
n, err = packet.WriteBack(buf[:n], from) n, err = packet.WriteBack(buf[:n], from)
if err != nil { if err != nil {
return return

View File

@ -182,6 +182,12 @@ func handleUDPConn(packet *inbound.PacketAdapter) {
return return
} }
// make a fAddr if requset ip is fakeip
var fAddr net.Addr
if enhancedMode != nil && enhancedMode.IsFakeIP(metadata.DstIP) {
fAddr = metadata.UDPAddr()
}
if err := preHandleMetadata(metadata); err != nil { if err := preHandleMetadata(metadata); err != nil {
log.Debugln("[Metadata PreHandle] error: %s", err) log.Debugln("[Metadata PreHandle] error: %s", err)
return return
@ -231,7 +237,7 @@ func handleUDPConn(packet *inbound.PacketAdapter) {
natTable.Set(key, pc) natTable.Set(key, pc)
natTable.Delete(lockKey) natTable.Delete(lockKey)
wg.Done() wg.Done()
go handleUDPToLocal(packet.UDPPacket, pc, key) go handleUDPToLocal(packet.UDPPacket, pc, key, fAddr)
} }
wg.Wait() wg.Wait()