router_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package conf_test
  2. import (
  3. "encoding/json"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. _ "unsafe"
  9. "github.com/xtls/xray-core/app/router"
  10. "github.com/xtls/xray-core/common"
  11. "github.com/xtls/xray-core/common/net"
  12. "github.com/xtls/xray-core/common/platform"
  13. "github.com/xtls/xray-core/common/platform/filesystem"
  14. "github.com/xtls/xray-core/common/serial"
  15. . "github.com/xtls/xray-core/infra/conf"
  16. "google.golang.org/protobuf/proto"
  17. )
  18. func init() {
  19. wd, err := os.Getwd()
  20. common.Must(err)
  21. if _, err := os.Stat(platform.GetAssetLocation("geoip.dat")); err != nil && os.IsNotExist(err) {
  22. common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "resources", "geoip.dat")))
  23. }
  24. os.Setenv("xray.location.asset", wd)
  25. }
  26. func TestToCidrList(t *testing.T) {
  27. t.Log(os.Getenv("xray.location.asset"))
  28. common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoiptestrouter.dat"), "geoip.dat"))
  29. ips := StringList([]string{
  30. "geoip:us",
  31. "geoip:cn",
  32. "geoip:!cn",
  33. "ext:geoiptestrouter.dat:!cn",
  34. "ext:geoiptestrouter.dat:ca",
  35. "ext-ip:geoiptestrouter.dat:!cn",
  36. "ext-ip:geoiptestrouter.dat:!ca",
  37. })
  38. _, err := ToCidrList(ips)
  39. if err != nil {
  40. t.Fatalf("Failed to parse geoip list, got %s", err)
  41. }
  42. }
  43. func TestRouterConfig(t *testing.T) {
  44. createParser := func() func(string) (proto.Message, error) {
  45. return func(s string) (proto.Message, error) {
  46. config := new(RouterConfig)
  47. if err := json.Unmarshal([]byte(s), config); err != nil {
  48. return nil, err
  49. }
  50. return config.Build()
  51. }
  52. }
  53. runMultiTestCase(t, []TestCase{
  54. {
  55. Input: `{
  56. "strategy": "rules",
  57. "settings": {
  58. "domainStrategy": "AsIs",
  59. "rules": [
  60. {
  61. "type": "field",
  62. "domain": [
  63. "baidu.com",
  64. "qq.com"
  65. ],
  66. "outboundTag": "direct"
  67. },
  68. {
  69. "type": "field",
  70. "ip": [
  71. "10.0.0.0/8",
  72. "::1/128"
  73. ],
  74. "outboundTag": "test"
  75. },{
  76. "type": "field",
  77. "port": "53, 443, 1000-2000",
  78. "outboundTag": "test"
  79. },{
  80. "type": "field",
  81. "port": 123,
  82. "outboundTag": "test"
  83. }
  84. ]
  85. },
  86. "balancers": [
  87. {
  88. "tag": "b1",
  89. "selector": ["test"]
  90. },
  91. {
  92. "tag": "b2",
  93. "selector": ["test"],
  94. "strategy": {
  95. "type": "leastload",
  96. "settings": {
  97. "healthCheck": {
  98. "interval": "5m0s",
  99. "sampling": 2,
  100. "timeout": "5s",
  101. "destination": "dest",
  102. "connectivity": "conn"
  103. },
  104. "costs": [
  105. {
  106. "regexp": true,
  107. "match": "\\d+(\\.\\d+)",
  108. "value": 5
  109. }
  110. ],
  111. "baselines": ["400ms", "600ms"],
  112. "expected": 6,
  113. "maxRTT": "1000ms",
  114. "tolerance": 0.5
  115. }
  116. },
  117. "fallbackTag": "fall"
  118. }
  119. ]
  120. }`,
  121. Parser: createParser(),
  122. Output: &router.Config{
  123. DomainStrategy: router.Config_AsIs,
  124. BalancingRule: []*router.BalancingRule{
  125. {
  126. Tag: "b1",
  127. OutboundSelector: []string{"test"},
  128. Strategy: "random",
  129. },
  130. {
  131. Tag: "b2",
  132. OutboundSelector: []string{"test"},
  133. Strategy: "leastload",
  134. StrategySettings: serial.ToTypedMessage(&router.StrategyLeastLoadConfig{
  135. Costs: []*router.StrategyWeight{
  136. {
  137. Regexp: true,
  138. Match: "\\d+(\\.\\d+)",
  139. Value: 5,
  140. },
  141. },
  142. Baselines: []int64{
  143. int64(time.Duration(400) * time.Millisecond),
  144. int64(time.Duration(600) * time.Millisecond),
  145. },
  146. Expected: 6,
  147. MaxRTT: int64(time.Duration(1000) * time.Millisecond),
  148. Tolerance: 0.5,
  149. }),
  150. FallbackTag: "fall",
  151. },
  152. },
  153. Rule: []*router.RoutingRule{
  154. {
  155. Domain: []*router.Domain{
  156. {
  157. Type: router.Domain_Plain,
  158. Value: "baidu.com",
  159. },
  160. {
  161. Type: router.Domain_Plain,
  162. Value: "qq.com",
  163. },
  164. },
  165. TargetTag: &router.RoutingRule_Tag{
  166. Tag: "direct",
  167. },
  168. },
  169. {
  170. Geoip: []*router.GeoIP{
  171. {
  172. Cidr: []*router.CIDR{
  173. {
  174. Ip: []byte{10, 0, 0, 0},
  175. Prefix: 8,
  176. },
  177. {
  178. Ip: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  179. Prefix: 128,
  180. },
  181. },
  182. },
  183. },
  184. TargetTag: &router.RoutingRule_Tag{
  185. Tag: "test",
  186. },
  187. },
  188. {
  189. PortList: &net.PortList{
  190. Range: []*net.PortRange{
  191. {From: 53, To: 53},
  192. {From: 443, To: 443},
  193. {From: 1000, To: 2000},
  194. },
  195. },
  196. TargetTag: &router.RoutingRule_Tag{
  197. Tag: "test",
  198. },
  199. },
  200. {
  201. PortList: &net.PortList{
  202. Range: []*net.PortRange{
  203. {From: 123, To: 123},
  204. },
  205. },
  206. TargetTag: &router.RoutingRule_Tag{
  207. Tag: "test",
  208. },
  209. },
  210. },
  211. },
  212. },
  213. {
  214. Input: `{
  215. "strategy": "rules",
  216. "settings": {
  217. "domainStrategy": "IPIfNonMatch",
  218. "rules": [
  219. {
  220. "type": "field",
  221. "domain": [
  222. "baidu.com",
  223. "qq.com"
  224. ],
  225. "outboundTag": "direct"
  226. },
  227. {
  228. "type": "field",
  229. "ip": [
  230. "10.0.0.0/8",
  231. "::1/128"
  232. ],
  233. "outboundTag": "test"
  234. }
  235. ]
  236. }
  237. }`,
  238. Parser: createParser(),
  239. Output: &router.Config{
  240. DomainStrategy: router.Config_IpIfNonMatch,
  241. Rule: []*router.RoutingRule{
  242. {
  243. Domain: []*router.Domain{
  244. {
  245. Type: router.Domain_Plain,
  246. Value: "baidu.com",
  247. },
  248. {
  249. Type: router.Domain_Plain,
  250. Value: "qq.com",
  251. },
  252. },
  253. TargetTag: &router.RoutingRule_Tag{
  254. Tag: "direct",
  255. },
  256. },
  257. {
  258. Geoip: []*router.GeoIP{
  259. {
  260. Cidr: []*router.CIDR{
  261. {
  262. Ip: []byte{10, 0, 0, 0},
  263. Prefix: 8,
  264. },
  265. {
  266. Ip: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  267. Prefix: 128,
  268. },
  269. },
  270. },
  271. },
  272. TargetTag: &router.RoutingRule_Tag{
  273. Tag: "test",
  274. },
  275. },
  276. },
  277. },
  278. },
  279. {
  280. Input: `{
  281. "domainStrategy": "AsIs",
  282. "rules": [
  283. {
  284. "type": "field",
  285. "domain": [
  286. "baidu.com",
  287. "qq.com"
  288. ],
  289. "outboundTag": "direct"
  290. },
  291. {
  292. "type": "field",
  293. "ip": [
  294. "10.0.0.0/8",
  295. "::1/128"
  296. ],
  297. "outboundTag": "test"
  298. }
  299. ]
  300. }`,
  301. Parser: createParser(),
  302. Output: &router.Config{
  303. DomainStrategy: router.Config_AsIs,
  304. Rule: []*router.RoutingRule{
  305. {
  306. Domain: []*router.Domain{
  307. {
  308. Type: router.Domain_Plain,
  309. Value: "baidu.com",
  310. },
  311. {
  312. Type: router.Domain_Plain,
  313. Value: "qq.com",
  314. },
  315. },
  316. TargetTag: &router.RoutingRule_Tag{
  317. Tag: "direct",
  318. },
  319. },
  320. {
  321. Geoip: []*router.GeoIP{
  322. {
  323. Cidr: []*router.CIDR{
  324. {
  325. Ip: []byte{10, 0, 0, 0},
  326. Prefix: 8,
  327. },
  328. {
  329. Ip: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  330. Prefix: 128,
  331. },
  332. },
  333. },
  334. },
  335. TargetTag: &router.RoutingRule_Tag{
  336. Tag: "test",
  337. },
  338. },
  339. },
  340. },
  341. },
  342. })
  343. }