|
|
|
@ -13,6 +13,7 @@ import (
|
|
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core" |
|
|
|
|
v2io "github.com/v2ray/v2ray-core/io" |
|
|
|
|
"github.com/v2ray/v2ray-core/log" |
|
|
|
|
v2net "github.com/v2ray/v2ray-core/net" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
@ -24,12 +25,11 @@ const (
|
|
|
|
|
Version = byte(0x01) |
|
|
|
|
|
|
|
|
|
blockSize = 16 |
|
|
|
|
|
|
|
|
|
CryptoMessage = "c48619fe-8f02-49e0-b9e9-edf763e17e21" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
ErrorInvalidUser = errors.New("Invalid User") |
|
|
|
|
ErrorInvalidUser = errors.New("Invalid User") |
|
|
|
|
ErrorInvalidVerion = errors.New("Invalid Version") |
|
|
|
|
|
|
|
|
|
emptyIV = make([]byte, blockSize) |
|
|
|
|
) |
|
|
|
@ -62,25 +62,21 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
|
|
|
|
request := new(VMessRequest) |
|
|
|
|
|
|
|
|
|
buffer := make([]byte, 256) |
|
|
|
|
nBytes, err := reader.Read(buffer[0:1]) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
// TODO: verify version number
|
|
|
|
|
request.Version = buffer[0] |
|
|
|
|
|
|
|
|
|
nBytes, err = reader.Read(buffer[:core.IDBytesLen]) |
|
|
|
|
nBytes, err := reader.Read(buffer[:core.IDBytesLen]) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
log.Debug("Read user hash: %v", buffer[:nBytes]) |
|
|
|
|
|
|
|
|
|
userId, valid := r.vUserSet.GetUser(buffer[:nBytes]) |
|
|
|
|
if !valid { |
|
|
|
|
return nil, ErrorInvalidUser |
|
|
|
|
} |
|
|
|
|
request.UserId = *userId |
|
|
|
|
|
|
|
|
|
aesCipher, err := aes.NewCipher(userId.Hash([]byte(CryptoMessage))) |
|
|
|
|
aesCipher, err := aes.NewCipher(userId.CmdKey()) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
@ -105,6 +101,17 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
nBytes, err = decryptor.Read(buffer[0:1]) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
request.Version = buffer[0] |
|
|
|
|
if request.Version != Version { |
|
|
|
|
log.Error("Unknown VMess version %d", request.Version) |
|
|
|
|
return nil, ErrorInvalidVerion |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO: check number of bytes returned
|
|
|
|
|
_, err = decryptor.Read(request.RequestIV[:]) |
|
|
|
|
if err != nil { |
|
|
|
@ -182,8 +189,10 @@ func NewVMessRequestWriter() *VMessRequestWriter {
|
|
|
|
|
|
|
|
|
|
func (w *VMessRequestWriter) Write(writer io.Writer, request *VMessRequest) error { |
|
|
|
|
buffer := make([]byte, 0, 300) |
|
|
|
|
buffer = append(buffer, request.Version) |
|
|
|
|
buffer = append(buffer, request.UserId.TimeRangeHash(30)...) |
|
|
|
|
userHash := request.UserId.TimeRangeHash(30) |
|
|
|
|
|
|
|
|
|
log.Debug("Writing userhash: %v", userHash) |
|
|
|
|
buffer = append(buffer, userHash...) |
|
|
|
|
|
|
|
|
|
encryptionBegin := len(buffer) |
|
|
|
|
|
|
|
|
@ -196,6 +205,7 @@ func (w *VMessRequestWriter) Write(writer io.Writer, request *VMessRequest) erro
|
|
|
|
|
buffer = append(buffer, byte(randomLength)) |
|
|
|
|
buffer = append(buffer, randomContent...) |
|
|
|
|
|
|
|
|
|
buffer = append(buffer, request.Version) |
|
|
|
|
buffer = append(buffer, request.RequestIV[:]...) |
|
|
|
|
buffer = append(buffer, request.RequestKey[:]...) |
|
|
|
|
buffer = append(buffer, request.ResponseHeader[:]...) |
|
|
|
@ -231,7 +241,7 @@ func (w *VMessRequestWriter) Write(writer io.Writer, request *VMessRequest) erro
|
|
|
|
|
buffer = append(buffer, paddingBuffer...) |
|
|
|
|
encryptionEnd := len(buffer) |
|
|
|
|
|
|
|
|
|
aesCipher, err := aes.NewCipher(request.UserId.Hash([]byte(CryptoMessage))) |
|
|
|
|
aesCipher, err := aes.NewCipher(request.UserId.CmdKey()) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|