1
0

port_conflict.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. }
  21. var bits transportBits
  22. // peek at streamSettings.network to spot udp-based transports.
  23. // parse errors are non-fatal: missing or weird streamSettings just
  24. // keeps the default tcp bit below.
  25. network := ""
  26. if streamSettings != "" {
  27. var ss map[string]any
  28. if json.Unmarshal([]byte(streamSettings), &ss) == nil {
  29. if n, _ := ss["network"].(string); n != "" {
  30. network = n
  31. }
  32. }
  33. }
  34. switch network {
  35. case "kcp", "quic":
  36. bits |= transportUDP
  37. default:
  38. bits |= transportTCP
  39. }
  40. // a few protocols carry their L4 choice in settings instead of (or in
  41. // addition to) streamSettings: SS / Tunnel via a CSV field that wins
  42. // outright, Mixed via an additive udp boolean.
  43. if settings != "" {
  44. var st map[string]any
  45. if json.Unmarshal([]byte(settings), &st) == nil {
  46. switch protocol {
  47. case model.Shadowsocks, model.Tunnel:
  48. key := "network"
  49. if protocol == model.Tunnel {
  50. key = "allowedNetwork"
  51. }
  52. if n, ok := st[key].(string); ok && n != "" {
  53. bits = 0
  54. for part := range strings.SplitSeq(n, ",") {
  55. switch strings.TrimSpace(part) {
  56. case "tcp":
  57. bits |= transportTCP
  58. case "udp":
  59. bits |= transportUDP
  60. }
  61. }
  62. }
  63. case model.Mixed:
  64. // socks/http "mixed" inbound: settings.udp=true means it
  65. // also relays udp on the same port (socks5 udp associate).
  66. if udpOn, _ := st["udp"].(bool); udpOn {
  67. bits |= transportUDP
  68. }
  69. }
  70. }
  71. }
  72. // safety net: never return zero, even if every parse failed.
  73. if bits == 0 {
  74. bits = transportTCP
  75. }
  76. return bits
  77. }
  78. func listenOverlaps(a, b string) bool {
  79. if isAnyListen(a) || isAnyListen(b) {
  80. return true
  81. }
  82. return a == b
  83. }
  84. func isAnyListen(s string) bool {
  85. return s == "" || s == "0.0.0.0" || s == "::" || s == "::0"
  86. }
  87. type portConflictDetail struct {
  88. InboundID int
  89. Remark string
  90. Tag string
  91. Listen string
  92. Port int
  93. Transports transportBits
  94. }
  95. // String renders the detail as a single-line, user-facing summary.
  96. func (d *portConflictDetail) String() string {
  97. name := d.Remark
  98. if name == "" {
  99. name = d.Tag
  100. }
  101. if name == "" {
  102. name = fmt.Sprintf("#%d", d.InboundID)
  103. } else {
  104. name = fmt.Sprintf("'%s' (#%d)", name, d.InboundID)
  105. }
  106. listen := d.Listen
  107. if isAnyListen(listen) {
  108. listen = "*"
  109. }
  110. return fmt.Sprintf("port %d (%s) already used by inbound %s on %s",
  111. d.Port, transportTagSuffix(d.Transports), name, listen)
  112. }
  113. func (s *InboundService) checkPortConflict(inbound *model.Inbound, ignoreId int) (*portConflictDetail, error) {
  114. db := database.GetDB()
  115. var candidates []*model.Inbound
  116. q := db.Model(model.Inbound{}).Where("port = ?", inbound.Port)
  117. if ignoreId > 0 {
  118. q = q.Where("id != ?", ignoreId)
  119. }
  120. if err := q.Find(&candidates).Error; err != nil {
  121. return nil, err
  122. }
  123. newBits := inboundTransports(inbound.Protocol, inbound.StreamSettings, inbound.Settings)
  124. for _, c := range candidates {
  125. if !sameNode(c.NodeID, inbound.NodeID) {
  126. continue
  127. }
  128. if !listenOverlaps(c.Listen, inbound.Listen) {
  129. continue
  130. }
  131. existingBits := inboundTransports(c.Protocol, c.StreamSettings, c.Settings)
  132. shared := existingBits & newBits
  133. if shared == 0 {
  134. continue
  135. }
  136. return &portConflictDetail{
  137. InboundID: c.Id,
  138. Remark: c.Remark,
  139. Tag: c.Tag,
  140. Listen: c.Listen,
  141. Port: c.Port,
  142. Transports: shared,
  143. }, nil
  144. }
  145. return nil, nil
  146. }
  147. func sameNode(a, b *int) bool {
  148. if a == nil && b == nil {
  149. return true
  150. }
  151. if a == nil || b == nil {
  152. return false
  153. }
  154. return *a == *b
  155. }
  156. func baseInboundTag(port int) string {
  157. return fmt.Sprintf("in-%v", port)
  158. }
  159. func transportTagSuffix(b transportBits) string {
  160. switch b {
  161. case transportTCP:
  162. return "tcp"
  163. case transportUDP:
  164. return "udp"
  165. case transportTCP | transportUDP:
  166. return "tcpudp"
  167. }
  168. return "any"
  169. }
  170. // nodeTagPrefix scopes a tag to one remote node so the same listen+port
  171. // can live on the central panel and on a node without bumping the global
  172. // UNIQUE(inbounds.tag) constraint. nil → "" (local panel).
  173. func nodeTagPrefix(nodeID *int) string {
  174. if nodeID == nil {
  175. return ""
  176. }
  177. return fmt.Sprintf("n%d-", *nodeID)
  178. }
  179. func composeInboundTag(port int, nodeID *int, bits transportBits) string {
  180. return nodeTagPrefix(nodeID) + baseInboundTag(port) + "-" + transportTagSuffix(bits)
  181. }
  182. func isAutoGeneratedTag(tag string, port int, nodeID *int, bits transportBits) bool {
  183. base := composeInboundTag(port, nodeID, bits)
  184. if tag == base {
  185. return true
  186. }
  187. suffix, ok := strings.CutPrefix(tag, base+"-")
  188. if !ok || suffix == "" {
  189. return false
  190. }
  191. for _, r := range suffix {
  192. if r < '0' || r > '9' {
  193. return false
  194. }
  195. }
  196. return true
  197. }
  198. func (s *InboundService) generateInboundTag(inbound *model.Inbound, ignoreId int) (string, error) {
  199. bits := inboundTransports(inbound.Protocol, inbound.StreamSettings, inbound.Settings)
  200. candidate := composeInboundTag(inbound.Port, inbound.NodeID, bits)
  201. exists, err := s.tagExists(candidate, ignoreId)
  202. if err != nil {
  203. return "", err
  204. }
  205. if !exists {
  206. return candidate, nil
  207. }
  208. for i := 2; i < 100; i++ {
  209. c := fmt.Sprintf("%s-%d", candidate, i)
  210. exists, err = s.tagExists(c, ignoreId)
  211. if err != nil {
  212. return "", err
  213. }
  214. if !exists {
  215. return c, nil
  216. }
  217. }
  218. return "", common.NewError("could not pick a unique inbound tag for port:", inbound.Port)
  219. }
  220. func (s *InboundService) resolveInboundTag(inbound *model.Inbound, ignoreId int) (string, error) {
  221. if inbound.Tag != "" {
  222. taken, err := s.tagExists(inbound.Tag, ignoreId)
  223. if err != nil {
  224. return "", err
  225. }
  226. if !taken {
  227. return inbound.Tag, nil
  228. }
  229. }
  230. return s.generateInboundTag(inbound, ignoreId)
  231. }
  232. func (s *InboundService) tagExists(tag string, ignoreId int) (bool, error) {
  233. db := database.GetDB()
  234. q := db.Model(model.Inbound{}).Where("tag = ?", tag)
  235. if ignoreId > 0 {
  236. q = q.Where("id != ?", ignoreId)
  237. }
  238. var count int64
  239. if err := q.Count(&count).Error; err != nil {
  240. return false, err
  241. }
  242. return count > 0, nil
  243. }