func BenchmarkVMessRequestWriting(b *testing.B) { id, err := uuid.ParseString("2b2966ac-16aa-4fbf-8d81-c5f172a3da51") assert.Error(err).IsNil() userId := vmess.NewID(id) userSet := mocks.MockUserSet{[]vmess.User{}, make(map[string]int), make(map[string]int64)} testUser := &TestUser{ id: userId, } userSet.AddUser(testUser) request := new(VMessRequest) request.Version = byte(0x01) request.User = testUser randBytes := make([]byte, 36) rand.Read(randBytes) request.RequestIV = randBytes[:16] request.RequestKey = randBytes[16:32] request.ResponseHeader = randBytes[32:] request.Command = byte(0x01) request.Address = v2net.DomainAddress("v2ray.com") request.Port = v2net.Port(80) for i := 0; i < b.N; i++ { request.ToBytes(user.NewTimeHash(user.HMACHash{}), user.GenerateRandomInt64InRange, nil) } }
func BenchmarkVMessRequestWriting(b *testing.B) { id, err := uuid.ParseString("2b2966ac-16aa-4fbf-8d81-c5f172a3da51") assert.Error(err).IsNil() userId := vmess.NewID(id) userSet := protocoltesting.MockUserSet{[]*vmess.User{}, make(map[string]int), make(map[string]Timestamp)} testUser := &vmess.User{ ID: userId, } userSet.AddUser(testUser) request := new(VMessRequest) request.Version = byte(0x01) request.User = testUser randBytes := make([]byte, 36) rand.Read(randBytes) request.RequestIV = randBytes[:16] request.RequestKey = randBytes[16:32] request.ResponseHeader = randBytes[32:] request.Command = byte(0x01) request.Address = v2net.DomainAddress("v2ray.com") request.Port = v2net.Port(80) for i := 0; i < b.N; i++ { request.ToBytes(NewRandomTimestampGenerator(Timestamp(time.Now().Unix()), 30), nil) } }
func TestReceiverUser(t *testing.T) { v2testing.Current(t) id := vmess.NewID(uuid.New()) user := vmess.NewUser(id, vmess.UserLevel(0), 100) rec := NewReceiver(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80), user) assert.Bool(rec.HasUser(user)).IsTrue() assert.Int(len(rec.Accounts)).Equals(1) id2 := vmess.NewID(uuid.New()) user2 := vmess.NewUser(id2, vmess.UserLevel(0), 100) assert.Bool(rec.HasUser(user2)).IsFalse() rec.AddUser(user2) assert.Bool(rec.HasUser(user2)).IsTrue() assert.Int(len(rec.Accounts)).Equals(2) }
func TestVMessSerialization(t *testing.T) { v2testing.Current(t) id, err := uuid.ParseString("2b2966ac-16aa-4fbf-8d81-c5f172a3da51") assert.Error(err).IsNil() userId := vmess.NewID(id) testUser := &vmess.User{ ID: userId, } userSet := protocoltesting.MockUserSet{[]*vmess.User{}, make(map[string]int), make(map[string]Timestamp)} userSet.AddUser(testUser) request := new(VMessRequest) request.Version = byte(0x01) request.User = testUser randBytes := make([]byte, 36) _, err = rand.Read(randBytes) assert.Error(err).IsNil() request.RequestIV = randBytes[:16] request.RequestKey = randBytes[16:32] request.ResponseHeader = randBytes[32:] request.Command = byte(0x01) request.Address = v2net.DomainAddress("v2ray.com") request.Port = v2net.Port(80) mockTime := Timestamp(1823730) buffer, err := request.ToBytes(&FakeTimestampGenerator{timestamp: mockTime}, nil) if err != nil { t.Fatal(err) } userSet.UserHashes[string(buffer.Value[:16])] = 0 userSet.Timestamps[string(buffer.Value[:16])] = mockTime requestReader := NewVMessRequestReader(&userSet) actualRequest, err := requestReader.Read(bytes.NewReader(buffer.Value)) if err != nil { t.Fatal(err) } assert.Byte(actualRequest.Version).Named("Version").Equals(byte(0x01)) assert.String(actualRequest.User.ID).Named("UserId").Equals(request.User.ID.String()) assert.Bytes(actualRequest.RequestIV).Named("RequestIV").Equals(request.RequestIV[:]) assert.Bytes(actualRequest.RequestKey).Named("RequestKey").Equals(request.RequestKey[:]) assert.Bytes(actualRequest.ResponseHeader).Named("ResponseHeader").Equals(request.ResponseHeader[:]) assert.Byte(actualRequest.Command).Named("Command").Equals(request.Command) assert.String(actualRequest.Address).Named("Address").Equals(request.Address.String()) }
func (u *ConfigUser) UnmarshalJSON(data []byte) error { type rawUser struct { IdString string `json:"id"` EmailString string `json:"email"` LevelInt int `json:"level"` } var rawUserValue rawUser if err := json.Unmarshal(data, &rawUserValue); err != nil { return err } id, err := uuid.ParseString(rawUserValue.IdString) if err != nil { return err } u.Id = vmess.NewID(id) u.Email = rawUserValue.EmailString u.LevelValue = vmess.UserLevel(rawUserValue.LevelInt) return nil }
func TestVMessInAndOut(t *testing.T) { v2testing.Current(t) id, err := uuid.ParseString("ad937d9d-6e23-4a5a-ba23-bce5092a7c51") assert.Error(err).IsNil() testAccount := vmess.NewID(id) portA := v2nettesting.PickPort() portB := v2nettesting.PickPort() ichConnInput := []byte("The data to be send to outbound server.") ichConnOutput := bytes.NewBuffer(make([]byte, 0, 1024)) ich := &proxymocks.InboundConnectionHandler{ ConnInput: bytes.NewReader(ichConnInput), ConnOutput: ichConnOutput, } protocol, err := proxytesting.RegisterInboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (proxy.InboundHandler, error) { ich.Space = space return ich, nil }) assert.Error(err).IsNil() configA := &point.Config{ Port: portA, InboundConfig: &point.ConnectionConfig{ Protocol: protocol, Settings: nil, }, OutboundConfig: &point.ConnectionConfig{ Protocol: "vmess", Settings: []byte(`{ "vnext": [ { "address": "127.0.0.1", "port": ` + portB.String() + `, "users": [ {"id": "` + testAccount.String() + `"} ] } ] }`), }, } pointA, err := point.NewPoint(configA) assert.Error(err).IsNil() err = pointA.Start() assert.Error(err).IsNil() ochConnInput := []byte("The data to be returned to inbound server.") ochConnOutput := bytes.NewBuffer(make([]byte, 0, 1024)) och := &proxymocks.OutboundConnectionHandler{ ConnInput: bytes.NewReader(ochConnInput), ConnOutput: ochConnOutput, } protocol, err = proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (proxy.OutboundHandler, error) { return och, nil }) assert.Error(err).IsNil() configB := &point.Config{ Port: portB, InboundConfig: &point.ConnectionConfig{ Protocol: "vmess", Settings: []byte(`{ "clients": [ {"id": "` + testAccount.String() + `"} ] }`), }, OutboundConfig: &point.ConnectionConfig{ Protocol: protocol, Settings: nil, }, } pointB, err := point.NewPoint(configB) assert.Error(err).IsNil() err = pointB.Start() assert.Error(err).IsNil() dest := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80) ich.Communicate(v2net.NewPacket(dest, nil, true)) assert.Bytes(ichConnInput).Equals(ochConnOutput.Bytes()) assert.Bytes(ichConnOutput.Bytes()).Equals(ochConnInput) }
func (this *VMessOutboundHandler) handleSwitchAccount(cmd *command.SwitchAccount) { user := vmess.NewUser(vmess.NewID(cmd.ID), cmd.Level, cmd.AlterIds.Value()) dest := v2net.TCPDestination(cmd.Host, cmd.Port) this.receiverManager.AddDetour(NewReceiver(dest, user), cmd.ValidMin) }
func (us *StaticUserSet) GetUser(userhash []byte) (*vmess.User, protocol.Timestamp, bool) { id, _ := uuid.ParseString("703e9102-eb57-499c-8b59-faf4f371bb21") return &vmess.User{ ID: vmess.NewID(id), }, 0, true }
func TestVMessInAndOut(t *testing.T) { v2testing.Current(t) id, err := uuid.ParseString("ad937d9d-6e23-4a5a-ba23-bce5092a7c51") assert.Error(err).IsNil() testAccount := vmess.NewID(id) portA := v2nettesting.PickPort() portB := v2nettesting.PickPort() ichConnInput := []byte("The data to be send to outbound server.") ichConnOutput := bytes.NewBuffer(make([]byte, 0, 1024)) ich := &proxymocks.InboundConnectionHandler{ ConnInput: bytes.NewReader(ichConnInput), ConnOutput: ichConnOutput, } connhandler.RegisterInboundConnectionHandlerFactory("mock_ich", ich) configA := mocks.Config{ PortValue: portA, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "mock_ich", SettingsValue: nil, }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "vmess", SettingsValue: &outboundjson.Outbound{ []*outboundjson.ConfigTarget{ &outboundjson.ConfigTarget{ Destination: v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), portB), Users: []*vmessjson.ConfigUser{ &vmessjson.ConfigUser{Id: testAccount}, }, }, }, }, }, } pointA, err := point.NewPoint(&configA) assert.Error(err).IsNil() err = pointA.Start() assert.Error(err).IsNil() ochConnInput := []byte("The data to be returned to inbound server.") ochConnOutput := bytes.NewBuffer(make([]byte, 0, 1024)) och := &proxymocks.OutboundConnectionHandler{ ConnInput: bytes.NewReader(ochConnInput), ConnOutput: ochConnOutput, } connhandler.RegisterOutboundConnectionHandlerFactory("mock_och", och) configB := mocks.Config{ PortValue: portB, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "vmess", SettingsValue: &inboundjson.Inbound{ AllowedClients: []*vmessjson.ConfigUser{ &vmessjson.ConfigUser{Id: testAccount}, }, }, }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "mock_och", SettingsValue: nil, }, } pointB, err := point.NewPoint(&configB) assert.Error(err).IsNil() err = pointB.Start() assert.Error(err).IsNil() dest := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80) ich.Communicate(v2net.NewPacket(dest, nil, true)) assert.Bytes(ichConnInput).Equals(ochConnOutput.Bytes()) assert.Bytes(ichConnOutput.Bytes()).Equals(ochConnInput) }