1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00

Shorten BytesLiteral

This commit is contained in:
v2ray 2016-05-23 20:21:23 +02:00
parent 5b23d25e35
commit cfdda19834
10 changed files with 23 additions and 23 deletions

View File

@ -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.BytesLiteral(ip[0:10]).All(0) && serial.BytesLiteral(ip[10:12]).All(0xff) {
if serial.BytesT(ip[0:10]).All(0) && serial.BytesT(ip[10:12]).All(0xff) {
return IPAddress(ip[12:16])
}
var addr ipv6Address = [16]byte{

View File

@ -18,7 +18,7 @@ type Port serial.Uint16Literal
// 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.BytesLiteral(port).Uint16Value())
return Port(serial.BytesT(port).Uint16Value())
}
// PortFromInt converts an integer to a Port.

View File

@ -14,5 +14,5 @@ func TestCmdKey(t *testing.T) {
v2testing.Current(t)
id := NewID(uuid.New())
assert.Bool(serial.BytesLiteral(id.CmdKey()).All(0)).IsFalse()
assert.Bool(serial.BytesT(id.CmdKey()).All(0)).IsFalse()
}

View File

@ -55,7 +55,7 @@ func UnmarshalCommand(cmdId byte, data []byte) (protocol.ResponseCommand, error)
return nil, transport.ErrorCorruptedPacket
}
expectedAuth := Authenticate(data[4:])
actualAuth := serial.BytesLiteral(data[:4]).Uint32Value()
actualAuth := serial.BytesT(data[:4]).Uint32Value()
if expectedAuth != actualAuth {
return nil, transport.ErrorCorruptedPacket
}
@ -132,7 +132,7 @@ func (this *CommandSwitchAccountFactory) Unmarshal(data []byte) (interface{}, er
if len(data) < alterIdStart+2 {
return nil, transport.ErrorCorruptedPacket
}
cmd.AlterIds = serial.BytesLiteral(data[alterIdStart : alterIdStart+2]).Uint16()
cmd.AlterIds = serial.BytesT(data[alterIdStart : alterIdStart+2]).Uint16()
levelStart := alterIdStart + 2
if len(data) < levelStart+1 {
return nil, transport.ErrorCorruptedPacket

View File

@ -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.BytesLiteral(buffer.Value[bufferLen : bufferLen+4]).Uint32Value()
expectedHash := serial.BytesT(buffer.Value[bufferLen : bufferLen+4]).Uint32Value()
if actualHash != expectedHash {
return nil, transport.ErrorCorruptedPacket

View File

@ -8,35 +8,35 @@ type Bytes interface {
Bytes() []byte
}
type BytesLiteral []byte
type BytesT []byte
func (this BytesLiteral) Value() []byte {
func (this BytesT) Value() []byte {
return []byte(this)
}
func (this BytesLiteral) Equals(another BytesLiteral) bool {
func (this BytesT) Equals(another BytesT) bool {
return bytes.Equal(this.Value(), another.Value())
}
func (this BytesLiteral) Uint8Value() uint8 {
func (this BytesT) Uint8Value() uint8 {
return this.Value()[0]
}
func (this BytesLiteral) Uint16() Uint16Literal {
func (this BytesT) Uint16() Uint16Literal {
return Uint16Literal(this.Uint16Value())
}
func (this BytesLiteral) Uint16Value() uint16 {
func (this BytesT) Uint16Value() uint16 {
value := this.Value()
return uint16(value[0])<<8 + uint16(value[1])
}
func (this BytesLiteral) IntValue() int {
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 BytesLiteral) Uint32Value() uint32 {
func (this BytesT) Uint32Value() uint32 {
value := this.Value()
return uint32(value[0])<<24 +
uint32(value[1])<<16 +
@ -44,7 +44,7 @@ func (this BytesLiteral) Uint32Value() uint32 {
uint32(value[3])
}
func (this BytesLiteral) Int64Value() int64 {
func (this BytesT) Int64Value() int64 {
value := this.Value()
return int64(value[0])<<56 +
int64(value[1])<<48 +
@ -57,12 +57,12 @@ func (this BytesLiteral) Int64Value() int64 {
}
// String returns a string presentation of this ByteLiteral
func (this BytesLiteral) String() string {
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 BytesLiteral) All(v byte) bool {
func (this BytesT) All(v byte) bool {
for _, b := range this {
if b != v {
return false

View File

@ -231,7 +231,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.BytesLiteral(requestBuffer.Value))
log.Debug("Request to remote:\n", serial.BytesT(requestBuffer.Value))
ray := this.packetDispatcher.DispatchToOutbound(dest)
ray.InboundInput().Write(requestBuffer)

View File

@ -79,7 +79,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.BytesLiteral(buffer.Value[:2]).Uint16Value() + AuthSize
length := serial.BytesT(buffer.Value[:2]).Uint16Value() + AuthSize
if _, err := io.ReadFull(this.reader, buffer.Value[:length]); err != nil {
buffer.Release()
return nil, err
@ -90,7 +90,7 @@ func (this *ChunkReader) Read() (*alloc.Buffer, error) {
payload := buffer.Value[AuthSize:]
actualAuthBytes := this.auth.Authenticate(nil, payload)
if !serial.BytesLiteral(authBytes).Equals(serial.BytesLiteral(actualAuthBytes)) {
if !serial.BytesT(authBytes).Equals(serial.BytesT(actualAuthBytes)) {
buffer.Release()
log.Debug("AuthenticationReader: Unexpected auth: ", authBytes)
return nil, transport.ErrorCorruptedPacket

View File

@ -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.BytesLiteral(actualAuth).Equals(serial.BytesLiteral(authBytes)) {
if !serial.BytesT(actualAuth).Equals(serial.BytesT(authBytes)) {
log.Debug("Shadowsocks: Invalid OTA. Expecting ", actualAuth, ", but got ", authBytes)
log.Warning("Shadowsocks: Invalid OTA.")
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)
length := serial.BytesLiteral(buffer.Value[:2]).Uint16Value()
length := serial.BytesT(buffer.Value[:2]).Uint16Value()
this.chunkLength = int(length) - 4
this.validator = NewValidator(serial.BytesLiteral(buffer.Value[2:6]).Uint32Value())
this.validator = NewValidator(serial.BytesT(buffer.Value[2:6]).Uint32Value())
buffer.SliceFrom(6)
} else if buffer.Len() < this.chunkLength {
_, err := buffer.FillFrom(this.reader)