2016-07-23 07:17:51 -04:00
|
|
|
package encoding_test
|
2016-02-27 16:37:14 -05:00
|
|
|
|
|
|
|
import (
|
2017-01-29 06:58:52 -05:00
|
|
|
"context"
|
2016-02-27 16:37:14 -05:00
|
|
|
"testing"
|
|
|
|
|
2017-10-22 14:17:06 -04:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 05:35:27 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2017-08-29 06:56:57 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/protocol"
|
2016-12-15 05:51:09 -05:00
|
|
|
"v2ray.com/core/common/serial"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/uuid"
|
|
|
|
"v2ray.com/core/proxy/vmess"
|
|
|
|
. "v2ray.com/core/proxy/vmess/encoding"
|
2017-10-24 10:15:35 -04:00
|
|
|
. "v2ray.com/ext/assert"
|
2016-02-27 16:37:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRequestSerialization(t *testing.T) {
|
2017-10-24 10:15:35 -04:00
|
|
|
assert := With(t)
|
2016-02-27 16:37:14 -05:00
|
|
|
|
2016-09-17 18:41:21 -04:00
|
|
|
user := &protocol.User{
|
|
|
|
Level: 0,
|
|
|
|
Email: "test@v2ray.com",
|
|
|
|
}
|
2016-10-12 12:43:55 -04:00
|
|
|
account := &vmess.Account{
|
2016-09-17 18:41:21 -04:00
|
|
|
Id: uuid.New().String(),
|
|
|
|
AlterId: 0,
|
2016-07-25 11:36:24 -04:00
|
|
|
}
|
2016-12-15 05:51:09 -05:00
|
|
|
user.Account = serial.ToTypedMessage(account)
|
2016-02-27 16:37:14 -05:00
|
|
|
|
|
|
|
expectedRequest := &protocol.RequestHeader{
|
2016-12-07 11:32:40 -05:00
|
|
|
Version: 1,
|
|
|
|
User: user,
|
|
|
|
Command: protocol.RequestCommandTCP,
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.DomainAddress("www.v2ray.com"),
|
|
|
|
Port: net.Port(443),
|
2016-12-07 11:32:40 -05:00
|
|
|
Security: protocol.Security(protocol.SecurityType_AES128_GCM),
|
2016-02-27 16:37:14 -05:00
|
|
|
}
|
|
|
|
|
2016-12-09 06:08:25 -05:00
|
|
|
buffer := buf.New()
|
2016-02-27 16:37:14 -05:00
|
|
|
client := NewClientSession(protocol.DefaultIDHash)
|
2017-10-22 14:17:06 -04:00
|
|
|
common.Must(client.EncodeRequestHeader(expectedRequest, buffer))
|
2016-02-27 16:37:14 -05:00
|
|
|
|
2017-02-12 08:00:17 -05:00
|
|
|
buffer2 := buf.New()
|
|
|
|
buffer2.Append(buffer.Bytes())
|
|
|
|
|
2017-01-29 06:58:52 -05:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2017-02-12 10:53:23 -05:00
|
|
|
sessionHistory := NewSessionHistory(ctx)
|
|
|
|
|
2017-01-29 06:58:52 -05:00
|
|
|
userValidator := vmess.NewTimedUserValidator(ctx, protocol.DefaultIDHash)
|
2016-02-27 16:37:14 -05:00
|
|
|
userValidator.Add(user)
|
|
|
|
|
2017-02-12 10:53:23 -05:00
|
|
|
server := NewServerSession(userValidator, sessionHistory)
|
2016-02-27 16:37:14 -05:00
|
|
|
actualRequest, err := server.DecodeRequestHeader(buffer)
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNil)
|
2016-02-27 16:37:14 -05:00
|
|
|
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(expectedRequest.Version, Equals, actualRequest.Version)
|
|
|
|
assert(byte(expectedRequest.Command), Equals, byte(actualRequest.Command))
|
|
|
|
assert(byte(expectedRequest.Option), Equals, byte(actualRequest.Option))
|
|
|
|
assert(expectedRequest.Address, Equals, actualRequest.Address)
|
|
|
|
assert(expectedRequest.Port, Equals, actualRequest.Port)
|
|
|
|
assert(byte(expectedRequest.Security), Equals, byte(actualRequest.Security))
|
2017-02-12 08:00:17 -05:00
|
|
|
|
|
|
|
_, err = server.DecodeRequestHeader(buffer2)
|
2017-02-21 09:48:21 -05:00
|
|
|
// anti replay attack
|
2017-10-24 10:15:35 -04:00
|
|
|
assert(err, IsNotNil)
|
2017-02-12 08:00:17 -05:00
|
|
|
|
2017-01-29 06:58:52 -05:00
|
|
|
cancel()
|
2016-02-27 16:37:14 -05:00
|
|
|
}
|