diff --git a/common/net/address.go b/common/net/address.go index 3d006201c..83bafe711 100644 --- a/common/net/address.go +++ b/common/net/address.go @@ -4,6 +4,7 @@ import ( "net" "github.com/v2ray/v2ray-core/common/log" + "github.com/v2ray/v2ray-core/common/serial" ) // Address represents a network address to be communicated with. It may be an IP address or domain @@ -28,15 +29,6 @@ func ParseAddress(addr string) Address { return DomainAddress(addr) } -func allZeros(data []byte) bool { - for _, v := range data { - if v != 0 { - return false - } - } - return true -} - // IPAddress creates an Address with given IP and port. func IPAddress(ip []byte) Address { switch len(ip) { @@ -44,7 +36,7 @@ func IPAddress(ip []byte) Address { var addr IPv4Address = [4]byte{ip[0], ip[1], ip[2], ip[3]} return &addr case net.IPv6len: - if allZeros(ip[0:10]) && ip[10] == 0xff && ip[11] == 0xff { + if serial.BytesLiteral(ip[0:10]).All(0) && serial.BytesLiteral(ip[10:12]).All(0xff) { return IPAddress(ip[12:16]) } var addr IPv6Address = [16]byte{ diff --git a/common/serial/bytes.go b/common/serial/bytes.go index 2ffb2570f..3e92eddee 100644 --- a/common/serial/bytes.go +++ b/common/serial/bytes.go @@ -22,13 +22,15 @@ func (this BytesLiteral) Int64Value() int64 { int64(value[7]) } +// String returns a string presentation of this ByteLiteral func (this BytesLiteral) String() string { return string(this.Value()) } -func (this BytesLiteral) AllZero() bool { +// All returns true if all bytes in the ByteLiteral are the same as given value. +func (this BytesLiteral) All(v byte) bool { for _, b := range this { - if b != 0 { + if b != v { return false } } diff --git a/proxy/vmess/id_test.go b/proxy/vmess/id_test.go index 0a6046d4c..e2f713f98 100644 --- a/proxy/vmess/id_test.go +++ b/proxy/vmess/id_test.go @@ -14,5 +14,5 @@ func TestCmdKey(t *testing.T) { v2testing.Current(t) id := NewID(uuid.New()) - assert.Bool(serial.BytesLiteral(id.CmdKey()).AllZero()).IsFalse() + assert.Bool(serial.BytesLiteral(id.CmdKey()).All(0)).IsFalse() }