v2ray-core/proxy/vmess/encoding/server.go

167 lines
5.0 KiB
Go
Raw Normal View History

2016-07-23 11:17:51 +00:00
package encoding
2016-02-26 23:05:53 +00:00
import (
"crypto/md5"
2016-11-19 13:38:13 +00:00
"errors"
2016-02-26 23:05:53 +00:00
"hash/fnv"
"io"
2016-08-20 18:55:45 +00: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/common/serial"
"v2ray.com/core/proxy/vmess"
2016-02-26 23:05:53 +00:00
)
type ServerSession struct {
userValidator protocol.UserValidator
requestBodyKey []byte
requestBodyIV []byte
responseBodyKey []byte
responseBodyIV []byte
responseHeader byte
responseWriter io.Writer
}
2016-04-28 20:31:33 +00:00
// NewServerSession creates a new ServerSession, using the given UserValidator.
// The ServerSession instance doesn't take ownership of the validator.
2016-02-27 16:28:21 +00:00
func NewServerSession(validator protocol.UserValidator) *ServerSession {
return &ServerSession{
userValidator: validator,
}
}
2016-04-28 20:31:33 +00:00
// Release implements common.Releaseable.
2016-11-27 20:39:09 +00:00
func (v *ServerSession) Release() {
v.userValidator = nil
v.requestBodyIV = nil
v.requestBodyKey = nil
v.responseBodyIV = nil
v.responseBodyKey = nil
v.responseWriter = nil
2016-04-25 16:39:30 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.RequestHeader, error) {
2016-07-16 10:11:45 +00:00
buffer := make([]byte, 512)
2016-02-26 23:05:53 +00:00
2016-07-16 10:11:45 +00:00
_, err := io.ReadFull(reader, buffer[:protocol.IDBytesLen])
2016-02-26 23:05:53 +00:00
if err != nil {
2016-05-30 22:21:41 +00:00
log.Info("Raw: Failed to read request header: ", err)
return nil, io.EOF
2016-02-26 23:05:53 +00:00
}
2016-11-27 20:39:09 +00:00
user, timestamp, valid := v.userValidator.Get(buffer[:protocol.IDBytesLen])
2016-02-26 23:05:53 +00:00
if !valid {
2016-06-27 06:53:35 +00:00
return nil, protocol.ErrInvalidUser
2016-02-26 23:05:53 +00:00
}
timestampHash := md5.New()
timestampHash.Write(hashTimestamp(timestamp))
iv := timestampHash.Sum(nil)
2016-10-16 12:22:21 +00:00
account, err := user.GetTypedAccount()
2016-09-17 22:41:21 +00:00
if err != nil {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|Server: Failed to get user account: " + err.Error())
2016-09-17 22:41:21 +00:00
}
2016-10-16 12:22:21 +00:00
2016-10-12 16:43:55 +00:00
aesStream := crypto.NewAesDecryptionStream(account.(*vmess.InternalAccount).ID.CmdKey(), iv)
2016-02-26 23:05:53 +00:00
decryptor := crypto.NewCryptionReader(aesStream, reader)
2016-07-16 10:11:45 +00:00
nBytes, err := io.ReadFull(decryptor, buffer[:41])
2016-02-26 23:05:53 +00:00
if err != nil {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|Server: Failed to read request header: " + err.Error())
2016-02-26 23:05:53 +00:00
}
bufferLen := nBytes
request := &protocol.RequestHeader{
User: user,
2016-07-16 10:11:45 +00:00
Version: buffer[0],
2016-02-26 23:05:53 +00:00
}
if request.Version != Version {
2016-11-19 13:38:13 +00:00
log.Info("VMess|Server: Invalid protocol version ", request.Version)
2016-06-27 06:53:35 +00:00
return nil, protocol.ErrInvalidVersion
2016-02-26 23:05:53 +00:00
}
2016-11-27 20:39:09 +00: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-07-16 10:11:45 +00:00
request.Option = protocol.RequestOption(buffer[34]) // 1 byte + 2 bytes reserved
request.Command = protocol.RequestCommand(buffer[37])
2016-02-26 23:05:53 +00:00
2016-07-16 10:11:45 +00:00
request.Port = v2net.PortFromBytes(buffer[38:40])
2016-02-26 23:05:53 +00:00
2016-07-16 10:11:45 +00:00
switch buffer[40] {
2016-02-26 23:05:53 +00:00
case AddrTypeIPv4:
2016-07-16 10:11:45 +00:00
nBytes, err = io.ReadFull(decryptor, buffer[41:45]) // 4 bytes
2016-02-26 23:05:53 +00:00
bufferLen += 4
if err != nil {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|Server: Failed to read IPv4: " + err.Error())
2016-02-26 23:05:53 +00:00
}
2016-07-16 10:11:45 +00:00
request.Address = v2net.IPAddress(buffer[41:45])
2016-02-26 23:05:53 +00:00
case AddrTypeIPv6:
2016-07-16 10:11:45 +00:00
nBytes, err = io.ReadFull(decryptor, buffer[41:57]) // 16 bytes
2016-02-26 23:05:53 +00:00
bufferLen += 16
if err != nil {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|Server: Failed to read IPv6 address: " + err.Error())
2016-02-26 23:05:53 +00:00
}
2016-07-16 10:11:45 +00:00
request.Address = v2net.IPAddress(buffer[41:57])
2016-02-26 23:05:53 +00:00
case AddrTypeDomain:
2016-11-19 13:38:13 +00:00
_, err = io.ReadFull(decryptor, buffer[41:42])
2016-02-26 23:05:53 +00:00
if err != nil {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess:Server: Failed to read domain: " + err.Error())
2016-02-26 23:05:53 +00:00
}
2016-07-16 10:11:45 +00:00
domainLength := int(buffer[41])
2016-02-26 23:05:53 +00:00
if domainLength == 0 {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|Server: Zero domain length.")
2016-02-26 23:05:53 +00:00
}
2016-07-16 10:11:45 +00:00
nBytes, err = io.ReadFull(decryptor, buffer[42:42+domainLength])
2016-02-26 23:05:53 +00:00
if err != nil {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|Server: Failed to read domain: " + err.Error())
2016-02-26 23:05:53 +00:00
}
bufferLen += 1 + domainLength
2016-07-16 10:11:45 +00:00
request.Address = v2net.DomainAddress(string(buffer[42 : 42+domainLength]))
2016-02-26 23:05:53 +00:00
}
2016-07-16 10:11:45 +00:00
nBytes, err = io.ReadFull(decryptor, buffer[bufferLen:bufferLen+4])
2016-02-26 23:05:53 +00:00
if err != nil {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|Server: Failed to read checksum: " + err.Error())
2016-02-26 23:05:53 +00:00
}
fnv1a := fnv.New32a()
2016-07-16 10:11:45 +00:00
fnv1a.Write(buffer[:bufferLen])
2016-02-26 23:05:53 +00:00
actualHash := fnv1a.Sum32()
2016-07-16 10:11:45 +00:00
expectedHash := serial.BytesToUint32(buffer[bufferLen : bufferLen+4])
2016-02-26 23:05:53 +00:00
if actualHash != expectedHash {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|Server: Invalid auth.")
2016-02-26 23:05:53 +00:00
}
return request, nil
}
2016-11-27 20:39:09 +00:00
func (v *ServerSession) DecodeRequestBody(reader io.Reader) io.Reader {
aesStream := crypto.NewAesDecryptionStream(v.requestBodyKey, v.requestBodyIV)
2016-02-26 23:05:53 +00:00
return crypto.NewCryptionReader(aesStream, reader)
}
2016-11-27 20:39:09 +00: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 23:05:53 +00:00
2016-11-27 20:39:09 +00:00
aesStream := crypto.NewAesEncryptionStream(v.responseBodyKey, v.responseBodyIV)
2016-02-26 23:05:53 +00:00
encryptionWriter := crypto.NewCryptionWriter(aesStream, writer)
2016-11-27 20:39:09 +00:00
v.responseWriter = encryptionWriter
2016-02-26 23:05:53 +00:00
2016-11-27 20:39:09 +00:00
encryptionWriter.Write([]byte{v.responseHeader, byte(header.Option)})
2016-02-27 16:28:21 +00:00
err := MarshalCommand(header.Command, encryptionWriter)
if err != nil {
encryptionWriter.Write([]byte{0x00, 0x00})
}
2016-02-27 15:41:21 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *ServerSession) EncodeResponseBody(writer io.Writer) io.Writer {
return v.responseWriter
2016-02-26 23:05:53 +00:00
}