freedom_test.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package freedom_test
  2. import (
  3. "net"
  4. "testing"
  5. "github.com/v2ray/v2ray-core/app"
  6. "github.com/v2ray/v2ray-core/app/dispatcher"
  7. dispatchers "github.com/v2ray/v2ray-core/app/dispatcher/impl"
  8. "github.com/v2ray/v2ray-core/app/dns"
  9. "github.com/v2ray/v2ray-core/app/proxyman"
  10. "github.com/v2ray/v2ray-core/app/router"
  11. "github.com/v2ray/v2ray-core/app/router/rules"
  12. "github.com/v2ray/v2ray-core/common/alloc"
  13. v2net "github.com/v2ray/v2ray-core/common/net"
  14. v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
  15. . "github.com/v2ray/v2ray-core/proxy/freedom"
  16. "github.com/v2ray/v2ray-core/testing/assert"
  17. "github.com/v2ray/v2ray-core/testing/servers/tcp"
  18. "github.com/v2ray/v2ray-core/transport/ray"
  19. )
  20. func TestSinglePacket(t *testing.T) {
  21. assert := assert.On(t)
  22. port := v2nettesting.PickPort()
  23. tcpServer := &tcp.Server{
  24. Port: port,
  25. MsgProcessor: func(data []byte) []byte {
  26. buffer := make([]byte, 0, 2048)
  27. buffer = append(buffer, []byte("Processed: ")...)
  28. buffer = append(buffer, data...)
  29. return buffer
  30. },
  31. }
  32. _, err := tcpServer.Start()
  33. assert.Error(err).IsNil()
  34. space := app.NewSpace()
  35. freedom := NewFreedomConnection(&Config{}, space, v2net.AnyIP)
  36. space.Initialize()
  37. traffic := ray.NewRay()
  38. data2Send := "Data to be sent to remote"
  39. payload := alloc.NewSmallBuffer().Clear().Append([]byte(data2Send))
  40. go freedom.Dispatch(v2net.TCPDestination(v2net.LocalHostIP, port), payload, traffic)
  41. traffic.InboundInput().Close()
  42. respPayload, err := traffic.InboundOutput().Read()
  43. assert.Error(err).IsNil()
  44. assert.Bytes(respPayload.Value).Equals([]byte("Processed: Data to be sent to remote"))
  45. tcpServer.Close()
  46. }
  47. func TestUnreachableDestination(t *testing.T) {
  48. assert := assert.On(t)
  49. freedom := NewFreedomConnection(&Config{}, app.NewSpace(), v2net.AnyIP)
  50. traffic := ray.NewRay()
  51. data2Send := "Data to be sent to remote"
  52. payload := alloc.NewSmallBuffer().Clear().Append([]byte(data2Send))
  53. err := freedom.Dispatch(v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 128), payload, traffic)
  54. assert.Error(err).IsNotNil()
  55. }
  56. func TestIPResolution(t *testing.T) {
  57. assert := assert.On(t)
  58. space := app.NewSpace()
  59. space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager())
  60. space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
  61. r, _ := router.CreateRouter("rules", &rules.RouterRuleConfig{}, space)
  62. space.BindApp(router.APP_ID, r)
  63. dnsServer := dns.NewCacheServer(space, &dns.Config{
  64. Hosts: map[string]net.IP{
  65. "v2ray.com": net.IP([]byte{127, 0, 0, 1}),
  66. },
  67. })
  68. space.BindApp(dns.APP_ID, dnsServer)
  69. freedom := NewFreedomConnection(&Config{DomainStrategy: DomainStrategyUseIP}, space, v2net.AnyIP)
  70. space.Initialize()
  71. ipDest := freedom.ResolveIP(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), v2net.Port(80)))
  72. assert.Destination(ipDest).IsTCP()
  73. assert.Address(ipDest.Address()).Equals(v2net.LocalHostIP)
  74. }