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"
|
2017-09-19 21:27:49 +00:00
|
|
|
"v2ray.com/core/common"
|
2017-10-21 19:04:24 +00:00
|
|
|
"v2ray.com/core/common/bitmask"
|
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"
|
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
|
2017-09-19 21:27:49 +00:00
|
|
|
common.Must2(rand.Read(randomBytes))
|
2016-02-25 20:50:38 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-10-22 18:17:06 +00:00
|
|
|
func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) error {
|
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-08 23:43:25 +00:00
|
|
|
log.Trace(newError("failed to get user account: ", err).AtError())
|
2017-10-22 18:17:06 +00:00
|
|
|
return nil
|
2016-09-17 22:41:21 +00:00
|
|
|
}
|
2017-09-19 21:27:49 +00:00
|
|
|
idHash := c.idHash(account.(*vmess.InternalAccount).AnyValidID().Bytes())
|
|
|
|
common.Must2(idHash.Write(timestamp.Bytes(nil)))
|
|
|
|
common.Must2(writer.Write(idHash.Sum(nil)))
|
2016-02-25 20:50:38 +00:00
|
|
|
|
2017-10-27 19:11:45 +00:00
|
|
|
buffer := buf.New()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
|
|
|
buffer.AppendBytes(Version)
|
|
|
|
buffer.Append(c.requestBodyIV)
|
|
|
|
buffer.Append(c.requestBodyKey)
|
|
|
|
buffer.AppendBytes(c.responseHeader, byte(header.Option))
|
|
|
|
|
2016-12-07 16:32:40 +00:00
|
|
|
padingLen := dice.Roll(16)
|
|
|
|
security := byte(padingLen<<4) | byte(header.Security)
|
2017-10-27 19:11:45 +00:00
|
|
|
buffer.AppendBytes(security, byte(0), byte(header.Command))
|
2017-05-17 19:13:01 +00:00
|
|
|
|
|
|
|
if header.Command != protocol.RequestCommandMux {
|
2017-10-27 19:11:45 +00:00
|
|
|
common.Must(buffer.AppendSupplier(serial.WriteUint16(header.Port.Value())))
|
2017-05-17 19:13:01 +00:00
|
|
|
|
|
|
|
switch header.Address.Family() {
|
|
|
|
case net.AddressFamilyIPv4:
|
2017-10-27 19:11:45 +00:00
|
|
|
buffer.AppendBytes(byte(protocol.AddressTypeIPv4))
|
|
|
|
buffer.Append(header.Address.IP())
|
2017-05-17 19:13:01 +00:00
|
|
|
case net.AddressFamilyIPv6:
|
2017-10-27 19:11:45 +00:00
|
|
|
buffer.AppendBytes(byte(protocol.AddressTypeIPv6))
|
|
|
|
buffer.Append(header.Address.IP())
|
2017-05-17 19:13:01 +00:00
|
|
|
case net.AddressFamilyDomain:
|
2017-10-22 18:17:06 +00:00
|
|
|
domain := header.Address.Domain()
|
|
|
|
if protocol.IsDomainTooLong(domain) {
|
|
|
|
return newError("long domain not supported: ", domain)
|
|
|
|
}
|
|
|
|
nDomain := len(domain)
|
2017-10-27 19:11:45 +00:00
|
|
|
buffer.AppendBytes(byte(protocol.AddressTypeDomain), byte(nDomain))
|
|
|
|
common.Must(buffer.AppendSupplier(serial.WriteString(domain)))
|
2017-05-17 19:13:01 +00:00
|
|
|
}
|
2016-02-25 20:50:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-10 00:05:55 +00:00
|
|
|
if padingLen > 0 {
|
2017-10-27 19:11:45 +00:00
|
|
|
common.Must(buffer.AppendSupplier(buf.ReadFullFrom(rand.Reader, padingLen)))
|
2016-12-10 00:05:55 +00:00
|
|
|
}
|
2016-12-07 16:32:40 +00:00
|
|
|
|
2016-02-25 20:50:38 +00:00
|
|
|
fnv1a := fnv.New32a()
|
2017-10-27 19:11:45 +00:00
|
|
|
common.Must2(fnv1a.Write(buffer.Bytes()))
|
2016-02-25 20:50:38 +00:00
|
|
|
|
2017-10-27 19:11:45 +00:00
|
|
|
common.Must(buffer.AppendSupplier(func(b []byte) (int, error) {
|
|
|
|
fnv1a.Sum(b[:0])
|
|
|
|
return fnv1a.Size(), nil
|
|
|
|
}))
|
2016-02-25 20:50:38 +00:00
|
|
|
|
|
|
|
timestampHash := md5.New()
|
2017-09-19 21:27:49 +00:00
|
|
|
common.Must2(timestampHash.Write(hashTimestamp(timestamp)))
|
2016-02-25 20:50:38 +00:00
|
|
|
iv := timestampHash.Sum(nil)
|
2016-10-12 16:43:55 +00:00
|
|
|
aesStream := crypto.NewAesEncryptionStream(account.(*vmess.InternalAccount).ID.CmdKey(), iv)
|
2017-10-27 19:11:45 +00:00
|
|
|
aesStream.XORKeyStream(buffer.Bytes(), buffer.Bytes())
|
|
|
|
common.Must2(writer.Write(buffer.Bytes()))
|
2017-10-22 18:17:06 +00:00
|
|
|
return nil
|
2016-02-25 20:50:38 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 21:27:49 +00:00
|
|
|
func (c *ClientSession) EncodeRequestBody(request *protocol.RequestHeader, writer io.Writer) buf.Writer {
|
2017-04-23 11:30:08 +00:00
|
|
|
var sizeParser crypto.ChunkSizeEncoder = crypto.PlainChunkSizeParser{}
|
2017-02-14 13:16:43 +00:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkMasking) {
|
2017-09-19 21:27:49 +00:00
|
|
|
sizeParser = NewShakeSizeParser(c.requestBodyIV)
|
2017-02-14 13:16:43 +00:00
|
|
|
}
|
2016-12-07 16:32:40 +00:00
|
|
|
if request.Security.Is(protocol.SecurityType_NONE) {
|
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
2017-05-01 22:28:16 +00:00
|
|
|
if request.Command == protocol.RequestCommandTCP {
|
|
|
|
return crypto.NewChunkStreamWriter(sizeParser, writer)
|
|
|
|
}
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(NoOpAuthenticator),
|
|
|
|
NonceGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-05-02 20:23:07 +00:00
|
|
|
return crypto.NewAuthenticationWriter(auth, sizeParser, writer, protocol.TransferTypePacket)
|
2016-12-07 16:32:40 +00:00
|
|
|
}
|
2017-04-23 11:30:08 +00:00
|
|
|
|
|
|
|
return buf.NewWriter(writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Security.Is(protocol.SecurityType_LEGACY) {
|
2017-09-19 21:27:49 +00:00
|
|
|
aesStream := crypto.NewAesEncryptionStream(c.requestBodyKey, c.requestBodyIV)
|
2016-12-07 16:32:40 +00:00
|
|
|
cryptionWriter := crypto.NewCryptionWriter(aesStream, writer)
|
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(FnvAuthenticator),
|
|
|
|
NonceGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-05-02 20:23:07 +00:00
|
|
|
return crypto.NewAuthenticationWriter(auth, sizeParser, cryptionWriter, request.Command.TransferType())
|
2016-12-07 16:32:40 +00:00
|
|
|
}
|
2017-04-23 11:30:08 +00:00
|
|
|
|
|
|
|
return buf.NewWriter(cryptionWriter)
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Security.Is(protocol.SecurityType_AES128_GCM) {
|
2017-09-19 21:27:49 +00:00
|
|
|
block, _ := aes.NewCipher(c.requestBodyKey)
|
2016-12-07 16:32:40 +00:00
|
|
|
aead, _ := cipher.NewGCM(block)
|
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &ChunkNonceGenerator{
|
2017-09-19 21:27:49 +00:00
|
|
|
Nonce: append([]byte(nil), c.requestBodyIV...),
|
2016-12-07 16:32:40 +00:00
|
|
|
Size: aead.NonceSize(),
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-05-02 20:23:07 +00:00
|
|
|
return crypto.NewAuthenticationWriter(auth, sizeParser, writer, request.Command.TransferType())
|
2017-04-23 11:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if request.Security.Is(protocol.SecurityType_CHACHA20_POLY1305) {
|
2017-09-19 21:27:49 +00:00
|
|
|
aead, _ := chacha20poly1305.New(GenerateChacha20Poly1305Key(c.requestBodyKey))
|
2016-12-07 16:32:40 +00:00
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &ChunkNonceGenerator{
|
2017-09-19 21:27:49 +00:00
|
|
|
Nonce: append([]byte(nil), c.requestBodyIV...),
|
2016-12-07 16:32:40 +00:00
|
|
|
Size: aead.NonceSize(),
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-05-02 20:23:07 +00:00
|
|
|
return crypto.NewAuthenticationWriter(auth, sizeParser, writer, request.Command.TransferType())
|
2016-12-07 16:32:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 11:30:08 +00:00
|
|
|
panic("Unknown security type.")
|
2016-02-25 20:50:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 20:21:59 +00:00
|
|
|
func (c *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.ResponseHeader, error) {
|
|
|
|
aesStream := crypto.NewAesDecryptionStream(c.responseBodyKey, c.responseBodyIV)
|
|
|
|
c.responseReader = crypto.NewCryptionReader(aesStream, reader)
|
2016-02-26 23:05:53 +00:00
|
|
|
|
2017-10-27 19:11:45 +00:00
|
|
|
buffer := buf.New()
|
|
|
|
defer buffer.Release()
|
2016-02-26 23:05:53 +00:00
|
|
|
|
2017-10-27 19:11:45 +00:00
|
|
|
if err := buffer.AppendSupplier(buf.ReadFullFrom(c.responseReader, 4)); err != nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("failed to read response header").Base(err))
|
2016-02-26 23:05:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-27 19:11:45 +00:00
|
|
|
if buffer.Byte(0) != c.responseHeader {
|
|
|
|
return nil, newError("unexpected response header. Expecting ", int(c.responseHeader), " but actually ", int(buffer.Byte(0)))
|
2016-02-26 23:05:53 +00:00
|
|
|
}
|
|
|
|
|
2016-06-02 19:34:25 +00:00
|
|
|
header := &protocol.ResponseHeader{
|
2017-10-27 19:11:45 +00:00
|
|
|
Option: bitmask.Byte(buffer.Byte(1)),
|
2016-06-02 19:34:25 +00:00
|
|
|
}
|
2016-02-26 23:05:53 +00:00
|
|
|
|
2017-10-27 19:11:45 +00:00
|
|
|
if buffer.Byte(2) != 0 {
|
|
|
|
cmdID := buffer.Byte(2)
|
|
|
|
dataLen := int(buffer.Byte(3))
|
|
|
|
|
|
|
|
if err := buffer.Reset(buf.ReadFullFrom(c.responseReader, dataLen)); err != nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
log.Trace(newError("failed to read response command").Base(err))
|
2016-02-26 23:05:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-27 19:11:45 +00:00
|
|
|
command, err := UnmarshalCommand(cmdID, buffer.Bytes())
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-25 20:21:59 +00:00
|
|
|
func (c *ClientSession) DecodeResponseBody(request *protocol.RequestHeader, reader io.Reader) buf.Reader {
|
2017-04-23 11:30:08 +00:00
|
|
|
var sizeParser crypto.ChunkSizeDecoder = crypto.PlainChunkSizeParser{}
|
2017-02-14 13:16:43 +00:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkMasking) {
|
2017-07-25 20:21:59 +00:00
|
|
|
sizeParser = NewShakeSizeParser(c.responseBodyIV)
|
2017-02-14 13:16:43 +00:00
|
|
|
}
|
2016-12-07 16:32:40 +00:00
|
|
|
if request.Security.Is(protocol.SecurityType_NONE) {
|
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
2017-05-01 22:28:16 +00:00
|
|
|
if request.Command == protocol.RequestCommandTCP {
|
|
|
|
return crypto.NewChunkStreamReader(sizeParser, reader)
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(NoOpAuthenticator),
|
|
|
|
NonceGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
|
|
|
|
2017-05-02 20:23:07 +00:00
|
|
|
return crypto.NewAuthenticationReader(auth, sizeParser, reader, protocol.TransferTypePacket)
|
2016-12-07 16:32:40 +00:00
|
|
|
}
|
2017-04-23 11:30:08 +00:00
|
|
|
|
|
|
|
return buf.NewReader(reader)
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Security.Is(protocol.SecurityType_LEGACY) {
|
2016-12-07 16:32:40 +00:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(FnvAuthenticator),
|
|
|
|
NonceGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-07-25 20:21:59 +00:00
|
|
|
return crypto.NewAuthenticationReader(auth, sizeParser, c.responseReader, request.Command.TransferType())
|
2016-12-07 16:32:40 +00:00
|
|
|
}
|
2017-04-23 11:30:08 +00:00
|
|
|
|
2017-07-25 20:21:59 +00:00
|
|
|
return buf.NewReader(c.responseReader)
|
2017-04-23 11:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if request.Security.Is(protocol.SecurityType_AES128_GCM) {
|
2017-07-25 20:21:59 +00:00
|
|
|
block, _ := aes.NewCipher(c.responseBodyKey)
|
2016-12-07 16:32:40 +00:00
|
|
|
aead, _ := cipher.NewGCM(block)
|
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &ChunkNonceGenerator{
|
2017-07-25 20:21:59 +00:00
|
|
|
Nonce: append([]byte(nil), c.responseBodyIV...),
|
2016-12-07 16:32:40 +00:00
|
|
|
Size: aead.NonceSize(),
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-05-02 20:23:07 +00:00
|
|
|
return crypto.NewAuthenticationReader(auth, sizeParser, reader, request.Command.TransferType())
|
2017-04-23 11:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if request.Security.Is(protocol.SecurityType_CHACHA20_POLY1305) {
|
2017-07-25 20:21:59 +00:00
|
|
|
aead, _ := chacha20poly1305.New(GenerateChacha20Poly1305Key(c.responseBodyKey))
|
2016-12-07 16:32:40 +00:00
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &ChunkNonceGenerator{
|
2017-07-25 20:21:59 +00:00
|
|
|
Nonce: append([]byte(nil), c.responseBodyIV...),
|
2016-12-07 16:32:40 +00:00
|
|
|
Size: aead.NonceSize(),
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-05-02 20:23:07 +00:00
|
|
|
return crypto.NewAuthenticationReader(auth, sizeParser, reader, request.Command.TransferType())
|
2016-12-07 16:32:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 11:30:08 +00:00
|
|
|
panic("Unknown security type.")
|
2016-12-07 16:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ChunkNonceGenerator struct {
|
|
|
|
Nonce []byte
|
|
|
|
Size int
|
|
|
|
count uint16
|
|
|
|
}
|
|
|
|
|
2017-09-19 21:27:49 +00:00
|
|
|
func (g *ChunkNonceGenerator) Next() []byte {
|
|
|
|
serial.Uint16ToBytes(g.count, g.Nonce[:0])
|
|
|
|
g.count++
|
|
|
|
return g.Nonce[:g.Size]
|
2016-02-26 23:05:53 +00:00
|
|
|
}
|