diff --git a/common/log/internal/log_entry.go b/common/log/internal/log_entry.go index 8e87d8910..44bbc1fbf 100644 --- a/common/log/internal/log_entry.go +++ b/common/log/internal/log_entry.go @@ -5,6 +5,7 @@ import ( "github.com/v2ray/v2ray-core/common" "github.com/v2ray/v2ray-core/common/alloc" + "github.com/v2ray/v2ray-core/common/serial" ) type LogEntry interface { @@ -40,6 +41,8 @@ func (this *ErrorLog) String() string { b.AppendString(typedVal.String()) case error: b.AppendString(typedVal.Error()) + case []byte: + b.AppendString(serial.BytesToHexString(typedVal)) default: b.AppendString(fmt.Sprint(value)) } diff --git a/common/net/address.go b/common/net/address.go index 2558f4a65..d05244b74 100644 --- a/common/net/address.go +++ b/common/net/address.go @@ -4,7 +4,7 @@ import ( "net" "github.com/v2ray/v2ray-core/common/log" - "github.com/v2ray/v2ray-core/common/serial" + "github.com/v2ray/v2ray-core/common/predicate" ) var ( @@ -42,7 +42,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 serial.BytesT(ip[0:10]).All(0) && serial.BytesT(ip[10:12]).All(0xff) { + if predicate.BytesAll(ip[0:10], 0) && predicate.BytesAll(ip[10:12], 0xff) { return IPAddress(ip[12:16]) } var addr ipv6Address = [16]byte{ diff --git a/common/net/port.go b/common/net/port.go index da3e34cc3..0b4ccb24a 100644 --- a/common/net/port.go +++ b/common/net/port.go @@ -18,7 +18,7 @@ type Port uint16 // PortFromBytes converts a byte array to a Port, assuming bytes are in big endian order. // @unsafe Caller must ensure that the byte array has at least 2 elements. func PortFromBytes(port []byte) Port { - return Port(serial.BytesT(port).Uint16Value()) + return Port(serial.BytesToUint16(port)) } // PortFromInt converts an integer to a Port. diff --git a/common/predicate/arrays.go b/common/predicate/arrays.go new file mode 100644 index 000000000..0b4427f7d --- /dev/null +++ b/common/predicate/arrays.go @@ -0,0 +1,10 @@ +package predicate + +func BytesAll(array []byte, b byte) bool { + for _, v := range array { + if v != b { + return false + } + } + return true +} diff --git a/common/protocol/id_test.go b/common/protocol/id_test.go index 5df9d87e5..f3edefbce 100644 --- a/common/protocol/id_test.go +++ b/common/protocol/id_test.go @@ -3,8 +3,8 @@ package protocol_test import ( "testing" + "github.com/v2ray/v2ray-core/common/predicate" . "github.com/v2ray/v2ray-core/common/protocol" - "github.com/v2ray/v2ray-core/common/serial" "github.com/v2ray/v2ray-core/common/uuid" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -13,5 +13,5 @@ func TestCmdKey(t *testing.T) { assert := assert.On(t) id := NewID(uuid.New()) - assert.Bool(serial.BytesT(id.CmdKey()).All(0)).IsFalse() + assert.Bool(predicate.BytesAll(id.CmdKey(), 0)).IsFalse() } diff --git a/common/protocol/raw/commands.go b/common/protocol/raw/commands.go index 01f4e9285..e86e9fab2 100644 --- a/common/protocol/raw/commands.go +++ b/common/protocol/raw/commands.go @@ -55,7 +55,7 @@ func UnmarshalCommand(cmdId byte, data []byte) (protocol.ResponseCommand, error) return nil, transport.ErrorCorruptedPacket } expectedAuth := Authenticate(data[4:]) - actualAuth := serial.BytesT(data[:4]).Uint32Value() + actualAuth := serial.BytesToUint32(data[:4]) if expectedAuth != actualAuth { return nil, transport.ErrorCorruptedPacket } diff --git a/common/protocol/raw/server.go b/common/protocol/raw/server.go index b196bbdb6..43850d053 100644 --- a/common/protocol/raw/server.go +++ b/common/protocol/raw/server.go @@ -134,7 +134,7 @@ func (this *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Requ fnv1a := fnv.New32a() fnv1a.Write(buffer.Value[:bufferLen]) actualHash := fnv1a.Sum32() - expectedHash := serial.BytesT(buffer.Value[bufferLen : bufferLen+4]).Uint32Value() + expectedHash := serial.BytesToUint32(buffer.Value[bufferLen : bufferLen+4]) if actualHash != expectedHash { return nil, transport.ErrorCorruptedPacket diff --git a/common/serial/bytes.go b/common/serial/bytes.go index c086933f7..26ac79ada 100644 --- a/common/serial/bytes.go +++ b/common/serial/bytes.go @@ -1,7 +1,6 @@ package serial import ( - "bytes" "encoding/hex" "strings" ) @@ -14,52 +13,14 @@ func BytesToUint16(value []byte) uint16 { return uint16(value[0])<<8 + uint16(value[1]) } -func BytesToHexString(value []byte) string { - strs := make([]string, len(value)) - for i, b := range value { - strs[i] = hex.EncodeToString([]byte{b}) - } - return "[" + strings.Join(strs, ",") + "]" -} - -type BytesT []byte - -func (this BytesT) Value() []byte { - return []byte(this) -} - -func (this BytesT) Equals(another BytesT) bool { - return bytes.Equal(this.Value(), another.Value()) -} - -func (this BytesT) Uint8Value() uint8 { - return this.Value()[0] -} - -func (this BytesT) Uint16() Uint16Literal { - return Uint16Literal(this.Uint16Value()) -} - -func (this BytesT) Uint16Value() uint16 { - value := this.Value() - return uint16(value[0])<<8 + uint16(value[1]) -} - -func (this BytesT) IntValue() int { - value := this.Value() - return int(value[0])<<24 + int(value[1])<<16 + int(value[2])<<8 + int(value[3]) -} - -func (this BytesT) Uint32Value() uint32 { - value := this.Value() +func BytesToUint32(value []byte) uint32 { return uint32(value[0])<<24 + uint32(value[1])<<16 + uint32(value[2])<<8 + uint32(value[3]) } -func (this BytesT) Int64Value() int64 { - value := this.Value() +func BytesToInt64(value []byte) int64 { return int64(value[0])<<56 + int64(value[1])<<48 + int64(value[2])<<40 + @@ -70,17 +31,10 @@ func (this BytesT) Int64Value() int64 { int64(value[7]) } -// String returns a string presentation of this ByteLiteral -func (this BytesT) String() string { - return string(this.Value()) -} - -// All returns true if all bytes in the ByteLiteral are the same as given value. -func (this BytesT) All(v byte) bool { - for _, b := range this { - if b != v { - return false - } +func BytesToHexString(value []byte) string { + strs := make([]string, len(value)) + for i, b := range value { + strs[i] = hex.EncodeToString([]byte{b}) } - return true + return "[" + strings.Join(strs, ",") + "]" } diff --git a/proxy/http/server.go b/proxy/http/server.go index f12d33b33..bf365bc93 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -16,7 +16,6 @@ import ( v2io "github.com/v2ray/v2ray-core/common/io" "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/common/serial" "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/transport/hub" @@ -231,7 +230,7 @@ func (this *HttpProxyServer) handlePlainHTTP(request *http.Request, dest v2net.D requestBuffer := alloc.NewBuffer().Clear() // Don't release this buffer as it is passed into a Packet. request.Write(requestBuffer) - log.Debug("Request to remote:\n", serial.BytesT(requestBuffer.Value)) + log.Debug("Request to remote:\n", requestBuffer.Value) ray := this.packetDispatcher.DispatchToOutbound(dest) ray.InboundInput().Write(requestBuffer) diff --git a/proxy/shadowsocks/ota.go b/proxy/shadowsocks/ota.go index cd692f8ce..9de61c20d 100644 --- a/proxy/shadowsocks/ota.go +++ b/proxy/shadowsocks/ota.go @@ -1,6 +1,7 @@ package shadowsocks import ( + "bytes" "crypto/hmac" "crypto/sha1" "io" @@ -79,7 +80,7 @@ func (this *ChunkReader) Read() (*alloc.Buffer, error) { } // There is a potential buffer overflow here. Large buffer is 64K bytes, // while uin16 + 10 will be more than that - length := serial.BytesT(buffer.Value[:2]).Uint16Value() + AuthSize + length := serial.BytesToUint16(buffer.Value[:2]) + AuthSize if _, err := io.ReadFull(this.reader, buffer.Value[:length]); err != nil { buffer.Release() return nil, err @@ -90,7 +91,7 @@ func (this *ChunkReader) Read() (*alloc.Buffer, error) { payload := buffer.Value[AuthSize:] actualAuthBytes := this.auth.Authenticate(nil, payload) - if !serial.BytesT(authBytes).Equals(serial.BytesT(actualAuthBytes)) { + if !bytes.Equal(authBytes, actualAuthBytes) { buffer.Release() log.Debug("AuthenticationReader: Unexpected auth: ", authBytes) return nil, transport.ErrorCorruptedPacket diff --git a/proxy/shadowsocks/protocol.go b/proxy/shadowsocks/protocol.go index 04f228d79..fc8a9e21d 100644 --- a/proxy/shadowsocks/protocol.go +++ b/proxy/shadowsocks/protocol.go @@ -1,12 +1,12 @@ package shadowsocks import ( + "bytes" "io" "github.com/v2ray/v2ray-core/common/alloc" "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/common/serial" "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/transport" ) @@ -129,7 +129,7 @@ func ReadRequest(reader io.Reader, auth *Authenticator, udp bool) (*Request, err if request.OTA { actualAuth := auth.Authenticate(nil, buffer.Value[0:lenBuffer]) - if !serial.BytesT(actualAuth).Equals(serial.BytesT(authBytes)) { + if !bytes.Equal(actualAuth, authBytes) { log.Debug("Shadowsocks: Invalid OTA. Expecting ", actualAuth, ", but got ", authBytes) log.Warning("Shadowsocks: Invalid OTA.") return nil, proxy.ErrorInvalidAuthentication diff --git a/proxy/vmess/io/reader.go b/proxy/vmess/io/reader.go index 6663d4bba..4c59f8856 100644 --- a/proxy/vmess/io/reader.go +++ b/proxy/vmess/io/reader.go @@ -73,9 +73,9 @@ func (this *AuthChunkReader) Read() (*alloc.Buffer, error) { } } log.Debug("VMess Reader: raw buffer: ", buffer.Value) - length := serial.BytesT(buffer.Value[:2]).Uint16Value() + length := serial.BytesToUint16(buffer.Value[:2]) this.chunkLength = int(length) - 4 - this.validator = NewValidator(serial.BytesT(buffer.Value[2:6]).Uint32Value()) + this.validator = NewValidator(serial.BytesToUint32(buffer.Value[2:6])) buffer.SliceFrom(6) } else if buffer.Len() < this.chunkLength { _, err := buffer.FillFrom(this.reader)