mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
3.9 KiB
139 lines
3.9 KiB
8 years ago
|
package encoding
|
||
9 years ago
|
|
||
|
import (
|
||
|
"crypto/md5"
|
||
|
"crypto/rand"
|
||
|
"hash/fnv"
|
||
|
"io"
|
||
|
|
||
|
"github.com/v2ray/v2ray-core/common/crypto"
|
||
9 years ago
|
"github.com/v2ray/v2ray-core/common/log"
|
||
9 years ago
|
"github.com/v2ray/v2ray-core/common/protocol"
|
||
9 years ago
|
"github.com/v2ray/v2ray-core/transport"
|
||
9 years ago
|
)
|
||
|
|
||
|
func hashTimestamp(t protocol.Timestamp) []byte {
|
||
|
bytes := make([]byte, 0, 32)
|
||
9 years ago
|
t.Bytes(bytes)
|
||
|
t.Bytes(bytes)
|
||
|
t.Bytes(bytes)
|
||
|
t.Bytes(bytes)
|
||
9 years ago
|
return bytes
|
||
|
}
|
||
|
|
||
|
type ClientSession struct {
|
||
|
requestBodyKey []byte
|
||
|
requestBodyIV []byte
|
||
|
responseHeader byte
|
||
|
responseBodyKey []byte
|
||
|
responseBodyIV []byte
|
||
9 years ago
|
responseReader io.Reader
|
||
9 years ago
|
idHash protocol.IDHash
|
||
|
}
|
||
|
|
||
|
func NewClientSession(idHash protocol.IDHash) *ClientSession {
|
||
|
randomBytes := make([]byte, 33) // 16 + 16 + 1
|
||
|
rand.Read(randomBytes)
|
||
|
|
||
|
session := &ClientSession{}
|
||
|
session.requestBodyKey = randomBytes[:16]
|
||
|
session.requestBodyIV = randomBytes[16:32]
|
||
|
session.responseHeader = randomBytes[32]
|
||
9 years ago
|
responseBodyKey := md5.Sum(session.requestBodyKey)
|
||
|
responseBodyIV := md5.Sum(session.requestBodyIV)
|
||
|
session.responseBodyKey = responseBodyKey[:]
|
||
|
session.responseBodyIV = responseBodyIV[:]
|
||
9 years ago
|
session.idHash = idHash
|
||
|
|
||
|
return session
|
||
|
}
|
||
|
|
||
|
func (this *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) {
|
||
|
timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)()
|
||
9 years ago
|
idHash := this.idHash(header.User.Account.(*protocol.VMessAccount).AnyValidID().Bytes())
|
||
9 years ago
|
idHash.Write(timestamp.Bytes(nil))
|
||
9 years ago
|
writer.Write(idHash.Sum(nil))
|
||
9 years ago
|
|
||
8 years ago
|
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)
|
||
9 years ago
|
|
||
|
switch {
|
||
|
case header.Address.IsIPv4():
|
||
8 years ago
|
buffer = append(buffer, AddrTypeIPv4)
|
||
|
buffer = append(buffer, header.Address.IP()...)
|
||
9 years ago
|
case header.Address.IsIPv6():
|
||
8 years ago
|
buffer = append(buffer, AddrTypeIPv6)
|
||
|
buffer = append(buffer, header.Address.IP()...)
|
||
9 years ago
|
case header.Address.IsDomain():
|
||
8 years ago
|
buffer = append(buffer, AddrTypeDomain, byte(len(header.Address.Domain())))
|
||
|
buffer = append(buffer, header.Address.Domain()...)
|
||
9 years ago
|
}
|
||
|
|
||
|
fnv1a := fnv.New32a()
|
||
8 years ago
|
fnv1a.Write(buffer)
|
||
9 years ago
|
|
||
8 years ago
|
buffer = fnv1a.Sum(buffer)
|
||
9 years ago
|
|
||
|
timestampHash := md5.New()
|
||
|
timestampHash.Write(hashTimestamp(timestamp))
|
||
|
iv := timestampHash.Sum(nil)
|
||
9 years ago
|
account := header.User.Account.(*protocol.VMessAccount)
|
||
|
aesStream := crypto.NewAesEncryptionStream(account.ID.CmdKey(), iv)
|
||
8 years ago
|
aesStream.XORKeyStream(buffer, buffer)
|
||
|
writer.Write(buffer)
|
||
9 years ago
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (this *ClientSession) EncodeRequestBody(writer io.Writer) io.Writer {
|
||
|
aesStream := crypto.NewAesEncryptionStream(this.requestBodyKey, this.requestBodyIV)
|
||
|
return crypto.NewCryptionWriter(aesStream, writer)
|
||
|
}
|
||
|
|
||
9 years ago
|
func (this *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.ResponseHeader, error) {
|
||
|
aesStream := crypto.NewAesDecryptionStream(this.responseBodyKey, this.responseBodyIV)
|
||
|
this.responseReader = crypto.NewCryptionReader(aesStream, reader)
|
||
|
|
||
8 years ago
|
buffer := make([]byte, 256)
|
||
9 years ago
|
|
||
8 years ago
|
_, err := io.ReadFull(this.responseReader, buffer[:4])
|
||
9 years ago
|
if err != nil {
|
||
9 years ago
|
log.Info("Raw: Failed to read response header: ", err)
|
||
9 years ago
|
return nil, err
|
||
|
}
|
||
|
|
||
8 years ago
|
if buffer[0] != this.responseHeader {
|
||
|
log.Info("Raw: Unexpected response header. Expecting ", this.responseHeader, " but actually ", buffer[0])
|
||
9 years ago
|
return nil, transport.ErrCorruptedPacket
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
header := &protocol.ResponseHeader{
|
||
8 years ago
|
Option: protocol.ResponseOption(buffer[1]),
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
if buffer[2] != 0 {
|
||
|
cmdId := buffer[2]
|
||
|
dataLen := int(buffer[3])
|
||
|
_, err := io.ReadFull(this.responseReader, buffer[:dataLen])
|
||
9 years ago
|
if err != nil {
|
||
9 years ago
|
log.Info("Raw: Failed to read response command: ", err)
|
||
9 years ago
|
return nil, err
|
||
|
}
|
||
8 years ago
|
data := buffer[:dataLen]
|
||
9 years ago
|
command, err := UnmarshalCommand(cmdId, data)
|
||
9 years ago
|
if err == nil {
|
||
|
header.Command = command
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
return header, nil
|
||
|
}
|
||
|
|
||
|
func (this *ClientSession) DecodeResponseBody(reader io.Reader) io.Reader {
|
||
|
return this.responseReader
|
||
|
}
|