From bd9e6e5cd96f647e9190369d2285d9ba179674b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 12 Apr 2023 20:44:17 +0800 Subject: [PATCH] Fix processing domain address in packet --- common/dialer/resolve.go | 2 +- common/mux/client.go | 6 +++++- go.mod | 4 ++-- go.sum | 8 ++++---- transport/fakeip/packet.go | 25 +++++++++++++++++++------ transport/hysteria/protocol.go | 7 ++++++- transport/trojan/protocol.go | 6 +++++- transport/vless/client.go | 6 +++++- transport/vless/service.go | 6 +++++- 9 files changed, 52 insertions(+), 18 deletions(-) diff --git a/common/dialer/resolve.go b/common/dialer/resolve.go index 66b86097..327d3610 100644 --- a/common/dialer/resolve.go +++ b/common/dialer/resolve.go @@ -73,7 +73,7 @@ func (d *ResolveDialer) ListenPacket(ctx context.Context, destination M.Socksadd if err != nil { return nil, err } - return bufio.NewNATPacketConn(bufio.NewPacketConn(conn), destination, M.SocksaddrFrom(destinationAddress, destination.Port)), nil + return bufio.NewNATPacketConn(bufio.NewPacketConn(conn), M.SocksaddrFrom(destinationAddress, destination.Port), destination), nil } func (d *ResolveDialer) Upstream() any { diff --git a/common/mux/client.go b/common/mux/client.go index 8ade94e4..265e17ed 100644 --- a/common/mux/client.go +++ b/common/mux/client.go @@ -414,7 +414,11 @@ func (c *ClientPacketAddrConn) ReadFrom(p []byte) (n int, addr net.Addr, err err if err != nil { return } - addr = destination.UDPAddr() + if destination.IsFqdn() { + addr = destination + } else { + addr = destination.UDPAddr() + } var length uint16 err = binary.Read(c.ExtendedConn, binary.BigEndian, &length) if err != nil { diff --git a/go.mod b/go.mod index db37c4ae..eddd1344 100644 --- a/go.mod +++ b/go.mod @@ -27,10 +27,10 @@ require ( github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 github.com/sagernet/sing v0.2.3-0.20230412070623-d88db59703cd github.com/sagernet/sing-dns v0.1.5-0.20230408004833-5adaf486d440 - github.com/sagernet/sing-shadowsocks v0.2.1-0.20230409094647-5c830455eb9b + github.com/sagernet/sing-shadowsocks v0.2.1-0.20230412123110-1a7c32b4e2e7 github.com/sagernet/sing-shadowtls v0.1.1-0.20230409094821-9abef019436f github.com/sagernet/sing-tun v0.1.4-0.20230326080954-8848c0e4cbab - github.com/sagernet/sing-vmess v0.1.4-0.20230409094745-dc784c704250 + github.com/sagernet/sing-vmess v0.1.4-0.20230412122845-9470e68f5e45 github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37 github.com/sagernet/tfo-go v0.0.0-20230303015439-ffcfd8c41cf9 github.com/sagernet/utls v0.0.0-20230309024959-6732c2ab36f2 diff --git a/go.sum b/go.sum index effc09d4..e05f2c7a 100644 --- a/go.sum +++ b/go.sum @@ -115,14 +115,14 @@ github.com/sagernet/sing v0.2.3-0.20230412070623-d88db59703cd h1:SVO7Lr04d8P6EMr github.com/sagernet/sing v0.2.3-0.20230412070623-d88db59703cd/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w= github.com/sagernet/sing-dns v0.1.5-0.20230408004833-5adaf486d440 h1:VH8/BcOVuApHtS+vKP+khxlGRcXH7KKhgkTDtNynqSQ= github.com/sagernet/sing-dns v0.1.5-0.20230408004833-5adaf486d440/go.mod h1:69PNSHyEmXdjf6C+bXBOdr2GQnPeEyWjIzo/MV8gmz8= -github.com/sagernet/sing-shadowsocks v0.2.1-0.20230409094647-5c830455eb9b h1:nmP+V4nlc8lqEMpwjjbny8ISkrFIjvKWIETsjs7nSic= -github.com/sagernet/sing-shadowsocks v0.2.1-0.20230409094647-5c830455eb9b/go.mod h1:WoVjGUvRqsx5yhYeDAB5CijCHpNDi0LUPHl3cf7u8Lc= +github.com/sagernet/sing-shadowsocks v0.2.1-0.20230412123110-1a7c32b4e2e7 h1:3WDMIF1aE/twc5gJ+9PF2ZJqUxwZ80MPtNBKE3yBevU= +github.com/sagernet/sing-shadowsocks v0.2.1-0.20230412123110-1a7c32b4e2e7/go.mod h1:WoVjGUvRqsx5yhYeDAB5CijCHpNDi0LUPHl3cf7u8Lc= github.com/sagernet/sing-shadowtls v0.1.1-0.20230409094821-9abef019436f h1:qzQvpcDm60zPW8UlZa8UEaBoFORFeGAnhDncPc3VWT4= github.com/sagernet/sing-shadowtls v0.1.1-0.20230409094821-9abef019436f/go.mod h1:MxB+Q9H0pAHcrlvNmwSs1crljRwHFFVhtXyOMBy44Nw= github.com/sagernet/sing-tun v0.1.4-0.20230326080954-8848c0e4cbab h1:a9oeWuPBuIZ70qMhIIH6RrYhp886xN9jJIwsuu4ZFUo= github.com/sagernet/sing-tun v0.1.4-0.20230326080954-8848c0e4cbab/go.mod h1:4YxIDEkkCjGXDOTMPw1SXpLmCQUFAWuaQN250oo+928= -github.com/sagernet/sing-vmess v0.1.4-0.20230409094745-dc784c704250 h1:Q5X89Z7wV201pLhPm+viAzzVgPcrYD1PniAVGt9Le/0= -github.com/sagernet/sing-vmess v0.1.4-0.20230409094745-dc784c704250/go.mod h1:eULig3LgaeNiWSquSlzXF42Joypsj3fO1W+Qy93o6hk= +github.com/sagernet/sing-vmess v0.1.4-0.20230412122845-9470e68f5e45 h1:QqYhWah3u+o2tvLRuTfEu3BwsGpf/wNnVK/VNQV2YBM= +github.com/sagernet/sing-vmess v0.1.4-0.20230412122845-9470e68f5e45/go.mod h1:eULig3LgaeNiWSquSlzXF42Joypsj3fO1W+Qy93o6hk= github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37 h1:HuE6xSwco/Xed8ajZ+coeYLmioq0Qp1/Z2zczFaV8as= github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37/go.mod h1:3skNSftZDJWTGVtVaM2jfbce8qHnmH/AGDRe62iNOg0= github.com/sagernet/tfo-go v0.0.0-20230303015439-ffcfd8c41cf9 h1:2ItpW1nMNkPzmBTxV0/eClCklHrFSQMnUGcpUmJxVeE= diff --git a/transport/fakeip/packet.go b/transport/fakeip/packet.go index 748d56f4..620acb92 100644 --- a/transport/fakeip/packet.go +++ b/transport/fakeip/packet.go @@ -17,22 +17,30 @@ type NATPacketConn struct { func NewNATPacketConn(conn N.PacketConn, origin M.Socksaddr, destination M.Socksaddr) *NATPacketConn { return &NATPacketConn{ PacketConn: conn, - origin: origin, - destination: destination, + origin: socksaddrWithoutPort(origin), + destination: socksaddrWithoutPort(destination), } } func (c *NATPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) { destination, err = c.PacketConn.ReadPacket(buffer) - if destination == c.origin { - destination = c.destination + if socksaddrWithoutPort(destination) == c.origin { + destination = M.Socksaddr{ + Addr: c.destination.Addr, + Fqdn: c.destination.Fqdn, + Port: destination.Port, + } } return } func (c *NATPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error { - if destination == c.destination { - destination = c.origin + if socksaddrWithoutPort(destination) == c.destination { + destination = M.Socksaddr{ + Addr: c.origin.Addr, + Fqdn: c.origin.Fqdn, + Port: destination.Port, + } } return c.PacketConn.WritePacket(buffer, destination) } @@ -40,3 +48,8 @@ func (c *NATPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) func (c *NATPacketConn) Upstream() any { return c.PacketConn } + +func socksaddrWithoutPort(destination M.Socksaddr) M.Socksaddr { + destination.Port = 0 + return destination +} diff --git a/transport/hysteria/protocol.go b/transport/hysteria/protocol.go index beed65a3..179dad34 100644 --- a/transport/hysteria/protocol.go +++ b/transport/hysteria/protocol.go @@ -498,7 +498,12 @@ func (c *PacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { return } n = copy(p, msg.Data) - addr = M.ParseSocksaddrHostPort(msg.Host, msg.Port).UDPAddr() + destination := M.ParseSocksaddrHostPort(msg.Host, msg.Port) + if destination.IsFqdn() { + addr = destination + } else { + addr = destination.UDPAddr() + } return } diff --git a/transport/trojan/protocol.go b/transport/trojan/protocol.go index ad9174d1..12195f58 100644 --- a/transport/trojan/protocol.go +++ b/transport/trojan/protocol.go @@ -131,7 +131,11 @@ func (c *ClientPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) return } n = buffer.Len() - addr = destination.UDPAddr() + if destination.IsFqdn() { + addr = destination + } else { + addr = destination.UDPAddr() + } return } diff --git a/transport/vless/client.go b/transport/vless/client.go index aef7fa82..80652c8e 100644 --- a/transport/vless/client.go +++ b/transport/vless/client.go @@ -198,7 +198,11 @@ func (c *PacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { if err != nil { return } - addr = c.destination.UDPAddr() + if c.destination.IsFqdn() { + addr = c.destination + } else { + addr = c.destination.UDPAddr() + } return } diff --git a/transport/vless/service.go b/transport/vless/service.go index cfa04be2..1110746a 100644 --- a/transport/vless/service.go +++ b/transport/vless/service.go @@ -147,7 +147,11 @@ func (c *serverPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) if err != nil { return } - addr = c.destination.UDPAddr() + if c.destination.IsFqdn() { + addr = c.destination + } else { + addr = c.destination.UDPAddr() + } return }