Chore: update tproxy udp packet read logic

This commit is contained in:
Dreamacro
2022-06-12 19:37:27 +08:00
parent 06d75da257
commit 2fa1a5c4b9
6 changed files with 71 additions and 53 deletions

View File

@ -398,6 +398,21 @@ func ParseAddrToSocksAddr(addr net.Addr) Addr {
return parsed
}
func AddrFromStdAddrPort(addrPort netip.AddrPort) Addr {
addr := addrPort.Addr()
if addr.Is4() {
ip4 := addr.As4()
return []byte{AtypIPv4, ip4[0], ip4[1], ip4[2], ip4[3], byte(addrPort.Port() >> 8), byte(addrPort.Port())}
}
buf := make([]byte, 1+net.IPv6len+2)
buf[0] = AtypIPv6
copy(buf[1:], addr.AsSlice())
buf[1+net.IPv6len] = byte(addrPort.Port() >> 8)
buf[1+net.IPv6len+1] = byte(addrPort.Port())
return buf
}
// DecodeUDPPacket split `packet` to addr payload, and this function is mutable with `packet`
func DecodeUDPPacket(packet []byte) (addr Addr, payload []byte, err error) {
if len(packet) < 5 {