1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-30 15:57:35 -04:00
v2fly/proxy/vmess/encoding/client.go

143 lines
4.0 KiB
Go
Raw Normal View History

2016-07-23 07:17:51 -04:00
package encoding
2016-02-25 15:50:38 -05:00
import (
"crypto/md5"
"crypto/rand"
2016-11-19 08:38:13 -05:00
"fmt"
2016-02-25 15:50:38 -05:00
"hash/fnv"
"io"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/crypto"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/proxy/vmess"
2016-02-25 15:50:38 -05:00
)
func hashTimestamp(t protocol.Timestamp) []byte {
bytes := make([]byte, 0, 32)
2016-07-27 10:36:46 -04:00
bytes = t.Bytes(bytes)
bytes = t.Bytes(bytes)
bytes = t.Bytes(bytes)
bytes = t.Bytes(bytes)
2016-02-25 15:50:38 -05:00
return bytes
}
type ClientSession struct {
requestBodyKey []byte
requestBodyIV []byte
responseHeader byte
responseBodyKey []byte
responseBodyIV []byte
2016-02-26 18:05:53 -05:00
responseReader io.Reader
2016-02-25 15:50:38 -05:00
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]
2016-02-26 18:05:53 -05:00
responseBodyKey := md5.Sum(session.requestBodyKey)
responseBodyIV := md5.Sum(session.requestBodyIV)
session.responseBodyKey = responseBodyKey[:]
session.responseBodyIV = responseBodyIV[:]
2016-02-25 15:50:38 -05:00
session.idHash = idHash
return session
}
2016-11-27 15:39:09 -05:00
func (v *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) {
2016-02-25 15:50:38 -05:00
timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)()
2016-10-16 08:22:21 -04:00
account, err := header.User.GetTypedAccount()
2016-09-17 18:41:21 -04:00
if err != nil {
log.Error("VMess: Failed to get user account: ", err)
return
}
2016-11-27 15:39:09 -05:00
idHash := v.idHash(account.(*vmess.InternalAccount).AnyValidID().Bytes())
2016-06-26 16:34:48 -04:00
idHash.Write(timestamp.Bytes(nil))
2016-02-27 10:41:21 -05:00
writer.Write(idHash.Sum(nil))
2016-02-25 15:50:38 -05:00
2016-07-15 15:15:41 -04:00
buffer := make([]byte, 0, 512)
buffer = append(buffer, Version)
2016-11-27 15:39:09 -05:00
buffer = append(buffer, v.requestBodyIV...)
buffer = append(buffer, v.requestBodyKey...)
buffer = append(buffer, v.responseHeader, byte(header.Option), byte(0), byte(0), byte(header.Command))
2016-07-15 15:15:41 -04:00
buffer = header.Port.Bytes(buffer)
2016-02-25 15:50:38 -05:00
2016-08-14 12:14:12 -04:00
switch header.Address.Family() {
case v2net.AddressFamilyIPv4:
2016-07-15 15:15:41 -04:00
buffer = append(buffer, AddrTypeIPv4)
buffer = append(buffer, header.Address.IP()...)
2016-08-14 12:14:12 -04:00
case v2net.AddressFamilyIPv6:
2016-07-15 15:15:41 -04:00
buffer = append(buffer, AddrTypeIPv6)
buffer = append(buffer, header.Address.IP()...)
2016-08-14 12:14:12 -04:00
case v2net.AddressFamilyDomain:
2016-07-15 15:15:41 -04:00
buffer = append(buffer, AddrTypeDomain, byte(len(header.Address.Domain())))
buffer = append(buffer, header.Address.Domain()...)
2016-02-25 15:50:38 -05:00
}
fnv1a := fnv.New32a()
2016-07-15 15:15:41 -04:00
fnv1a.Write(buffer)
2016-02-25 15:50:38 -05:00
2016-07-15 15:15:41 -04:00
buffer = fnv1a.Sum(buffer)
2016-02-25 15:50:38 -05:00
timestampHash := md5.New()
timestampHash.Write(hashTimestamp(timestamp))
iv := timestampHash.Sum(nil)
2016-10-12 12:43:55 -04:00
aesStream := crypto.NewAesEncryptionStream(account.(*vmess.InternalAccount).ID.CmdKey(), iv)
2016-07-15 15:15:41 -04:00
aesStream.XORKeyStream(buffer, buffer)
writer.Write(buffer)
2016-02-25 15:50:38 -05:00
return
}
2016-11-27 15:39:09 -05:00
func (v *ClientSession) EncodeRequestBody(writer io.Writer) io.Writer {
aesStream := crypto.NewAesEncryptionStream(v.requestBodyKey, v.requestBodyIV)
2016-02-25 15:50:38 -05:00
return crypto.NewCryptionWriter(aesStream, writer)
}
2016-11-27 15:39:09 -05:00
func (v *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.ResponseHeader, error) {
aesStream := crypto.NewAesDecryptionStream(v.responseBodyKey, v.responseBodyIV)
v.responseReader = crypto.NewCryptionReader(aesStream, reader)
2016-02-26 18:05:53 -05:00
2016-07-16 04:45:32 -04:00
buffer := make([]byte, 256)
2016-02-26 18:05:53 -05:00
2016-11-27 15:39:09 -05:00
_, err := io.ReadFull(v.responseReader, buffer[:4])
2016-02-26 18:05:53 -05:00
if err != nil {
2016-06-11 16:54:23 -04:00
log.Info("Raw: Failed to read response header: ", err)
2016-02-26 18:05:53 -05:00
return nil, err
}
2016-11-27 15:39:09 -05:00
if buffer[0] != v.responseHeader {
return nil, fmt.Errorf("VMess|Client: Unexpected response header. Expecting %d but actually %d", v.responseHeader, buffer[0])
2016-02-26 18:05:53 -05:00
}
2016-06-02 15:34:25 -04:00
header := &protocol.ResponseHeader{
2016-07-16 04:45:32 -04:00
Option: protocol.ResponseOption(buffer[1]),
2016-06-02 15:34:25 -04:00
}
2016-02-26 18:05:53 -05:00
2016-07-16 04:45:32 -04:00
if buffer[2] != 0 {
cmdId := buffer[2]
dataLen := int(buffer[3])
2016-11-27 15:39:09 -05:00
_, err := io.ReadFull(v.responseReader, buffer[:dataLen])
2016-02-26 18:05:53 -05:00
if err != nil {
2016-06-11 16:54:23 -04:00
log.Info("Raw: Failed to read response command: ", err)
2016-02-26 18:05:53 -05:00
return nil, err
}
2016-07-16 04:45:32 -04:00
data := buffer[:dataLen]
2016-02-26 18:05:53 -05:00
command, err := UnmarshalCommand(cmdId, data)
2016-04-25 18:46:04 -04:00
if err == nil {
header.Command = command
}
2016-02-26 18:05:53 -05:00
}
return header, nil
}
2016-11-27 15:39:09 -05:00
func (v *ClientSession) DecodeResponseBody(reader io.Reader) io.Reader {
return v.responseReader
2016-02-26 18:05:53 -05:00
}