func BenchmarkVMessRequestWriting(b *testing.B) { userId, _ := config.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51") userSet := mocks.MockUserSet{[]config.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", 80) for i := 0; i < b.N; i++ { request.ToBytes(user.NewTimeHash(user.HMACHash{}), user.GenerateRandomInt64InRange, nil) } }
func TestVMessSerialization(t *testing.T) { assert := unit.Assert(t) userId, err := config.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51") if err != nil { t.Fatal(err) } testUser := &TestUser{ id: userId, } userSet := mocks.MockUserSet{[]config.User{}, make(map[string]int), make(map[string]int64)} 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", 80) mockTime := int64(1823730) buffer, err := request.ToBytes(user.NewTimeHash(user.HMACHash{}), func(base int64, delta int) int64 { return 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().String).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.String()).Named("Address").Equals(request.Address.String()) }
func (u *ConfigUser) UnmarshalJSON(data []byte) error { type rawUser struct { IdString string `json:"id"` EmailString string `json:"email"` } var rawUserValue rawUser if err := json.Unmarshal(data, &rawUserValue); err != nil { return err } id, err := config.NewID(rawUserValue.IdString) if err != nil { return err } u.Id = id u.Email = rawUserValue.EmailString return nil }
func setUpV2Ray() (uint16, error) { id1, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51") if err != nil { return 0, err } id2, err := config.NewID("93ccfc71-b136-4015-ac85-e037bd1ead9e") if err != nil { return 0, err } users := []*vmessjson.ConfigUser{ &vmessjson.ConfigUser{Id: id1}, &vmessjson.ConfigUser{Id: id2}, } portB := v2nettesting.PickPort() configB := mocks.Config{ PortValue: portB, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "vmess", SettingsValue: &vmessjson.Inbound{ AllowedClients: users, }, }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "freedom", SettingsValue: nil, }, } pointB, err := point.NewPoint(&configB) if err != nil { return 0, err } err = pointB.Start() if err != nil { return 0, err } portA := v2nettesting.PickPort() configA := mocks.Config{ PortValue: portA, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "socks", SettingsValue: &socksjson.SocksConfig{ AuthMethod: "noauth", UDPEnabled: true, HostIP: socksjson.IPAddress(net.IPv4(127, 0, 0, 1)), }, }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "vmess", SettingsValue: &vmessjson.Outbound{ []*vmessjson.ConfigTarget{ &vmessjson.ConfigTarget{ Address: v2net.IPAddress([]byte{127, 0, 0, 1}, portB), Users: users, }, }, }, }, } pointA, err := point.NewPoint(&configA) if err != nil { return 0, err } err = pointA.Start() if err != nil { return 0, err } return portA, nil }
func TestVMessInAndOutUDP(t *testing.T) { assert := unit.Assert(t) data2Send := "The data to be send to outbound server." testAccount, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51") assert.Error(err).IsNil() portA := uint16(17394) ich := &mocks.InboundConnectionHandler{ Data2Send: []byte(data2Send), DataReturned: bytes.NewBuffer(make([]byte, 0, 1024)), } proxy.RegisterInboundConnectionHandlerFactory("mock_ich", ich) configA := mocks.Config{ PortValue: portA, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "mock_ich", SettingsValue: nil, }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "vmess", SettingsValue: &json.Outbound{ []*json.ConfigTarget{ &json.ConfigTarget{ Address: v2net.IPAddress([]byte{127, 0, 0, 1}, 13841), UDPEnabled: true, Users: []*json.ConfigUser{ &json.ConfigUser{Id: testAccount}, }, }, }, }, }, } pointA, err := point.NewPoint(&configA) assert.Error(err).IsNil() err = pointA.Start() assert.Error(err).IsNil() portB := uint16(13841) och := &mocks.OutboundConnectionHandler{ Data2Send: bytes.NewBuffer(make([]byte, 0, 1024)), Data2Return: []byte("The data to be returned to inbound server."), } proxy.RegisterOutboundConnectionHandlerFactory("mock_och", och) configB := mocks.Config{ PortValue: portB, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "vmess", SettingsValue: &json.Inbound{ AllowedClients: []*json.ConfigUser{ &json.ConfigUser{Id: testAccount}, }, UDP: true, }, }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "mock_och", SettingsValue: nil, }, } pointB, err := point.NewPoint(&configB) assert.Error(err).IsNil() err = pointB.Start() assert.Error(err).IsNil() data2SendBuffer := alloc.NewBuffer() data2SendBuffer.Clear() data2SendBuffer.Append([]byte(data2Send)) dest := v2net.NewUDPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}, 80)) ich.Communicate(v2net.NewPacket(dest, data2SendBuffer, false)) assert.Bytes([]byte(data2Send)).Equals(och.Data2Send.Bytes()) assert.Bytes(ich.DataReturned.Bytes()).Equals(och.Data2Return) }
func TestVMessInAndOut(t *testing.T) { assert := unit.Assert(t) testAccount, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51") assert.Error(err).IsNil() 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: &json.Outbound{ []*json.ConfigTarget{ &json.ConfigTarget{ Address: v2net.IPAddress([]byte{127, 0, 0, 1}, portB), TCPEnabled: true, Users: []*json.ConfigUser{ &json.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: &json.Inbound{ AllowedClients: []*json.ConfigUser{ &json.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.NewTCPDestination(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) }