2016-07-23 07:17:51 -04:00
|
|
|
package encoding
|
2016-02-26 18:05:53 -05:00
|
|
|
|
|
|
|
import (
|
2016-12-07 11:32:40 -05:00
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
2016-02-26 18:05:53 -05:00
|
|
|
"crypto/md5"
|
|
|
|
"hash/fnv"
|
|
|
|
"io"
|
2016-12-07 11:32:40 -05:00
|
|
|
|
|
|
|
"golang.org/x/crypto/chacha20poly1305"
|
|
|
|
|
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-04 03:10:47 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/log"
|
2017-01-13 17:42:39 -05:00
|
|
|
"v2ray.com/core/common/net"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
"v2ray.com/core/common/serial"
|
|
|
|
"v2ray.com/core/proxy/vmess"
|
2016-02-26 18:05:53 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type ServerSession struct {
|
|
|
|
userValidator protocol.UserValidator
|
|
|
|
requestBodyKey []byte
|
|
|
|
requestBodyIV []byte
|
|
|
|
responseBodyKey []byte
|
|
|
|
responseBodyIV []byte
|
|
|
|
responseHeader byte
|
|
|
|
responseWriter io.Writer
|
|
|
|
}
|
|
|
|
|
2016-04-28 16:31:33 -04:00
|
|
|
// NewServerSession creates a new ServerSession, using the given UserValidator.
|
|
|
|
// The ServerSession instance doesn't take ownership of the validator.
|
2016-02-27 11:28:21 -05:00
|
|
|
func NewServerSession(validator protocol.UserValidator) *ServerSession {
|
|
|
|
return &ServerSession{
|
|
|
|
userValidator: validator,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.RequestHeader, error) {
|
2016-07-16 06:11:45 -04:00
|
|
|
buffer := make([]byte, 512)
|
2016-02-26 18:05:53 -05:00
|
|
|
|
2016-07-16 06:11:45 -04:00
|
|
|
_, err := io.ReadFull(reader, buffer[:protocol.IDBytesLen])
|
2016-02-26 18:05:53 -05:00
|
|
|
if err != nil {
|
2016-11-30 18:40:20 -05:00
|
|
|
log.Info("VMess|Server: Failed to read request header: ", err)
|
2016-05-30 18:21:41 -04:00
|
|
|
return nil, io.EOF
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
user, timestamp, valid := v.userValidator.Get(buffer[:protocol.IDBytesLen])
|
2016-02-26 18:05:53 -05:00
|
|
|
if !valid {
|
2016-12-15 11:12:53 -05:00
|
|
|
return nil, errors.New("VMess|Server: Invalid user.")
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
timestampHash := md5.New()
|
|
|
|
timestampHash.Write(hashTimestamp(timestamp))
|
|
|
|
iv := timestampHash.Sum(nil)
|
2016-10-16 08:22:21 -04:00
|
|
|
account, err := user.GetTypedAccount()
|
2016-09-17 18:41:21 -04:00
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.Base(err).Message("VMess|Server: Failed to get user account.")
|
2016-09-17 18:41:21 -04:00
|
|
|
}
|
2016-10-16 08:22:21 -04:00
|
|
|
|
2016-10-12 12:43:55 -04:00
|
|
|
aesStream := crypto.NewAesDecryptionStream(account.(*vmess.InternalAccount).ID.CmdKey(), iv)
|
2016-02-26 18:05:53 -05:00
|
|
|
decryptor := crypto.NewCryptionReader(aesStream, reader)
|
|
|
|
|
2016-07-16 06:11:45 -04:00
|
|
|
nBytes, err := io.ReadFull(decryptor, buffer[:41])
|
2016-02-26 18:05:53 -05:00
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.Base(err).Message("VMess|Server: Failed to read request header.")
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
|
|
|
bufferLen := nBytes
|
|
|
|
|
|
|
|
request := &protocol.RequestHeader{
|
|
|
|
User: user,
|
2016-07-16 06:11:45 -04:00
|
|
|
Version: buffer[0],
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if request.Version != Version {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.New("VMess|Server: Invalid protocol version ", request.Version)
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
v.requestBodyIV = append([]byte(nil), buffer[1:17]...) // 16 bytes
|
|
|
|
v.requestBodyKey = append([]byte(nil), buffer[17:33]...) // 16 bytes
|
|
|
|
v.responseHeader = buffer[33] // 1 byte
|
2016-12-07 11:32:40 -05:00
|
|
|
request.Option = protocol.RequestOption(buffer[34]) // 1 byte
|
|
|
|
padingLen := int(buffer[35] >> 4)
|
2016-12-13 04:45:21 -05:00
|
|
|
request.Security = protocol.NormSecurity(protocol.Security(buffer[35] & 0x0F))
|
2016-12-07 11:32:40 -05:00
|
|
|
// 1 bytes reserved
|
2016-07-16 06:11:45 -04:00
|
|
|
request.Command = protocol.RequestCommand(buffer[37])
|
2016-02-26 18:05:53 -05:00
|
|
|
|
2017-01-13 17:42:39 -05:00
|
|
|
request.Port = net.PortFromBytes(buffer[38:40])
|
2016-02-26 18:05:53 -05:00
|
|
|
|
2016-07-16 06:11:45 -04:00
|
|
|
switch buffer[40] {
|
2016-02-26 18:05:53 -05:00
|
|
|
case AddrTypeIPv4:
|
2016-11-27 15:43:20 -05:00
|
|
|
_, err = io.ReadFull(decryptor, buffer[41:45]) // 4 bytes
|
2016-02-26 18:05:53 -05:00
|
|
|
bufferLen += 4
|
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.Base(err).Message("VMess|Server: Failed to read IPv4.")
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
2017-01-13 17:42:39 -05:00
|
|
|
request.Address = net.IPAddress(buffer[41:45])
|
2016-02-26 18:05:53 -05:00
|
|
|
case AddrTypeIPv6:
|
2016-11-27 15:43:20 -05:00
|
|
|
_, err = io.ReadFull(decryptor, buffer[41:57]) // 16 bytes
|
2016-02-26 18:05:53 -05:00
|
|
|
bufferLen += 16
|
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.Base(err).Message("VMess|Server: Failed to read IPv6 address.")
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
2017-01-13 17:42:39 -05:00
|
|
|
request.Address = net.IPAddress(buffer[41:57])
|
2016-02-26 18:05:53 -05:00
|
|
|
case AddrTypeDomain:
|
2016-11-19 08:38:13 -05:00
|
|
|
_, err = io.ReadFull(decryptor, buffer[41:42])
|
2016-02-26 18:05:53 -05:00
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.Base(err).Message("VMess:Server: Failed to read domain.")
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
2016-07-16 06:11:45 -04:00
|
|
|
domainLength := int(buffer[41])
|
2016-02-26 18:05:53 -05:00
|
|
|
if domainLength == 0 {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.New("VMess|Server: Zero length domain.")
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
2016-11-27 15:43:20 -05:00
|
|
|
_, err = io.ReadFull(decryptor, buffer[42:42+domainLength])
|
2016-02-26 18:05:53 -05:00
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.Base(err).Message("VMess|Server: Failed to read domain.")
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
|
|
|
bufferLen += 1 + domainLength
|
2017-01-13 17:42:39 -05:00
|
|
|
request.Address = net.DomainAddress(string(buffer[42 : 42+domainLength]))
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
2016-12-07 11:32:40 -05:00
|
|
|
if padingLen > 0 {
|
|
|
|
_, err = io.ReadFull(decryptor, buffer[bufferLen:bufferLen+padingLen])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("VMess|Server: Failed to read padding.")
|
|
|
|
}
|
|
|
|
bufferLen += padingLen
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:43:20 -05:00
|
|
|
_, err = io.ReadFull(decryptor, buffer[bufferLen:bufferLen+4])
|
2016-02-26 18:05:53 -05:00
|
|
|
if err != nil {
|
2016-12-04 03:43:33 -05:00
|
|
|
return nil, errors.Base(err).Message("VMess|Server: Failed to read checksum.")
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fnv1a := fnv.New32a()
|
2016-07-16 06:11:45 -04:00
|
|
|
fnv1a.Write(buffer[:bufferLen])
|
2016-02-26 18:05:53 -05:00
|
|
|
actualHash := fnv1a.Sum32()
|
2016-07-16 06:11:45 -04:00
|
|
|
expectedHash := serial.BytesToUint32(buffer[bufferLen : bufferLen+4])
|
2016-02-26 18:05:53 -05:00
|
|
|
|
|
|
|
if actualHash != expectedHash {
|
2016-11-19 08:38:13 -05:00
|
|
|
return nil, errors.New("VMess|Server: Invalid auth.")
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
2016-12-13 03:17:39 -05:00
|
|
|
if request.Address == nil {
|
|
|
|
return nil, errors.New("VMess|Server: Invalid remote address.")
|
|
|
|
}
|
|
|
|
|
2016-02-26 18:05:53 -05:00
|
|
|
return request, nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 07:17:34 -05:00
|
|
|
func (v *ServerSession) DecodeRequestBody(request *protocol.RequestHeader, reader io.Reader) buf.Reader {
|
2016-12-07 11:32:40 -05:00
|
|
|
var authReader io.Reader
|
|
|
|
if request.Security.Is(protocol.SecurityType_NONE) {
|
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
2017-01-22 14:43:01 -05:00
|
|
|
AEAD: NoOpAuthenticator{},
|
2016-12-07 11:32:40 -05:00
|
|
|
NonceGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-01-30 16:07:22 -05:00
|
|
|
authReader = crypto.NewAuthenticationReader(auth, reader)
|
2016-12-07 11:32:40 -05:00
|
|
|
} else {
|
|
|
|
authReader = reader
|
|
|
|
}
|
|
|
|
} else if request.Security.Is(protocol.SecurityType_LEGACY) {
|
|
|
|
aesStream := crypto.NewAesDecryptionStream(v.requestBodyKey, v.requestBodyIV)
|
|
|
|
cryptionReader := crypto.NewCryptionReader(aesStream, reader)
|
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(FnvAuthenticator),
|
|
|
|
NonceGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-01-30 16:07:22 -05:00
|
|
|
authReader = crypto.NewAuthenticationReader(auth, cryptionReader)
|
2016-12-07 11:32:40 -05:00
|
|
|
} else {
|
|
|
|
authReader = cryptionReader
|
|
|
|
}
|
|
|
|
} else if request.Security.Is(protocol.SecurityType_AES128_GCM) {
|
2016-12-07 16:52:56 -05:00
|
|
|
block, _ := aes.NewCipher(v.requestBodyKey)
|
2016-12-07 11:32:40 -05:00
|
|
|
aead, _ := cipher.NewGCM(block)
|
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &ChunkNonceGenerator{
|
2016-12-07 16:52:56 -05:00
|
|
|
Nonce: append([]byte(nil), v.requestBodyIV...),
|
2016-12-07 11:32:40 -05:00
|
|
|
Size: aead.NonceSize(),
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-01-30 16:07:22 -05:00
|
|
|
authReader = crypto.NewAuthenticationReader(auth, reader)
|
2016-12-07 11:32:40 -05:00
|
|
|
} else if request.Security.Is(protocol.SecurityType_CHACHA20_POLY1305) {
|
2016-12-07 16:52:56 -05:00
|
|
|
aead, _ := chacha20poly1305.New(GenerateChacha20Poly1305Key(v.requestBodyKey))
|
2016-12-07 11:32:40 -05:00
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &ChunkNonceGenerator{
|
2016-12-07 16:52:56 -05:00
|
|
|
Nonce: append([]byte(nil), v.requestBodyIV...),
|
2016-12-07 11:32:40 -05:00
|
|
|
Size: aead.NonceSize(),
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
2017-01-30 16:07:22 -05:00
|
|
|
authReader = crypto.NewAuthenticationReader(auth, reader)
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
|
|
|
|
2016-12-09 07:17:34 -05:00
|
|
|
return buf.NewReader(authReader)
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *ServerSession) EncodeResponseHeader(header *protocol.ResponseHeader, writer io.Writer) {
|
|
|
|
responseBodyKey := md5.Sum(v.requestBodyKey)
|
|
|
|
responseBodyIV := md5.Sum(v.requestBodyIV)
|
|
|
|
v.responseBodyKey = responseBodyKey[:]
|
|
|
|
v.responseBodyIV = responseBodyIV[:]
|
2016-02-26 18:05:53 -05:00
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
aesStream := crypto.NewAesEncryptionStream(v.responseBodyKey, v.responseBodyIV)
|
2016-02-26 18:05:53 -05:00
|
|
|
encryptionWriter := crypto.NewCryptionWriter(aesStream, writer)
|
2016-11-27 15:39:09 -05:00
|
|
|
v.responseWriter = encryptionWriter
|
2016-02-26 18:05:53 -05:00
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
encryptionWriter.Write([]byte{v.responseHeader, byte(header.Option)})
|
2016-02-27 11:28:21 -05:00
|
|
|
err := MarshalCommand(header.Command, encryptionWriter)
|
|
|
|
if err != nil {
|
|
|
|
encryptionWriter.Write([]byte{0x00, 0x00})
|
|
|
|
}
|
2016-02-27 10:41:21 -05:00
|
|
|
}
|
|
|
|
|
2016-12-09 07:17:34 -05:00
|
|
|
func (v *ServerSession) EncodeResponseBody(request *protocol.RequestHeader, writer io.Writer) buf.Writer {
|
2016-12-07 11:32:40 -05:00
|
|
|
var authWriter io.Writer
|
|
|
|
if request.Security.Is(protocol.SecurityType_NONE) {
|
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: new(FnvAuthenticator),
|
|
|
|
NonceGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
|
|
|
authWriter = crypto.NewAuthenticationWriter(auth, writer)
|
|
|
|
} else {
|
|
|
|
authWriter = writer
|
|
|
|
}
|
|
|
|
} 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{},
|
|
|
|
}
|
|
|
|
authWriter = crypto.NewAuthenticationWriter(auth, v.responseWriter)
|
|
|
|
} else {
|
|
|
|
authWriter = v.responseWriter
|
|
|
|
}
|
|
|
|
} 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{},
|
|
|
|
}
|
|
|
|
authWriter = crypto.NewAuthenticationWriter(auth, writer)
|
|
|
|
} else if request.Security.Is(protocol.SecurityType_CHACHA20_POLY1305) {
|
2016-12-07 15:43:41 -05:00
|
|
|
aead, _ := chacha20poly1305.New(GenerateChacha20Poly1305Key(v.responseBodyKey))
|
2016-12-07 11:32:40 -05:00
|
|
|
|
|
|
|
auth := &crypto.AEADAuthenticator{
|
|
|
|
AEAD: aead,
|
|
|
|
NonceGenerator: &ChunkNonceGenerator{
|
|
|
|
Nonce: append([]byte(nil), v.responseBodyIV...),
|
|
|
|
Size: aead.NonceSize(),
|
|
|
|
},
|
|
|
|
AdditionalDataGenerator: crypto.NoOpBytesGenerator{},
|
|
|
|
}
|
|
|
|
authWriter = crypto.NewAuthenticationWriter(auth, writer)
|
|
|
|
}
|
|
|
|
|
2016-12-09 07:17:34 -05:00
|
|
|
return buf.NewWriter(authWriter)
|
2016-02-26 18:05:53 -05:00
|
|
|
}
|