2016-07-23 07:17:51 -04:00
|
|
|
package encoding
|
2016-02-25 15:50:38 -05:00
|
|
|
|
|
|
|
import (
|
2016-12-07 11:32:40 -05:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
2016-02-25 15:50:38 -05:00
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rand"
|
|
|
|
"hash/fnv"
|
|
|
|
"io"
|
2018-09-11 14:15:15 -04:00
|
|
|
"sync"
|
2016-12-07 11:32:40 -05:00
|
|
|
|
|
|
|
"golang.org/x/crypto/chacha20poly1305"
|
|
|
|
|
2017-09-19 17:27:49 -04:00
|
|
|
"v2ray.com/core/common"
|
2017-10-21 15:04:24 -04:00
|
|
|
"v2ray.com/core/common/bitmask"
|
2016-12-09 07:17:34 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/crypto"
|
2016-12-07 11:32:40 -05:00
|
|
|
"v2ray.com/core/common/dice"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/protocol"
|
2016-12-07 11:32:40 -05:00
|
|
|
"v2ray.com/core/common/serial"
|
2016-08-20 14:55:45 -04:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2016-12-13 03:32:29 -05:00
|
|
|
// ClientSession stores connection session info for VMess client.
|
2016-02-25 15:50:38 -05:00
|
|
|
type ClientSession struct {
|
|
|
|
idHash protocol.IDHash
|
2018-04-14 09:05:49 -04:00
|
|
|
requestBodyKey [16]byte
|
|
|
|
requestBodyIV [16]byte
|
|
|
|
responseBodyKey [16]byte
|
|
|
|
responseBodyIV [16]byte
|
|
|
|
responseReader io.Reader
|
|
|
|
responseHeader byte
|
2018-09-12 09:27:45 -04:00
|
|
|
|
|
|
|
buffer [33]byte // 16 + 16 + 1
|
2016-02-25 15:50:38 -05:00
|
|
|
}
|
|
|
|
|
2018-09-11 14:15:15 -04:00
|
|
|
var clientSessionPool = sync.Pool{
|
|
|
|
New: func() interface{} { return &ClientSession{} },
|
|
|
|
}
|
|
|
|
|
2016-12-13 03:32:29 -05:00
|
|
|
// NewClientSession creates a new ClientSession.
|
2016-02-25 15:50:38 -05:00
|
|
|
func NewClientSession(idHash protocol.IDHash) *ClientSession {
|
2018-09-12 09:27:45 -04:00
|
|
|
session := clientSessionPool.Get().(*ClientSession)
|
|
|
|
|
|
|
|
randomBytes := session.buffer[:]
|
2017-09-19 17:27:49 -04:00
|
|
|
common.Must2(rand.Read(randomBytes))
|
2016-02-25 15:50:38 -05:00
|
|
|
|
2018-04-14 09:05:49 -04:00
|
|
|
copy(session.requestBodyKey[:], randomBytes[:16])
|
|
|
|
copy(session.requestBodyIV[:], randomBytes[16:32])
|
2016-02-25 15:50:38 -05:00
|
|
|
session.responseHeader = randomBytes[32]
|
2018-04-14 09:05:49 -04:00
|
|
|
session.responseBodyKey = md5.Sum(session.requestBodyKey[:])
|
|
|
|
session.responseBodyIV = md5.Sum(session.requestBodyIV[:])
|
2016-02-25 15:50:38 -05:00
|
|
|
session.idHash = idHash
|
|
|
|
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2018-09-11 14:15:15 -04:00
|
|
|
func ReleaseClientSession(session *ClientSession) {
|
|
|
|
session.idHash = nil
|
|
|
|
session.responseReader = nil
|
|
|
|
clientSessionPool.Put(session)
|
|
|
|
}
|
|
|
|
|
2017-10-22 14:17:06 -04:00
|
|
|
func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writer io.Writer) error {
|
2016-02-25 15:50:38 -05:00
|
|
|
timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)()
|
2018-08-26 18:11:32 -04:00
|
|
|
account := header.User.Account.(*vmess.InternalAccount)
|
|
|
|
idHash := c.idHash(account.AnyValidID().Bytes())
|
2017-09-19 17:27:49 -04:00
|
|
|
common.Must2(idHash.Write(timestamp.Bytes(nil)))
|
|
|
|
common.Must2(writer.Write(idHash.Sum(nil)))
|
2016-02-25 15:50:38 -05:00
|
|
|
|
2017-10-27 15:11:45 -04:00
|
|
|
buffer := buf.New()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
2018-07-30 16:45:06 -04:00
|
|
|
common.Must2(buffer.WriteBytes(Version))
|
|
|
|
common.Must2(buffer.Write(c.requestBodyIV[:]))
|
|
|
|
common.Must2(buffer.Write(c.requestBodyKey[:]))
|
|
|
|
common.Must2(buffer.WriteBytes(c.responseHeader, byte(header.Option)))
|
2017-10-27 15:11:45 -04:00
|
|
|
|
2016-12-07 11:32:40 -05:00
|
|
|
padingLen := dice.Roll(16)
|
|
|
|
security := byte(padingLen<<4) | byte(header.Security)
|
2018-07-30 16:45:06 -04:00
|
|
|
common.Must2(buffer.WriteBytes(security, byte(0), byte(header.Command)))
|
2017-05-17 15:13:01 -04:00
|
|
|
|
|
|
|
if header.Command != protocol.RequestCommandMux {
|
2018-02-23 17:42:01 -05:00
|
|
|
if err := addrParser.WriteAddressPort(buffer, header.Address, header.Port); err != nil {
|
|
|
|
return newError("failed to writer address and port").Base(err)
|
2017-05-17 15:13:01 -04:00
|
|
|
}
|
2016-02-25 15:50:38 -05:00
|
|
|
}
|
|
|
|
|
2016-12-09 19:05:55 -05:00
|
|
|
if padingLen > 0 {
|
2018-04-01 18:44:47 -04:00
|
|
|
common.Must(buffer.AppendSupplier(buf.ReadFullFrom(rand.Reader, int32(padingLen))))
|
2016-12-09 19:05:55 -05:00
|
|
|
}
|
2016-12-07 11:32:40 -05:00
|
|
|
|
2018-07-30 16:54:24 -04:00
|
|
|
{
|
|
|
|
fnv1a := fnv.New32a()
|
|
|
|
common.Must2(fnv1a.Write(buffer.Bytes()))
|
|
|
|
common.Must(buffer.AppendSupplier(serial.WriteHash(fnv1a)))
|
|
|
|
}
|
2016-02-25 15:50:38 -05:00
|
|
|
|
|
|
|
timestampHash := md5.New()
|
2017-09-19 17:27:49 -04:00
|
|
|
common.Must2(timestampHash.Write(hashTimestamp(timestamp)))
|
2016-02-25 15:50:38 -05:00
|
|
|
iv := timestampHash.Sum(nil)
|
2018-08-26 18:11:32 -04:00
|
|
|
aesStream := crypto.NewAesEncryptionStream(account.ID.CmdKey(), iv)
|
2017-10-27 15:11:45 -04:00
|
|
|
aesStream.XORKeyStream(buffer.Bytes(), buffer.Bytes())
|
|
|
|
common.Must2(writer.Write(buffer.Bytes()))
|
2017-10-22 14:17:06 -04:00
|
|
|
return nil
|
2016-02-25 15:50:38 -05:00
|
|
|
}
|
|
|
|
|
2017-09-19 17:27:49 -04:00
|
|
|
func (c *ClientSession) EncodeRequestBody(request *protocol.RequestHeader, writer io.Writer) buf.Writer {
|
2017-04-23 07:30:08 -04:00
|
|
|
var sizeParser crypto.ChunkSizeEncoder = crypto.PlainChunkSizeParser{}
|
2017-02-14 08:16:43 -05:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkMasking) {
|
2018-04-14 09:05:49 -04:00
|
|
|
sizeParser = NewShakeSizeParser(c.requestBodyIV[:])
|
2017-02-14 08:16:43 -05:00
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
var padding crypto.PaddingLengthGenerator = nil
|
|
|
|
if request.Option.Has(protocol.RequestOptionGlobalPadding) {
|
|
|
|
padding = sizeParser.(crypto.PaddingLengthGenerator)
|
|
|
|
}
|
|
|
|
|
2018-02-23 06:13:02 -05:00
|
|
|
switch request.Security {
|
|
|
|
case protocol.SecurityType_NONE:
|
2016-12-07 11:32:40 -05:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
2018-02-09 16:29:30 -05:00
|
|
|
if request.Command.TransferType() == protocol.TransferTypeStream {
|
2017-05-01 18:28:16 -04:00
|
|
|
return crypto.NewChunkStreamWriter(sizeParser, writer)
|
|
|
|
}
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(NoOpAuthenticator),
|
2018-04-14 07:10:12 -04:00
|
|
|
NonceGenerator: crypto.GenerateEmptyBytes(),
|
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2017-05-01 18:28:16 -04:00
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationWriter(auth, sizeParser, writer, protocol.TransferTypePacket, padding)
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2017-04-23 07:30:08 -04:00
|
|
|
|
|
|
|
return buf.NewWriter(writer)
|
2018-02-23 06:13:02 -05:00
|
|
|
case protocol.SecurityType_LEGACY:
|
2018-04-14 09:05:49 -04:00
|
|
|
aesStream := crypto.NewAesEncryptionStream(c.requestBodyKey[:], c.requestBodyIV[:])
|
2016-12-07 11:32:40 -05:00
|
|
|
cryptionWriter := crypto.NewCryptionWriter(aesStream, writer)
|
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(FnvAuthenticator),
|
2018-04-14 07:10:12 -04:00
|
|
|
NonceGenerator: crypto.GenerateEmptyBytes(),
|
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationWriter(auth, sizeParser, cryptionWriter, request.Command.TransferType(), padding)
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2017-04-23 07:30:08 -04:00
|
|
|
|
2018-07-31 10:05:57 -04:00
|
|
|
return &buf.SequentialWriter{Writer: cryptionWriter}
|
2018-02-23 06:13:02 -05:00
|
|
|
case protocol.SecurityType_AES128_GCM:
|
2018-04-14 09:05:49 -04:00
|
|
|
block, _ := aes.NewCipher(c.requestBodyKey[:])
|
2016-12-07 11:32:40 -05:00
|
|
|
aead, _ := cipher.NewGCM(block)
|
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
2018-04-14 07:10:12 -04:00
|
|
|
AEAD: aead,
|
2018-04-14 09:05:49 -04:00
|
|
|
NonceGenerator: GenerateChunkNonce(c.requestBodyIV[:], uint32(aead.NonceSize())),
|
2018-04-14 07:10:12 -04:00
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationWriter(auth, sizeParser, writer, request.Command.TransferType(), padding)
|
2018-02-23 06:13:02 -05:00
|
|
|
case protocol.SecurityType_CHACHA20_POLY1305:
|
2018-04-14 09:05:49 -04:00
|
|
|
aead, _ := chacha20poly1305.New(GenerateChacha20Poly1305Key(c.requestBodyKey[:]))
|
2016-12-07 11:32:40 -05:00
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
2018-04-14 07:10:12 -04:00
|
|
|
AEAD: aead,
|
2018-04-14 09:05:49 -04:00
|
|
|
NonceGenerator: GenerateChunkNonce(c.requestBodyIV[:], uint32(aead.NonceSize())),
|
2018-04-14 07:10:12 -04:00
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationWriter(auth, sizeParser, writer, request.Command.TransferType(), padding)
|
2018-02-23 06:13:02 -05:00
|
|
|
default:
|
|
|
|
panic("Unknown security type.")
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2016-02-25 15:50:38 -05:00
|
|
|
}
|
|
|
|
|
2017-07-25 16:21:59 -04:00
|
|
|
func (c *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.ResponseHeader, error) {
|
2018-04-14 09:05:49 -04:00
|
|
|
aesStream := crypto.NewAesDecryptionStream(c.responseBodyKey[:], c.responseBodyIV[:])
|
2017-07-25 16:21:59 -04:00
|
|
|
c.responseReader = crypto.NewCryptionReader(aesStream, reader)
|
2016-02-26 18:05:53 -05:00
|
|
|
|
2017-10-27 15:11:45 -04:00
|
|
|
buffer := buf.New()
|
|
|
|
defer buffer.Release()
|
2016-02-26 18:05:53 -05:00
|
|
|
|
2017-10-27 15:11:45 -04:00
|
|
|
if err := buffer.AppendSupplier(buf.ReadFullFrom(c.responseReader, 4)); err != nil {
|
2018-02-22 09:26:00 -05:00
|
|
|
return nil, newError("failed to read response header").Base(err)
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
2017-10-27 15:11:45 -04: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 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
2016-06-02 15:34:25 -04:00
|
|
|
header := &protocol.ResponseHeader{
|
2017-10-27 15:11:45 -04:00
|
|
|
Option: bitmask.Byte(buffer.Byte(1)),
|
2016-06-02 15:34:25 -04:00
|
|
|
}
|
2016-02-26 18:05:53 -05:00
|
|
|
|
2017-10-27 15:11:45 -04:00
|
|
|
if buffer.Byte(2) != 0 {
|
|
|
|
cmdID := buffer.Byte(2)
|
2018-04-01 18:44:47 -04:00
|
|
|
dataLen := int32(buffer.Byte(3))
|
2017-10-27 15:11:45 -04:00
|
|
|
|
|
|
|
if err := buffer.Reset(buf.ReadFullFrom(c.responseReader, dataLen)); err != nil {
|
2018-02-22 09:26:00 -05:00
|
|
|
return nil, newError("failed to read response command").Base(err)
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
2017-10-27 15:11:45 -04:00
|
|
|
command, err := UnmarshalCommand(cmdID, buffer.Bytes())
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-25 16:21:59 -04:00
|
|
|
func (c *ClientSession) DecodeResponseBody(request *protocol.RequestHeader, reader io.Reader) buf.Reader {
|
2017-04-23 07:30:08 -04:00
|
|
|
var sizeParser crypto.ChunkSizeDecoder = crypto.PlainChunkSizeParser{}
|
2017-02-14 08:16:43 -05:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkMasking) {
|
2018-04-14 09:05:49 -04:00
|
|
|
sizeParser = NewShakeSizeParser(c.responseBodyIV[:])
|
2017-02-14 08:16:43 -05:00
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
var padding crypto.PaddingLengthGenerator = nil
|
|
|
|
if request.Option.Has(protocol.RequestOptionGlobalPadding) {
|
|
|
|
padding = sizeParser.(crypto.PaddingLengthGenerator)
|
|
|
|
}
|
|
|
|
|
2018-02-23 06:13:02 -05:00
|
|
|
switch request.Security {
|
|
|
|
case protocol.SecurityType_NONE:
|
2016-12-07 11:32:40 -05:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
2018-02-09 16:29:30 -05:00
|
|
|
if request.Command.TransferType() == protocol.TransferTypeStream {
|
2017-05-01 18:28:16 -04:00
|
|
|
return crypto.NewChunkStreamReader(sizeParser, reader)
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(NoOpAuthenticator),
|
2018-04-14 07:10:12 -04:00
|
|
|
NonceGenerator: crypto.GenerateEmptyBytes(),
|
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2017-05-01 18:28:16 -04:00
|
|
|
}
|
|
|
|
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationReader(auth, sizeParser, reader, protocol.TransferTypePacket, padding)
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2017-04-23 07:30:08 -04:00
|
|
|
|
|
|
|
return buf.NewReader(reader)
|
2018-02-23 06:13:02 -05:00
|
|
|
case protocol.SecurityType_LEGACY:
|
2016-12-07 11:32:40 -05:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(FnvAuthenticator),
|
2018-04-14 07:10:12 -04:00
|
|
|
NonceGenerator: crypto.GenerateEmptyBytes(),
|
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationReader(auth, sizeParser, c.responseReader, request.Command.TransferType(), padding)
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2017-04-23 07:30:08 -04:00
|
|
|
|
2017-07-25 16:21:59 -04:00
|
|
|
return buf.NewReader(c.responseReader)
|
2018-02-23 06:13:02 -05:00
|
|
|
case protocol.SecurityType_AES128_GCM:
|
2018-04-14 09:05:49 -04:00
|
|
|
block, _ := aes.NewCipher(c.responseBodyKey[:])
|
2016-12-07 11:32:40 -05:00
|
|
|
aead, _ := cipher.NewGCM(block)
|
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
2018-04-14 07:10:12 -04:00
|
|
|
AEAD: aead,
|
2018-04-14 09:05:49 -04:00
|
|
|
NonceGenerator: GenerateChunkNonce(c.responseBodyIV[:], uint32(aead.NonceSize())),
|
2018-04-14 07:10:12 -04:00
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationReader(auth, sizeParser, reader, request.Command.TransferType(), padding)
|
2018-02-23 06:13:02 -05:00
|
|
|
case protocol.SecurityType_CHACHA20_POLY1305:
|
2018-04-14 09:05:49 -04:00
|
|
|
aead, _ := chacha20poly1305.New(GenerateChacha20Poly1305Key(c.responseBodyKey[:]))
|
2016-12-07 11:32:40 -05:00
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
2018-04-14 07:10:12 -04:00
|
|
|
AEAD: aead,
|
2018-04-14 09:05:49 -04:00
|
|
|
NonceGenerator: GenerateChunkNonce(c.responseBodyIV[:], uint32(aead.NonceSize())),
|
2018-04-14 07:10:12 -04:00
|
|
|
AdditionalDataGenerator: crypto.GenerateEmptyBytes(),
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
2018-07-07 09:42:24 -04:00
|
|
|
return crypto.NewAuthenticationReader(auth, sizeParser, reader, request.Command.TransferType(), padding)
|
2018-02-23 06:13:02 -05:00
|
|
|
default:
|
|
|
|
panic("Unknown security type.")
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 07:10:12 -04:00
|
|
|
func GenerateChunkNonce(nonce []byte, size uint32) crypto.BytesGenerator {
|
|
|
|
c := append([]byte(nil), nonce...)
|
|
|
|
count := uint16(0)
|
|
|
|
return func() []byte {
|
|
|
|
serial.Uint16ToBytes(count, c[:0])
|
|
|
|
count++
|
|
|
|
return c[:size]
|
|
|
|
}
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|