proxy: improve addr handling

This commit is contained in:
nadoo committed 2022-01-28 23:35:29 +08:00
1 parent e12642b47a
commit 7e7c7553cc
12 files changed
+99 -109

No files matched your search

+8 -7
View File
@@ -2,6 +2,7 @@ package vmess
import (
"net"
"net/netip"
"strconv"
)
@@ -19,6 +20,9 @@ const (
// Addr is vmess addr.
type Addr []byte
// MaxHostLen is the maximum size of host in bytes.
const MaxHostLen = 255
// Port is vmess addr port.
type Port uint16
@@ -32,18 +36,15 @@ func ParseAddr(s string) (Atyp, Addr, Port, error) {
return 0, nil, 0, err
}
if ip := net.ParseIP(host); ip != nil {
if ip4 := ip.To4(); ip4 != nil {
addr = make([]byte, net.IPv4len)
if ip, err := netip.ParseAddr(host); err == nil {
if ip.Is4() {
atyp = AtypIP4
copy(addr[:], ip4)
} else {
addr = make([]byte, net.IPv6len)
atyp = AtypIP6
copy(addr[:], ip)
}
addr = ip.AsSlice()
} else {
if len(host) > 255 {
if len(host) > MaxHostLen {
return 0, nil, 0, err
}
addr = make([]byte, 1+len(host))