func TestNetIPv4Address(t *testing.T) { assert := assert.On(t) ip := net.IPv4(1, 2, 3, 4) addr := v2net.IPAddress(ip) assert.Address(addr).IsIPv4() assert.Address(addr).EqualsString("1.2.3.4") }
func TestDomainAddress(t *testing.T) { assert := assert.On(t) domain := "v2ray.com" addr := v2net.DomainAddress(domain) assert.Address(addr).IsDomain() assert.Address(addr).IsNotIPv6() assert.Address(addr).IsNotIPv4() assert.String(addr.Domain()).Equals(domain) assert.Address(addr).EqualsString("v2ray.com") }
func TestIPv4Address(t *testing.T) { assert := assert.On(t) ip := []byte{byte(1), byte(2), byte(3), byte(4)} addr := v2net.IPAddress(ip) assert.Address(addr).IsIPv4() assert.Address(addr).IsNotIPv6() assert.Address(addr).IsNotDomain() assert.Bytes(addr.IP()).Equals(ip) assert.Address(addr).EqualsString("1.2.3.4") }
func TestIPv4AddressEquals(t *testing.T) { assert := assert.On(t) addr := v2net.IPAddress([]byte{1, 2, 3, 4}) assert.Address(addr).NotEquals(nil) addr2 := v2net.IPAddress([]byte{1, 2, 3, 4}) assert.Address(addr).Equals(addr2) addr3 := v2net.IPAddress([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6}) assert.Address(addr).NotEquals(addr3) addr4 := v2net.IPAddress([]byte{1, 2, 3, 5}) assert.Address(addr).NotEquals(addr4) }
func TestUDPEncoding(t *testing.T) { assert := assert.On(t) request := &protocol.RequestHeader{ Version: Version, Command: protocol.RequestCommandUDP, Address: v2net.LocalHostIP, Port: 1234, User: &protocol.User{ Email: "*****@*****.**", Account: serial.ToTypedMessage(&Account{ Password: "******", CipherType: CipherType_AES_128_CFB, Ota: Account_Disabled, }), }, } data := buf.NewLocal(256) data.AppendSupplier(serial.WriteString("test string")) encodedData, err := EncodeUDPPacket(request, data) assert.Error(err).IsNil() decodedRequest, decodedData, err := DecodeUDPPacket(request.User, encodedData) assert.Error(err).IsNil() assert.Bytes(decodedData.Bytes()).Equals(data.Bytes()) assert.Address(decodedRequest.Address).Equals(request.Address) assert.Port(decodedRequest.Port).Equals(request.Port) }
func TestSocksInboundConfig(t *testing.T) { assert := assert.On(t) rawJson := `{ "auth": "password", "accounts": [ { "user": "******", "pass": "******" } ], "udp": false, "ip": "127.0.0.1", "timeout": 5 }` config := new(SocksServerConfig) err := json.Unmarshal([]byte(rawJson), &config) assert.Error(err).IsNil() message, err := config.Build() assert.Error(err).IsNil() iConfig, err := message.GetInstance() assert.Error(err).IsNil() socksConfig := iConfig.(*socks.ServerConfig) assert.Bool(socksConfig.AuthType == socks.AuthType_PASSWORD).IsTrue() assert.Int(len(socksConfig.Accounts)).Equals(1) assert.String(socksConfig.Accounts["my-username"]).Equals("my-password") assert.Bool(socksConfig.UdpEnabled).IsFalse() assert.Address(socksConfig.Address.AsAddress()).Equals(net.LocalHostIP) assert.Uint32(socksConfig.Timeout).Equals(5) }
func TestIPResolution(t *testing.T) { assert := assert.On(t) space := app.NewSpace() space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager()) space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) r, _ := router.CreateRouter("rules", &rules.RouterRuleConfig{}, space) space.BindApp(router.APP_ID, r) dnsServer := dns.NewCacheServer(space, &dns.Config{ Hosts: map[string]net.IP{ "v2ray.com": net.IP([]byte{127, 0, 0, 1}), }, }) space.BindApp(dns.APP_ID, dnsServer) freedom := NewFreedomConnection( &Config{DomainStrategy: DomainStrategyUseIP}, space, &proxy.OutboundHandlerMeta{ Address: v2net.AnyIP, StreamSettings: &internet.StreamSettings{ Type: internet.StreamConnectionTypeRawTCP, }, }) space.Initialize() ipDest := freedom.ResolveIP(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), v2net.Port(80))) assert.Destination(ipDest).IsTCP() assert.Address(ipDest.Address()).Equals(v2net.LocalHostIP) }
func TestTCPRequest(t *testing.T) { assert := assert.On(t) request := &protocol.RequestHeader{ Version: Version, Command: protocol.RequestCommandTCP, Address: v2net.LocalHostIP, Option: RequestOptionOneTimeAuth, Port: 1234, User: &protocol.User{ Email: "*****@*****.**", Account: loader.NewTypedSettings(&Account{ Password: "******", CipherType: CipherType_CHACHA20, }), }, } data := alloc.NewLocalBuffer(256).Clear().AppendString("test string") cache := alloc.NewBuffer().Clear() writer, err := WriteTCPRequest(request, cache) assert.Error(err).IsNil() writer.Write(data) decodedRequest, reader, err := ReadTCPSession(request.User, cache) assert.Error(err).IsNil() assert.Address(decodedRequest.Address).Equals(request.Address) assert.Port(decodedRequest.Port).Equals(request.Port) decodedData, err := reader.Read() assert.Error(err).IsNil() assert.Bytes(decodedData.Value).Equals([]byte("test string")) }
func TestUDPEncoding(t *testing.T) { assert := assert.On(t) request := &protocol.RequestHeader{ Version: Version, Command: protocol.RequestCommandUDP, Address: v2net.LocalHostIP, Port: 1234, User: &protocol.User{ Email: "*****@*****.**", Account: loader.NewTypedSettings(&Account{ Password: "******", CipherType: CipherType_AES_128_CFB, Ota: Account_Disabled, }), }, } data := alloc.NewLocalBuffer(256).Clear().AppendString("test string") encodedData, err := EncodeUDPPacket(request, data) assert.Error(err).IsNil() decodedRequest, decodedData, err := DecodeUDPPacket(request.User, encodedData) assert.Error(err).IsNil() assert.Bytes(decodedData.Value).Equals(data.Value) assert.Address(decodedRequest.Address).Equals(request.Address) assert.Port(decodedRequest.Port).Equals(request.Port) }
func TestIPResolution(t *testing.T) { assert := assert.On(t) space := app.NewSpace() space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outbound.New()) space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) r := router.NewRouter(&router.Config{}, space) space.BindApp(router.APP_ID, r) dnsServer := dnsserver.NewCacheServer(space, &dns.Config{ Hosts: map[string]*v2net.IPOrDomain{ "v2ray.com": v2net.NewIPOrDomain(v2net.LocalHostIP), }, }) space.BindApp(dns.APP_ID, dnsServer) freedom := NewFreedomConnection( &Config{DomainStrategy: Config_USE_IP}, space, &proxy.OutboundHandlerMeta{ Address: v2net.AnyIP, StreamSettings: &internet.StreamConfig{ Network: v2net.Network_RawTCP, }, }) space.Initialize() ipDest := freedom.ResolveIP(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), v2net.Port(80))) assert.Destination(ipDest).IsTCP() assert.Address(ipDest.Address).Equals(v2net.LocalHostIP) }
func TestIPv6Address(t *testing.T) { assert := assert.On(t) ip := []byte{ byte(1), byte(2), byte(3), byte(4), byte(1), byte(2), byte(3), byte(4), byte(1), byte(2), byte(3), byte(4), byte(1), byte(2), byte(3), byte(4), } addr := v2net.IPAddress(ip) assert.Address(addr).IsIPv6() assert.Address(addr).IsNotIPv4() assert.Address(addr).IsNotDomain() assert.IP(addr.IP()).Equals(net.IP(ip)) assert.Address(addr).EqualsString("[102:304:102:304:102:304:102:304]") }
func TestDefaultIPAddress(t *testing.T) { assert := assert.On(t) socksConfig, err := registry.CreateInboundConfig("socks", []byte(`{ "auth": "noauth" }`)) assert.Error(err).IsNil() assert.Address(socksConfig.(*socks.Config).Address).EqualsString("127.0.0.1") }
func TestIPv4Asv6(t *testing.T) { assert := assert.On(t) ip := []byte{ byte(0), byte(0), byte(0), byte(0), byte(0), byte(0), byte(0), byte(0), byte(0), byte(0), byte(255), byte(255), byte(1), byte(2), byte(3), byte(4), } addr := v2net.IPAddress(ip) assert.Address(addr).EqualsString("1.2.3.4") }
func TestConfigParsing(t *testing.T) { assert := assert.On(t) rawJson := `{ "servers": ["8.8.8.8"] }` config := new(Config) err := json.Unmarshal([]byte(rawJson), config) assert.Error(err).IsNil() assert.Int(len(config.NameServers)).Equals(1) assert.Destination(config.NameServers[0]).IsUDP() assert.Address(config.NameServers[0].Address()).Equals(v2net.IPAddress([]byte{8, 8, 8, 8})) assert.Port(config.NameServers[0].Port()).Equals(v2net.Port(53)) }
func TestDomainAddressRequest(t *testing.T) { assert := assert.On(t) payload := make([]byte, 0, 1024) payload = append(payload, 0, 0, 1, AddrTypeDomain, byte(len("v2ray.com"))) payload = append(payload, []byte("v2ray.com")...) payload = append(payload, 0, 80) payload = append(payload, []byte("Actual payload")...) request, err := ReadUDPRequest(payload) assert.Error(err).IsNil() assert.Byte(request.Fragment).Equals(1) assert.Address(request.Address).EqualsString("v2ray.com") assert.Port(request.Port).Equals(v2net.Port(80)) assert.String(request.Data.String()).Equals("Actual payload") }
func TestDnsConfigParsing(t *testing.T) { assert := assert.On(t) rawJson := `{ "servers": ["8.8.8.8"] }` jsonConfig := new(DnsConfig) err := json.Unmarshal([]byte(rawJson), jsonConfig) assert.Error(err).IsNil() config := jsonConfig.Build() assert.Int(len(config.NameServers)).Equals(1) dest := config.NameServers[0].AsDestination() assert.Destination(dest).IsUDP() assert.Address(dest.Address).Equals(v2net.IPAddress([]byte{8, 8, 8, 8})) assert.Port(dest.Port).Equals(v2net.Port(53)) }
func TestRequestSerialization(t *testing.T) { assert := assert.On(t) user := &protocol.User{ Level: 0, Email: "*****@*****.**", } account := &vmess.Account{ Id: uuid.New().String(), AlterId: 0, } user.Account = serial.ToTypedMessage(account) expectedRequest := &protocol.RequestHeader{ Version: 1, User: user, Command: protocol.RequestCommandTCP, Option: protocol.RequestOptionConnectionReuse, Address: v2net.DomainAddress("www.v2ray.com"), Port: v2net.Port(443), Security: protocol.Security(protocol.SecurityType_AES128_GCM), } buffer := buf.New() client := NewClientSession(protocol.DefaultIDHash) client.EncodeRequestHeader(expectedRequest, buffer) userValidator := vmess.NewTimedUserValidator(protocol.DefaultIDHash) userValidator.Add(user) server := NewServerSession(userValidator) actualRequest, err := server.DecodeRequestHeader(buffer) assert.Error(err).IsNil() assert.Byte(expectedRequest.Version).Equals(actualRequest.Version) assert.Byte(byte(expectedRequest.Command)).Equals(byte(actualRequest.Command)) assert.Byte(byte(expectedRequest.Option)).Equals(byte(actualRequest.Option)) assert.Address(expectedRequest.Address).Equals(actualRequest.Address) assert.Port(expectedRequest.Port).Equals(actualRequest.Port) assert.Byte(byte(expectedRequest.Security)).Equals(byte(actualRequest.Security)) }
func TestRequestSerialization(t *testing.T) { assert := assert.On(t) user := protocol.NewUser( protocol.UserLevelUntrusted, "*****@*****.**") user.Account = &vmess.Account{ ID: protocol.NewID(uuid.New()), AlterIDs: nil, } expectedRequest := &protocol.RequestHeader{ Version: 1, User: user, Command: protocol.RequestCommandTCP, Option: protocol.RequestOption(0), Address: v2net.DomainAddress("www.v2ray.com"), Port: v2net.Port(443), } buffer := alloc.NewBuffer().Clear() client := NewClientSession(protocol.DefaultIDHash) client.EncodeRequestHeader(expectedRequest, buffer) userValidator := vmess.NewTimedUserValidator(protocol.DefaultIDHash) userValidator.Add(user) server := NewServerSession(userValidator) actualRequest, err := server.DecodeRequestHeader(buffer) assert.Error(err).IsNil() assert.Byte(expectedRequest.Version).Equals(actualRequest.Version) assert.Byte(byte(expectedRequest.Command)).Equals(byte(actualRequest.Command)) assert.Byte(byte(expectedRequest.Option)).Equals(byte(actualRequest.Option)) assert.Address(expectedRequest.Address).Equals(actualRequest.Address) assert.Port(expectedRequest.Port).Equals(actualRequest.Port) }
func TestTCPRequest(t *testing.T) { assert := assert.On(t) request := &protocol.RequestHeader{ Version: Version, Command: protocol.RequestCommandTCP, Address: v2net.LocalHostIP, Option: RequestOptionOneTimeAuth, Port: 1234, User: &protocol.User{ Email: "*****@*****.**", Account: serial.ToTypedMessage(&Account{ Password: "******", CipherType: CipherType_CHACHA20, }), }, } data := buf.NewLocal(256) data.AppendSupplier(serial.WriteString("test string")) cache := buf.New() writer, err := WriteTCPRequest(request, cache) assert.Error(err).IsNil() writer.Write(data) decodedRequest, reader, err := ReadTCPSession(request.User, cache) assert.Error(err).IsNil() assert.Address(decodedRequest.Address).Equals(request.Address) assert.Port(decodedRequest.Port).Equals(request.Port) decodedData, err := reader.Read() assert.Error(err).IsNil() assert.String(decodedData.String()).Equals("test string") }