Fix TPROXY UDP/IPv6

https://github.com/XTLS/Xray-core/issues/137#issuecomment-756064627

十分感谢 @Ninedyz @changyp6
This commit is contained in:
RPRX 2021-01-07 12:21:27 +00:00 committed by GitHub
parent be9421fedf
commit 161e18299c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 27 deletions

View File

@ -163,14 +163,19 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
if !destinationOverridden { if !destinationOverridden {
writer = &buf.SequentialWriter{Writer: conn} writer = &buf.SequentialWriter{Writer: conn}
} else { } else {
var addr *net.UDPAddr back := conn.RemoteAddr().(*net.UDPAddr)
var mark int if !dest.Address.Family().IsIP() {
if dest.Address.Family().IsIP() { if len(back.IP) == 4 {
addr = &net.UDPAddr{ dest.Address = net.AnyIP
} else {
dest.Address = net.AnyIPv6
}
}
addr := &net.UDPAddr{
IP: dest.Address.IP(), IP: dest.Address.IP(),
Port: int(dest.Port), Port: int(dest.Port),
} }
} var mark int
if d.sockopt != nil { if d.sockopt != nil {
mark = int(d.sockopt.Mark) mark = int(d.sockopt.Mark)
} }
@ -178,8 +183,7 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
if err != nil { if err != nil {
return err return err
} }
back := net.DestinationFromAddr(conn.RemoteAddr()) writer = NewPacketWriter(pConn, &dest, mark, back)
writer = NewPacketWriter(pConn, &dest, mark, &back)
defer writer.(*PacketWriter).Close() defer writer.(*PacketWriter).Close()
/* /*
sockopt := &internet.SocketConfig{ sockopt := &internet.SocketConfig{
@ -236,15 +240,12 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
return nil return nil
} }
func NewPacketWriter(conn net.PacketConn, d *net.Destination, mark int, back *net.Destination) buf.Writer { func NewPacketWriter(conn net.PacketConn, d *net.Destination, mark int, back *net.UDPAddr) buf.Writer {
writer := &PacketWriter{ writer := &PacketWriter{
conn: conn, conn: conn,
conns: make(map[net.Destination]net.PacketConn), conns: make(map[net.Destination]net.PacketConn),
mark: mark, mark: mark,
back: &net.UDPAddr{ back: back,
IP: back.Address.IP(),
Port: int(back.Port),
},
} }
writer.conns[*d] = conn writer.conns[*d] = conn
return writer return writer

View File

@ -6,19 +6,11 @@ import (
"fmt" "fmt"
"net" "net"
"os" "os"
"strconv"
"syscall" "syscall"
) )
func FakeUDP(addr *net.UDPAddr, mark int) (net.PacketConn, error) { func FakeUDP(addr *net.UDPAddr, mark int) (net.PacketConn, error) {
if addr == nil {
addr = &net.UDPAddr{
IP: []byte{0, 0, 0, 0},
Port: 0,
}
}
localSocketAddress, af, err := udpAddrToSocketAddr(addr) localSocketAddress, af, err := udpAddrToSocketAddr(addr)
if err != nil { if err != nil {
return nil, &net.OpError{Op: "fake", Err: fmt.Errorf("build local socket address: %s", err)} return nil, &net.OpError{Op: "fake", Err: fmt.Errorf("build local socket address: %s", err)}
@ -75,11 +67,6 @@ func udpAddrToSocketAddr(addr *net.UDPAddr) (syscall.Sockaddr, int, error) {
ip := [16]byte{} ip := [16]byte{}
copy(ip[:], addr.IP.To16()) copy(ip[:], addr.IP.To16())
zoneID, err := strconv.ParseUint(addr.Zone, 10, 32) return &syscall.SockaddrInet6{Addr: ip, Port: addr.Port, ZoneId: 0}, syscall.AF_INET6, nil
if err != nil {
return nil, 0, err
}
return &syscall.SockaddrInet6{Addr: ip, Port: addr.Port, ZoneId: uint32(zoneID)}, syscall.AF_INET6, nil
} }
} }