handler_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/net"
  12. "github.com/v2fly/v2ray-core/v5/common/serial"
  13. "github.com/v2fly/v2ray-core/v5/features/outbound"
  14. "github.com/v2fly/v2ray-core/v5/proxy/freedom"
  15. "github.com/v2fly/v2ray-core/v5/transport/internet"
  16. )
  17. func TestInterfaces(t *testing.T) {
  18. _ = (outbound.Handler)(new(Handler))
  19. _ = (outbound.Manager)(new(Manager))
  20. }
  21. //go:linkname toContext github.com/v2fly/v2ray-core/v5.toContext
  22. func toContext(ctx context.Context, v *core.Instance) context.Context
  23. func TestOutboundWithoutStatCounter(t *testing.T) {
  24. config := &core.Config{
  25. App: []*anypb.Any{
  26. serial.ToTypedMessage(&stats.Config{}),
  27. serial.ToTypedMessage(&policy.Config{
  28. System: &policy.SystemPolicy{
  29. Stats: &policy.SystemPolicy_Stats{
  30. InboundUplink: true,
  31. },
  32. },
  33. }),
  34. },
  35. }
  36. v, _ := core.New(config)
  37. v.AddFeature((outbound.Manager)(new(Manager)))
  38. ctx := toContext(context.Background(), v)
  39. h, _ := NewHandler(ctx, &core.OutboundHandlerConfig{
  40. Tag: "tag",
  41. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  42. })
  43. conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
  44. _, ok := conn.(*internet.StatCouterConnection)
  45. if ok {
  46. t.Errorf("Expected conn to not be StatCouterConnection")
  47. }
  48. }
  49. func TestOutboundWithStatCounter(t *testing.T) {
  50. config := &core.Config{
  51. App: []*anypb.Any{
  52. serial.ToTypedMessage(&stats.Config{}),
  53. serial.ToTypedMessage(&policy.Config{
  54. System: &policy.SystemPolicy{
  55. Stats: &policy.SystemPolicy_Stats{
  56. OutboundUplink: true,
  57. OutboundDownlink: true,
  58. },
  59. },
  60. }),
  61. },
  62. }
  63. v, _ := core.New(config)
  64. v.AddFeature((outbound.Manager)(new(Manager)))
  65. ctx := toContext(context.Background(), v)
  66. h, _ := NewHandler(ctx, &core.OutboundHandlerConfig{
  67. Tag: "tag",
  68. ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
  69. })
  70. conn, _ := h.(*Handler).Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
  71. _, ok := conn.(*internet.StatCouterConnection)
  72. if !ok {
  73. t.Errorf("Expected conn to be StatCouterConnection")
  74. }
  75. }