port_conflict.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "github.com/mhsanaei/3x-ui/v3/database"
  7. "github.com/mhsanaei/3x-ui/v3/database/model"
  8. "github.com/mhsanaei/3x-ui/v3/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 {
  106. name = fmt.Sprintf("'%s' (#%d)", name, d.InboundID)
  107. }
  108. listen := d.Listen
  109. if isAnyListen(listen) {
  110. listen = "*"
  111. }
  112. return fmt.Sprintf("port %d (%s) already used by inbound %s on %s",
  113. d.Port, transportTagSuffix(d.Transports), name, listen)
  114. }
  115. func (s *InboundService) checkPortConflict(inbound *model.Inbound, ignoreId int) (*portConflictDetail, error) {
  116. db := database.GetDB()
  117. var candidates []*model.Inbound
  118. q := db.Model(model.Inbound{}).Where("port = ?", inbound.Port)
  119. if ignoreId > 0 {
  120. q = q.Where("id != ?", ignoreId)
  121. }
  122. if err := q.Find(&candidates).Error; err != nil {
  123. return nil, err
  124. }
  125. newBits := inboundTransports(inbound.Protocol, inbound.StreamSettings, inbound.Settings)
  126. for _, c := range candidates {
  127. if !sameNode(c.NodeID, inbound.NodeID) {
  128. continue
  129. }
  130. if !listenOverlaps(c.Listen, inbound.Listen) {
  131. continue
  132. }
  133. existingBits := inboundTransports(c.Protocol, c.StreamSettings, c.Settings)
  134. shared := existingBits & newBits
  135. if shared == 0 {
  136. continue
  137. }
  138. return &portConflictDetail{
  139. InboundID: c.Id,
  140. Remark: c.Remark,
  141. Tag: c.Tag,
  142. Listen: c.Listen,
  143. Port: c.Port,
  144. Transports: shared,
  145. }, nil
  146. }
  147. return nil, nil
  148. }
  149. func sameNode(a, b *int) bool {
  150. if a == nil && b == nil {
  151. return true
  152. }
  153. if a == nil || b == nil {
  154. return false
  155. }
  156. return *a == *b
  157. }
  158. func baseInboundTag(port int) string {
  159. return fmt.Sprintf("in-%v", port)
  160. }
  161. func transportTagSuffix(b transportBits) string {
  162. switch b {
  163. case transportTCP:
  164. return "tcp"
  165. case transportUDP:
  166. return "udp"
  167. case transportTCP | transportUDP:
  168. return "tcpudp"
  169. }
  170. return "any"
  171. }
  172. // nodeTagPrefix scopes a tag to one remote node so the same listen+port
  173. // can live on the central panel and on a node without bumping the global
  174. // UNIQUE(inbounds.tag) constraint. nil → "" (local panel).
  175. func nodeTagPrefix(nodeID *int) string {
  176. if nodeID == nil {
  177. return ""
  178. }
  179. return fmt.Sprintf("n%d-", *nodeID)
  180. }
  181. func composeInboundTag(port int, nodeID *int, bits transportBits) string {
  182. return nodeTagPrefix(nodeID) + baseInboundTag(port) + "-" + transportTagSuffix(bits)
  183. }
  184. func isAutoGeneratedTag(tag string, port int, nodeID *int, bits transportBits) bool {
  185. base := composeInboundTag(port, nodeID, bits)
  186. if tag == base {
  187. return true
  188. }
  189. suffix, ok := strings.CutPrefix(tag, base+"-")
  190. if !ok || suffix == "" {
  191. return false
  192. }
  193. for _, r := range suffix {
  194. if r < '0' || r > '9' {
  195. return false
  196. }
  197. }
  198. return true
  199. }
  200. func (s *InboundService) generateInboundTag(inbound *model.Inbound, ignoreId int) (string, error) {
  201. bits := inboundTransports(inbound.Protocol, inbound.StreamSettings, inbound.Settings)
  202. candidate := composeInboundTag(inbound.Port, inbound.NodeID, bits)
  203. exists, err := s.tagExists(candidate, ignoreId)
  204. if err != nil {
  205. return "", err
  206. }
  207. if !exists {
  208. return candidate, nil
  209. }
  210. for i := 2; i < 100; i++ {
  211. c := fmt.Sprintf("%s-%d", candidate, i)
  212. exists, err = s.tagExists(c, ignoreId)
  213. if err != nil {
  214. return "", err
  215. }
  216. if !exists {
  217. return c, nil
  218. }
  219. }
  220. return "", common.NewError("could not pick a unique inbound tag for port:", inbound.Port)
  221. }
  222. func (s *InboundService) resolveInboundTag(inbound *model.Inbound, ignoreId int) (string, error) {
  223. if inbound.Tag != "" {
  224. taken, err := s.tagExists(inbound.Tag, ignoreId)
  225. if err != nil {
  226. return "", err
  227. }
  228. if !taken {
  229. return inbound.Tag, nil
  230. }
  231. }
  232. return s.generateInboundTag(inbound, ignoreId)
  233. }
  234. func (s *InboundService) tagExists(tag string, ignoreId int) (bool, error) {
  235. db := database.GetDB()
  236. q := db.Model(model.Inbound{}).Where("tag = ?", tag)
  237. if ignoreId > 0 {
  238. q = q.Where("id != ?", ignoreId)
  239. }
  240. var count int64
  241. if err := q.Count(&count).Error; err != nil {
  242. return false, err
  243. }
  244. return count > 0, nil
  245. }