router_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package router_test
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/golang/mock/gomock"
  6. . "github.com/v2fly/v2ray-core/v4/app/router"
  7. "github.com/v2fly/v2ray-core/v4/common"
  8. "github.com/v2fly/v2ray-core/v4/common/net"
  9. "github.com/v2fly/v2ray-core/v4/common/session"
  10. "github.com/v2fly/v2ray-core/v4/features/outbound"
  11. routing_session "github.com/v2fly/v2ray-core/v4/features/routing/session"
  12. "github.com/v2fly/v2ray-core/v4/testing/mocks"
  13. )
  14. type mockOutboundManager struct {
  15. outbound.Manager
  16. outbound.HandlerSelector
  17. }
  18. func TestSimpleRouter(t *testing.T) {
  19. config := &Config{
  20. Rule: []*RoutingRule{
  21. {
  22. TargetTag: &RoutingRule_Tag{
  23. Tag: "test",
  24. },
  25. Networks: []net.Network{net.Network_TCP},
  26. },
  27. },
  28. }
  29. mockCtl := gomock.NewController(t)
  30. defer mockCtl.Finish()
  31. mockDNS := mocks.NewDNSClient(mockCtl)
  32. mockOhm := mocks.NewOutboundManager(mockCtl)
  33. mockHs := mocks.NewOutboundHandlerSelector(mockCtl)
  34. r := new(Router)
  35. common.Must(r.Init(config, mockDNS, &mockOutboundManager{
  36. Manager: mockOhm,
  37. HandlerSelector: mockHs,
  38. }))
  39. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2fly.org"), 80)})
  40. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  41. common.Must(err)
  42. if tag := route.GetOutboundTag(); tag != "test" {
  43. t.Error("expect tag 'test', bug actually ", tag)
  44. }
  45. }
  46. func TestSimpleBalancer(t *testing.T) {
  47. config := &Config{
  48. Rule: []*RoutingRule{
  49. {
  50. TargetTag: &RoutingRule_BalancingTag{
  51. BalancingTag: "balance",
  52. },
  53. Networks: []net.Network{net.Network_TCP},
  54. },
  55. },
  56. BalancingRule: []*BalancingRule{
  57. {
  58. Tag: "balance",
  59. OutboundSelector: []string{"test-"},
  60. },
  61. },
  62. }
  63. mockCtl := gomock.NewController(t)
  64. defer mockCtl.Finish()
  65. mockDNS := mocks.NewDNSClient(mockCtl)
  66. mockOhm := mocks.NewOutboundManager(mockCtl)
  67. mockHs := mocks.NewOutboundHandlerSelector(mockCtl)
  68. mockHs.EXPECT().Select(gomock.Eq([]string{"test-"})).Return([]string{"test"})
  69. r := new(Router)
  70. common.Must(r.Init(config, mockDNS, &mockOutboundManager{
  71. Manager: mockOhm,
  72. HandlerSelector: mockHs,
  73. }))
  74. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2fly.org"), 80)})
  75. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  76. common.Must(err)
  77. if tag := route.GetOutboundTag(); tag != "test" {
  78. t.Error("expect tag 'test', bug actually ", tag)
  79. }
  80. }
  81. func TestIPOnDemand(t *testing.T) {
  82. config := &Config{
  83. DomainStrategy: Config_IpOnDemand,
  84. Rule: []*RoutingRule{
  85. {
  86. TargetTag: &RoutingRule_Tag{
  87. Tag: "test",
  88. },
  89. Cidr: []*CIDR{
  90. {
  91. Ip: []byte{192, 168, 0, 0},
  92. Prefix: 16,
  93. },
  94. },
  95. },
  96. },
  97. }
  98. mockCtl := gomock.NewController(t)
  99. defer mockCtl.Finish()
  100. mockDNS := mocks.NewDNSClient(mockCtl)
  101. mockDNS.EXPECT().LookupIP(gomock.Eq("v2fly.org")).Return([]net.IP{{192, 168, 0, 1}}, nil).AnyTimes()
  102. r := new(Router)
  103. common.Must(r.Init(config, mockDNS, nil))
  104. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2fly.org"), 80)})
  105. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  106. common.Must(err)
  107. if tag := route.GetOutboundTag(); tag != "test" {
  108. t.Error("expect tag 'test', bug actually ", tag)
  109. }
  110. }
  111. func TestIPIfNonMatchDomain(t *testing.T) {
  112. config := &Config{
  113. DomainStrategy: Config_IpIfNonMatch,
  114. Rule: []*RoutingRule{
  115. {
  116. TargetTag: &RoutingRule_Tag{
  117. Tag: "test",
  118. },
  119. Cidr: []*CIDR{
  120. {
  121. Ip: []byte{192, 168, 0, 0},
  122. Prefix: 16,
  123. },
  124. },
  125. },
  126. },
  127. }
  128. mockCtl := gomock.NewController(t)
  129. defer mockCtl.Finish()
  130. mockDNS := mocks.NewDNSClient(mockCtl)
  131. mockDNS.EXPECT().LookupIP(gomock.Eq("v2fly.org")).Return([]net.IP{{192, 168, 0, 1}}, nil).AnyTimes()
  132. r := new(Router)
  133. common.Must(r.Init(config, mockDNS, nil))
  134. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2fly.org"), 80)})
  135. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  136. common.Must(err)
  137. if tag := route.GetOutboundTag(); tag != "test" {
  138. t.Error("expect tag 'test', bug actually ", tag)
  139. }
  140. }
  141. func TestIPIfNonMatchIP(t *testing.T) {
  142. config := &Config{
  143. DomainStrategy: Config_IpIfNonMatch,
  144. Rule: []*RoutingRule{
  145. {
  146. TargetTag: &RoutingRule_Tag{
  147. Tag: "test",
  148. },
  149. Cidr: []*CIDR{
  150. {
  151. Ip: []byte{127, 0, 0, 0},
  152. Prefix: 8,
  153. },
  154. },
  155. },
  156. },
  157. }
  158. mockCtl := gomock.NewController(t)
  159. defer mockCtl.Finish()
  160. mockDNS := mocks.NewDNSClient(mockCtl)
  161. r := new(Router)
  162. common.Must(r.Init(config, mockDNS, nil))
  163. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 80)})
  164. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  165. common.Must(err)
  166. if tag := route.GetOutboundTag(); tag != "test" {
  167. t.Error("expect tag 'test', bug actually ", tag)
  168. }
  169. }