router_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package router_test
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/golang/mock/gomock"
  6. . "github.com/xtls/xray-core/app/router"
  7. "github.com/xtls/xray-core/common"
  8. "github.com/xtls/xray-core/common/net"
  9. "github.com/xtls/xray-core/common/session"
  10. "github.com/xtls/xray-core/features/dns"
  11. "github.com/xtls/xray-core/features/outbound"
  12. routing_session "github.com/xtls/xray-core/features/routing/session"
  13. "github.com/xtls/xray-core/testing/mocks"
  14. )
  15. type mockOutboundManager struct {
  16. outbound.Manager
  17. outbound.HandlerSelector
  18. }
  19. func TestSimpleRouter(t *testing.T) {
  20. config := &Config{
  21. Rule: []*RoutingRule{
  22. {
  23. TargetTag: &RoutingRule_Tag{
  24. Tag: "test",
  25. },
  26. Networks: []net.Network{net.Network_TCP},
  27. },
  28. },
  29. }
  30. mockCtl := gomock.NewController(t)
  31. defer mockCtl.Finish()
  32. mockDNS := mocks.NewDNSClient(mockCtl)
  33. mockOhm := mocks.NewOutboundManager(mockCtl)
  34. mockHs := mocks.NewOutboundHandlerSelector(mockCtl)
  35. r := new(Router)
  36. common.Must(r.Init(context.TODO(), config, mockDNS, &mockOutboundManager{
  37. Manager: mockOhm,
  38. HandlerSelector: mockHs,
  39. }, nil))
  40. ctx := session.ContextWithOutbounds(context.Background(), []*session.Outbound{{
  41. Target: net.TCPDestination(net.DomainAddress("example.com"), 80),
  42. }})
  43. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  44. common.Must(err)
  45. if tag := route.GetOutboundTag(); tag != "test" {
  46. t.Error("expect tag 'test', bug actually ", tag)
  47. }
  48. }
  49. func TestSimpleBalancer(t *testing.T) {
  50. config := &Config{
  51. Rule: []*RoutingRule{
  52. {
  53. TargetTag: &RoutingRule_BalancingTag{
  54. BalancingTag: "balance",
  55. },
  56. Networks: []net.Network{net.Network_TCP},
  57. },
  58. },
  59. BalancingRule: []*BalancingRule{
  60. {
  61. Tag: "balance",
  62. OutboundSelector: []string{"test-"},
  63. },
  64. },
  65. }
  66. mockCtl := gomock.NewController(t)
  67. defer mockCtl.Finish()
  68. mockDNS := mocks.NewDNSClient(mockCtl)
  69. mockOhm := mocks.NewOutboundManager(mockCtl)
  70. mockHs := mocks.NewOutboundHandlerSelector(mockCtl)
  71. mockHs.EXPECT().Select(gomock.Eq([]string{"test-"})).Return([]string{"test"})
  72. r := new(Router)
  73. common.Must(r.Init(context.TODO(), config, mockDNS, &mockOutboundManager{
  74. Manager: mockOhm,
  75. HandlerSelector: mockHs,
  76. }, nil))
  77. ctx := session.ContextWithOutbounds(context.Background(), []*session.Outbound{{
  78. Target: net.TCPDestination(net.DomainAddress("example.com"), 80),
  79. }})
  80. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  81. common.Must(err)
  82. if tag := route.GetOutboundTag(); tag != "test" {
  83. t.Error("expect tag 'test', bug actually ", tag)
  84. }
  85. }
  86. /*
  87. Do not work right now: need a full client setup
  88. func TestLeastLoadBalancer(t *testing.T) {
  89. config := &Config{
  90. Rule: []*RoutingRule{
  91. {
  92. TargetTag: &RoutingRule_BalancingTag{
  93. BalancingTag: "balance",
  94. },
  95. Networks: []net.Network{net.Network_TCP},
  96. },
  97. },
  98. BalancingRule: []*BalancingRule{
  99. {
  100. Tag: "balance",
  101. OutboundSelector: []string{"test-"},
  102. Strategy: "leastLoad",
  103. StrategySettings: serial.ToTypedMessage(&StrategyLeastLoadConfig{
  104. Baselines: nil,
  105. Expected: 1,
  106. }),
  107. },
  108. },
  109. }
  110. mockCtl := gomock.NewController(t)
  111. defer mockCtl.Finish()
  112. mockDNS := mocks.NewDNSClient(mockCtl)
  113. mockOhm := mocks.NewOutboundManager(mockCtl)
  114. mockHs := mocks.NewOutboundHandlerSelector(mockCtl)
  115. mockHs.EXPECT().Select(gomock.Eq([]string{"test-"})).Return([]string{"test1"})
  116. r := new(Router)
  117. common.Must(r.Init(context.TODO(), config, mockDNS, &mockOutboundManager{
  118. Manager: mockOhm,
  119. HandlerSelector: mockHs,
  120. }, nil))
  121. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("v2ray.com"), 80)})
  122. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  123. common.Must(err)
  124. if tag := route.GetOutboundTag(); tag != "test1" {
  125. t.Error("expect tag 'test1', bug actually ", tag)
  126. }
  127. }*/
  128. func TestIPOnDemand(t *testing.T) {
  129. config := &Config{
  130. DomainStrategy: Config_IpOnDemand,
  131. Rule: []*RoutingRule{
  132. {
  133. TargetTag: &RoutingRule_Tag{
  134. Tag: "test",
  135. },
  136. Cidr: []*CIDR{
  137. {
  138. Ip: []byte{192, 168, 0, 0},
  139. Prefix: 16,
  140. },
  141. },
  142. },
  143. },
  144. }
  145. mockCtl := gomock.NewController(t)
  146. defer mockCtl.Finish()
  147. mockDNS := mocks.NewDNSClient(mockCtl)
  148. mockDNS.EXPECT().LookupIP(gomock.Eq("example.com"), dns.IPOption{
  149. IPv4Enable: true,
  150. IPv6Enable: true,
  151. FakeEnable: false,
  152. }).Return([]net.IP{{192, 168, 0, 1}}, nil).AnyTimes()
  153. r := new(Router)
  154. common.Must(r.Init(context.TODO(), config, mockDNS, nil, nil))
  155. ctx := session.ContextWithOutbounds(context.Background(), []*session.Outbound{{
  156. Target: net.TCPDestination(net.DomainAddress("example.com"), 80),
  157. }})
  158. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  159. common.Must(err)
  160. if tag := route.GetOutboundTag(); tag != "test" {
  161. t.Error("expect tag 'test', bug actually ", tag)
  162. }
  163. }
  164. func TestIPIfNonMatchDomain(t *testing.T) {
  165. config := &Config{
  166. DomainStrategy: Config_IpIfNonMatch,
  167. Rule: []*RoutingRule{
  168. {
  169. TargetTag: &RoutingRule_Tag{
  170. Tag: "test",
  171. },
  172. Cidr: []*CIDR{
  173. {
  174. Ip: []byte{192, 168, 0, 0},
  175. Prefix: 16,
  176. },
  177. },
  178. },
  179. },
  180. }
  181. mockCtl := gomock.NewController(t)
  182. defer mockCtl.Finish()
  183. mockDNS := mocks.NewDNSClient(mockCtl)
  184. mockDNS.EXPECT().LookupIP(gomock.Eq("example.com"), dns.IPOption{
  185. IPv4Enable: true,
  186. IPv6Enable: true,
  187. FakeEnable: false,
  188. }).Return([]net.IP{{192, 168, 0, 1}}, nil).AnyTimes()
  189. r := new(Router)
  190. common.Must(r.Init(context.TODO(), config, mockDNS, nil, nil))
  191. ctx := session.ContextWithOutbounds(context.Background(), []*session.Outbound{{
  192. Target: net.TCPDestination(net.DomainAddress("example.com"), 80),
  193. }})
  194. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  195. common.Must(err)
  196. if tag := route.GetOutboundTag(); tag != "test" {
  197. t.Error("expect tag 'test', bug actually ", tag)
  198. }
  199. }
  200. func TestIPIfNonMatchIP(t *testing.T) {
  201. config := &Config{
  202. DomainStrategy: Config_IpIfNonMatch,
  203. Rule: []*RoutingRule{
  204. {
  205. TargetTag: &RoutingRule_Tag{
  206. Tag: "test",
  207. },
  208. Cidr: []*CIDR{
  209. {
  210. Ip: []byte{127, 0, 0, 0},
  211. Prefix: 8,
  212. },
  213. },
  214. },
  215. },
  216. }
  217. mockCtl := gomock.NewController(t)
  218. defer mockCtl.Finish()
  219. mockDNS := mocks.NewDNSClient(mockCtl)
  220. r := new(Router)
  221. common.Must(r.Init(context.TODO(), config, mockDNS, nil, nil))
  222. ctx := session.ContextWithOutbounds(context.Background(), []*session.Outbound{{
  223. Target: net.TCPDestination(net.LocalHostIP, 80),
  224. }})
  225. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  226. common.Must(err)
  227. if tag := route.GetOutboundTag(); tag != "test" {
  228. t.Error("expect tag 'test', bug actually ", tag)
  229. }
  230. }