1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-06 10:20:44 -05:00
v2fly/proxy/vmess/protocol/vmess.go

212 lines
5.6 KiB
Go
Raw Normal View History

2015-09-07 11:12:31 -04:00
// Package vmess contains protocol definition, io lib for VMess.
package protocol
2015-09-05 11:48:38 -04:00
import (
2015-09-08 19:11:02 -04:00
"crypto/aes"
"crypto/cipher"
"encoding/binary"
"hash/fnv"
2015-09-07 17:13:55 -04:00
"io"
2015-09-16 15:13:13 -04:00
"time"
2015-09-08 19:11:02 -04:00
2015-09-24 06:54:10 -04:00
"github.com/v2ray/v2ray-core/common/errors"
v2io "github.com/v2ray/v2ray-core/common/io"
v2net "github.com/v2ray/v2ray-core/common/net"
2015-09-19 18:50:21 -04:00
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
2015-09-08 19:11:02 -04:00
)
const (
addrTypeIPv4 = byte(0x01)
addrTypeIPv6 = byte(0x03)
addrTypeDomain = byte(0x02)
CmdTCP = byte(0x01)
CmdUDP = byte(0x02)
2015-09-10 18:24:18 -04:00
Version = byte(0x01)
2015-09-14 12:19:17 -04:00
blockSize = 16
2015-09-05 11:48:38 -04:00
)
2015-09-21 13:56:58 -04:00
// VMessRequest implements the request message of VMess protocol. It only contains the header of a
// request message. The data part will be handled by conection handler directly, in favor of data
// streaming.
type VMessRequest struct {
Version byte
2015-09-19 18:11:14 -04:00
UserId user.ID
RequestIV [16]byte
RequestKey [16]byte
ResponseHeader [4]byte
Command byte
2015-09-12 16:11:54 -04:00
Address v2net.Address
2015-09-08 19:11:02 -04:00
}
2015-09-21 13:56:58 -04:00
// Destination is the final destination of this request.
2015-09-20 12:22:29 -04:00
func (request *VMessRequest) Destination() v2net.Destination {
if request.Command == CmdTCP {
return v2net.NewTCPDestination(request.Address)
} else {
return v2net.NewUDPDestination(request.Address)
}
}
2015-09-21 13:56:58 -04:00
// VMessRequestReader is a parser to read VMessRequest from a byte stream.
2015-09-08 19:11:02 -04:00
type VMessRequestReader struct {
2015-09-19 18:11:14 -04:00
vUserSet user.UserSet
2015-09-08 19:11:02 -04:00
}
2015-09-21 13:56:58 -04:00
// NewVMessRequestReader creates a new VMessRequestReader with a given UserSet
2015-09-19 18:11:14 -04:00
func NewVMessRequestReader(vUserSet user.UserSet) *VMessRequestReader {
2015-09-16 10:27:36 -04:00
return &VMessRequestReader{
vUserSet: vUserSet,
}
2015-09-08 19:11:02 -04:00
}
2015-09-21 13:56:58 -04:00
// Read reads a VMessRequest from a byte stream.
2015-09-08 19:11:02 -04:00
func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
buffer := make([]byte, 256)
2015-09-19 18:11:14 -04:00
nBytes, err := reader.Read(buffer[:user.IDBytesLen])
if err != nil {
2015-09-08 19:11:02 -04:00
return nil, err
}
2015-09-15 18:06:22 -04:00
2015-09-14 19:32:55 -04:00
userId, timeSec, valid := r.vUserSet.GetUser(buffer[:nBytes])
2015-09-08 19:11:02 -04:00
if !valid {
2015-09-24 06:54:10 -04:00
return nil, errors.NewAuthenticationError(buffer[:nBytes])
2015-09-08 19:11:02 -04:00
}
2015-09-14 12:19:17 -04:00
2015-09-14 15:59:44 -04:00
aesCipher, err := aes.NewCipher(userId.CmdKey())
if err != nil {
return nil, err
}
2015-09-19 18:11:14 -04:00
aesStream := cipher.NewCFBDecrypter(aesCipher, user.Int64Hash(timeSec))
decryptor := v2io.NewCryptionReader(aesStream, reader)
2015-09-08 19:11:02 -04:00
if err != nil {
return nil, err
}
nBytes, err = decryptor.Read(buffer[:41])
2015-09-14 15:59:44 -04:00
if err != nil {
return nil, err
}
bufferLen := nBytes
2015-09-14 15:59:44 -04:00
2015-09-16 10:27:36 -04:00
request := &VMessRequest{
UserId: *userId,
Version: buffer[0],
}
2015-09-14 15:59:44 -04:00
if request.Version != Version {
2015-09-24 06:54:10 -04:00
return nil, errors.NewProtocolVersionError(int(request.Version))
2015-09-14 15:59:44 -04:00
}
copy(request.RequestIV[:], buffer[1:17]) // 16 bytes
copy(request.RequestKey[:], buffer[17:33]) // 16 bytes
copy(request.ResponseHeader[:], buffer[33:37]) // 4 bytes
request.Command = buffer[37]
2015-09-08 19:11:02 -04:00
port := binary.BigEndian.Uint16(buffer[38:40])
2015-09-08 19:11:02 -04:00
switch buffer[40] {
2015-09-08 19:11:02 -04:00
case addrTypeIPv4:
_, err = decryptor.Read(buffer[41:45]) // 4 bytes
bufferLen += 4
2015-09-08 19:11:02 -04:00
if err != nil {
return nil, err
}
request.Address = v2net.IPAddress(buffer[41:45], port)
2015-09-08 19:11:02 -04:00
case addrTypeIPv6:
_, err = decryptor.Read(buffer[41:57]) // 16 bytes
bufferLen += 16
2015-09-08 19:11:02 -04:00
if err != nil {
return nil, err
}
request.Address = v2net.IPAddress(buffer[41:57], port)
2015-09-08 19:11:02 -04:00
case addrTypeDomain:
_, err = decryptor.Read(buffer[41:42])
2015-09-08 19:11:02 -04:00
if err != nil {
return nil, err
}
domainLength := int(buffer[41])
_, err = decryptor.Read(buffer[42 : 42+domainLength])
2015-09-08 19:11:02 -04:00
if err != nil {
return nil, err
}
bufferLen += 1 + domainLength
request.Address = v2net.DomainAddress(string(buffer[42:42+domainLength]), port)
2015-09-08 19:11:02 -04:00
}
_, err = decryptor.Read(buffer[bufferLen : bufferLen+4])
2015-09-08 19:11:02 -04:00
if err != nil {
return nil, err
}
fnv1a := fnv.New32a()
fnv1a.Write(buffer[:bufferLen])
actualHash := fnv1a.Sum32()
expectedHash := binary.BigEndian.Uint32(buffer[bufferLen : bufferLen+4])
if actualHash != expectedHash {
2015-09-24 06:54:10 -04:00
return nil, errors.NewCorruptedPacketError()
2015-09-08 19:11:02 -04:00
}
return request, nil
}
2015-09-21 13:56:58 -04:00
// ToBytes returns a VMessRequest in the form of byte array.
2015-09-22 08:50:34 -04:00
func (request *VMessRequest) ToBytes(idHash user.CounterHash, randomRangeInt64 user.RandomInt64InRange, buffer []byte) ([]byte, error) {
if buffer == nil {
buffer = make([]byte, 0, 300)
}
2015-09-15 18:06:22 -04:00
counter := randomRangeInt64(time.Now().UTC().Unix(), 30)
2015-09-23 16:17:25 -04:00
hash := idHash.Hash(request.UserId.Bytes[:], counter)
2015-09-16 15:13:13 -04:00
buffer = append(buffer, hash...)
2015-09-08 19:11:02 -04:00
encryptionBegin := len(buffer)
2015-09-14 15:59:44 -04:00
buffer = append(buffer, request.Version)
buffer = append(buffer, request.RequestIV[:]...)
buffer = append(buffer, request.RequestKey[:]...)
buffer = append(buffer, request.ResponseHeader[:]...)
buffer = append(buffer, request.Command)
buffer = append(buffer, request.Address.PortBytes()...)
switch {
case request.Address.IsIPv4():
buffer = append(buffer, addrTypeIPv4)
buffer = append(buffer, request.Address.IP()...)
case request.Address.IsIPv6():
buffer = append(buffer, addrTypeIPv6)
buffer = append(buffer, request.Address.IP()...)
case request.Address.IsDomain():
buffer = append(buffer, addrTypeDomain)
buffer = append(buffer, byte(len(request.Address.Domain())))
buffer = append(buffer, []byte(request.Address.Domain())...)
}
2015-09-08 19:11:02 -04:00
encryptionEnd := len(buffer)
fnv1a := fnv.New32a()
fnv1a.Write(buffer[encryptionBegin:encryptionEnd])
fnvHash := fnv1a.Sum32()
buffer = append(buffer, byte(fnvHash>>24))
buffer = append(buffer, byte(fnvHash>>16))
buffer = append(buffer, byte(fnvHash>>8))
buffer = append(buffer, byte(fnvHash))
encryptionEnd += 4
2015-09-14 15:59:44 -04:00
aesCipher, err := aes.NewCipher(request.UserId.CmdKey())
2015-09-08 19:11:02 -04:00
if err != nil {
return nil, err
2015-09-08 19:11:02 -04:00
}
2015-09-19 18:11:14 -04:00
aesStream := cipher.NewCFBEncrypter(aesCipher, user.Int64Hash(counter))
aesStream.XORKeyStream(buffer[encryptionBegin:encryptionEnd], buffer[encryptionBegin:encryptionEnd])
2015-09-07 17:21:47 -04:00
return buffer, nil
2015-09-05 11:48:38 -04:00
}