xray_setting_routing_sync_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package service
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. )
  8. func seedXrayTemplate(t *testing.T, template string) {
  9. t.Helper()
  10. s := &SettingService{}
  11. if err := s.saveSetting("xrayTemplateConfig", template); err != nil {
  12. t.Fatalf("saveSetting: %v", err)
  13. }
  14. }
  15. func routingRulesFromTemplate(t *testing.T, template string) []map[string]any {
  16. t.Helper()
  17. var cfg map[string]any
  18. if err := json.Unmarshal([]byte(template), &cfg); err != nil {
  19. t.Fatalf("unmarshal template: %v", err)
  20. }
  21. return routingRulesFromCfg(cfg)
  22. }
  23. func TestPropagateInboundTagRename_UpdatesRoutingRule(t *testing.T) {
  24. setupSettingTestDB(t)
  25. seedXrayTemplate(t, `{
  26. "routing": {
  27. "rules": [
  28. {"type":"field","inboundTag":["in-21368-tcp"],"outboundTag":"direct"},
  29. {"type":"field","inboundTag":["api"],"outboundTag":"api"}
  30. ]
  31. },
  32. "outbounds": [{"tag":"direct","protocol":"freedom"}]
  33. }`)
  34. svc := &XraySettingService{}
  35. changed, err := svc.PropagateInboundTagRename("in-21368-tcp", "in-33000-tcp")
  36. if err != nil {
  37. t.Fatalf("PropagateInboundTagRename: %v", err)
  38. }
  39. if !changed {
  40. t.Fatal("expected routing template to change")
  41. }
  42. got, err := svc.GetXrayConfigTemplate()
  43. if err != nil {
  44. t.Fatalf("GetXrayConfigTemplate: %v", err)
  45. }
  46. rules := routingRulesFromTemplate(t, got)
  47. if len(rules) != 2 {
  48. t.Fatalf("rules len = %d, want 2", len(rules))
  49. }
  50. if tags := readInboundTags(rules[1]["inboundTag"]); tags[0] != "in-33000-tcp" {
  51. t.Fatalf("renamed rule inboundTag = %v, want [in-33000-tcp]", tags)
  52. }
  53. if tags := readInboundTags(rules[0]["inboundTag"]); tags[0] != "api" {
  54. t.Fatalf("api rule should stay untouched, got %v", tags)
  55. }
  56. }
  57. func TestPropagateInboundTagRename_UpdatesLoopbackOutbound(t *testing.T) {
  58. setupSettingTestDB(t)
  59. seedXrayTemplate(t, `{
  60. "routing": {"rules": []},
  61. "outbounds": [
  62. {"tag":"loop","protocol":"loopback","settings":{"inboundTag":"in-21368-tcp"}}
  63. ]
  64. }`)
  65. svc := &XraySettingService{}
  66. if _, err := svc.PropagateInboundTagRename("in-21368-tcp", "in-33000-tcp"); err != nil {
  67. t.Fatalf("PropagateInboundTagRename: %v", err)
  68. }
  69. got, err := svc.GetXrayConfigTemplate()
  70. if err != nil {
  71. t.Fatalf("GetXrayConfigTemplate: %v", err)
  72. }
  73. var cfg map[string]any
  74. if err := json.Unmarshal([]byte(got), &cfg); err != nil {
  75. t.Fatalf("unmarshal: %v", err)
  76. }
  77. outbounds := outboundsFromCfg(cfg)
  78. settings := outbounds[0].(map[string]any)["settings"].(map[string]any)
  79. if settings["inboundTag"] != "in-33000-tcp" {
  80. t.Fatalf("loopback inboundTag = %v, want in-33000-tcp", settings["inboundTag"])
  81. }
  82. }
  83. func TestRemoveInboundTagReferences_DropsInboundOnlyRule(t *testing.T) {
  84. setupSettingTestDB(t)
  85. seedXrayTemplate(t, `{
  86. "routing": {
  87. "rules": [
  88. {"type":"field","inboundTag":["in-21368-tcp"],"outboundTag":"direct"},
  89. {"type":"field","inboundTag":["api"],"outboundTag":"api"}
  90. ]
  91. }
  92. }`)
  93. svc := &XraySettingService{}
  94. changed, err := svc.RemoveInboundTagReferences("in-21368-tcp")
  95. if err != nil {
  96. t.Fatalf("RemoveInboundTagReferences: %v", err)
  97. }
  98. if !changed {
  99. t.Fatal("expected template to change")
  100. }
  101. got, err := svc.GetXrayConfigTemplate()
  102. if err != nil {
  103. t.Fatalf("GetXrayConfigTemplate: %v", err)
  104. }
  105. rules := routingRulesFromTemplate(t, got)
  106. if len(rules) != 1 {
  107. t.Fatalf("rules len = %d, want 1 (api rule only)", len(rules))
  108. }
  109. if tags := readInboundTags(rules[0]["inboundTag"]); tags[0] != "api" {
  110. t.Fatalf("remaining rule = %v, want api rule", tags)
  111. }
  112. }
  113. func TestRemoveInboundTagReferences_KeepsRuleWithOtherMatchers(t *testing.T) {
  114. setupSettingTestDB(t)
  115. seedXrayTemplate(t, `{
  116. "routing": {
  117. "rules": [
  118. {"type":"field","inboundTag":["api"],"outboundTag":"api"},
  119. {
  120. "type":"field",
  121. "inboundTag":["in-21368-tcp"],
  122. "domain":["example.com"],
  123. "outboundTag":"direct"
  124. }
  125. ]
  126. }
  127. }`)
  128. svc := &XraySettingService{}
  129. if _, err := svc.RemoveInboundTagReferences("in-21368-tcp"); err != nil {
  130. t.Fatalf("RemoveInboundTagReferences: %v", err)
  131. }
  132. got, err := svc.GetXrayConfigTemplate()
  133. if err != nil {
  134. t.Fatalf("GetXrayConfigTemplate: %v", err)
  135. }
  136. rule := findRuleByOutbound(t, got, "direct")
  137. if _, ok := rule["inboundTag"]; ok {
  138. t.Fatalf("inboundTag should be removed, rule = %#v", rule)
  139. }
  140. if domain, _ := rule["domain"].([]any); len(domain) != 1 {
  141. t.Fatalf("domain matcher should remain, rule = %#v", rule)
  142. }
  143. }
  144. func TestRemoveInboundTagReferences_KeepsSourceScopedRule(t *testing.T) {
  145. setupSettingTestDB(t)
  146. seedXrayTemplate(t, `{
  147. "routing": {
  148. "rules": [
  149. {
  150. "type":"field",
  151. "inboundTag":["in-443-tcp"],
  152. "source":["10.0.0.0/8"],
  153. "outboundTag":"blocked"
  154. }
  155. ]
  156. }
  157. }`)
  158. svc := &XraySettingService{}
  159. if _, err := svc.RemoveInboundTagReferences("in-443-tcp"); err != nil {
  160. t.Fatalf("RemoveInboundTagReferences: %v", err)
  161. }
  162. got, err := svc.GetXrayConfigTemplate()
  163. if err != nil {
  164. t.Fatalf("GetXrayConfigTemplate: %v", err)
  165. }
  166. rule := findRuleByOutbound(t, got, "blocked")
  167. if _, ok := rule["inboundTag"]; ok {
  168. t.Fatalf("inboundTag should be trimmed, rule = %#v", rule)
  169. }
  170. if src, _ := rule["source"].([]any); len(src) != 1 {
  171. t.Fatalf("source-scoped rule was dropped instead of kept; rule = %#v", rule)
  172. }
  173. }
  174. func TestRemoveInboundTagReferences_RemovesOneTagFromMultiInboundRule(t *testing.T) {
  175. setupSettingTestDB(t)
  176. seedXrayTemplate(t, `{
  177. "routing": {
  178. "rules": [
  179. {"type":"field","inboundTag":["api"],"outboundTag":"api"},
  180. {
  181. "type":"field",
  182. "inboundTag":["in-21368-tcp","in-443-tcp"],
  183. "outboundTag":"direct"
  184. }
  185. ]
  186. }
  187. }`)
  188. svc := &XraySettingService{}
  189. if _, err := svc.RemoveInboundTagReferences("in-21368-tcp"); err != nil {
  190. t.Fatalf("RemoveInboundTagReferences: %v", err)
  191. }
  192. got, err := svc.GetXrayConfigTemplate()
  193. if err != nil {
  194. t.Fatalf("GetXrayConfigTemplate: %v", err)
  195. }
  196. rule := findRuleByOutbound(t, got, "direct")
  197. if tags := readInboundTags(rule["inboundTag"]); len(tags) != 1 || tags[0] != "in-443-tcp" {
  198. t.Fatalf("inboundTag = %v, want [in-443-tcp]", tags)
  199. }
  200. }
  201. // The core lowercases a protocol id before resolving the handler, so "Loopback"
  202. // is the loopback outbound whose inboundTag has to follow the inbound it names.
  203. func TestReplaceInboundTagInOutbounds_ReadsTheProtocolIDLikeTheCore(t *testing.T) {
  204. tests := []struct {
  205. name string
  206. protocol any
  207. want bool
  208. wantTag any
  209. }{
  210. {"canonical loopback is rewritten", "loopback", true, "new-tag"},
  211. {"capitalised loopback is rewritten", "Loopback", true, "new-tag"},
  212. {"uppercase loopback is rewritten", "LOOPBACK", true, "new-tag"},
  213. {"another protocol keeps its tag", "vmess", false, "old-tag"},
  214. }
  215. for _, tt := range tests {
  216. t.Run(tt.name, func(t *testing.T) {
  217. outbounds := []any{map[string]any{
  218. "protocol": tt.protocol,
  219. "settings": map[string]any{"inboundTag": "old-tag"},
  220. }}
  221. if got := replaceInboundTagInOutbounds(outbounds, "old-tag", "new-tag"); got != tt.want {
  222. t.Errorf("changed = %v, want %v", got, tt.want)
  223. }
  224. settings := outbounds[0].(map[string]any)["settings"].(map[string]any)
  225. if got := settings["inboundTag"]; got != tt.wantTag {
  226. t.Errorf("inboundTag = %v, want %v", got, tt.wantTag)
  227. }
  228. })
  229. }
  230. }
  231. func TestRemoveInboundTagFromOutbounds_ReadsTheProtocolIDLikeTheCore(t *testing.T) {
  232. tests := []struct {
  233. name string
  234. protocol any
  235. want bool
  236. wantTag any
  237. }{
  238. {"canonical loopback is cleared", "loopback", true, nil},
  239. {"capitalised loopback is cleared", "Loopback", true, nil},
  240. {"uppercase loopback is cleared", "LOOPBACK", true, nil},
  241. {"another protocol keeps its tag", "vmess", false, "gone-tag"},
  242. }
  243. for _, tt := range tests {
  244. t.Run(tt.name, func(t *testing.T) {
  245. outbounds := []any{map[string]any{
  246. "protocol": tt.protocol,
  247. "settings": map[string]any{"inboundTag": "gone-tag"},
  248. }}
  249. if got := removeInboundTagFromOutbounds(outbounds, "gone-tag"); got != tt.want {
  250. t.Errorf("changed = %v, want %v", got, tt.want)
  251. }
  252. settings := outbounds[0].(map[string]any)["settings"].(map[string]any)
  253. got, ok := settings["inboundTag"]
  254. if tt.wantTag == nil {
  255. if ok {
  256. t.Errorf("inboundTag = %v, want the key gone", got)
  257. }
  258. return
  259. }
  260. if !ok || got != tt.wantTag {
  261. t.Errorf("inboundTag = %v (present=%v), want %v", got, ok, tt.wantTag)
  262. }
  263. })
  264. }
  265. }
  266. func findRuleByOutbound(t *testing.T, template, outbound string) map[string]any {
  267. t.Helper()
  268. for _, rule := range routingRulesFromTemplate(t, template) {
  269. if rule["outboundTag"] == outbound {
  270. return rule
  271. }
  272. }
  273. t.Fatalf("no rule with outboundTag %q in %s", outbound, template)
  274. return nil
  275. }
  276. func TestPropagateInboundTagRename_WorksWithConflictDB(t *testing.T) {
  277. setupConflictDB(t)
  278. seedXrayTemplate(t, `{
  279. "routing": {
  280. "rules": [
  281. {"type":"field","inboundTag":["in-22435-tcp"],"outboundTag":"direct"}
  282. ]
  283. },
  284. "outbounds": [{"tag":"direct","protocol":"freedom"}]
  285. }`)
  286. svc := &XraySettingService{}
  287. changed, err := svc.PropagateInboundTagRename("in-22435-tcp", "in-33000-tcp")
  288. if err != nil {
  289. t.Fatalf("PropagateInboundTagRename: %v", err)
  290. }
  291. if !changed {
  292. t.Fatal("expected template to change")
  293. }
  294. }
  295. func TestUpdateInbound_PropagatesRoutingRuleOnPortChange(t *testing.T) {
  296. setupConflictDB(t)
  297. seedXrayTemplate(t, `{
  298. "routing": {
  299. "rules": [
  300. {"type":"field","inboundTag":["api"],"outboundTag":"api"},
  301. {"type":"field","inboundTag":["in-22435-tcp"],"outboundTag":"direct"}
  302. ]
  303. },
  304. "outbounds": [{"tag":"direct","protocol":"freedom"}]
  305. }`)
  306. seedInboundConflict(t, "in-22435-tcp", "0.0.0.0", 22435, model.VLESS, `{"network":"tcp"}`, `{"clients":[]}`)
  307. var existing model.Inbound
  308. if err := database.GetDB().Where("tag = ?", "in-22435-tcp").First(&existing).Error; err != nil {
  309. t.Fatalf("read seeded row: %v", err)
  310. }
  311. svc := &InboundService{}
  312. update := existing
  313. update.Port = 33000
  314. update.Tag = "in-22435-tcp"
  315. got, needRestart, err := svc.UpdateInbound(&update)
  316. if err != nil {
  317. t.Fatalf("UpdateInbound: %v", err)
  318. }
  319. if got.Tag != "in-33000-tcp" {
  320. t.Fatalf("returned tag = %q, want in-33000-tcp", got.Tag)
  321. }
  322. if !needRestart {
  323. t.Fatal("expected needRestart after routing template sync on tag rename")
  324. }
  325. xraySvc := &XraySettingService{}
  326. template, err := xraySvc.GetXrayConfigTemplate()
  327. if err != nil {
  328. t.Fatalf("GetXrayConfigTemplate: %v", err)
  329. }
  330. rule := findRuleByOutbound(t, template, "direct")
  331. if tags := readInboundTags(rule["inboundTag"]); tags[0] != "in-33000-tcp" {
  332. t.Fatalf("routing inboundTag = %v, want [in-33000-tcp]", tags)
  333. }
  334. }
  335. func TestDelInbound_RemovesInboundOnlyRoutingRule(t *testing.T) {
  336. setupConflictDB(t)
  337. seedXrayTemplate(t, `{
  338. "routing": {
  339. "rules": [
  340. {"type":"field","inboundTag":["api"],"outboundTag":"api"},
  341. {"type":"field","inboundTag":["in-22435-tcp"],"outboundTag":"direct"},
  342. {"type":"field","inboundTag":["in-443-tcp"],"outboundTag":"blocked"}
  343. ]
  344. }
  345. }`)
  346. seedInboundConflict(t, "in-22435-tcp", "0.0.0.0", 22435, model.VLESS, `{"network":"tcp"}`, `{"clients":[]}`)
  347. var existing model.Inbound
  348. if err := database.GetDB().Where("tag = ?", "in-22435-tcp").First(&existing).Error; err != nil {
  349. t.Fatalf("read seeded row: %v", err)
  350. }
  351. svc := &InboundService{}
  352. if _, err := svc.DelInbound(existing.Id); err != nil {
  353. t.Fatalf("DelInbound: %v", err)
  354. }
  355. xraySvc := &XraySettingService{}
  356. template, err := xraySvc.GetXrayConfigTemplate()
  357. if err != nil {
  358. t.Fatalf("GetXrayConfigTemplate: %v", err)
  359. }
  360. rules := routingRulesFromTemplate(t, template)
  361. for _, rule := range rules {
  362. if rule["outboundTag"] == "direct" {
  363. t.Fatalf("direct rule should be removed, got %#v", rule)
  364. }
  365. }
  366. findRuleByOutbound(t, template, "blocked")
  367. }