port_conflict_test.go 22 KB

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