1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/io/vmess/vmess.go

266 lines
6.1 KiB
Go
Raw Normal View History

2015-09-07 11:12:31 -04:00
// Package vmess contains protocol definition, io lib for VMess.
package vmess
2015-09-05 11:48:38 -04:00
import (
2015-09-08 19:11:02 -04:00
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/binary"
"errors"
2015-09-07 17:21:47 -04:00
"fmt"
2015-09-07 17:13:55 -04:00
"io"
2015-09-08 19:11:02 -04:00
mrand "math/rand"
"github.com/v2ray/v2ray-core"
v2io "github.com/v2ray/v2ray-core/io"
2015-09-14 15:59:44 -04:00
"github.com/v2ray/v2ray-core/log"
2015-09-10 18:24:18 -04:00
v2net "github.com/v2ray/v2ray-core/net"
2015-09-08 19:11:02 -04:00
)
const (
addrTypeIPv4 = byte(0x01)
addrTypeIPv6 = byte(0x03)
addrTypeDomain = 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-08 19:11:02 -04:00
var (
2015-09-14 15:59:44 -04:00
ErrorInvalidUser = errors.New("Invalid User")
ErrorInvalidVerion = errors.New("Invalid Version")
2015-09-08 19:11:02 -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
2015-09-07 09:10:37 -04:00
// handler directly, in favor of data streaming.
2015-09-08 19:11:02 -04:00
type VMessRequest struct {
Version byte
2015-09-12 16:11:54 -04:00
UserId core.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
}
type VMessRequestReader struct {
2015-09-14 13:03:31 -04:00
vUserSet core.UserSet
2015-09-08 19:11:02 -04:00
}
2015-09-14 13:03:31 -04:00
func NewVMessRequestReader(vUserSet core.UserSet) *VMessRequestReader {
2015-09-16 10:27:36 -04:00
return &VMessRequestReader{
vUserSet: vUserSet,
}
2015-09-08 19:11:02 -04:00
}
func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
buffer := make([]byte, 256)
2015-09-14 15:59:44 -04:00
nBytes, err := reader.Read(buffer[:core.IDBytesLen])
if err != nil {
2015-09-08 19:11:02 -04:00
return nil, err
}
2015-09-15 18:06:22 -04:00
log.Debug("Read user hash: %v", buffer[:nBytes])
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 {
return nil, ErrorInvalidUser
}
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-14 19:32:55 -04:00
aesStream := cipher.NewCFBDecrypter(aesCipher, core.TimestampHash(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[0:1])
if err != nil {
return nil, err
}
randomLength := buffer[0]
if randomLength <= 0 || randomLength > 32 {
return nil, fmt.Errorf("Unexpected random length %d", randomLength)
}
_, err = decryptor.Read(buffer[:randomLength])
if err != nil {
return nil, err
}
2015-09-14 15:59:44 -04:00
nBytes, err = decryptor.Read(buffer[0:1])
if err != nil {
return nil, err
}
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 {
log.Error("Unknown VMess version %d", request.Version)
return nil, ErrorInvalidVerion
}
2015-09-08 19:11:02 -04:00
// TODO: check number of bytes returned
_, err = decryptor.Read(request.RequestIV[:])
2015-09-08 19:11:02 -04:00
if err != nil {
return nil, err
}
_, err = decryptor.Read(request.RequestKey[:])
2015-09-08 19:11:02 -04:00
if err != nil {
return nil, err
}
_, err = decryptor.Read(request.ResponseHeader[:])
2015-09-08 19:11:02 -04:00
if err != nil {
return nil, err
}
_, err = decryptor.Read(buffer[0:1])
if err != nil {
return nil, err
}
request.Command = buffer[0]
2015-09-08 19:11:02 -04:00
_, err = decryptor.Read(buffer[0:2])
if err != nil {
return nil, err
}
port := binary.BigEndian.Uint16(buffer[0:2])
2015-09-08 19:11:02 -04:00
_, err = decryptor.Read(buffer[0:1])
if err != nil {
return nil, err
}
switch buffer[0] {
case addrTypeIPv4:
_, err = decryptor.Read(buffer[1:5])
if err != nil {
return nil, err
}
request.Address = v2net.IPAddress(buffer[1:5], port)
2015-09-08 19:11:02 -04:00
case addrTypeIPv6:
_, err = decryptor.Read(buffer[1:17])
if err != nil {
return nil, err
}
request.Address = v2net.IPAddress(buffer[1:17], port)
2015-09-08 19:11:02 -04:00
case addrTypeDomain:
_, err = decryptor.Read(buffer[1:2])
if err != nil {
return nil, err
}
domainLength := buffer[1]
_, err = decryptor.Read(buffer[2 : 2+domainLength])
if err != nil {
return nil, err
}
request.Address = v2net.DomainAddress(string(buffer[2:2+domainLength]), port)
2015-09-08 19:11:02 -04:00
}
_, err = decryptor.Read(buffer[0:1])
if err != nil {
return nil, err
}
randomLength = buffer[0]
_, err = decryptor.Read(buffer[:randomLength])
if err != nil {
return nil, err
}
return request, nil
}
type VMessRequestWriter struct {
}
func NewVMessRequestWriter() *VMessRequestWriter {
2015-09-16 10:27:36 -04:00
return &VMessRequestWriter{}
2015-09-08 19:11:02 -04:00
}
func (w *VMessRequestWriter) Write(writer io.Writer, request *VMessRequest) error {
buffer := make([]byte, 0, 300)
2015-09-15 18:06:22 -04:00
userHash, timeSec := request.UserId.TimeRangeHash(30)
log.Debug("Writing userhash: %v", userHash)
2015-09-14 15:59:44 -04:00
buffer = append(buffer, userHash...)
2015-09-08 19:11:02 -04:00
encryptionBegin := len(buffer)
randomLength := mrand.Intn(32) + 1
randomContent := make([]byte, randomLength)
_, err := rand.Read(randomContent)
if err != nil {
return err
}
buffer = append(buffer, byte(randomLength))
buffer = append(buffer, randomContent...)
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)
portBytes := make([]byte, 2)
binary.BigEndian.PutUint16(portBytes, request.Address.Port)
buffer = append(buffer, 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
2015-09-16 06:26:50 -04:00
paddingLength := mrand.Intn(32) + 1
2015-09-08 19:11:02 -04:00
paddingBuffer := make([]byte, paddingLength)
_, err = rand.Read(paddingBuffer)
if err != nil {
return err
}
buffer = append(buffer, byte(paddingLength))
buffer = append(buffer, paddingBuffer...)
encryptionEnd := len(buffer)
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 err
}
2015-09-14 19:32:55 -04:00
aesStream := cipher.NewCFBEncrypter(aesCipher, core.TimestampHash(timeSec))
2015-09-12 14:36:21 -04:00
cWriter := v2io.NewCryptionWriter(aesStream, writer)
2015-09-08 19:11:02 -04:00
_, err = writer.Write(buffer[0:encryptionBegin])
if err != nil {
return err
}
_, err = cWriter.Write(buffer[encryptionBegin:encryptionEnd])
if err != nil {
return err
2015-09-07 17:21:47 -04:00
}
2015-09-08 19:11:02 -04:00
return nil
2015-09-05 11:48:38 -04:00
}
2015-09-10 18:24:18 -04:00
type VMessResponse [4]byte
func NewVMessResponse(request *VMessRequest) *VMessResponse {
response := new(VMessResponse)
copy(response[:], request.ResponseHeader[:])
2015-09-10 18:24:18 -04:00
return response
}