freedom_test.go 2.9 KB

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