2016-07-23 11:17:51 +00:00
|
|
|
package encoding
|
2016-02-25 20:50:38 +00:00
|
|
|
|
|
|
|
import (
|
2016-12-07 16:32:40 +00:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
2016-02-25 20:50:38 +00:00
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rand"
|
|
|
|
"hash/fnv"
|
|
|
|
"io"
|
2016-12-07 16:32:40 +00:00
|
|
|
|
|
|
|
"golang.org/x/crypto/chacha20poly1305"
|
|
|
|
|
2017-02-10 15:42:24 +00:00
|
|
|
"v2ray.com/core/app/log"
|
2016-12-09 12:17:34 +00:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/common/crypto"
|
2016-12-07 16:32:40 +00:00
|
|
|
"v2ray.com/core/common/dice"
|
2016-12-15 10:05:32 +00:00
|
|
|
"v2ray.com/core/common/errors"
|
2017-01-13 22:42:39 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/common/protocol"
|
2016-12-07 16:32:40 +00:00
|
|
|
"v2ray.com/core/common/serial"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/proxy/vmess"
|
2016-02-25 20:50:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func hashTimestamp(t protocol.Timestamp) []byte {
|
|
|
|
bytes := make([]byte, 0, 32)
|
2016-07-27 14:36:46 +00:00
|
|
|
bytes = t.Bytes(bytes)
|
|
|
|
bytes = t.Bytes(bytes)
|
|
|
|
bytes = t.Bytes(bytes)
|
|
|
|
bytes = t.Bytes(bytes)
|
2016-02-25 20:50:38 +00:00
|
|
|
return bytes
|
|
|
|
}
|
|
|
|
|
2016-12-13 08:32:29 +00:00
|
|
|
// ClientSession stores connection session info for VMess client.
|
2016-02-25 20:50:38 +00:00
|
|
|
type ClientSession struct {
|
|
|
|
requestBodyKey []byte
|
|
|
|
requestBodyIV []byte
|
|
|
|
responseHeader byte
|
|
|
|
responseBodyKey []byte
|
|
|
|
responseBodyIV []byte
|
2016-02-26 23:05:53 +00:00
|
|
|
responseReader io.Reader
|
2016-02-25 20:50:38 +00:00
|
|
|
idHash protocol.IDHash
|
|
|
|
}
|
|
|
|
|
2016-12-13 08:32:29 +00:00
|
|
|
// NewClientSession creates a new ClientSession.
|
2016-02-25 20:50:38 +00:00
|
|
|
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 23:05:53 +00:00
|
|
|
responseBodyKey := md5.Sum(session.requestBodyKey)
|
|
|
|
responseBodyIV := md5.Sum(session.requestBodyIV)
|
|
|
|
session.responseBodyKey = responseBodyKey[:]
|
|
|
|
session.responseBodyIV = responseBodyIV[:]
|
2016-02-25 20:50:38 +00:00
|
|
|
session.idHash = idHash
|
|
|
|
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) {
|
2016-02-25 20:50:38 +00:00
|
|
|
timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)()
|
2016-10-16 12:22:21 +00:00
|
|
|
account, err := header.User.GetTypedAccount()
|
2016-09-17 22:41:21 +00:00
|
|
|
if err != nil {
|
2017-04-06 19:13:17 +00:00
|
|
|
log.Trace(errors.New("VMess: Failed to get user account: ", err).AtError())
|
2016-09-17 22:41:21 +00:00
|
|
|
return
|
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
idHash := v.idHash(account.(*vmess.InternalAccount).AnyValidID().Bytes())
|
2016-06-26 20:34:48 +00:00
|
|
|
idHash.Write(timestamp.Bytes(nil))
|
2016-02-27 15:41:21 +00:00
|
|
|
writer.Write(idHash.Sum(nil))
|
2016-02-25 20:50:38 +00:00
|
|
|
|
2016-07-15 19:15:41 +00:00
|
|
|
buffer := make([]byte, 0, 512)
|
|
|
|
buffer = append(buffer, Version)
|
2016-11-27 20:39:09 +00:00
|
|
|
buffer = append(buffer, v.requestBodyIV...)
|
|
|
|
buffer = append(buffer, v.requestBodyKey...)
|
2016-12-07 16:32:40 +00:00
|
|
|
buffer = append(buffer, v.responseHeader, byte(header.Option))
|
|
|
|
padingLen := dice.Roll(16)
|
2016-12-10 00:05:55 +00:00
|
|
|
if header.Security.Is(protocol.SecurityType_LEGACY) {
|
|
|
|
// Disable padding in legacy mode for a smooth transition.
|
|
|
|
padingLen = 0
|
|
|
|
}
|
2016-12-07 16:32:40 +00:00
|
|
|
security := byte(padingLen<<4) | byte(header.Security)
|
|
|
|
buffer = append(buffer, security, byte(0), byte(header.Command))
|
2016-07-15 19:15:41 +00:00
|
|
|
buffer = header.Port.Bytes(buffer)
|
2016-02-25 20:50:38 +00:00
|
|
|
|
2016-08-14 16:14:12 +00:00
|
|
|
switch header.Address.Family() {
|
2017-01-13 22:42:39 +00:00
|
|
|
case net.AddressFamilyIPv4:
|
2016-07-15 19:15:41 +00:00
|
|
|
buffer = append(buffer, AddrTypeIPv4)
|
|
|
|
buffer = append(buffer, header.Address.IP()...)
|
2017-01-13 22:42:39 +00:00
|
|
|
case net.AddressFamilyIPv6:
|
2016-07-15 19:15:41 +00:00
|
|
|
buffer = append(buffer, AddrTypeIPv6)
|
|
|
|
buffer = append(buffer, header.Address.IP()...)
|
2017-01-13 22:42:39 +00:00
|
|
|
case net.AddressFamilyDomain:
|
2016-07-15 19:15:41 +00:00
|
|
|
buffer = append(buffer, AddrTypeDomain, byte(len(header.Address.Domain())))
|
|
|
|
buffer = append(buffer, header.Address.Domain()...)
|
2016-02-25 20:50:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-10 00:05:55 +00:00
|
|
|
if padingLen > 0 {
|
|
|
|
pading := make([]byte, padingLen)
|
|
|
|
rand.Read(pading)
|
|
|
|
buffer = append(buffer, pading...)
|
|
|
|
}
|
2016-12-07 16:32:40 +00:00
|
|
|
|
2016-02-25 20:50:38 +00:00
|
|
|
fnv1a := fnv.New32a()
|
2016-07-15 19:15:41 +00:00
|
|
|
fnv1a.Write(buffer)
|
2016-02-25 20:50:38 +00:00
|
|
|
|
2016-07-15 19:15:41 +00:00
|
|
|
buffer = fnv1a.Sum(buffer)
|
2016-02-25 20:50:38 +00:00
|
|
|
|
|
|
|
timestampHash := md5.New()
|
|
|
|
timestampHash.Write(hashTimestamp(timestamp))
|
|
|
|
iv := timestampHash.Sum(nil)
|
2016-10-12 16:43:55 +00:00
|
|
|
aesStream := crypto.NewAesEncryptionStream(account.(*vmess.InternalAccount).ID.CmdKey(), iv)
|
2016-07-15 19:15:41 +00:00
|
|
|
aesStream.XORKeyStream(buffer, buffer)
|
|
|
|
writer.Write(buffer)
|
2016-02-25 20:50:38 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-09 12:17:34 +00:00
|
|
|
func (v *ClientSession) EncodeRequestBody(request *protocol.RequestHeader, writer io.Writer) buf.Writer {
|
2016-12-07 16:32:40 +00:00
|
|
|
var authWriter io.Writer
|
2017-02-14 13:16:43 +00:00
|
|
|
var sizeMask crypto.Uint16Generator = crypto.StaticUint16Generator(0)
|
|
|
|
if request.Option.Has(protocol.RequestOptionChunkMasking) {
|
|
|
|
sizeMask = getSizeMask(v.requestBodyIV)
|
|
|
|
}
|
2016-12-07 16:32:40 +00:00
|
|
|
if request.Security.Is(protocol.SecurityType_NONE) {
|
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
2017-01-22 19:43:01 +00:00
|
|
|
AEAD: NoOpAuthenticator{},
|
2016-12-07 16:32:40 +00:00
|
|
|
NonceGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-02-14 13:16:43 +00:00
|
|
|
authWriter = crypto.NewAuthenticationWriter(auth, writer, sizeMask)
|
2016-12-07 16:32:40 +00:00
|
|
|
} else {
|
|
|
|
authWriter = writer
|
|
|
|
}
|
|
|
|
} else if request.Security.Is(protocol.SecurityType_LEGACY) {
|
|
|
|
aesStream := crypto.NewAesEncryptionStream(v.requestBodyKey, v.requestBodyIV)
|
|
|
|
cryptionWriter := crypto.NewCryptionWriter(aesStream, writer)
|
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(FnvAuthenticator),
|
|
|
|
NonceGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-02-14 13:16:43 +00:00
|
|
|
authWriter = crypto.NewAuthenticationWriter(auth, cryptionWriter, sizeMask)
|
2016-12-07 16:32:40 +00:00
|
|
|
} else {
|
|
|
|
authWriter = cryptionWriter
|
|
|
|
}
|
|
|
|
} else if request.Security.Is(protocol.SecurityType_AES128_GCM) {
|
2016-12-07 21:52:56 +00:00
|
|
|
block, _ := aes.NewCipher(v.requestBodyKey)
|
2016-12-07 16:32:40 +00:00
|
|
|
aead, _ := cipher.NewGCM(block)
|
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &ChunkNonceGenerator{
|
2016-12-07 21:52:56 +00:00
|
|
|
Nonce: append([]byte(nil), v.requestBodyIV...),
|
2016-12-07 16:32:40 +00:00
|
|
|
Size: aead.NonceSize(),
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-02-14 13:16:43 +00:00
|
|
|
authWriter = crypto.NewAuthenticationWriter(auth, writer, sizeMask)
|
2016-12-07 16:32:40 +00:00
|
|
|
} else if request.Security.Is(protocol.SecurityType_CHACHA20_POLY1305) {
|
2016-12-07 21:52:56 +00:00
|
|
|
aead, _ := chacha20poly1305.New(GenerateChacha20Poly1305Key(v.requestBodyKey))
|
2016-12-07 16:32:40 +00:00
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &ChunkNonceGenerator{
|
2016-12-07 21:52:56 +00:00
|
|
|
Nonce: append([]byte(nil), v.requestBodyIV...),
|
2016-12-07 16:32:40 +00:00
|
|
|
Size: aead.NonceSize(),
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-02-14 13:16:43 +00:00
|
|
|
authWriter = crypto.NewAuthenticationWriter(auth, writer, sizeMask)
|
2016-12-07 16:32:40 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 12:17:34 +00:00
|
|
|
return buf.NewWriter(authWriter)
|
2016-12-07 16:32:40 +00:00
|
|
|
|
2016-02-25 20:50:38 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00: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 23:05:53 +00:00
|
|
|
|
2016-07-16 08:45:32 +00:00
|
|
|
buffer := make([]byte, 256)
|
2016-02-26 23:05:53 +00:00
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
_, err := io.ReadFull(v.responseReader, buffer[:4])
|
2016-02-26 23:05:53 +00:00
|
|
|
if err != nil {
|
2017-04-06 19:13:17 +00:00
|
|
|
log.Trace(errors.New("VMess|Client: Failed to read response header: ", err))
|
2016-02-26 23:05:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
if buffer[0] != v.responseHeader {
|
2016-12-15 10:05:32 +00:00
|
|
|
return nil, errors.Format("VMess|Client: Unexpected response header. Expecting %d but actually %d", v.responseHeader, buffer[0])
|
2016-02-26 23:05:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-02 19:34:25 +00:00
|
|
|
header := &protocol.ResponseHeader{
|
2016-07-16 08:45:32 +00:00
|
|
|
Option: protocol.ResponseOption(buffer[1]),
|
2016-06-02 19:34:25 +00:00
|
|
|
}
|
2016-02-26 23:05:53 +00:00
|
|
|
|
2016-07-16 08:45:32 +00:00
|
|
|
if buffer[2] != 0 {
|
2016-12-13 10:15:11 +00:00
|
|
|
cmdID := buffer[2]
|
2016-07-16 08:45:32 +00:00
|
|
|
dataLen := int(buffer[3])
|
2016-11-27 20:39:09 +00:00
|
|
|
_, err := io.ReadFull(v.responseReader, buffer[:dataLen])
|
2016-02-26 23:05:53 +00:00
|
|
|
if err != nil {
|
2017-04-06 19:13:17 +00:00
|
|
|
log.Trace(errors.New("VMess|Client: Failed to read response command: ", err))
|
2016-02-26 23:05:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-07-16 08:45:32 +00:00
|
|
|
data := buffer[:dataLen]
|
2016-12-13 10:15:11 +00:00
|
|
|
command, err := UnmarshalCommand(cmdID, data)
|
2016-04-25 22:46:04 +00:00
|
|
|
if err == nil {
|
|
|
|
header.Command = command
|
|
|
|
}
|
2016-02-26 23:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return header, nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 12:17:34 +00:00
|
|
|
func (v *ClientSession) DecodeResponseBody(request *protocol.RequestHeader, reader io.Reader) buf.Reader {
|
2016-12-07 16:32:40 +00:00
|
|
|
var authReader io.Reader
|
2017-02-14 13:16:43 +00:00
|
|
|
var sizeMask crypto.Uint16Generator = crypto.StaticUint16Generator(0)
|
|
|
|
if request.Option.Has(protocol.RequestOptionChunkMasking) {
|
|
|
|
sizeMask = getSizeMask(v.responseBodyIV)
|
|
|
|
}
|
2016-12-07 16:32:40 +00:00
|
|
|
if request.Security.Is(protocol.SecurityType_NONE) {
|
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
2017-02-14 13:16:43 +00:00
|
|
|
AEAD: NoOpAuthenticator{},
|
2016-12-07 16:32:40 +00:00
|
|
|
NonceGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-02-14 13:16:43 +00:00
|
|
|
authReader = crypto.NewAuthenticationReader(auth, reader, sizeMask)
|
2016-12-07 16:32:40 +00:00
|
|
|
} else {
|
|
|
|
authReader = reader
|
|
|
|
}
|
|
|
|
} else if request.Security.Is(protocol.SecurityType_LEGACY) {
|
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(FnvAuthenticator),
|
|
|
|
NonceGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-02-14 13:16:43 +00:00
|
|
|
authReader = crypto.NewAuthenticationReader(auth, v.responseReader, sizeMask)
|
2016-12-07 16:32:40 +00:00
|
|
|
} else {
|
|
|
|
authReader = v.responseReader
|
|
|
|
}
|
|
|
|
} else if request.Security.Is(protocol.SecurityType_AES128_GCM) {
|
|
|
|
block, _ := aes.NewCipher(v.responseBodyKey)
|
|
|
|
aead, _ := cipher.NewGCM(block)
|
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &ChunkNonceGenerator{
|
|
|
|
Nonce: append([]byte(nil), v.responseBodyIV...),
|
|
|
|
Size: aead.NonceSize(),
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-02-14 13:16:43 +00:00
|
|
|
authReader = crypto.NewAuthenticationReader(auth, reader, sizeMask)
|
2016-12-07 16:32:40 +00:00
|
|
|
} else if request.Security.Is(protocol.SecurityType_CHACHA20_POLY1305) {
|
2016-12-07 20:43:41 +00:00
|
|
|
aead, _ := chacha20poly1305.New(GenerateChacha20Poly1305Key(v.responseBodyKey))
|
2016-12-07 16:32:40 +00:00
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &ChunkNonceGenerator{
|
|
|
|
Nonce: append([]byte(nil), v.responseBodyIV...),
|
|
|
|
Size: aead.NonceSize(),
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-02-14 13:16:43 +00:00
|
|
|
authReader = crypto.NewAuthenticationReader(auth, reader, sizeMask)
|
2016-12-07 16:32:40 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 12:17:34 +00:00
|
|
|
return buf.NewReader(authReader)
|
2016-12-07 16:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ChunkNonceGenerator struct {
|
|
|
|
Nonce []byte
|
|
|
|
Size int
|
|
|
|
count uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *ChunkNonceGenerator) Next() []byte {
|
2016-12-07 21:52:56 +00:00
|
|
|
serial.Uint16ToBytes(v.count, v.Nonce[:0])
|
2016-12-07 16:32:40 +00:00
|
|
|
v.count++
|
|
|
|
return v.Nonce[:v.Size]
|
2016-02-26 23:05:53 +00:00
|
|
|
}
|