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 (u *User) UnmarshalJSON(data []byte) error { type rawUser struct { IdString string `json:"id"` EmailString string `json:"email"` LevelInt int `json:"level"` AlterIdCount uint16 `json:"alterId"` } 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 = NewID(id) //u.Email = rawUserValue.EmailString u.Level = UserLevel(rawUserValue.LevelInt) if rawUserValue.AlterIdCount > 0 { prevId := u.ID.UUID() // TODO: check duplicate u.AlterIDs = make([]*ID, rawUserValue.AlterIdCount) for idx, _ := range u.AlterIDs { newid := prevId.Next() u.AlterIDs[idx] = NewID(newid) prevId = newid } } return nil }
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 (this *AccountJson) GetAccount() (Account, error) { if len(this.ID) > 0 { id, err := uuid.ParseString(this.ID) if err != nil { return nil, err } primaryID := NewID(id) alterIDs := NewAlterIDs(primaryID, this.AlterIds) return &VMessAccount{ ID: primaryID, AlterIDs: alterIDs, }, nil } return nil, errors.New("Protocol: Malformed account.") }
func (u *Account) UnmarshalJSON(data []byte) error { type JsonConfig struct { ID string `json:"id"` AlterIds uint16 `json:"alterId"` } var rawConfig JsonConfig if err := json.Unmarshal(data, &rawConfig); err != nil { return err } id, err := uuid.ParseString(rawConfig.ID) if err != nil { log.Error("VMess: Failed to parse ID: ", err) return err } u.ID = protocol.NewID(id) u.AlterIDs = protocol.NewAlterIDs(u.ID, rawConfig.AlterIds) return nil }
func (u *User) UnmarshalJSON(data []byte) error { type rawUser struct { IdString string `json:"id"` EmailString string `json:"email"` LevelByte byte `json:"level"` AlterIdCount uint16 `json:"alterId"` } 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 = *NewUser(NewID(id), UserLevel(rawUserValue.LevelByte), rawUserValue.AlterIdCount) return nil }
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 (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) }