1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-28 10:15:23 +00:00

remove unnecessary use of buffer

This commit is contained in:
v2ray 2016-07-15 21:15:41 +02:00
parent 33e0cfe233
commit b6a6c154a3
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 23 additions and 37 deletions

View File

@ -2,9 +2,9 @@ package internal
import ( import (
"fmt" "fmt"
"strings"
"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/serial" "github.com/v2ray/v2ray-core/common/serial"
) )
@ -46,15 +46,12 @@ func (this *ErrorLog) Release() {
} }
func (this *ErrorLog) String() string { func (this *ErrorLog) String() string {
b := alloc.NewSmallBuffer().Clear() values := make([]string, len(this.Values)+1)
defer b.Release() values[0] = this.Prefix
for i, value := range this.Values {
b.AppendString(this.Prefix) values[i+1] = InterfaceToString(value)
for _, value := range this.Values {
b.AppendString(InterfaceToString(value))
} }
return b.String() return strings.Join(values, "")
} }
type AccessLog struct { type AccessLog struct {
@ -71,12 +68,5 @@ func (this *AccessLog) Release() {
} }
func (this *AccessLog) String() string { func (this *AccessLog) String() string {
b := alloc.NewSmallBuffer().Clear() return strings.Join([]string{InterfaceToString(this.From), this.Status, InterfaceToString(this.To), InterfaceToString(this.Reason)}, " ")
defer b.Release()
b.AppendString(InterfaceToString(this.From)).AppendString(" ")
b.AppendString(this.Status).AppendString(" ")
b.AppendString(InterfaceToString(this.To)).AppendString(" ")
b.AppendString(InterfaceToString(this.Reason))
return b.String()
} }

View File

@ -55,41 +55,37 @@ func (this *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, w
idHash.Write(timestamp.Bytes(nil)) idHash.Write(timestamp.Bytes(nil))
writer.Write(idHash.Sum(nil)) writer.Write(idHash.Sum(nil))
buffer := alloc.NewSmallBuffer().Clear() buffer := make([]byte, 0, 512)
defer buffer.Release() buffer = append(buffer, Version)
buffer = append(buffer, this.requestBodyIV...)
buffer.AppendBytes(Version) buffer = append(buffer, this.requestBodyKey...)
buffer.Append(this.requestBodyIV) buffer = append(buffer, this.responseHeader, byte(header.Option), byte(0), byte(0), byte(header.Command))
buffer.Append(this.requestBodyKey) buffer = header.Port.Bytes(buffer)
buffer.AppendBytes(this.responseHeader, byte(header.Option), byte(0), byte(0))
buffer.AppendBytes(byte(header.Command))
buffer.AppendUint16(header.Port.Value())
switch { switch {
case header.Address.IsIPv4(): case header.Address.IsIPv4():
buffer.AppendBytes(AddrTypeIPv4) buffer = append(buffer, AddrTypeIPv4)
buffer.Append(header.Address.IP()) buffer = append(buffer, header.Address.IP()...)
case header.Address.IsIPv6(): case header.Address.IsIPv6():
buffer.AppendBytes(AddrTypeIPv6) buffer = append(buffer, AddrTypeIPv6)
buffer.Append(header.Address.IP()) buffer = append(buffer, header.Address.IP()...)
case header.Address.IsDomain(): case header.Address.IsDomain():
buffer.AppendBytes(AddrTypeDomain, byte(len(header.Address.Domain()))) buffer = append(buffer, AddrTypeDomain, byte(len(header.Address.Domain())))
buffer.Append([]byte(header.Address.Domain())) buffer = append(buffer, header.Address.Domain()...)
} }
fnv1a := fnv.New32a() fnv1a := fnv.New32a()
fnv1a.Write(buffer.Value) fnv1a.Write(buffer)
fnvHash := fnv1a.Sum32() buffer = fnv1a.Sum(buffer)
buffer.AppendBytes(byte(fnvHash>>24), byte(fnvHash>>16), byte(fnvHash>>8), byte(fnvHash))
timestampHash := md5.New() timestampHash := md5.New()
timestampHash.Write(hashTimestamp(timestamp)) timestampHash.Write(hashTimestamp(timestamp))
iv := timestampHash.Sum(nil) iv := timestampHash.Sum(nil)
account := header.User.Account.(*protocol.VMessAccount) account := header.User.Account.(*protocol.VMessAccount)
aesStream := crypto.NewAesEncryptionStream(account.ID.CmdKey(), iv) aesStream := crypto.NewAesEncryptionStream(account.ID.CmdKey(), iv)
aesStream.XORKeyStream(buffer.Value, buffer.Value) aesStream.XORKeyStream(buffer, buffer)
writer.Write(buffer.Value) writer.Write(buffer)
return return
} }