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

View File

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