port_conflict.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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(listen string, port int) string {
  157. if isAnyListen(listen) {
  158. return fmt.Sprintf("in-%v", port)
  159. }
  160. return fmt.Sprintf("in-%v:%v", listen, port)
  161. }
  162. func transportTagSuffix(b transportBits) string {
  163. switch b {
  164. case transportTCP:
  165. return "tcp"
  166. case transportUDP:
  167. return "udp"
  168. case transportTCP | transportUDP:
  169. return "tcpudp"
  170. }
  171. return "any"
  172. }
  173. // nodeTagPrefix scopes a tag to one remote node so the same listen+port
  174. // can live on the central panel and on a node without bumping the global
  175. // UNIQUE(inbounds.tag) constraint. nil → "" (local panel).
  176. func nodeTagPrefix(nodeID *int) string {
  177. if nodeID == nil {
  178. return ""
  179. }
  180. return fmt.Sprintf("n%d-", *nodeID)
  181. }
  182. func composeInboundTag(listen string, port int, nodeID *int, bits transportBits) string {
  183. return nodeTagPrefix(nodeID) + baseInboundTag(listen, port) + "-" + transportTagSuffix(bits)
  184. }
  185. func (s *InboundService) generateInboundTag(inbound *model.Inbound, ignoreId int) (string, error) {
  186. bits := inboundTransports(inbound.Protocol, inbound.StreamSettings, inbound.Settings)
  187. candidate := composeInboundTag(inbound.Listen, inbound.Port, inbound.NodeID, bits)
  188. exists, err := s.tagExists(candidate, ignoreId)
  189. if err != nil {
  190. return "", err
  191. }
  192. if !exists {
  193. return candidate, nil
  194. }
  195. for i := 2; i < 100; i++ {
  196. c := fmt.Sprintf("%s-%d", candidate, i)
  197. exists, err = s.tagExists(c, ignoreId)
  198. if err != nil {
  199. return "", err
  200. }
  201. if !exists {
  202. return c, nil
  203. }
  204. }
  205. return "", common.NewError("could not pick a unique inbound tag for port:", inbound.Port)
  206. }
  207. func (s *InboundService) resolveInboundTag(inbound *model.Inbound, ignoreId int) (string, error) {
  208. if inbound.Tag != "" {
  209. taken, err := s.tagExists(inbound.Tag, ignoreId)
  210. if err != nil {
  211. return "", err
  212. }
  213. if !taken {
  214. return inbound.Tag, nil
  215. }
  216. }
  217. return s.generateInboundTag(inbound, ignoreId)
  218. }
  219. func (s *InboundService) tagExists(tag string, ignoreId int) (bool, error) {
  220. db := database.GetDB()
  221. q := db.Model(model.Inbound{}).Where("tag = ?", tag)
  222. if ignoreId > 0 {
  223. q = q.Where("id != ?", ignoreId)
  224. }
  225. var count int64
  226. if err := q.Count(&count).Error; err != nil {
  227. return false, err
  228. }
  229. return count > 0, nil
  230. }