1
0

port_conflict.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  9. )
  10. type transportBits uint8
  11. const (
  12. transportTCP transportBits = 1 << iota
  13. transportUDP
  14. )
  15. func inboundTransports(protocol model.Protocol, streamSettings, settings string) transportBits {
  16. // protocols that ignore streamSettings entirely.
  17. switch protocol {
  18. case model.Hysteria, model.WireGuard:
  19. return transportUDP
  20. case model.MTProto:
  21. return transportTCP
  22. }
  23. var bits transportBits
  24. // peek at streamSettings.network to spot udp-based transports.
  25. // parse errors are non-fatal: missing or weird streamSettings just
  26. // keeps the default tcp bit below.
  27. network := ""
  28. if streamSettings != "" {
  29. var ss map[string]any
  30. if json.Unmarshal([]byte(streamSettings), &ss) == nil {
  31. if n, _ := ss["network"].(string); n != "" {
  32. network = n
  33. }
  34. }
  35. }
  36. switch network {
  37. case "kcp", "quic":
  38. bits |= transportUDP
  39. default:
  40. bits |= transportTCP
  41. }
  42. // a few protocols carry their L4 choice in settings instead of (or in
  43. // addition to) streamSettings: SS / Tunnel via a CSV field that wins
  44. // outright, Mixed via an additive udp boolean.
  45. if settings != "" {
  46. var st map[string]any
  47. if json.Unmarshal([]byte(settings), &st) == nil {
  48. switch protocol {
  49. case model.Shadowsocks, model.Tunnel:
  50. key := "network"
  51. if protocol == model.Tunnel {
  52. key = "allowedNetwork"
  53. }
  54. if n, ok := st[key].(string); ok && n != "" {
  55. bits = 0
  56. for part := range strings.SplitSeq(n, ",") {
  57. switch strings.TrimSpace(part) {
  58. case "tcp":
  59. bits |= transportTCP
  60. case "udp":
  61. bits |= transportUDP
  62. }
  63. }
  64. }
  65. case model.Mixed:
  66. // socks/http "mixed" inbound: settings.udp=true means it
  67. // also relays udp on the same port (socks5 udp associate).
  68. if udpOn, _ := st["udp"].(bool); udpOn {
  69. bits |= transportUDP
  70. }
  71. }
  72. }
  73. }
  74. // safety net: never return zero, even if every parse failed.
  75. if bits == 0 {
  76. bits = transportTCP
  77. }
  78. return bits
  79. }
  80. func listenOverlaps(a, b string) bool {
  81. if isAnyListen(a) || isAnyListen(b) {
  82. return true
  83. }
  84. return a == b
  85. }
  86. func isAnyListen(s string) bool {
  87. return s == "" || s == "0.0.0.0" || s == "::" || s == "::0"
  88. }
  89. type portConflictDetail struct {
  90. InboundID int
  91. Remark string
  92. Tag string
  93. Listen string
  94. Port int
  95. Transports transportBits
  96. }
  97. // String renders the detail as a single-line, user-facing summary.
  98. func (d *portConflictDetail) String() string {
  99. name := d.Remark
  100. if name == "" {
  101. name = d.Tag
  102. }
  103. if name == "" {
  104. name = fmt.Sprintf("#%d", d.InboundID)
  105. } else if d.InboundID > 0 {
  106. name = fmt.Sprintf("'%s' (#%d)", name, d.InboundID)
  107. } else {
  108. // reserved/system inbounds (e.g. the Xray API) have no DB id.
  109. name = fmt.Sprintf("'%s'", name)
  110. }
  111. listen := d.Listen
  112. if isAnyListen(listen) {
  113. listen = "*"
  114. }
  115. return fmt.Sprintf("port %d (%s) already used by inbound %s on %s",
  116. d.Port, transportTagSuffix(d.Transports), name, listen)
  117. }
  118. // defaultXrayAPIPort is the loopback port of the internal Xray API inbound
  119. // (tag "api") seeded into the config template. Used as a fallback when the
  120. // template can't be parsed.
  121. const defaultXrayAPIPort = 62789
  122. // reservedAPIPort returns the port of the internal Xray API inbound declared
  123. // in the config template, falling back to defaultXrayAPIPort.
  124. func reservedAPIPort() int {
  125. tmpl, err := (&SettingService{}).GetXrayConfigTemplate()
  126. if err != nil || tmpl == "" {
  127. return defaultXrayAPIPort
  128. }
  129. var parsed struct {
  130. Inbounds []struct {
  131. Port int `json:"port"`
  132. Tag string `json:"tag"`
  133. } `json:"inbounds"`
  134. }
  135. if json.Unmarshal([]byte(tmpl), &parsed) != nil {
  136. return defaultXrayAPIPort
  137. }
  138. for _, in := range parsed.Inbounds {
  139. if in.Tag == "api" && in.Port > 0 {
  140. return in.Port
  141. }
  142. }
  143. return defaultXrayAPIPort
  144. }
  145. func (s *InboundService) checkPortConflict(inbound *model.Inbound, ignoreId int) (*portConflictDetail, error) {
  146. newBits := inboundTransports(inbound.Protocol, inbound.StreamSettings, inbound.Settings)
  147. // The internal Xray API inbound (tag "api", loopback TCP) isn't a DB row,
  148. // so a local user inbound reusing its port would leave Xray binding the
  149. // port twice (#5304). Nodes run their own Xray, so this only applies to
  150. // the local panel.
  151. if inbound.NodeID == nil && inbound.Port == reservedAPIPort() &&
  152. newBits&transportTCP != 0 && listenOverlaps("127.0.0.1", inbound.Listen) {
  153. return &portConflictDetail{
  154. Tag: "api",
  155. Listen: "127.0.0.1",
  156. Port: inbound.Port,
  157. Transports: transportTCP,
  158. }, nil
  159. }
  160. db := database.GetDB()
  161. var candidates []*model.Inbound
  162. q := db.Model(model.Inbound{}).Where("port = ?", inbound.Port)
  163. if ignoreId > 0 {
  164. q = q.Where("id != ?", ignoreId)
  165. }
  166. if err := q.Find(&candidates).Error; err != nil {
  167. return nil, err
  168. }
  169. for _, c := range candidates {
  170. if !sameNode(c.NodeID, inbound.NodeID) {
  171. continue
  172. }
  173. if !listenOverlaps(c.Listen, inbound.Listen) {
  174. continue
  175. }
  176. existingBits := inboundTransports(c.Protocol, c.StreamSettings, c.Settings)
  177. shared := existingBits & newBits
  178. if shared == 0 {
  179. continue
  180. }
  181. return &portConflictDetail{
  182. InboundID: c.Id,
  183. Remark: c.Remark,
  184. Tag: c.Tag,
  185. Listen: c.Listen,
  186. Port: c.Port,
  187. Transports: shared,
  188. }, nil
  189. }
  190. return nil, nil
  191. }
  192. func sameNode(a, b *int) bool {
  193. if a == nil && b == nil {
  194. return true
  195. }
  196. if a == nil || b == nil {
  197. return false
  198. }
  199. return *a == *b
  200. }
  201. func baseInboundTag(port int) string {
  202. return fmt.Sprintf("in-%v", port)
  203. }
  204. func transportTagSuffix(b transportBits) string {
  205. switch b {
  206. case transportTCP:
  207. return "tcp"
  208. case transportUDP:
  209. return "udp"
  210. case transportTCP | transportUDP:
  211. return "tcpudp"
  212. }
  213. return "any"
  214. }
  215. // nodeTagPrefix scopes a tag to one remote node so the same listen+port
  216. // can live on the central panel and on a node without bumping the global
  217. // UNIQUE(inbounds.tag) constraint. nil → "" (local panel).
  218. func nodeTagPrefix(nodeID *int) string {
  219. if nodeID == nil {
  220. return ""
  221. }
  222. return fmt.Sprintf("n%d-", *nodeID)
  223. }
  224. func composeInboundTag(port int, nodeID *int, bits transportBits) string {
  225. return nodeTagPrefix(nodeID) + baseInboundTag(port) + "-" + transportTagSuffix(bits)
  226. }
  227. func isAutoGeneratedTag(tag string, port int, nodeID *int, bits transportBits) bool {
  228. base := composeInboundTag(port, nodeID, bits)
  229. if tag == base {
  230. return true
  231. }
  232. suffix, ok := strings.CutPrefix(tag, base+"-")
  233. if !ok || suffix == "" {
  234. return false
  235. }
  236. for _, r := range suffix {
  237. if r < '0' || r > '9' {
  238. return false
  239. }
  240. }
  241. return true
  242. }
  243. func (s *InboundService) generateInboundTag(inbound *model.Inbound, ignoreId int) (string, error) {
  244. bits := inboundTransports(inbound.Protocol, inbound.StreamSettings, inbound.Settings)
  245. candidate := composeInboundTag(inbound.Port, inbound.NodeID, bits)
  246. exists, err := s.tagExists(candidate, ignoreId)
  247. if err != nil {
  248. return "", err
  249. }
  250. if !exists {
  251. return candidate, nil
  252. }
  253. for i := 2; i < 100; i++ {
  254. c := fmt.Sprintf("%s-%d", candidate, i)
  255. exists, err = s.tagExists(c, ignoreId)
  256. if err != nil {
  257. return "", err
  258. }
  259. if !exists {
  260. return c, nil
  261. }
  262. }
  263. return "", common.NewError("could not pick a unique inbound tag for port:", inbound.Port)
  264. }
  265. func (s *InboundService) resolveInboundTag(inbound *model.Inbound, ignoreId int) (string, error) {
  266. if inbound.Tag != "" {
  267. taken, err := s.tagExists(inbound.Tag, ignoreId)
  268. if err != nil {
  269. return "", err
  270. }
  271. if !taken {
  272. return inbound.Tag, nil
  273. }
  274. }
  275. return s.generateInboundTag(inbound, ignoreId)
  276. }
  277. func (s *InboundService) tagExists(tag string, ignoreId int) (bool, error) {
  278. db := database.GetDB()
  279. q := db.Model(model.Inbound{}).Where("tag = ?", tag)
  280. if ignoreId > 0 {
  281. q = q.Where("id != ?", ignoreId)
  282. }
  283. var count int64
  284. if err := q.Count(&count).Error; err != nil {
  285. return false, err
  286. }
  287. return count > 0, nil
  288. }