router_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. Geoip: []*GeoIP{
  137. {
  138. Cidr: []*CIDR{
  139. {
  140. Ip: []byte{192, 168, 0, 0},
  141. Prefix: 16,
  142. },
  143. },
  144. },
  145. },
  146. },
  147. },
  148. }
  149. mockCtl := gomock.NewController(t)
  150. defer mockCtl.Finish()
  151. mockDNS := mocks.NewDNSClient(mockCtl)
  152. mockDNS.EXPECT().LookupIP(gomock.Eq("example.com"), dns.IPOption{
  153. IPv4Enable: true,
  154. IPv6Enable: true,
  155. FakeEnable: false,
  156. }).Return([]net.IP{{192, 168, 0, 1}}, nil).AnyTimes()
  157. r := new(Router)
  158. common.Must(r.Init(context.TODO(), config, mockDNS, nil, nil))
  159. ctx := session.ContextWithOutbounds(context.Background(), []*session.Outbound{{
  160. Target: net.TCPDestination(net.DomainAddress("example.com"), 80),
  161. }})
  162. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  163. common.Must(err)
  164. if tag := route.GetOutboundTag(); tag != "test" {
  165. t.Error("expect tag 'test', bug actually ", tag)
  166. }
  167. }
  168. func TestIPIfNonMatchDomain(t *testing.T) {
  169. config := &Config{
  170. DomainStrategy: Config_IpIfNonMatch,
  171. Rule: []*RoutingRule{
  172. {
  173. TargetTag: &RoutingRule_Tag{
  174. Tag: "test",
  175. },
  176. Geoip: []*GeoIP{
  177. {
  178. Cidr: []*CIDR{
  179. {
  180. Ip: []byte{192, 168, 0, 0},
  181. Prefix: 16,
  182. },
  183. },
  184. },
  185. },
  186. },
  187. },
  188. }
  189. mockCtl := gomock.NewController(t)
  190. defer mockCtl.Finish()
  191. mockDNS := mocks.NewDNSClient(mockCtl)
  192. mockDNS.EXPECT().LookupIP(gomock.Eq("example.com"), dns.IPOption{
  193. IPv4Enable: true,
  194. IPv6Enable: true,
  195. FakeEnable: false,
  196. }).Return([]net.IP{{192, 168, 0, 1}}, nil).AnyTimes()
  197. r := new(Router)
  198. common.Must(r.Init(context.TODO(), config, mockDNS, nil, nil))
  199. ctx := session.ContextWithOutbounds(context.Background(), []*session.Outbound{{
  200. Target: net.TCPDestination(net.DomainAddress("example.com"), 80),
  201. }})
  202. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  203. common.Must(err)
  204. if tag := route.GetOutboundTag(); tag != "test" {
  205. t.Error("expect tag 'test', bug actually ", tag)
  206. }
  207. }
  208. func TestIPIfNonMatchIP(t *testing.T) {
  209. config := &Config{
  210. DomainStrategy: Config_IpIfNonMatch,
  211. Rule: []*RoutingRule{
  212. {
  213. TargetTag: &RoutingRule_Tag{
  214. Tag: "test",
  215. },
  216. Geoip: []*GeoIP{
  217. {
  218. Cidr: []*CIDR{
  219. {
  220. Ip: []byte{127, 0, 0, 0},
  221. Prefix: 8,
  222. },
  223. },
  224. },
  225. },
  226. },
  227. },
  228. }
  229. mockCtl := gomock.NewController(t)
  230. defer mockCtl.Finish()
  231. mockDNS := mocks.NewDNSClient(mockCtl)
  232. r := new(Router)
  233. common.Must(r.Init(context.TODO(), config, mockDNS, nil, nil))
  234. ctx := session.ContextWithOutbounds(context.Background(), []*session.Outbound{{
  235. Target: net.TCPDestination(net.LocalHostIP, 80),
  236. }})
  237. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  238. common.Must(err)
  239. if tag := route.GetOutboundTag(); tag != "test" {
  240. t.Error("expect tag 'test', bug actually ", tag)
  241. }
  242. }