Improve bufio.NATPacketConn for fake ip, domain_strategy and sniff_override_destination

This commit is contained in:
dyhkwong 2024-04-09 23:07:50 +08:00 committed by GitHub
parent 10f242e3c9
commit 40190f9aa3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -37,9 +37,15 @@ type unidirectionalNATPacketConn struct {
}
func (c *unidirectionalNATPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
if M.SocksaddrFromNet(addr) == c.destination {
addr = c.origin.UDPAddr()
destination := M.SocksaddrFromNet(addr)
if destination == c.destination {
destination = c.origin
}
if c.destination.Port == c.origin.Port && destination.AddrString() == c.destination.AddrString() {
destination.Addr = c.origin.Addr
destination.Fqdn = c.origin.Fqdn
}
addr = destination.UDPAddr()
return c.NetPacketConn.WriteTo(p, addr)
}
@ -47,6 +53,10 @@ func (c *unidirectionalNATPacketConn) WritePacket(buffer *buf.Buffer, destinatio
if destination == c.destination {
destination = c.origin
}
if c.destination.Port == c.origin.Port && destination.AddrString() == c.destination.AddrString() {
destination.Addr = c.origin.Addr
destination.Fqdn = c.origin.Fqdn
}
return c.NetPacketConn.WritePacket(buffer, destination)
}
@ -66,16 +76,28 @@ type bidirectionalNATPacketConn struct {
func (c *bidirectionalNATPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
n, addr, err = c.NetPacketConn.ReadFrom(p)
if err == nil && M.SocksaddrFromNet(addr) == c.origin {
addr = c.destination.UDPAddr()
destination := M.SocksaddrFromNet(addr)
if err == nil && destination == c.origin {
destination = c.destination
}
if c.destination.Port == c.origin.Port && destination.AddrString() == c.origin.AddrString() {
destination.Addr = c.destination.Addr
destination.Fqdn = c.destination.Fqdn
}
addr = destination.UDPAddr()
return
}
func (c *bidirectionalNATPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
if M.SocksaddrFromNet(addr) == c.destination {
addr = c.origin.UDPAddr()
destination := M.SocksaddrFromNet(addr)
if destination == c.destination {
destination = c.origin
}
if c.destination.Port == c.origin.Port && destination.AddrString() == c.destination.AddrString() {
destination.Addr = c.origin.Addr
destination.Fqdn = c.origin.Fqdn
}
addr = destination.UDPAddr()
return c.NetPacketConn.WriteTo(p, addr)
}
@ -84,6 +106,10 @@ func (c *bidirectionalNATPacketConn) ReadPacket(buffer *buf.Buffer) (destination
if destination == c.origin {
destination = c.destination
}
if c.destination.Port == c.origin.Port && destination.AddrString() == c.origin.AddrString() {
destination.Addr = c.destination.Addr
destination.Fqdn = c.destination.Fqdn
}
return
}
@ -91,6 +117,10 @@ func (c *bidirectionalNATPacketConn) WritePacket(buffer *buf.Buffer, destination
if destination == c.destination {
destination = c.origin
}
if c.destination.Port == c.origin.Port && destination.AddrString() == c.destination.AddrString() {
destination.Addr = c.origin.Addr
destination.Fqdn = c.origin.Fqdn
}
return c.NetPacketConn.WritePacket(buffer, destination)
}