host_sub_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package sub
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. "testing"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  9. )
  10. func seedSubDB(t *testing.T) {
  11. t.Helper()
  12. dbDir := t.TempDir()
  13. t.Setenv("XUI_DB_FOLDER", dbDir)
  14. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  15. t.Fatalf("InitDB: %v", err)
  16. }
  17. t.Cleanup(func() { _ = database.CloseDB() })
  18. }
  19. // seedSubInbound creates a VLESS inbound with one client wired into the
  20. // normalized clients/client_inbounds tables so getInboundsBySubId resolves it.
  21. func seedSubInbound(t *testing.T, subId, tag string, port, subSortIndex int, stream string) *model.Inbound {
  22. t.Helper()
  23. db := database.GetDB()
  24. uuid := "11111111-2222-4333-8444-" + fmt.Sprintf("%012d", port)
  25. email := tag + "@e"
  26. settings := fmt.Sprintf(`{"clients":[{"id":%q,"email":%q,"subId":%q,"enable":true}],"decryption":"none"}`, uuid, email, subId)
  27. ib := &model.Inbound{
  28. UserId: 1, Tag: tag, Enable: true, Listen: "203.0.113.5", Port: port,
  29. Protocol: model.VLESS, Remark: tag, Settings: settings, StreamSettings: stream,
  30. SubSortIndex: subSortIndex,
  31. }
  32. if err := db.Create(ib).Error; err != nil {
  33. t.Fatalf("seed inbound %s: %v", tag, err)
  34. }
  35. client := &model.ClientRecord{Email: email, SubID: subId, UUID: uuid, Enable: true}
  36. if err := db.Create(client).Error; err != nil {
  37. t.Fatalf("seed client %s: %v", email, err)
  38. }
  39. if err := db.Create(&model.ClientInbound{ClientId: client.Id, InboundId: ib.Id}).Error; err != nil {
  40. t.Fatalf("seed client_inbound %s: %v", email, err)
  41. }
  42. return ib
  43. }
  44. func seedHost(t *testing.T, h *model.Host) *model.Host {
  45. t.Helper()
  46. if err := database.GetDB().Create(h).Error; err != nil {
  47. t.Fatalf("seed host: %v", err)
  48. }
  49. return h
  50. }
  51. const wsTLSStream = `{"network":"ws","security":"tls","wsSettings":{"path":"/base","host":"base.host"},"tlsSettings":{"serverName":"base.sni"}}`
  52. // #1 — an inbound with no hosts renders identically to the legacy path: a single
  53. // link from the inbound's own address. Mutation-checks the zero-hosts fallback.
  54. func TestSub_ZeroHosts_IdenticalOutput(t *testing.T) {
  55. seedSubDB(t)
  56. seedSubInbound(t, "s1", "z", 4431, 1, `{"network":"tcp","security":"tls","tlsSettings":{"serverName":"base.sni"}}`)
  57. links, _, _, _, err := NewSubService("").GetSubs("s1", "req.example.com")
  58. if err != nil {
  59. t.Fatalf("GetSubs: %v", err)
  60. }
  61. if len(links) != 1 {
  62. t.Fatalf("links = %d, want 1", len(links))
  63. }
  64. if !strings.Contains(links[0], "203.0.113.5:4431") {
  65. t.Fatalf("zero-hosts link should use the inbound address: %s", links[0])
  66. }
  67. if strings.Contains(links[0], "\n") {
  68. t.Fatalf("zero-hosts must be a single link: %s", links[0])
  69. }
  70. }
  71. // #2 — N enabled hosts render N links, ordered by sort_order, each carrying its
  72. // own address/port/sni and host-header/path override.
  73. func TestSub_NHosts_EmitsNLinksOrdered(t *testing.T) {
  74. seedSubDB(t)
  75. ib := seedSubInbound(t, "s1", "n", 4432, 1, wsTLSStream)
  76. seedHost(t, &model.Host{InboundId: ib.Id, SortOrder: 2, Remark: "B", Address: "b.cdn.com", Port: 8443, Security: "tls", Sni: "b.sni", HostHeader: "b.host", Path: "/b"})
  77. seedHost(t, &model.Host{InboundId: ib.Id, SortOrder: 1, Remark: "A", Address: "a.cdn.com", Port: 2096, Security: "tls", Sni: "a.sni", HostHeader: "a.host", Path: "/a"})
  78. links, _, _, _, err := NewSubService("").GetSubs("s1", "req.example.com")
  79. if err != nil {
  80. t.Fatalf("GetSubs: %v", err)
  81. }
  82. parts := strings.Split(strings.Join(links, "\n"), "\n")
  83. if len(parts) != 2 {
  84. t.Fatalf("want 2 host links, got %d: %v", len(parts), parts)
  85. }
  86. if !strings.Contains(parts[0], "a.cdn.com:2096") || !strings.Contains(parts[0], "sni=a.sni") ||
  87. !strings.Contains(parts[0], "host=a.host") || !strings.Contains(parts[0], "path=%2Fa") {
  88. t.Fatalf("host A link (sort_order 1) wrong: %s", parts[0])
  89. }
  90. if !strings.Contains(parts[1], "b.cdn.com:8443") || !strings.Contains(parts[1], "sni=b.sni") ||
  91. !strings.Contains(parts[1], "host=b.host") || !strings.Contains(parts[1], "path=%2Fb") {
  92. t.Fatalf("host B link (sort_order 2) wrong: %s", parts[1])
  93. }
  94. }
  95. // #3 — a disabled host is omitted; the inbound falls back to its legacy link.
  96. func TestSub_DisabledHostSkipped(t *testing.T) {
  97. seedSubDB(t)
  98. ib := seedSubInbound(t, "s1", "d", 4433, 1, wsTLSStream)
  99. seedHost(t, &model.Host{InboundId: ib.Id, SortOrder: 1, Remark: "OFF", Address: "off.cdn.com", Port: 8443, IsDisabled: true})
  100. links, _, _, _, err := NewSubService("").GetSubs("s1", "req.example.com")
  101. if err != nil {
  102. t.Fatalf("GetSubs: %v", err)
  103. }
  104. joined := strings.Join(links, "\n")
  105. if strings.Contains(joined, "off.cdn.com") {
  106. t.Fatalf("disabled host must not render: %s", joined)
  107. }
  108. if !strings.Contains(joined, "203.0.113.5:4433") {
  109. t.Fatalf("with only a disabled host, the inbound's own link should render: %s", joined)
  110. }
  111. }
  112. // #4 — when both hosts and a legacy externalProxy are set, hosts win and the
  113. // externalProxy entry is ignored.
  114. func TestSub_HostAndExternalProxy_Precedence(t *testing.T) {
  115. seedSubDB(t)
  116. stream := `{"network":"ws","security":"tls","wsSettings":{"path":"/base","host":"base.host"},"tlsSettings":{"serverName":"base.sni"},"externalProxy":[{"forceTls":"tls","dest":"legacy.cdn.com","port":7443,"remark":"L"}]}`
  117. ib := seedSubInbound(t, "s1", "p", 4434, 1, stream)
  118. seedHost(t, &model.Host{InboundId: ib.Id, SortOrder: 1, Remark: "H", Address: "host.cdn.com", Port: 8443, Security: "tls", Sni: "host.sni"})
  119. links, _, _, _, err := NewSubService("").GetSubs("s1", "req.example.com")
  120. if err != nil {
  121. t.Fatalf("GetSubs: %v", err)
  122. }
  123. joined := strings.Join(links, "\n")
  124. if !strings.Contains(joined, "host.cdn.com:8443") {
  125. t.Fatalf("host should win: %s", joined)
  126. }
  127. if strings.Contains(joined, "legacy.cdn.com") {
  128. t.Fatalf("externalProxy must be ignored when hosts exist: %s", joined)
  129. }
  130. }
  131. // #5 — hosts that share a remark but differ in address/port are NOT deduped:
  132. // distinct hosts produce distinct links. Mutation-checks the (absent) dedup.
  133. func TestSub_NHosts_NoDedup(t *testing.T) {
  134. seedSubDB(t)
  135. ib := seedSubInbound(t, "s1", "dd", 4435, 1, wsTLSStream)
  136. seedHost(t, &model.Host{InboundId: ib.Id, SortOrder: 1, Remark: "SAME", Address: "one.cdn.com", Port: 8443, Security: "tls"})
  137. seedHost(t, &model.Host{InboundId: ib.Id, SortOrder: 2, Remark: "SAME", Address: "two.cdn.com", Port: 8443, Security: "tls"})
  138. links, _, _, _, err := NewSubService("").GetSubs("s1", "req.example.com")
  139. if err != nil {
  140. t.Fatalf("GetSubs: %v", err)
  141. }
  142. joined := strings.Join(links, "\n")
  143. parts := strings.Split(joined, "\n")
  144. if len(parts) != 2 {
  145. t.Fatalf("two distinct hosts must yield two links, got %d: %v", len(parts), parts)
  146. }
  147. if !strings.Contains(joined, "one.cdn.com") || !strings.Contains(joined, "two.cdn.com") {
  148. t.Fatalf("both distinct host addresses must appear: %s", joined)
  149. }
  150. }
  151. // #6 — host sort_order composes with inbound SubSortIndex: inbounds order by
  152. // SubSortIndex, hosts within an inbound by sort_order.
  153. func TestSub_HostSortComposesWithSubSortIndex(t *testing.T) {
  154. seedSubDB(t)
  155. // inbound "second" has a higher SubSortIndex so it must come after "first".
  156. ibFirst := seedSubInbound(t, "s1", "first", 4436, 1, wsTLSStream)
  157. ibSecond := seedSubInbound(t, "s1", "second", 4437, 2, wsTLSStream)
  158. seedHost(t, &model.Host{InboundId: ibSecond.Id, SortOrder: 1, Remark: "S", Address: "second-host.com", Port: 8443, Security: "tls"})
  159. seedHost(t, &model.Host{InboundId: ibFirst.Id, SortOrder: 1, Remark: "F", Address: "first-host.com", Port: 8443, Security: "tls"})
  160. links, _, _, _, err := NewSubService("").GetSubs("s1", "req.example.com")
  161. if err != nil {
  162. t.Fatalf("GetSubs: %v", err)
  163. }
  164. joined := strings.Join(links, "\n")
  165. firstAt := strings.Index(joined, "first-host.com")
  166. secondAt := strings.Index(joined, "second-host.com")
  167. if firstAt < 0 || secondAt < 0 {
  168. t.Fatalf("both inbound hosts should render: %s", joined)
  169. }
  170. if firstAt > secondAt {
  171. t.Fatalf("inbound order must follow SubSortIndex (first before second): %s", joined)
  172. }
  173. }
  174. // #7 — host overrides apply AFTER projectThroughFallbackMaster: the host's
  175. // address/sni win over the projected master stream.
  176. func TestSub_HostOverFallback(t *testing.T) {
  177. seedSubDB(t)
  178. db := database.GetDB()
  179. master := &model.Inbound{
  180. UserId: 1, Tag: "master", Enable: true, Listen: "203.0.113.9", Port: 9443,
  181. Protocol: model.VLESS, Remark: "master",
  182. Settings: `{"clients":[],"decryption":"none"}`,
  183. StreamSettings: `{"network":"tcp","security":"tls","tlsSettings":{"serverName":"master.sni"}}`,
  184. }
  185. if err := db.Create(master).Error; err != nil {
  186. t.Fatalf("seed master: %v", err)
  187. }
  188. // child listens internal-only so projection triggers.
  189. child := seedSubInbound(t, "s1", "child", 4438, 1, `{"network":"tcp","security":"none"}`)
  190. child.Listen = "127.0.0.1"
  191. if err := db.Model(&model.Inbound{}).Where("id = ?", child.Id).Update("listen", "127.0.0.1").Error; err != nil {
  192. t.Fatalf("set child listen: %v", err)
  193. }
  194. if err := db.Create(&model.InboundFallback{MasterId: master.Id, ChildId: child.Id}).Error; err != nil {
  195. t.Fatalf("seed fallback: %v", err)
  196. }
  197. seedHost(t, &model.Host{InboundId: child.Id, SortOrder: 1, Remark: "H", Address: "host.cdn.com", Port: 8443, Security: "tls", Sni: "host.sni"})
  198. links, _, _, _, err := NewSubService("").GetSubs("s1", "req.example.com")
  199. if err != nil {
  200. t.Fatalf("GetSubs: %v", err)
  201. }
  202. joined := strings.Join(links, "\n")
  203. if !strings.Contains(joined, "host.cdn.com:8443") || !strings.Contains(joined, "sni=host.sni") {
  204. t.Fatalf("host override must win over fallback master: %s", joined)
  205. }
  206. if strings.Contains(joined, "203.0.113.9") || strings.Contains(joined, "sni=master.sni") {
  207. t.Fatalf("master endpoint/sni must be overridden by the host: %s", joined)
  208. }
  209. }
  210. // #8 — a client only gets hosts for inbounds it is actually on (the
  211. // clients ⋈ client_inbounds ⋈ inbounds join), never arbitrary inbounds.
  212. func TestSub_HostsResolveViaClientInbounds(t *testing.T) {
  213. seedSubDB(t)
  214. seedSubInbound(t, "s1", "mine", 4439, 1, wsTLSStream) // client on s1
  215. other := seedSubInbound(t, "s2", "other", 4440, 1, wsTLSStream) // client on s2 only
  216. seedHost(t, &model.Host{InboundId: other.Id, SortOrder: 1, Remark: "X", Address: "other-host.com", Port: 8443, Security: "tls"})
  217. links, _, _, _, err := NewSubService("").GetSubs("s1", "req.example.com")
  218. if err != nil {
  219. t.Fatalf("GetSubs: %v", err)
  220. }
  221. joined := strings.Join(links, "\n")
  222. if strings.Contains(joined, "other-host.com") {
  223. t.Fatalf("host on an inbound the client is not on must not appear: %s", joined)
  224. }
  225. }
  226. // allowInsecure renders as allowInsecure=1 in the raw link and
  227. // skip-cert-verify: true in the Clash proxy.
  228. func TestSub_HostAllowInsecure(t *testing.T) {
  229. seedSubDB(t)
  230. ib := seedSubInbound(t, "s1", "ai", 4450, 1, wsTLSStream)
  231. seedHost(t, &model.Host{InboundId: ib.Id, SortOrder: 0, Remark: "AI", Address: "ai.cdn.com", Port: 8443, Security: "tls", AllowInsecure: true})
  232. links, _, _, _, err := NewSubService("").GetSubs("s1", "req.example.com")
  233. if err != nil {
  234. t.Fatalf("GetSubs: %v", err)
  235. }
  236. if !strings.Contains(strings.Join(links, "\n"), "allowInsecure=1") {
  237. t.Fatalf("raw link should carry allowInsecure=1: %s", strings.Join(links, "\n"))
  238. }
  239. clash := NewSubClashService(false, "", NewSubService(""))
  240. yaml, _, err := clash.GetClash("s1", "req.example.com")
  241. if err != nil {
  242. t.Fatalf("GetClash: %v", err)
  243. }
  244. if !strings.Contains(yaml, "skip-cert-verify: true") {
  245. t.Fatalf("clash proxy should carry skip-cert-verify: true:\n%s", yaml)
  246. }
  247. }
  248. // A host's sockoptParams is injected into the JSON output stream (sockopt is
  249. // stripped from the base stream, re-added per host).
  250. func TestSub_HostSockoptJSON(t *testing.T) {
  251. seedSubDB(t)
  252. ib := seedSubInbound(t, "s1", "so", 4460, 1,
  253. `{"network":"xhttp","security":"tls","xhttpSettings":{"path":"/x","mode":"auto"},"tlsSettings":{"serverName":"base.sni"}}`)
  254. seedHost(t, &model.Host{
  255. InboundId: ib.Id, SortOrder: 0, Remark: "SO", Address: "so.cdn.com", Port: 8443, Security: "tls",
  256. SockoptParams: `{"tcpFastOpen":true}`,
  257. })
  258. js := NewSubJsonService("", "", "", NewSubService(""))
  259. out, _, err := js.GetJson("s1", "req.example.com")
  260. if err != nil {
  261. t.Fatalf("GetJson: %v", err)
  262. }
  263. if !strings.Contains(out, "sockopt") || !strings.Contains(out, "tcpFastOpen") {
  264. t.Fatalf("json should include the host sockopt:\n%s", out)
  265. }
  266. }
  267. // A host's muxParams override the JSON outbound's mux.
  268. func TestSub_HostMuxJSON(t *testing.T) {
  269. seedSubDB(t)
  270. ib := seedSubInbound(t, "s1", "mx", 4470, 1, wsTLSStream)
  271. seedHost(t, &model.Host{
  272. InboundId: ib.Id, SortOrder: 0, Remark: "MX", Address: "mx.cdn.com", Port: 8443, Security: "tls",
  273. MuxParams: `{"enabled":true,"concurrency":8}`,
  274. })
  275. js := NewSubJsonService("", "", "", NewSubService(""))
  276. out, _, err := js.GetJson("s1", "req.example.com")
  277. if err != nil {
  278. t.Fatalf("GetJson: %v", err)
  279. }
  280. if !strings.Contains(out, "concurrency") {
  281. t.Fatalf("json should include the host mux override:\n%s", out)
  282. }
  283. }
  284. // A reality host overrides SNI + fingerprint while inheriting pbk/sid from the
  285. // inbound (reality keys can't be host-supplied).
  286. func TestSub_HostRealitySniOverride(t *testing.T) {
  287. seedSubDB(t)
  288. realityStream := `{"network":"tcp","security":"reality","tcpSettings":{"header":{"type":"none"}},"realitySettings":{"serverNames":["base.reality.com"],"shortIds":["abcd"],"settings":{"publicKey":"PBK","fingerprint":"chrome"}}}`
  289. ib := seedSubInbound(t, "s1", "rl", 4490, 1, realityStream)
  290. seedHost(t, &model.Host{
  291. InboundId: ib.Id, SortOrder: 0, Remark: "RL", Address: "rl.cdn.com", Port: 8443,
  292. Security: "reality", Sni: "host.reality.com", Fingerprint: "firefox",
  293. })
  294. links, _, _, _, err := NewSubService("").GetSubs("s1", "req.example.com")
  295. if err != nil {
  296. t.Fatalf("GetSubs: %v", err)
  297. }
  298. joined := strings.Join(links, "\n")
  299. if !strings.Contains(joined, "rl.cdn.com:8443") || !strings.Contains(joined, "security=reality") {
  300. t.Fatalf("reality host base wrong: %s", joined)
  301. }
  302. if !strings.Contains(joined, "sni=host.reality.com") || !strings.Contains(joined, "fp=firefox") {
  303. t.Fatalf("reality host sni/fp override not applied: %s", joined)
  304. }
  305. if strings.Contains(joined, "sni=base.reality.com") {
  306. t.Fatalf("base reality sni must be overridden: %s", joined)
  307. }
  308. if !strings.Contains(joined, "pbk=PBK") || !strings.Contains(joined, "sid=abcd") {
  309. t.Fatalf("reality pbk/sid must be inherited from the inbound: %s", joined)
  310. }
  311. }
  312. // #9 — ExcludeFromSubTypes is honored per format: a host excluded from clash is
  313. // absent from GetClash but present in the raw GetSubs output.
  314. func TestSub_ExcludeFromSubTypes(t *testing.T) {
  315. seedSubDB(t)
  316. ib := seedSubInbound(t, "s1", "x", 4441, 1, wsTLSStream)
  317. seedHost(t, &model.Host{InboundId: ib.Id, SortOrder: 1, Remark: "H", Address: "clashless.cdn.com", Port: 8443, Security: "tls", ExcludeFromSubTypes: []string{"clash"}})
  318. sub := NewSubService("")
  319. links, _, _, _, err := sub.GetSubs("s1", "req.example.com")
  320. if err != nil {
  321. t.Fatalf("GetSubs: %v", err)
  322. }
  323. if !strings.Contains(strings.Join(links, "\n"), "clashless.cdn.com") {
  324. t.Fatalf("host not excluded from raw should appear in GetSubs")
  325. }
  326. clash := NewSubClashService(false, "", NewSubService(""))
  327. yaml, _, err := clash.GetClash("s1", "req.example.com")
  328. if err != nil {
  329. t.Fatalf("GetClash: %v", err)
  330. }
  331. if strings.Contains(yaml, "clashless.cdn.com") {
  332. t.Fatalf("host excluded from clash must not appear in GetClash:\n%s", yaml)
  333. }
  334. }