2020-11-25 14:01:53 +03:00
|
|
|
package encoding_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2020-12-04 04:36:16 +03:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
|
|
|
"github.com/xtls/xray-core/common/uuid"
|
|
|
|
"github.com/xtls/xray-core/proxy/vmess"
|
|
|
|
. "github.com/xtls/xray-core/proxy/vmess/encoding"
|
2020-11-25 14:01:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func toAccount(a *vmess.Account) protocol.Account {
|
|
|
|
account, err := a.AsAccount()
|
|
|
|
common.Must(err)
|
|
|
|
return account
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestSerialization(t *testing.T) {
|
|
|
|
user := &protocol.MemoryUser{
|
|
|
|
Level: 0,
|
|
|
|
Email: "test@example.com",
|
|
|
|
}
|
|
|
|
id := uuid.New()
|
|
|
|
account := &vmess.Account{
|
2021-12-15 03:28:47 +03:00
|
|
|
Id: id.String(),
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
user.Account = toAccount(account)
|
|
|
|
|
|
|
|
expectedRequest := &protocol.RequestHeader{
|
|
|
|
Version: 1,
|
|
|
|
User: user,
|
|
|
|
Command: protocol.RequestCommandTCP,
|
|
|
|
Address: net.DomainAddress("www.example.com"),
|
|
|
|
Port: net.Port(443),
|
|
|
|
Security: protocol.SecurityType_AES128_GCM,
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer := buf.New()
|
2021-12-15 03:27:31 +03:00
|
|
|
client := NewClientSession(context.TODO(), true, protocol.DefaultIDHash, 0)
|
2020-11-25 14:01:53 +03:00
|
|
|
common.Must(client.EncodeRequestHeader(expectedRequest, buffer))
|
|
|
|
|
|
|
|
buffer2 := buf.New()
|
|
|
|
buffer2.Write(buffer.Bytes())
|
|
|
|
|
|
|
|
sessionHistory := NewSessionHistory()
|
|
|
|
defer common.Close(sessionHistory)
|
|
|
|
|
|
|
|
userValidator := vmess.NewTimedUserValidator(protocol.DefaultIDHash)
|
|
|
|
userValidator.Add(user)
|
|
|
|
defer common.Close(userValidator)
|
|
|
|
|
|
|
|
server := NewServerSession(userValidator, sessionHistory)
|
2020-12-18 15:45:47 +03:00
|
|
|
actualRequest, err := server.DecodeRequestHeader(buffer, false)
|
2020-11-25 14:01:53 +03:00
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
if r := cmp.Diff(actualRequest, expectedRequest, cmp.AllowUnexported(protocol.ID{})); r != "" {
|
|
|
|
t.Error(r)
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:45:47 +03:00
|
|
|
_, err = server.DecodeRequestHeader(buffer2, false)
|
2020-11-25 14:01:53 +03:00
|
|
|
// anti replay attack
|
|
|
|
if err == nil {
|
|
|
|
t.Error("nil error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidRequest(t *testing.T) {
|
|
|
|
user := &protocol.MemoryUser{
|
|
|
|
Level: 0,
|
|
|
|
Email: "test@example.com",
|
|
|
|
}
|
|
|
|
id := uuid.New()
|
|
|
|
account := &vmess.Account{
|
2021-12-15 03:28:47 +03:00
|
|
|
Id: id.String(),
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
user.Account = toAccount(account)
|
|
|
|
|
|
|
|
expectedRequest := &protocol.RequestHeader{
|
|
|
|
Version: 1,
|
|
|
|
User: user,
|
|
|
|
Command: protocol.RequestCommand(100),
|
|
|
|
Address: net.DomainAddress("www.example.com"),
|
|
|
|
Port: net.Port(443),
|
|
|
|
Security: protocol.SecurityType_AES128_GCM,
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer := buf.New()
|
2021-12-15 03:27:31 +03:00
|
|
|
client := NewClientSession(context.TODO(), true, protocol.DefaultIDHash, 0)
|
2020-11-25 14:01:53 +03:00
|
|
|
common.Must(client.EncodeRequestHeader(expectedRequest, buffer))
|
|
|
|
|
|
|
|
buffer2 := buf.New()
|
|
|
|
buffer2.Write(buffer.Bytes())
|
|
|
|
|
|
|
|
sessionHistory := NewSessionHistory()
|
|
|
|
defer common.Close(sessionHistory)
|
|
|
|
|
|
|
|
userValidator := vmess.NewTimedUserValidator(protocol.DefaultIDHash)
|
|
|
|
userValidator.Add(user)
|
|
|
|
defer common.Close(userValidator)
|
|
|
|
|
|
|
|
server := NewServerSession(userValidator, sessionHistory)
|
2020-12-18 15:45:47 +03:00
|
|
|
_, err := server.DecodeRequestHeader(buffer, false)
|
2020-11-25 14:01:53 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Error("nil error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMuxRequest(t *testing.T) {
|
|
|
|
user := &protocol.MemoryUser{
|
|
|
|
Level: 0,
|
|
|
|
Email: "test@example.com",
|
|
|
|
}
|
|
|
|
id := uuid.New()
|
|
|
|
account := &vmess.Account{
|
2021-12-15 03:28:47 +03:00
|
|
|
Id: id.String(),
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
user.Account = toAccount(account)
|
|
|
|
|
|
|
|
expectedRequest := &protocol.RequestHeader{
|
|
|
|
Version: 1,
|
|
|
|
User: user,
|
|
|
|
Command: protocol.RequestCommandMux,
|
|
|
|
Security: protocol.SecurityType_AES128_GCM,
|
|
|
|
Address: net.DomainAddress("v1.mux.cool"),
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer := buf.New()
|
2021-12-15 03:27:31 +03:00
|
|
|
client := NewClientSession(context.TODO(), true, protocol.DefaultIDHash, 0)
|
2020-11-25 14:01:53 +03:00
|
|
|
common.Must(client.EncodeRequestHeader(expectedRequest, buffer))
|
|
|
|
|
|
|
|
buffer2 := buf.New()
|
|
|
|
buffer2.Write(buffer.Bytes())
|
|
|
|
|
|
|
|
sessionHistory := NewSessionHistory()
|
|
|
|
defer common.Close(sessionHistory)
|
|
|
|
|
|
|
|
userValidator := vmess.NewTimedUserValidator(protocol.DefaultIDHash)
|
|
|
|
userValidator.Add(user)
|
|
|
|
defer common.Close(userValidator)
|
|
|
|
|
|
|
|
server := NewServerSession(userValidator, sessionHistory)
|
2020-12-18 15:45:47 +03:00
|
|
|
actualRequest, err := server.DecodeRequestHeader(buffer, false)
|
2020-11-25 14:01:53 +03:00
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
if r := cmp.Diff(actualRequest, expectedRequest, cmp.AllowUnexported(protocol.ID{})); r != "" {
|
|
|
|
t.Error(r)
|
|
|
|
}
|
|
|
|
}
|