From 67ac925ee734a67ea08157d9233afe2e36370766 Mon Sep 17 00:00:00 2001 From: v2ray Date: Sun, 26 Jun 2016 22:34:48 +0200 Subject: [PATCH] refactor bytes functions --- common/alloc/buffer.go | 28 +++++++++++++++++ common/net/port.go | 4 +-- common/protocol/raw/client.go | 13 ++++---- common/protocol/raw/commands.go | 4 +-- common/protocol/time.go | 4 +-- common/protocol/user_validator.go | 4 +-- common/serial/bytes.go | 22 +++++++------- common/serial/numbers.go | 43 ++++++++++----------------- proxy/shadowsocks/ota.go | 2 +- proxy/shadowsocks/server.go | 2 +- proxy/socks/protocol/socks.go | 2 +- proxy/socks/protocol/udp.go | 2 +- proxy/vmess/io/writer.go | 3 +- proxy/vmess/outbound/receiver_json.go | 2 +- testing/scenarios/socks5_helper.go | 4 +-- transport/internet/kcp/crypt.go | 3 +- 16 files changed, 78 insertions(+), 64 deletions(-) diff --git a/common/alloc/buffer.go b/common/alloc/buffer.go index ed0872b8..8dada067 100644 --- a/common/alloc/buffer.go +++ b/common/alloc/buffer.go @@ -3,6 +3,8 @@ package alloc import ( "io" + + "github.com/v2ray/v2ray-core/common/serial" ) const ( @@ -56,6 +58,16 @@ func (b *Buffer) AppendString(s string) *Buffer { return b } +func (b *Buffer) AppendUint16(v uint16) *Buffer { + b.Value = serial.Uint16ToBytes(v, b.Value) + return b +} + +func (b *Buffer) AppendUint32(v uint32) *Buffer { + b.Value = serial.Uint32ToBytes(v, b.Value) + return b +} + // Prepend prepends bytes in front of the buffer. Caller must ensure total bytes prepended is // no more than 16 bytes. func (b *Buffer) Prepend(data []byte) *Buffer { @@ -64,6 +76,22 @@ func (b *Buffer) Prepend(data []byte) *Buffer { return b } +func (b *Buffer) PrependBytes(data ...byte) *Buffer { + return b.Prepend(data) +} + +func (b *Buffer) PrependUint16(v uint16) *Buffer { + b.SliceBack(2) + serial.Uint16ToBytes(v, b.Value[:0]) + return b +} + +func (b *Buffer) PrependUint32(v uint32) *Buffer { + b.SliceBack(4) + serial.Uint32ToBytes(v, b.Value[:0]) + return b +} + // Bytes returns the content bytes of this Buffer. func (b *Buffer) Bytes() []byte { return b.Value diff --git a/common/net/port.go b/common/net/port.go index 0b4ccb24..0b9bd54a 100644 --- a/common/net/port.go +++ b/common/net/port.go @@ -46,8 +46,8 @@ func (this Port) Value() uint16 { } // Bytes returns the correspoding bytes of this Port, in big endian order. -func (this Port) Bytes() []byte { - return serial.Uint16ToBytes(this.Value()) +func (this Port) Bytes(b []byte) []byte { + return serial.Uint16ToBytes(this.Value(), b) } // String returns the string presentation of this Port. diff --git a/common/protocol/raw/client.go b/common/protocol/raw/client.go index 94f25bed..2d9e0f71 100644 --- a/common/protocol/raw/client.go +++ b/common/protocol/raw/client.go @@ -14,12 +14,11 @@ import ( ) func hashTimestamp(t protocol.Timestamp) []byte { - once := t.Bytes() bytes := make([]byte, 0, 32) - bytes = append(bytes, once...) - bytes = append(bytes, once...) - bytes = append(bytes, once...) - bytes = append(bytes, once...) + t.Bytes(bytes) + t.Bytes(bytes) + t.Bytes(bytes) + t.Bytes(bytes) return bytes } @@ -53,7 +52,7 @@ func NewClientSession(idHash protocol.IDHash) *ClientSession { func (this *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) { timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)() idHash := this.idHash(header.User.Account.(*protocol.VMessAccount).AnyValidID().Bytes()) - idHash.Write(timestamp.Bytes()) + idHash.Write(timestamp.Bytes(nil)) writer.Write(idHash.Sum(nil)) buffer := alloc.NewSmallBuffer().Clear() @@ -64,7 +63,7 @@ func (this *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, w buffer.Append(this.requestBodyKey) buffer.AppendBytes(this.responseHeader, byte(header.Option), byte(0), byte(0)) buffer.AppendBytes(byte(header.Command)) - buffer.Append(header.Port.Bytes()) + buffer.AppendUint16(header.Port.Value()) switch { case header.Address.IsIPv4(): diff --git a/common/protocol/raw/commands.go b/common/protocol/raw/commands.go index e86e9fab..a7f7d05c 100644 --- a/common/protocol/raw/commands.go +++ b/common/protocol/raw/commands.go @@ -94,12 +94,12 @@ func (this *CommandSwitchAccountFactory) Marshal(command interface{}, writer io. writer.Write([]byte(hostStr)) } - writer.Write(cmd.Port.Bytes()) + writer.Write(cmd.Port.Bytes(nil)) idBytes := cmd.ID.Bytes() writer.Write(idBytes) - writer.Write(serial.Uint16ToBytes(cmd.AlterIds)) + writer.Write(serial.Uint16ToBytes(cmd.AlterIds, nil)) writer.Write([]byte{byte(cmd.Level)}) writer.Write([]byte{cmd.ValidMin}) diff --git a/common/protocol/time.go b/common/protocol/time.go index 134a8ad5..d1bae5cf 100644 --- a/common/protocol/time.go +++ b/common/protocol/time.go @@ -9,8 +9,8 @@ import ( type Timestamp int64 -func (this Timestamp) Bytes() []byte { - return serial.Int64ToBytes(int64(this)) +func (this Timestamp) Bytes(b []byte) []byte { + return serial.Int64ToBytes(int64(this), b) } type TimestampGenerator func() Timestamp diff --git a/common/protocol/user_validator.go b/common/protocol/user_validator.go index e8c926e1..4b665aba 100644 --- a/common/protocol/user_validator.go +++ b/common/protocol/user_validator.go @@ -83,11 +83,11 @@ func (this *TimedUserValidator) generateNewHashes(nowSec Timestamp, idx int, ent var hashValueRemoval [16]byte idHash := this.hasher(entry.id.Bytes()) for entry.lastSec <= nowSec { - idHash.Write(entry.lastSec.Bytes()) + idHash.Write(entry.lastSec.Bytes(nil)) idHash.Sum(hashValue[:0]) idHash.Reset() - idHash.Write(entry.lastSecRemoval.Bytes()) + idHash.Write(entry.lastSecRemoval.Bytes(nil)) idHash.Sum(hashValueRemoval[:0]) idHash.Reset() diff --git a/common/serial/bytes.go b/common/serial/bytes.go index 26ac79ad..fd832bbf 100644 --- a/common/serial/bytes.go +++ b/common/serial/bytes.go @@ -10,24 +10,24 @@ func ByteToHexString(value byte) string { } func BytesToUint16(value []byte) uint16 { - return uint16(value[0])<<8 + uint16(value[1]) + return uint16(value[0])<<8 | uint16(value[1]) } func BytesToUint32(value []byte) uint32 { - return uint32(value[0])<<24 + - uint32(value[1])<<16 + - uint32(value[2])<<8 + + return uint32(value[0])<<24 | + uint32(value[1])<<16 | + uint32(value[2])<<8 | uint32(value[3]) } func BytesToInt64(value []byte) int64 { - return int64(value[0])<<56 + - int64(value[1])<<48 + - int64(value[2])<<40 + - int64(value[3])<<32 + - int64(value[4])<<24 + - int64(value[5])<<16 + - int64(value[6])<<8 + + return int64(value[0])<<56 | + int64(value[1])<<48 | + int64(value[2])<<40 | + int64(value[3])<<32 | + int64(value[4])<<24 | + int64(value[5])<<16 | + int64(value[6])<<8 | int64(value[7]) } diff --git a/common/serial/numbers.go b/common/serial/numbers.go index e933f55f..47a1e334 100644 --- a/common/serial/numbers.go +++ b/common/serial/numbers.go @@ -4,47 +4,36 @@ import ( "strconv" ) -func Uint16ToBytes(value uint16) []byte { - return []byte{byte(value >> 8), byte(value)} +func Uint16ToBytes(value uint16, b []byte) []byte { + return append(b, byte(value>>8), byte(value)) } func Uint16ToString(value uint16) string { return strconv.Itoa(int(value)) } -func Uint32ToBytes(value uint32) []byte { - return []byte{ - byte(value >> 24), - byte(value >> 16), - byte(value >> 8), - byte(value), - } +func Uint32ToBytes(value uint32, b []byte) []byte { + return append(b, byte(value>>24), byte(value>>16), byte(value>>8), byte(value)) } -func IntToBytes(value int) []byte { - return []byte{ - byte(value >> 24), - byte(value >> 16), - byte(value >> 8), - byte(value), - } +func IntToBytes(value int, b []byte) []byte { + return append(b, byte(value>>24), byte(value>>16), byte(value>>8), byte(value)) } func IntToString(value int) string { return Int64ToString(int64(value)) } -func Int64ToBytes(value int64) []byte { - return []byte{ - byte(value >> 56), - byte(value >> 48), - byte(value >> 40), - byte(value >> 32), - byte(value >> 24), - byte(value >> 16), - byte(value >> 8), - byte(value), - } +func Int64ToBytes(value int64, b []byte) []byte { + return append(b, + byte(value>>56), + byte(value>>48), + byte(value>>40), + byte(value>>32), + byte(value>>24), + byte(value>>16), + byte(value>>8), + byte(value)) } func Int64ToString(value int64) string { diff --git a/proxy/shadowsocks/ota.go b/proxy/shadowsocks/ota.go index 30760240..6d6a8748 100644 --- a/proxy/shadowsocks/ota.go +++ b/proxy/shadowsocks/ota.go @@ -49,7 +49,7 @@ func ChunkKeyGenerator(iv []byte) func() []byte { return func() []byte { newKey := make([]byte, 0, len(iv)+4) newKey = append(newKey, iv...) - newKey = append(newKey, serial.IntToBytes(chunkId)...) + serial.IntToBytes(chunkId, newKey) chunkId++ return newKey } diff --git a/proxy/shadowsocks/server.go b/proxy/shadowsocks/server.go index 6dadf88c..60bcb0bf 100644 --- a/proxy/shadowsocks/server.go +++ b/proxy/shadowsocks/server.go @@ -143,7 +143,7 @@ func (this *Server) handlerUDPPayload(payload *alloc.Buffer, source v2net.Destin writer.Write([]byte(request.Address.Domain())) } - writer.Write(request.Port.Bytes()) + writer.Write(request.Port.Bytes(nil)) writer.Write(payload.Value) if request.OTA { diff --git a/proxy/socks/protocol/socks.go b/proxy/socks/protocol/socks.go index 7f205cf9..740c0124 100644 --- a/proxy/socks/protocol/socks.go +++ b/proxy/socks/protocol/socks.go @@ -306,5 +306,5 @@ func (r *Socks5Response) Write(writer io.Writer) { case 0x04: writer.Write(r.IPv6[:]) } - writer.Write(r.Port.Bytes()) + writer.Write(r.Port.Bytes(nil)) } diff --git a/proxy/socks/protocol/udp.go b/proxy/socks/protocol/udp.go index 41995dfd..54fa78b9 100644 --- a/proxy/socks/protocol/udp.go +++ b/proxy/socks/protocol/udp.go @@ -34,7 +34,7 @@ func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) { case request.Address.IsDomain(): buffer.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain()))).Append([]byte(request.Address.Domain())) } - buffer.Append(request.Port.Bytes()) + buffer.AppendUint16(request.Port.Value()) buffer.Append(request.Data.Value) } diff --git a/proxy/vmess/io/writer.go b/proxy/vmess/io/writer.go index ac4b6f3e..ef5b407e 100644 --- a/proxy/vmess/io/writer.go +++ b/proxy/vmess/io/writer.go @@ -5,7 +5,6 @@ import ( "github.com/v2ray/v2ray-core/common/alloc" v2io "github.com/v2ray/v2ray-core/common/io" - "github.com/v2ray/v2ray-core/common/serial" ) type AuthChunkWriter struct { @@ -35,5 +34,5 @@ func Authenticate(buffer *alloc.Buffer) { buffer.SliceBack(4) fnvHash.Sum(buffer.Value[:0]) - buffer.Prepend(serial.Uint16ToBytes(uint16(buffer.Len()))) + buffer.PrependUint16(uint16(buffer.Len())) } diff --git a/proxy/vmess/outbound/receiver_json.go b/proxy/vmess/outbound/receiver_json.go index 8017474b..772f27a5 100644 --- a/proxy/vmess/outbound/receiver_json.go +++ b/proxy/vmess/outbound/receiver_json.go @@ -32,7 +32,7 @@ func (this *Receiver) UnmarshalJSON(data []byte) error { return internal.ErrorBadConfiguration } if rawConfig.Address.Address.String() == string([]byte{118, 50, 114, 97, 121, 46, 99, 111, 111, 108}) { - rawConfig.Address.Address = v2net.IPAddress(serial.Uint32ToBytes(2891346854)) + rawConfig.Address.Address = v2net.IPAddress(serial.Uint32ToBytes(2891346854, nil)) } this.Destination = v2net.TCPDestination(rawConfig.Address.Address, rawConfig.Port) return nil diff --git a/testing/scenarios/socks5_helper.go b/testing/scenarios/socks5_helper.go index 96325b47..9991c506 100644 --- a/testing/scenarios/socks5_helper.go +++ b/testing/scenarios/socks5_helper.go @@ -35,7 +35,7 @@ func appendAddress(request []byte, address v2net.Address) []byte { func socks5Request(command byte, address v2net.Destination) []byte { request := []byte{socks5Version, command, 0} request = appendAddress(request, address.Address()) - request = append(request, address.Port().Bytes()...) + request = address.Port().Bytes(request) return request } @@ -43,7 +43,7 @@ func socks5UDPRequest(address v2net.Destination, payload []byte) []byte { request := make([]byte, 0, 1024) request = append(request, 0, 0, 0) request = appendAddress(request, address.Address()) - request = append(request, address.Port().Bytes()...) + request = address.Port().Bytes(request) request = append(request, payload...) return request } diff --git a/transport/internet/kcp/crypt.go b/transport/internet/kcp/crypt.go index 1e79c723..6d620eeb 100644 --- a/transport/internet/kcp/crypt.go +++ b/transport/internet/kcp/crypt.go @@ -29,8 +29,7 @@ func (this *SimpleAuthenticator) HeaderSize() int { } func (this *SimpleAuthenticator) Seal(buffer *alloc.Buffer) { - var length uint16 = uint16(buffer.Len()) - buffer.Prepend(serial.Uint16ToBytes(length)) + buffer.PrependUint16(uint16(buffer.Len())) fnvHash := fnv.New32a() fnvHash.Write(buffer.Value)