router_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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(config, mockDNS, &mockOutboundManager{
  37. Manager: mockOhm,
  38. HandlerSelector: mockHs,
  39. }))
  40. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("example.com"), 80)})
  41. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  42. common.Must(err)
  43. if tag := route.GetOutboundTag(); tag != "test" {
  44. t.Error("expect tag 'test', bug actually ", tag)
  45. }
  46. }
  47. func TestSimpleBalancer(t *testing.T) {
  48. config := &Config{
  49. Rule: []*RoutingRule{
  50. {
  51. TargetTag: &RoutingRule_BalancingTag{
  52. BalancingTag: "balance",
  53. },
  54. Networks: []net.Network{net.Network_TCP},
  55. },
  56. },
  57. BalancingRule: []*BalancingRule{
  58. {
  59. Tag: "balance",
  60. OutboundSelector: []string{"test-"},
  61. },
  62. },
  63. }
  64. mockCtl := gomock.NewController(t)
  65. defer mockCtl.Finish()
  66. mockDNS := mocks.NewDNSClient(mockCtl)
  67. mockOhm := mocks.NewOutboundManager(mockCtl)
  68. mockHs := mocks.NewOutboundHandlerSelector(mockCtl)
  69. mockHs.EXPECT().Select(gomock.Eq([]string{"test-"})).Return([]string{"test"})
  70. r := new(Router)
  71. common.Must(r.Init(config, mockDNS, &mockOutboundManager{
  72. Manager: mockOhm,
  73. HandlerSelector: mockHs,
  74. }))
  75. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("example.com"), 80)})
  76. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  77. common.Must(err)
  78. if tag := route.GetOutboundTag(); tag != "test" {
  79. t.Error("expect tag 'test', bug actually ", tag)
  80. }
  81. }
  82. func TestIPOnDemand(t *testing.T) {
  83. config := &Config{
  84. DomainStrategy: Config_IpOnDemand,
  85. Rule: []*RoutingRule{
  86. {
  87. TargetTag: &RoutingRule_Tag{
  88. Tag: "test",
  89. },
  90. Cidr: []*CIDR{
  91. {
  92. Ip: []byte{192, 168, 0, 0},
  93. Prefix: 16,
  94. },
  95. },
  96. },
  97. },
  98. }
  99. mockCtl := gomock.NewController(t)
  100. defer mockCtl.Finish()
  101. mockDNS := mocks.NewDNSClient(mockCtl)
  102. mockDNS.EXPECT().LookupIP(gomock.Eq("example.com"), dns.IPOption{
  103. IPv4Enable: true,
  104. IPv6Enable: true,
  105. FakeEnable: false,
  106. }).Return([]net.IP{{192, 168, 0, 1}}, nil).AnyTimes()
  107. r := new(Router)
  108. common.Must(r.Init(config, mockDNS, nil))
  109. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("example.com"), 80)})
  110. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  111. common.Must(err)
  112. if tag := route.GetOutboundTag(); tag != "test" {
  113. t.Error("expect tag 'test', bug actually ", tag)
  114. }
  115. }
  116. func TestIPIfNonMatchDomain(t *testing.T) {
  117. config := &Config{
  118. DomainStrategy: Config_IpIfNonMatch,
  119. Rule: []*RoutingRule{
  120. {
  121. TargetTag: &RoutingRule_Tag{
  122. Tag: "test",
  123. },
  124. Cidr: []*CIDR{
  125. {
  126. Ip: []byte{192, 168, 0, 0},
  127. Prefix: 16,
  128. },
  129. },
  130. },
  131. },
  132. }
  133. mockCtl := gomock.NewController(t)
  134. defer mockCtl.Finish()
  135. mockDNS := mocks.NewDNSClient(mockCtl)
  136. mockDNS.EXPECT().LookupIP(gomock.Eq("example.com"), dns.IPOption{
  137. IPv4Enable: true,
  138. IPv6Enable: true,
  139. FakeEnable: false,
  140. }).Return([]net.IP{{192, 168, 0, 1}}, nil).AnyTimes()
  141. r := new(Router)
  142. common.Must(r.Init(config, mockDNS, nil))
  143. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.DomainAddress("example.com"), 80)})
  144. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  145. common.Must(err)
  146. if tag := route.GetOutboundTag(); tag != "test" {
  147. t.Error("expect tag 'test', bug actually ", tag)
  148. }
  149. }
  150. func TestIPIfNonMatchIP(t *testing.T) {
  151. config := &Config{
  152. DomainStrategy: Config_IpIfNonMatch,
  153. Rule: []*RoutingRule{
  154. {
  155. TargetTag: &RoutingRule_Tag{
  156. Tag: "test",
  157. },
  158. Cidr: []*CIDR{
  159. {
  160. Ip: []byte{127, 0, 0, 0},
  161. Prefix: 8,
  162. },
  163. },
  164. },
  165. },
  166. }
  167. mockCtl := gomock.NewController(t)
  168. defer mockCtl.Finish()
  169. mockDNS := mocks.NewDNSClient(mockCtl)
  170. r := new(Router)
  171. common.Must(r.Init(config, mockDNS, nil))
  172. ctx := session.ContextWithOutbound(context.Background(), &session.Outbound{Target: net.TCPDestination(net.LocalHostIP, 80)})
  173. route, err := r.PickRoute(routing_session.AsRoutingContext(ctx))
  174. common.Must(err)
  175. if tag := route.GetOutboundTag(); tag != "test" {
  176. t.Error("expect tag 'test', bug actually ", tag)
  177. }
  178. }