1
0

port_conflict_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. package service
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "sync"
  6. "testing"
  7. "github.com/op/go-logging"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database"
  9. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  10. xuilogger "github.com/mhsanaei/3x-ui/v3/internal/logger"
  11. )
  12. // the panel logger is a process-wide singleton. init it once per test
  13. // binary so a stray warning from gorm doesn't blow up on a nil logger.
  14. var portConflictLoggerOnce sync.Once
  15. // setupConflictDB wires a temp sqlite db so checkPortConflict can read
  16. // real candidates. closes the db before t.TempDir cleans up so windows
  17. // doesn't refuse to remove the file.
  18. func setupConflictDB(t *testing.T) {
  19. t.Helper()
  20. portConflictLoggerOnce.Do(func() { xuilogger.InitLogger(logging.ERROR) })
  21. dbDir := t.TempDir()
  22. t.Setenv("XUI_DB_FOLDER", dbDir)
  23. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  24. t.Fatalf("InitDB: %v", err)
  25. }
  26. t.Cleanup(func() {
  27. if err := database.CloseDB(); err != nil {
  28. t.Logf("CloseDB warning: %v", err)
  29. }
  30. })
  31. }
  32. func seedInboundConflict(t *testing.T, tag, listen string, port int, protocol model.Protocol, streamSettings, settings string) {
  33. t.Helper()
  34. seedInboundConflictNode(t, tag, listen, port, protocol, streamSettings, settings, nil)
  35. }
  36. func seedInboundConflictNode(t *testing.T, tag, listen string, port int, protocol model.Protocol, streamSettings, settings string, nodeID *int) {
  37. t.Helper()
  38. in := &model.Inbound{
  39. Tag: tag,
  40. Enable: true,
  41. Listen: listen,
  42. Port: port,
  43. Protocol: protocol,
  44. StreamSettings: streamSettings,
  45. Settings: settings,
  46. NodeID: nodeID,
  47. }
  48. if err := database.GetDB().Create(in).Error; err != nil {
  49. t.Fatalf("seed inbound %s: %v", tag, err)
  50. }
  51. }
  52. func TestInboundTransports(t *testing.T) {
  53. cases := []struct {
  54. name string
  55. protocol model.Protocol
  56. streamSettings string
  57. settings string
  58. want transportBits
  59. }{
  60. {"vless default tcp", model.VLESS, `{"network":"tcp"}`, ``, transportTCP},
  61. {"vless ws (still tcp)", model.VLESS, `{"network":"ws"}`, ``, transportTCP},
  62. {"vless kcp is udp", model.VLESS, `{"network":"kcp"}`, ``, transportUDP},
  63. {"vless empty stream defaults to tcp", model.VLESS, ``, ``, transportTCP},
  64. {"vless garbage stream stays tcp", model.VLESS, `not json`, ``, transportTCP},
  65. {"vmess default tcp", model.VMESS, `{"network":"tcp"}`, ``, transportTCP},
  66. {"trojan grpc is tcp", model.Trojan, `{"network":"grpc"}`, ``, transportTCP},
  67. {"hysteria forced udp", model.Hysteria, `{"network":"tcp"}`, ``, transportUDP},
  68. {"wireguard forced udp", model.WireGuard, ``, ``, transportUDP},
  69. {"shadowsocks tcp,udp", model.Shadowsocks, ``, `{"network":"tcp,udp"}`, transportTCP | transportUDP},
  70. {"shadowsocks udp only", model.Shadowsocks, ``, `{"network":"udp"}`, transportUDP},
  71. {"shadowsocks tcp only", model.Shadowsocks, ``, `{"network":"tcp"}`, transportTCP},
  72. {"shadowsocks empty network falls back to streamSettings", model.Shadowsocks, `{"network":"tcp"}`, `{}`, transportTCP},
  73. {"mixed udp on", model.Mixed, `{"network":"tcp"}`, `{"udp":true}`, transportTCP | transportUDP},
  74. {"mixed udp off", model.Mixed, `{"network":"tcp"}`, `{"udp":false}`, transportTCP},
  75. {"mixed udp missing", model.Mixed, `{"network":"tcp"}`, `{}`, transportTCP},
  76. }
  77. for _, c := range cases {
  78. t.Run(c.name, func(t *testing.T) {
  79. got := inboundTransports(c.protocol, c.streamSettings, c.settings)
  80. if got != c.want {
  81. t.Fatalf("got bits %#b, want %#b", got, c.want)
  82. }
  83. })
  84. }
  85. }
  86. func TestListenOverlaps(t *testing.T) {
  87. cases := []struct {
  88. a, b string
  89. want bool
  90. }{
  91. {"", "", true},
  92. {"0.0.0.0", "", true},
  93. {"0.0.0.0", "1.2.3.4", true},
  94. {"::", "1.2.3.4", true},
  95. {"::0", "fe80::1", true},
  96. {"1.2.3.4", "1.2.3.4", true},
  97. {"1.2.3.4", "5.6.7.8", false},
  98. {"1.2.3.4", "::1", false},
  99. }
  100. for _, c := range cases {
  101. if got := listenOverlaps(c.a, c.b); got != c.want {
  102. t.Errorf("listenOverlaps(%q, %q) = %v, want %v", c.a, c.b, got, c.want)
  103. }
  104. }
  105. }
  106. // the actual case from #4103: tcp/443 vless reality and udp/443
  107. // hysteria must be allowed to coexist on the same port.
  108. func TestCheckPortConflict_TCPandUDPCoexistOnSamePort(t *testing.T) {
  109. setupConflictDB(t)
  110. seedInboundConflict(t, "vless-443-tcp", "0.0.0.0", 443, model.VLESS, `{"network":"tcp"}`, `{}`)
  111. svc := &InboundService{}
  112. hyst2 := &model.Inbound{
  113. Tag: "hyst2-443-udp",
  114. Listen: "0.0.0.0",
  115. Port: 443,
  116. Protocol: model.Hysteria,
  117. }
  118. exist, err := svc.checkPortConflict(hyst2, 0)
  119. if err != nil {
  120. t.Fatalf("checkPortConflict: %v", err)
  121. }
  122. if exist != nil {
  123. t.Fatalf("vless/tcp and hysteria2/udp on the same port must be allowed to coexist")
  124. }
  125. }
  126. // two tcp inbounds on the same port still conflict.
  127. func TestCheckPortConflict_TCPCollidesWithTCP(t *testing.T) {
  128. setupConflictDB(t)
  129. seedInboundConflict(t, "vless-443-a", "0.0.0.0", 443, model.VLESS, `{"network":"tcp"}`, `{}`)
  130. svc := &InboundService{}
  131. other := &model.Inbound{
  132. Tag: "vless-443-b",
  133. Listen: "0.0.0.0",
  134. Port: 443,
  135. Protocol: model.Trojan,
  136. StreamSettings: `{"network":"ws"}`,
  137. }
  138. exist, err := svc.checkPortConflict(other, 0)
  139. if err != nil {
  140. t.Fatalf("checkPortConflict: %v", err)
  141. }
  142. if exist == nil {
  143. t.Fatalf("two tcp inbounds on the same port must still conflict")
  144. }
  145. }
  146. // two udp inbounds (e.g. hysteria2 vs wireguard) on the same port also
  147. // conflict, since they fight for the same socket.
  148. func TestCheckPortConflict_UDPCollidesWithUDP(t *testing.T) {
  149. setupConflictDB(t)
  150. seedInboundConflict(t, "hyst2-443", "0.0.0.0", 443, model.Hysteria, ``, ``)
  151. svc := &InboundService{}
  152. wg := &model.Inbound{
  153. Tag: "wg-443",
  154. Listen: "0.0.0.0",
  155. Port: 443,
  156. Protocol: model.WireGuard,
  157. }
  158. exist, err := svc.checkPortConflict(wg, 0)
  159. if err != nil {
  160. t.Fatalf("checkPortConflict: %v", err)
  161. }
  162. if exist == nil {
  163. t.Fatalf("two udp inbounds on the same port must conflict")
  164. }
  165. }
  166. // shadowsocks listening on tcp+udp eats the whole port for both
  167. // transports, so neither a tcp nor a udp neighbour is allowed.
  168. func TestCheckPortConflict_ShadowsocksDualListenBlocksBoth(t *testing.T) {
  169. setupConflictDB(t)
  170. seedInboundConflict(t, "ss-443-dual", "0.0.0.0", 443, model.Shadowsocks, ``, `{"network":"tcp,udp"}`)
  171. svc := &InboundService{}
  172. tcpClash := &model.Inbound{
  173. Tag: "vless-443",
  174. Listen: "0.0.0.0",
  175. Port: 443,
  176. Protocol: model.VLESS,
  177. StreamSettings: `{"network":"tcp"}`,
  178. }
  179. if exist, err := svc.checkPortConflict(tcpClash, 0); err != nil || exist == nil {
  180. t.Fatalf("tcp inbound should clash with shadowsocks tcp,udp; exist=%v err=%v", exist, err)
  181. }
  182. udpClash := &model.Inbound{
  183. Tag: "hyst2-443",
  184. Listen: "0.0.0.0",
  185. Port: 443,
  186. Protocol: model.Hysteria,
  187. }
  188. if exist, err := svc.checkPortConflict(udpClash, 0); err != nil || exist == nil {
  189. t.Fatalf("udp inbound should clash with shadowsocks tcp,udp; exist=%v err=%v", exist, err)
  190. }
  191. }
  192. // different ports never conflict regardless of transport.
  193. func TestCheckPortConflict_DifferentPortNeverConflicts(t *testing.T) {
  194. setupConflictDB(t)
  195. seedInboundConflict(t, "vless-443", "0.0.0.0", 443, model.VLESS, `{"network":"tcp"}`, `{}`)
  196. svc := &InboundService{}
  197. other := &model.Inbound{
  198. Tag: "vless-444",
  199. Listen: "0.0.0.0",
  200. Port: 444,
  201. Protocol: model.VLESS,
  202. StreamSettings: `{"network":"tcp"}`,
  203. }
  204. if exist, err := svc.checkPortConflict(other, 0); err != nil || exist != nil {
  205. t.Fatalf("different port must not conflict; exist=%v err=%v", exist, err)
  206. }
  207. }
  208. // specific listen addresses on the same port don't clash with each other,
  209. // but do clash with any-address on the same port (preserved from the old
  210. // check).
  211. func TestCheckPortConflict_ListenOverlapPreserved(t *testing.T) {
  212. setupConflictDB(t)
  213. seedInboundConflict(t, "vless-1.2.3.4", "1.2.3.4", 443, model.VLESS, `{"network":"tcp"}`, `{}`)
  214. svc := &InboundService{}
  215. // different specific address, same port + transport: no conflict.
  216. other := &model.Inbound{
  217. Tag: "vless-5.6.7.8",
  218. Listen: "5.6.7.8",
  219. Port: 443,
  220. Protocol: model.VLESS,
  221. StreamSettings: `{"network":"tcp"}`,
  222. }
  223. if exist, err := svc.checkPortConflict(other, 0); err != nil || exist != nil {
  224. t.Fatalf("different specific listen must not conflict; exist=%v err=%v", exist, err)
  225. }
  226. // any-address vs specific on same transport: conflict (any-addr wins).
  227. anyAddr := &model.Inbound{
  228. Tag: "vless-any",
  229. Listen: "0.0.0.0",
  230. Port: 443,
  231. Protocol: model.VLESS,
  232. StreamSettings: `{"network":"tcp"}`,
  233. }
  234. if exist, err := svc.checkPortConflict(anyAddr, 0); err != nil || exist == nil {
  235. t.Fatalf("any-addr on same port+transport must conflict with specific; exist=%v err=%v", exist, err)
  236. }
  237. }
  238. // even with a stale legacy tag owning "in-443", a new UDP-side
  239. // inbound gets a fully qualified canonical tag and does not collide.
  240. func TestGenerateInboundTag_DisambiguatesByTransportOnSamePort(t *testing.T) {
  241. setupConflictDB(t)
  242. seedInboundConflict(t, "in-443", "0.0.0.0", 443, model.VLESS, `{"network":"tcp"}`, `{}`)
  243. svc := &InboundService{}
  244. udp := &model.Inbound{
  245. Listen: "0.0.0.0",
  246. Port: 443,
  247. Protocol: model.Hysteria,
  248. }
  249. got, err := svc.generateInboundTag(udp, 0)
  250. if err != nil {
  251. t.Fatalf("generateInboundTag: %v", err)
  252. }
  253. if got != "in-443-udp" {
  254. t.Fatalf("expected in-443-udp, got %q", got)
  255. }
  256. }
  257. // when the port is free, the canonical tag carries the transport so
  258. // tcp/8443 and udp/8443 get distinct tags out of the box.
  259. func TestGenerateInboundTag_KeepsBaseTagWhenFree(t *testing.T) {
  260. setupConflictDB(t)
  261. svc := &InboundService{}
  262. in := &model.Inbound{
  263. Listen: "0.0.0.0",
  264. Port: 8443,
  265. Protocol: model.VLESS,
  266. }
  267. got, err := svc.generateInboundTag(in, 0)
  268. if err != nil {
  269. t.Fatalf("generateInboundTag: %v", err)
  270. }
  271. if got != "in-8443-tcp" {
  272. t.Fatalf("expected in-8443-tcp, got %q", got)
  273. }
  274. }
  275. // updating an inbound on its own port must not flag its own tag as taken;
  276. // that's what ignoreId is for.
  277. func TestGenerateInboundTag_IgnoresSelfOnUpdate(t *testing.T) {
  278. setupConflictDB(t)
  279. seedInboundConflict(t, "in-443-tcp", "0.0.0.0", 443, model.VLESS, `{"network":"tcp"}`, `{}`)
  280. var existing model.Inbound
  281. if err := database.GetDB().Where("tag = ?", "in-443-tcp").First(&existing).Error; err != nil {
  282. t.Fatalf("read seeded row: %v", err)
  283. }
  284. svc := &InboundService{}
  285. got, err := svc.generateInboundTag(&existing, existing.Id)
  286. if err != nil {
  287. t.Fatalf("generateInboundTag: %v", err)
  288. }
  289. if got != "in-443-tcp" {
  290. t.Fatalf("self-update must keep base tag, got %q", got)
  291. }
  292. }
  293. // the listen address never appears in the tag; the transport suffix still
  294. // keeps a udp inbound distinct from a tcp one on the same port.
  295. func TestGenerateInboundTag_ListenIgnoredTransportDisambiguates(t *testing.T) {
  296. setupConflictDB(t)
  297. seedInboundConflict(t, "in-443-tcp", "1.2.3.4", 443, model.VLESS, `{"network":"tcp"}`, `{}`)
  298. svc := &InboundService{}
  299. udp := &model.Inbound{
  300. Listen: "1.2.3.4",
  301. Port: 443,
  302. Protocol: model.Hysteria,
  303. }
  304. got, err := svc.generateInboundTag(udp, 0)
  305. if err != nil {
  306. t.Fatalf("generateInboundTag: %v", err)
  307. }
  308. if got != "in-443-udp" {
  309. t.Fatalf("expected in-443-udp, got %q", got)
  310. }
  311. }
  312. // inbounds bound to different nodes run on different physical machines,
  313. // so the same port + transport must be allowed across nodes. covers
  314. // local-vs-remote, remote-A-vs-remote-B, and the still-clashing
  315. // same-node case.
  316. func TestCheckPortConflict_NodeScope(t *testing.T) {
  317. setupConflictDB(t)
  318. seedInboundConflictNode(t, "local-443-tcp", "0.0.0.0", 443, model.VLESS, `{"network":"tcp"}`, `{}`, nil)
  319. seedInboundConflictNode(t, "node1-443-tcp", "0.0.0.0", 443, model.VLESS, `{"network":"tcp"}`, `{}`, new(1))
  320. svc := &InboundService{}
  321. cases := []struct {
  322. name string
  323. nodeID *int
  324. want bool
  325. }{
  326. {"new local same port + tcp clashes with local", nil, true},
  327. {"new remote on different node from local is fine", new(2), false},
  328. {"new remote on existing node 1 clashes", new(1), true},
  329. }
  330. for _, c := range cases {
  331. t.Run(c.name, func(t *testing.T) {
  332. candidate := &model.Inbound{
  333. Listen: "0.0.0.0",
  334. Port: 443,
  335. Protocol: model.VLESS,
  336. StreamSettings: `{"network":"tcp"}`,
  337. NodeID: c.nodeID,
  338. }
  339. got, err := svc.checkPortConflict(candidate, 0)
  340. if err != nil {
  341. t.Fatalf("checkPortConflict: %v", err)
  342. }
  343. if (got != nil) != c.want {
  344. t.Fatalf("got conflict=%v, want %v", got != nil, c.want)
  345. }
  346. })
  347. }
  348. }
  349. // when the caller passes an explicit non-empty Tag that doesn't collide,
  350. // resolveInboundTag returns it verbatim. this is the cross-panel path:
  351. // the central panel picks a tag, pushes the inbound to a node, and the
  352. // node must keep that exact tag so the eventual traffic sync-back can
  353. // match the row by tag. previously the node regenerated and the two
  354. // panels diverged, causing a UNIQUE constraint failure on sync.
  355. func TestResolveInboundTag_RespectsCallerTagWhenFree(t *testing.T) {
  356. setupConflictDB(t)
  357. seedInboundConflictNode(t, "in-5000-tcp", "0.0.0.0", 5000, model.VLESS, `{"network":"tcp"}`, `{}`, nil)
  358. seedInboundConflictNode(t, "in-5000-udp", "0.0.0.0", 5000, model.Hysteria, ``, ``, nil)
  359. svc := &InboundService{}
  360. pushed := &model.Inbound{
  361. Tag: "custom-pushed-tag",
  362. Listen: "0.0.0.0",
  363. Port: 5000,
  364. Protocol: model.VLESS,
  365. StreamSettings: `{"network":"tcp"}`,
  366. NodeID: new(1),
  367. }
  368. got, err := svc.resolveInboundTag(pushed, 0)
  369. if err != nil {
  370. t.Fatalf("resolveInboundTag: %v", err)
  371. }
  372. if got != "custom-pushed-tag" {
  373. t.Fatalf("caller tag must be preserved when free, got %q", got)
  374. }
  375. }
  376. // when the caller leaves Tag empty (the local UI path) resolveInboundTag
  377. // falls back to generateInboundTag, which emits the canonical
  378. // "in-<port>-<transport>" shape.
  379. func TestResolveInboundTag_GeneratesWhenTagEmpty(t *testing.T) {
  380. setupConflictDB(t)
  381. svc := &InboundService{}
  382. in := &model.Inbound{
  383. Listen: "0.0.0.0",
  384. Port: 8443,
  385. Protocol: model.VLESS,
  386. }
  387. got, err := svc.resolveInboundTag(in, 0)
  388. if err != nil {
  389. t.Fatalf("resolveInboundTag: %v", err)
  390. }
  391. if got != "in-8443-tcp" {
  392. t.Fatalf("expected generated in-8443-tcp, got %q", got)
  393. }
  394. }
  395. // when the caller's Tag collides (e.g. a node that was used standalone
  396. // happens to already own the tag the central panel picked),
  397. // resolveInboundTag falls back to generateInboundTag rather than
  398. // failing — the inbound still lands, just under a slightly different
  399. // tag that the central will pick up via the AddInbound response.
  400. func TestResolveInboundTag_RegeneratesOnCollision(t *testing.T) {
  401. setupConflictDB(t)
  402. seedInboundConflictNode(t, "in-5000-tcp", "0.0.0.0", 5000, model.VLESS, `{"network":"tcp"}`, `{}`, nil)
  403. svc := &InboundService{}
  404. pushed := &model.Inbound{
  405. Tag: "in-5000-tcp",
  406. Listen: "0.0.0.0",
  407. Port: 5000,
  408. Protocol: model.Hysteria,
  409. StreamSettings: ``,
  410. Settings: ``,
  411. }
  412. got, err := svc.resolveInboundTag(pushed, 0)
  413. if err != nil {
  414. t.Fatalf("resolveInboundTag: %v", err)
  415. }
  416. if got == "in-5000-tcp" {
  417. t.Fatalf("colliding caller tag must be replaced, but resolver kept %q", got)
  418. }
  419. }
  420. // inbounds bound to a remote node get the canonical tag prefixed with
  421. // "n<id>-" so the same listen+port+transport can live on the central
  422. // panel and on the node simultaneously without bumping the global
  423. // UNIQUE(inbounds.tag) constraint.
  424. func TestGenerateInboundTag_NodePrefix(t *testing.T) {
  425. setupConflictDB(t)
  426. svc := &InboundService{}
  427. in := &model.Inbound{
  428. Listen: "0.0.0.0",
  429. Port: 443,
  430. Protocol: model.VLESS,
  431. NodeID: new(1),
  432. }
  433. got, err := svc.generateInboundTag(in, 0)
  434. if err != nil {
  435. t.Fatalf("generateInboundTag: %v", err)
  436. }
  437. if got != "n1-in-443-tcp" {
  438. t.Fatalf("expected n1-in-443-tcp, got %q", got)
  439. }
  440. }
  441. // a node-prefixed inbound shouldn't collide with a same-port local one:
  442. // the prefix scopes the tag to that specific node.
  443. func TestGenerateInboundTag_NodePrefixedDoesNotCollideWithLocal(t *testing.T) {
  444. setupConflictDB(t)
  445. seedInboundConflict(t, "in-443-tcp", "0.0.0.0", 443, model.VLESS, `{"network":"tcp"}`, `{}`)
  446. svc := &InboundService{}
  447. in := &model.Inbound{
  448. Listen: "0.0.0.0",
  449. Port: 443,
  450. Protocol: model.VLESS,
  451. NodeID: new(1),
  452. }
  453. got, err := svc.generateInboundTag(in, 0)
  454. if err != nil {
  455. t.Fatalf("generateInboundTag: %v", err)
  456. }
  457. if got != "n1-in-443-tcp" {
  458. t.Fatalf("expected n1-in-443-tcp, got %q", got)
  459. }
  460. }
  461. // updating an inbound must not see itself as a conflict, that's what
  462. // ignoreId is for.
  463. func TestCheckPortConflict_IgnoreSelfOnUpdate(t *testing.T) {
  464. setupConflictDB(t)
  465. seedInboundConflict(t, "vless-443", "0.0.0.0", 443, model.VLESS, `{"network":"tcp"}`, `{}`)
  466. var existing model.Inbound
  467. if err := database.GetDB().Where("tag = ?", "vless-443").First(&existing).Error; err != nil {
  468. t.Fatalf("read seeded row: %v", err)
  469. }
  470. svc := &InboundService{}
  471. if exist, err := svc.checkPortConflict(&existing, existing.Id); err != nil || exist != nil {
  472. t.Fatalf("self-update must not be flagged as conflict; exist=%v err=%v", exist, err)
  473. }
  474. }
  475. // streamSettings.network=quic rides on UDP at L4, so a QUIC inbound must
  476. // conflict with a UDP-only neighbour (hysteria) on the same port but not
  477. // with a TCP-only one. covers the gap left by the original kcp-only check.
  478. func TestCheckPortConflict_QUICTreatedAsUDP(t *testing.T) {
  479. quic := &model.Inbound{
  480. Tag: "vless-quic-443",
  481. Listen: "0.0.0.0",
  482. Port: 443,
  483. Protocol: model.VLESS,
  484. StreamSettings: `{"network":"quic"}`,
  485. }
  486. t.Run("conflicts with hysteria/udp", func(t *testing.T) {
  487. setupConflictDB(t)
  488. seedInboundConflict(t, "hyst-443", "0.0.0.0", 443, model.Hysteria, ``, ``)
  489. svc := &InboundService{}
  490. if exist, err := svc.checkPortConflict(quic, 0); err != nil || exist == nil {
  491. t.Fatalf("quic on same port as hysteria must conflict; exist=%v err=%v", exist, err)
  492. }
  493. })
  494. t.Run("coexists with vless/tcp", func(t *testing.T) {
  495. setupConflictDB(t)
  496. seedInboundConflict(t, "vless-tcp-443", "0.0.0.0", 443, model.VLESS, `{"network":"tcp"}`, `{}`)
  497. svc := &InboundService{}
  498. if exist, err := svc.checkPortConflict(quic, 0); err != nil || exist != nil {
  499. t.Fatalf("quic and tcp on same port must coexist; exist=%v err=%v", exist, err)
  500. }
  501. })
  502. }
  503. // tunnel (dokodemo-door) carries its L4 transport list in
  504. // settings.allowedNetwork, not settings.network. verify the predicate
  505. // picks the right field for each protocol.
  506. func TestCheckPortConflict_TunnelAllowedNetwork(t *testing.T) {
  507. setupConflictDB(t)
  508. seedInboundConflict(t, "tunnel-udp-443", "0.0.0.0", 443, model.Tunnel, ``, `{"allowedNetwork":"udp"}`)
  509. svc := &InboundService{}
  510. // tcp inbound on same port should coexist with udp-only tunnel.
  511. tcpNeighbour := &model.Inbound{
  512. Tag: "vless-443",
  513. Listen: "0.0.0.0",
  514. Port: 443,
  515. Protocol: model.VLESS,
  516. StreamSettings: `{"network":"tcp"}`,
  517. }
  518. if exist, err := svc.checkPortConflict(tcpNeighbour, 0); err != nil || exist != nil {
  519. t.Fatalf("tunnel/udp and vless/tcp on same port must coexist; exist=%v err=%v", exist, err)
  520. }
  521. // udp neighbour (hysteria) on same port must conflict.
  522. udpNeighbour := &model.Inbound{
  523. Tag: "hyst-443",
  524. Listen: "0.0.0.0",
  525. Port: 443,
  526. Protocol: model.Hysteria,
  527. }
  528. if exist, err := svc.checkPortConflict(udpNeighbour, 0); err != nil || exist == nil {
  529. t.Fatalf("tunnel/udp and hysteria on same port must conflict; exist=%v err=%v", exist, err)
  530. }
  531. }
  532. // the rich conflict detail surfaced to the user must name the offending
  533. // inbound (by remark when available) and the shared L4 transport(s).
  534. func TestCheckPortConflict_DetailMessage(t *testing.T) {
  535. setupConflictDB(t)
  536. seeded := &model.Inbound{
  537. Tag: "vless-443",
  538. Remark: "my-vless",
  539. Enable: true,
  540. Listen: "0.0.0.0",
  541. Port: 443,
  542. Protocol: model.VLESS,
  543. StreamSettings: `{"network":"tcp"}`,
  544. Settings: `{}`,
  545. }
  546. if err := database.GetDB().Create(seeded).Error; err != nil {
  547. t.Fatalf("seed inbound: %v", err)
  548. }
  549. svc := &InboundService{}
  550. candidate := &model.Inbound{
  551. Tag: "trojan-443",
  552. Listen: "0.0.0.0",
  553. Port: 443,
  554. Protocol: model.Trojan,
  555. StreamSettings: `{"network":"ws"}`,
  556. }
  557. got, err := svc.checkPortConflict(candidate, 0)
  558. if err != nil || got == nil {
  559. t.Fatalf("expected conflict, got=%v err=%v", got, err)
  560. }
  561. msg := got.String()
  562. if !strings.Contains(msg, "my-vless") {
  563. t.Fatalf("message should mention the conflicting inbound's remark; got %q", msg)
  564. }
  565. if !strings.Contains(msg, "tcp") {
  566. t.Fatalf("message should mention the shared L4 transport; got %q", msg)
  567. }
  568. if !strings.Contains(msg, "443") {
  569. t.Fatalf("message should mention the port; got %q", msg)
  570. }
  571. }
  572. // isAutoGeneratedTag must recognise the tags generateInboundTag emits (so an
  573. // edit that changes port/transport re-derives them) while leaving user-typed
  574. // or cross-panel tags untouched.
  575. func TestIsAutoGeneratedTag(t *testing.T) {
  576. tcp := transportTCP
  577. cases := []struct {
  578. name string
  579. tag string
  580. port int
  581. nodeID *int
  582. bits transportBits
  583. want bool
  584. }{
  585. {"canonical", "in-443-tcp", 443, nil, tcp, true},
  586. {"canonical udp", "in-443-udp", 443, nil, transportUDP, true},
  587. {"dedup suffix", "in-443-tcp-2", 443, nil, tcp, true},
  588. {"node prefixed", "n1-in-443-tcp", 443, new(1), tcp, true},
  589. {"legacy listen-scoped is now custom", "in-127.0.0.1:443-tcp", 443, nil, tcp, false},
  590. {"custom tag", "my-cool-tag", 443, nil, tcp, false},
  591. {"stale port", "in-443-tcp", 8443, nil, tcp, false},
  592. {"stale transport", "in-443-tcp", 443, nil, transportUDP, false},
  593. {"non-numeric suffix", "in-443-tcp-x", 443, nil, tcp, false},
  594. {"empty suffix", "in-443-tcp-", 443, nil, tcp, false},
  595. }
  596. for _, c := range cases {
  597. t.Run(c.name, func(t *testing.T) {
  598. if got := isAutoGeneratedTag(c.tag, c.port, c.nodeID, c.bits); got != c.want {
  599. t.Fatalf("isAutoGeneratedTag(%q) = %v, want %v", c.tag, got, c.want)
  600. }
  601. })
  602. }
  603. }
  604. // the internal Xray API inbound (tag "api", loopback TCP) isn't a DB row, so
  605. // checkPortConflict must still reject a local user inbound that reuses its
  606. // reserved port — otherwise Xray binds the port twice (#5304).
  607. func TestCheckPortConflict_ReservedAPIPortBlockedLocal(t *testing.T) {
  608. setupConflictDB(t)
  609. svc := &InboundService{}
  610. candidate := &model.Inbound{
  611. Tag: "user-62789",
  612. Listen: "0.0.0.0",
  613. Port: defaultXrayAPIPort,
  614. Protocol: model.VLESS,
  615. StreamSettings: `{"network":"tcp"}`,
  616. }
  617. got, err := svc.checkPortConflict(candidate, 0)
  618. if err != nil {
  619. t.Fatalf("checkPortConflict: %v", err)
  620. }
  621. if got == nil {
  622. t.Fatalf("local inbound on the reserved API port %d must conflict", defaultXrayAPIPort)
  623. }
  624. if msg := got.String(); !strings.Contains(msg, "api") {
  625. t.Fatalf("conflict message should name the api inbound; got %q", msg)
  626. }
  627. }
  628. // nodes run their own Xray with their own API port, so a node inbound on the
  629. // central panel's reserved API port must be allowed.
  630. func TestCheckPortConflict_ReservedAPIPortAllowedOnNode(t *testing.T) {
  631. setupConflictDB(t)
  632. svc := &InboundService{}
  633. candidate := &model.Inbound{
  634. Tag: "node-62789",
  635. Listen: "0.0.0.0",
  636. Port: defaultXrayAPIPort,
  637. Protocol: model.VLESS,
  638. StreamSettings: `{"network":"tcp"}`,
  639. NodeID: new(1),
  640. }
  641. if got, err := svc.checkPortConflict(candidate, 0); err != nil || got != nil {
  642. t.Fatalf("node inbound on the reserved API port must be allowed; got=%v err=%v", got, err)
  643. }
  644. }
  645. // the API inbound is TCP-only, so a UDP-only inbound (e.g. hysteria) may share
  646. // its port — same tcp/udp coexistence the rest of the checks allow.
  647. func TestCheckPortConflict_ReservedAPIPortUDPCoexists(t *testing.T) {
  648. setupConflictDB(t)
  649. svc := &InboundService{}
  650. candidate := &model.Inbound{
  651. Tag: "hyst-62789",
  652. Listen: "0.0.0.0",
  653. Port: defaultXrayAPIPort,
  654. Protocol: model.Hysteria,
  655. }
  656. if got, err := svc.checkPortConflict(candidate, 0); err != nil || got != nil {
  657. t.Fatalf("udp-only inbound must coexist with the tcp API inbound; got=%v err=%v", got, err)
  658. }
  659. }