1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -05:00

Remove serial.Bytes

This commit is contained in:
v2ray 2016-05-24 22:09:22 +02:00
parent fc63f0432c
commit ab39750ceb
12 changed files with 35 additions and 68 deletions

View File

@ -5,6 +5,7 @@ import (
"github.com/v2ray/v2ray-core/common" "github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/common/alloc" "github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/serial"
) )
type LogEntry interface { type LogEntry interface {
@ -40,6 +41,8 @@ func (this *ErrorLog) String() string {
b.AppendString(typedVal.String()) b.AppendString(typedVal.String())
case error: case error:
b.AppendString(typedVal.Error()) b.AppendString(typedVal.Error())
case []byte:
b.AppendString(serial.BytesToHexString(typedVal))
default: default:
b.AppendString(fmt.Sprint(value)) b.AppendString(fmt.Sprint(value))
} }

View File

@ -4,7 +4,7 @@ import (
"net" "net"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
"github.com/v2ray/v2ray-core/common/serial" "github.com/v2ray/v2ray-core/common/predicate"
) )
var ( var (
@ -42,7 +42,7 @@ func IPAddress(ip []byte) Address {
var addr ipv4Address = [4]byte{ip[0], ip[1], ip[2], ip[3]} var addr ipv4Address = [4]byte{ip[0], ip[1], ip[2], ip[3]}
return &addr return &addr
case net.IPv6len: 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]) return IPAddress(ip[12:16])
} }
var addr ipv6Address = [16]byte{ var addr ipv6Address = [16]byte{

View File

@ -18,7 +18,7 @@ type Port uint16
// PortFromBytes converts a byte array to a Port, assuming bytes are in big endian order. // 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. // @unsafe Caller must ensure that the byte array has at least 2 elements.
func PortFromBytes(port []byte) Port { func PortFromBytes(port []byte) Port {
return Port(serial.BytesT(port).Uint16Value()) return Port(serial.BytesToUint16(port))
} }
// PortFromInt converts an integer to a Port. // PortFromInt converts an integer to a Port.

View File

@ -0,0 +1,10 @@
package predicate
func BytesAll(array []byte, b byte) bool {
for _, v := range array {
if v != b {
return false
}
}
return true
}

View File

@ -3,8 +3,8 @@ package protocol_test
import ( import (
"testing" "testing"
"github.com/v2ray/v2ray-core/common/predicate"
. "github.com/v2ray/v2ray-core/common/protocol" . "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/common/uuid"
"github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/assert"
) )
@ -13,5 +13,5 @@ func TestCmdKey(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
id := NewID(uuid.New()) id := NewID(uuid.New())
assert.Bool(serial.BytesT(id.CmdKey()).All(0)).IsFalse() assert.Bool(predicate.BytesAll(id.CmdKey(), 0)).IsFalse()
} }

View File

@ -55,7 +55,7 @@ func UnmarshalCommand(cmdId byte, data []byte) (protocol.ResponseCommand, error)
return nil, transport.ErrorCorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
expectedAuth := Authenticate(data[4:]) expectedAuth := Authenticate(data[4:])
actualAuth := serial.BytesT(data[:4]).Uint32Value() actualAuth := serial.BytesToUint32(data[:4])
if expectedAuth != actualAuth { if expectedAuth != actualAuth {
return nil, transport.ErrorCorruptedPacket return nil, transport.ErrorCorruptedPacket
} }

View File

@ -134,7 +134,7 @@ func (this *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Requ
fnv1a := fnv.New32a() fnv1a := fnv.New32a()
fnv1a.Write(buffer.Value[:bufferLen]) fnv1a.Write(buffer.Value[:bufferLen])
actualHash := fnv1a.Sum32() actualHash := fnv1a.Sum32()
expectedHash := serial.BytesT(buffer.Value[bufferLen : bufferLen+4]).Uint32Value() expectedHash := serial.BytesToUint32(buffer.Value[bufferLen : bufferLen+4])
if actualHash != expectedHash { if actualHash != expectedHash {
return nil, transport.ErrorCorruptedPacket return nil, transport.ErrorCorruptedPacket

View File

@ -1,7 +1,6 @@
package serial package serial
import ( import (
"bytes"
"encoding/hex" "encoding/hex"
"strings" "strings"
) )
@ -14,52 +13,14 @@ func BytesToUint16(value []byte) uint16 {
return uint16(value[0])<<8 + uint16(value[1]) return uint16(value[0])<<8 + uint16(value[1])
} }
func BytesToHexString(value []byte) string { func BytesToUint32(value []byte) uint32 {
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()
return uint32(value[0])<<24 + return uint32(value[0])<<24 +
uint32(value[1])<<16 + uint32(value[1])<<16 +
uint32(value[2])<<8 + uint32(value[2])<<8 +
uint32(value[3]) uint32(value[3])
} }
func (this BytesT) Int64Value() int64 { func BytesToInt64(value []byte) int64 {
value := this.Value()
return int64(value[0])<<56 + return int64(value[0])<<56 +
int64(value[1])<<48 + int64(value[1])<<48 +
int64(value[2])<<40 + int64(value[2])<<40 +
@ -70,17 +31,10 @@ func (this BytesT) Int64Value() int64 {
int64(value[7]) int64(value[7])
} }
// String returns a string presentation of this ByteLiteral func BytesToHexString(value []byte) string {
func (this BytesT) String() string { strs := make([]string, len(value))
return string(this.Value()) for i, b := range value {
} strs[i] = hex.EncodeToString([]byte{b})
// 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
}
} }
return true return "[" + strings.Join(strs, ",") + "]"
} }

View File

@ -16,7 +16,6 @@ import (
v2io "github.com/v2ray/v2ray-core/common/io" v2io "github.com/v2ray/v2ray-core/common/io"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net" 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"
"github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/proxy/internal"
"github.com/v2ray/v2ray-core/transport/hub" "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. requestBuffer := alloc.NewBuffer().Clear() // Don't release this buffer as it is passed into a Packet.
request.Write(requestBuffer) 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 := this.packetDispatcher.DispatchToOutbound(dest)
ray.InboundInput().Write(requestBuffer) ray.InboundInput().Write(requestBuffer)

View File

@ -1,6 +1,7 @@
package shadowsocks package shadowsocks
import ( import (
"bytes"
"crypto/hmac" "crypto/hmac"
"crypto/sha1" "crypto/sha1"
"io" "io"
@ -79,7 +80,7 @@ func (this *ChunkReader) Read() (*alloc.Buffer, error) {
} }
// There is a potential buffer overflow here. Large buffer is 64K bytes, // There is a potential buffer overflow here. Large buffer is 64K bytes,
// while uin16 + 10 will be more than that // 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 { if _, err := io.ReadFull(this.reader, buffer.Value[:length]); err != nil {
buffer.Release() buffer.Release()
return nil, err return nil, err
@ -90,7 +91,7 @@ func (this *ChunkReader) Read() (*alloc.Buffer, error) {
payload := buffer.Value[AuthSize:] payload := buffer.Value[AuthSize:]
actualAuthBytes := this.auth.Authenticate(nil, payload) actualAuthBytes := this.auth.Authenticate(nil, payload)
if !serial.BytesT(authBytes).Equals(serial.BytesT(actualAuthBytes)) { if !bytes.Equal(authBytes, actualAuthBytes) {
buffer.Release() buffer.Release()
log.Debug("AuthenticationReader: Unexpected auth: ", authBytes) log.Debug("AuthenticationReader: Unexpected auth: ", authBytes)
return nil, transport.ErrorCorruptedPacket return nil, transport.ErrorCorruptedPacket

View File

@ -1,12 +1,12 @@
package shadowsocks package shadowsocks
import ( import (
"bytes"
"io" "io"
"github.com/v2ray/v2ray-core/common/alloc" "github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net" 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"
"github.com/v2ray/v2ray-core/transport" "github.com/v2ray/v2ray-core/transport"
) )
@ -129,7 +129,7 @@ func ReadRequest(reader io.Reader, auth *Authenticator, udp bool) (*Request, err
if request.OTA { if request.OTA {
actualAuth := auth.Authenticate(nil, buffer.Value[0:lenBuffer]) 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.Debug("Shadowsocks: Invalid OTA. Expecting ", actualAuth, ", but got ", authBytes)
log.Warning("Shadowsocks: Invalid OTA.") log.Warning("Shadowsocks: Invalid OTA.")
return nil, proxy.ErrorInvalidAuthentication return nil, proxy.ErrorInvalidAuthentication

View File

@ -73,9 +73,9 @@ func (this *AuthChunkReader) Read() (*alloc.Buffer, error) {
} }
} }
log.Debug("VMess Reader: raw buffer: ", buffer.Value) 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.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) buffer.SliceFrom(6)
} else if buffer.Len() < this.chunkLength { } else if buffer.Len() < this.chunkLength {
_, err := buffer.FillFrom(this.reader) _, err := buffer.FillFrom(this.reader)