chore: tuic decrease unneeded copy

This commit is contained in:
wwqgtxx 2023-01-16 09:54:52 +08:00
parent d1565bb46f
commit 643fdd0bce

View file

@ -454,12 +454,10 @@ func NewAddress(metadata *C.Metadata) Address {
switch metadata.AddrType() { switch metadata.AddrType() {
case socks5.AtypIPv4: case socks5.AtypIPv4:
addrType = AtypIPv4 addrType = AtypIPv4
addr = make([]byte, net.IPv4len) addr = metadata.DstIP.AsSlice()
copy(addr[:], metadata.DstIP.AsSlice())
case socks5.AtypIPv6: case socks5.AtypIPv6:
addrType = AtypIPv6 addrType = AtypIPv6
addr = make([]byte, net.IPv6len) addr = metadata.DstIP.AsSlice()
copy(addr[:], metadata.DstIP.AsSlice())
case socks5.AtypDomainName: case socks5.AtypDomainName:
addrType = AtypDomainName addrType = AtypDomainName
addr = make([]byte, len(metadata.Host)+1) addr = make([]byte, len(metadata.Host)+1)
@ -478,18 +476,14 @@ func NewAddress(metadata *C.Metadata) Address {
func NewAddressAddrPort(addrPort netip.AddrPort) Address { func NewAddressAddrPort(addrPort netip.AddrPort) Address {
var addrType byte var addrType byte
var addr []byte
if addrPort.Addr().Is4() { if addrPort.Addr().Is4() {
addrType = AtypIPv4 addrType = AtypIPv4
addr = make([]byte, net.IPv4len)
} else { } else {
addrType = AtypIPv6 addrType = AtypIPv6
addr = make([]byte, net.IPv6len)
} }
copy(addr[:], addrPort.Addr().AsSlice())
return Address{ return Address{
TYPE: addrType, TYPE: addrType,
ADDR: addr, ADDR: addrPort.Addr().AsSlice(),
PORT: addrPort.Port(), PORT: addrPort.Port(),
} }
} }