handler_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package outbound_test
  2. import (
  3. "context"
  4. "testing"
  5. _ "unsafe"
  6. "google.golang.org/protobuf/types/known/anypb"
  7. core "github.com/v2fly/v2ray-core/v5"
  8. "github.com/v2fly/v2ray-core/v5/app/policy"
  9. . "github.com/v2fly/v2ray-core/v5/app/proxyman/outbound"
  10. "github.com/v2fly/v2ray-core/v5/app/stats"
  11. "github.com/v2fly/v2ray-core/v5/common/environment"
  12. "github.com/v2fly/v2ray-core/v5/common/environment/deferredpersistentstorage"
  13. "github.com/v2fly/v2ray-core/v5/common/environment/envctx"
  14. "github.com/v2fly/v2ray-core/v5/common/environment/filesystemimpl"
  15. "github.com/v2fly/v2ray-core/v5/common/environment/systemnetworkimpl"
  16. "github.com/v2fly/v2ray-core/v5/common/environment/transientstorageimpl"
  17. "github.com/v2fly/v2ray-core/v5/common/net"
  18. "github.com/v2fly/v2ray-core/v5/common/serial"
  19. "github.com/v2fly/v2ray-core/v5/features/outbound"
  20. "github.com/v2fly/v2ray-core/v5/proxy/freedom"
  21. "github.com/v2fly/v2ray-core/v5/transport/internet"
  22. )
  23. func TestInterfaces(t *testing.T) {
  24. _ = (outbound.Handler)(new(Handler))
  25. _ = (outbound.Manager)(new(Manager))
  26. }
  27. //go:linkname toContext github.com/v2fly/v2ray-core/v5.toContext
  28. func toContext(ctx context.Context, v *core.Instance) context.Context
  29. func TestOutboundWithoutStatCounter(t *testing.T) {
  30. config := &core.Config{
  31. App: []*anypb.Any{
  32. serial.ToTypedMessage(&stats.Config{}),
  33. serial.ToTypedMessage(&policy.Config{
  34. System: &policy.SystemPolicy{
  35. Stats: &policy.SystemPolicy_Stats{
  36. InboundUplink: true,
  37. },
  38. },
  39. }),
  40. },
  41. }
  42. v, _ := core.New(config)
  43. v.AddFeature((outbound.Manager)(new(Manager)))
  44. ctx := toContext(context.Background(), v)
  45. defaultNetworkImpl := systemnetworkimpl.NewSystemNetworkDefault()
  46. defaultFilesystemImpl := filesystemimpl.NewDefaultFileSystemDefaultImpl()
  47. deferredPersistentStorageImpl := deferredpersistentstorage.NewDeferredPersistentStorage(ctx)
  48. rootEnv := environment.NewRootEnvImpl(ctx,
  49. transientstorageimpl.NewScopedTransientStorageImpl(), defaultNetworkImpl.Dialer(), defaultNetworkImpl.Listener(),
  50. defaultFilesystemImpl, deferredPersistentStorageImpl)
  51. proxyEnvironment := rootEnv.ProxyEnvironment("o")
  52. ctx = envctx.ContextWithEnvironment(ctx, proxyEnvironment)
  53. h, _ := NewHandler(ctx, &core.OutboundHandlerConfig{
  54. Tag: "tag",
  55. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  56. })
  57. conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
  58. _, ok := conn.(*internet.StatCouterConnection)
  59. if ok {
  60. t.Errorf("Expected conn to not be StatCouterConnection")
  61. }
  62. }
  63. func TestOutboundWithStatCounter(t *testing.T) {
  64. config := &core.Config{
  65. App: []*anypb.Any{
  66. serial.ToTypedMessage(&stats.Config{}),
  67. serial.ToTypedMessage(&policy.Config{
  68. System: &policy.SystemPolicy{
  69. Stats: &policy.SystemPolicy_Stats{
  70. OutboundUplink: true,
  71. OutboundDownlink: true,
  72. },
  73. },
  74. }),
  75. },
  76. }
  77. v, _ := core.New(config)
  78. v.AddFeature((outbound.Manager)(new(Manager)))
  79. ctx := toContext(context.Background(), v)
  80. defaultNetworkImpl := systemnetworkimpl.NewSystemNetworkDefault()
  81. defaultFilesystemImpl := filesystemimpl.NewDefaultFileSystemDefaultImpl()
  82. deferredPersistentStorageImpl := deferredpersistentstorage.NewDeferredPersistentStorage(ctx)
  83. rootEnv := environment.NewRootEnvImpl(ctx,
  84. transientstorageimpl.NewScopedTransientStorageImpl(), defaultNetworkImpl.Dialer(), defaultNetworkImpl.Listener(),
  85. defaultFilesystemImpl, deferredPersistentStorageImpl)
  86. proxyEnvironment := rootEnv.ProxyEnvironment("o")
  87. ctx = envctx.ContextWithEnvironment(ctx, proxyEnvironment)
  88. h, _ := NewHandler(ctx, &core.OutboundHandlerConfig{
  89. Tag: "tag",
  90. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  91. })
  92. conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
  93. _, ok := conn.(*internet.StatCouterConnection)
  94. if !ok {
  95. t.Errorf("Expected conn to be StatCouterConnection")
  96. }
  97. }