Browse Source

BytesLiteral.All()

Darien Raymond 9 years ago
parent
commit
85feb725a5
3 changed files with 7 additions and 13 deletions
  1. 2 10
      common/net/address.go
  2. 4 2
      common/serial/bytes.go
  3. 1 1
      proxy/vmess/id_test.go

+ 2 - 10
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{

+ 4 - 2
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
     }
   }

+ 1 - 1
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()
 }