예제 #1
0
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")
}
예제 #2
0
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")
}
예제 #3
0
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")
}
예제 #4
0
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)
}
예제 #5
0
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)
}
예제 #6
0
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]")
}
예제 #7
0
func TestDefaultIPAddress(t *testing.T) {
	assert := assert.On(t)

	socksConfig, err := internal.CreateInboundConfig("socks", []byte(`{
    "auth": "noauth"
  }`))
	assert.Error(err).IsNil()
	assert.Address(socksConfig.(*socks.Config).Address).EqualsString("127.0.0.1")
}
예제 #8
0
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")
}
예제 #9
0
func TestNormalRequestParsing(t *testing.T) {
	assert := assert.On(t)

	buffer := alloc.NewSmallBuffer().Clear()
	buffer.AppendBytes(1, 127, 0, 0, 1, 0, 80)

	request, err := ReadRequest(buffer, nil, false)
	assert.Error(err).IsNil()
	assert.Address(request.Address).Equals(v2net.LocalHostIP)
	assert.Port(request.Port).Equals(v2net.Port(80))
	assert.Bool(request.OTA).IsFalse()
}
예제 #10
0
func TestUDPRequestParsing(t *testing.T) {
	assert := assert.On(t)

	buffer := alloc.NewSmallBuffer().Clear()
	buffer.AppendBytes(1, 127, 0, 0, 1, 0, 80, 1, 2, 3, 4, 5, 6)

	request, err := ReadRequest(buffer, nil, true)
	assert.Error(err).IsNil()
	assert.Address(request.Address).Equals(v2net.LocalHostIP)
	assert.Port(request.Port).Equals(v2net.Port(80))
	assert.Bool(request.OTA).IsFalse()
	assert.Bytes(request.UDPPayload.Value).Equals([]byte{1, 2, 3, 4, 5, 6})
}
예제 #11
0
func TestOTARequest(t *testing.T) {
	assert := assert.On(t)

	buffer := alloc.NewSmallBuffer().Clear()
	buffer.AppendBytes(0x13, 13, 119, 119, 119, 46, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 0, 239, 115, 52, 212, 178, 172, 26, 6, 168, 0)

	auth := NewAuthenticator(HeaderKeyGenerator(
		[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5},
		[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5}))
	request, err := ReadRequest(buffer, auth, false)
	assert.Error(err).IsNil()
	assert.Address(request.Address).Equals(v2net.DomainAddress("www.v2ray.com"))
	assert.Bool(request.OTA).IsTrue()
}
예제 #12
0
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))
}
예제 #13
0
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.Bytes(request.Data.Value).Equals([]byte("Actual payload"))
}
예제 #14
0
func TestUDPRequestWithOTA(t *testing.T) {
	assert := assert.On(t)

	buffer := alloc.NewSmallBuffer().Clear()
	buffer.AppendBytes(
		0x13, 13, 119, 119, 119, 46, 118, 50, 114, 97, 121, 46, 99, 111, 109, 0, 0,
		1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
		58, 32, 223, 30, 57, 199, 50, 139, 143, 101)

	auth := NewAuthenticator(HeaderKeyGenerator(
		[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5},
		[]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5}))
	request, err := ReadRequest(buffer, auth, true)
	assert.Error(err).IsNil()
	assert.Address(request.Address).Equals(v2net.DomainAddress("www.v2ray.com"))
	assert.Bool(request.OTA).IsTrue()
	assert.Bytes(request.UDPPayload.Value).Equals([]byte{
		1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0})
}
예제 #15
0
func TestRequestSerialization(t *testing.T) {
	assert := assert.On(t)

	user := protocol.NewUser(
		&protocol.VMessAccount{
			ID:       protocol.NewID(uuid.New()),
			AlterIDs: nil,
		},
		protocol.UserLevelUntrusted,
		"*****@*****.**")

	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 := protocol.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)
}