xray_bind_conflict.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package service
  2. import (
  3. "cmp"
  4. "encoding/json"
  5. "fmt"
  6. "slices"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. "github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
  9. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  10. )
  11. // bindConflict names two generated inbounds whose listens cannot coexist.
  12. type bindConflict struct {
  13. tagA string
  14. tagB string
  15. listen string
  16. listenB string
  17. port int
  18. shared transportBits
  19. }
  20. func (c bindConflict) String() string {
  21. return fmt.Sprintf("inbounds %q and %q both bind %s:%d (%s)",
  22. c.tagA, c.tagB, displayListen(c.listen), c.port, transportTagSuffix(c.shared))
  23. }
  24. // bindConflicts reports inbounds of newCfg whose sockets collide. A collision the
  25. // running config already serves is excused: it is demonstration, not a guess.
  26. func bindConflicts(newCfg, runningCfg *xray.Config) []bindConflict {
  27. conflicts := rawBindConflicts(newCfg)
  28. if len(conflicts) == 0 {
  29. return nil
  30. }
  31. excused := runningBindPairs(runningCfg)
  32. if len(excused) == 0 {
  33. return conflicts
  34. }
  35. kept := make([]bindConflict, 0, len(conflicts))
  36. for _, c := range conflicts {
  37. if _, ok := excused[bindPairKey(c)]; !ok {
  38. kept = append(kept, c)
  39. }
  40. }
  41. return kept
  42. }
  43. // rawBindConflicts groups inbounds by port first: only a port two inbounds share
  44. // is worth parsing transports for, which keeps the probe free on clean configs.
  45. func rawBindConflicts(cfg *xray.Config) []bindConflict {
  46. if cfg == nil {
  47. return nil
  48. }
  49. byPort := make(map[int][]*xray.InboundConfig, len(cfg.InboundConfigs))
  50. for i := range cfg.InboundConfigs {
  51. ib := &cfg.InboundConfigs[i]
  52. if ib.Port > 0 {
  53. byPort[ib.Port] = append(byPort[ib.Port], ib)
  54. }
  55. }
  56. var conflicts []bindConflict
  57. for port, group := range byPort {
  58. for i := range group {
  59. for j := i + 1; j < len(group); j++ {
  60. left, right := group[i], group[j]
  61. listenLeft, listenRight := configListen(left.Listen), configListen(right.Listen)
  62. if !listenOverlaps(listenLeft, listenRight) {
  63. continue
  64. }
  65. // One port carrying tcp on one inbound and udp on another is a
  66. // supported deployment (vless/tcp + hysteria2/udp), never a clash.
  67. shared := configTransports(left) & configTransports(right)
  68. if shared == 0 {
  69. continue
  70. }
  71. conflicts = append(conflicts, bindConflict{
  72. tagA: left.Tag,
  73. tagB: right.Tag,
  74. listen: listenLeft,
  75. listenB: listenRight,
  76. port: port,
  77. shared: shared,
  78. })
  79. }
  80. }
  81. }
  82. // Port grouping iterates a map: order the report so the first conflict the
  83. // caller shows is the same on every restart.
  84. slices.SortFunc(conflicts, func(a, b bindConflict) int {
  85. return cmp.Or(cmp.Compare(a.port, b.port), cmp.Compare(a.tagA, b.tagA), cmp.Compare(a.tagB, b.tagB))
  86. })
  87. return conflicts
  88. }
  89. // runningBindPairs is what the core is demonstrably binding right now, keyed the
  90. // way a new config's conflicts are, so only the identical one is excused.
  91. func runningBindPairs(cfg *xray.Config) map[string]struct{} {
  92. conflicts := rawBindConflicts(cfg)
  93. if len(conflicts) == 0 {
  94. return nil
  95. }
  96. pairs := make(map[string]struct{}, len(conflicts))
  97. for _, c := range conflicts {
  98. pairs[bindPairKey(c)] = struct{}{}
  99. }
  100. return pairs
  101. }
  102. // bindPairKey is the socket set an excuse was granted for: the two listens, the
  103. // port and the shared transports, never the tags, which the generator reorders.
  104. func bindPairKey(c bindConflict) string {
  105. left, right := c.listen, c.listenB
  106. if left > right {
  107. left, right = right, left
  108. }
  109. return fmt.Sprintf("%d\x00%s\x00%s\x00%d", c.port, left, right, c.shared)
  110. }
  111. func displayListen(listen string) string {
  112. if isAnyListen(listen) {
  113. return "*"
  114. }
  115. return listen
  116. }
  117. // configListen decodes a generated inbound's listen field: absent or empty means
  118. // every address, which is what the core does with an empty listen too.
  119. func configListen(raw json_util.RawMessage) string {
  120. var listen string
  121. if len(raw) == 0 || json.Unmarshal(raw, &listen) != nil {
  122. return ""
  123. }
  124. return listen
  125. }
  126. // configTransports reads a generated inbound's transports through the same rule
  127. // the save-time guards use, so the two cannot drift apart.
  128. func configTransports(ib *xray.InboundConfig) transportBits {
  129. // "socks" is the panel's own bridge shape (injectAmneziawgnetSocks and
  130. // friends): its udp flag lives in settings like a mixed inbound's.
  131. if ib.Protocol == "socks" {
  132. return inboundTransports(model.Mixed, string(ib.StreamSettings), string(ib.Settings))
  133. }
  134. return inboundTransports(model.Protocol(ib.Protocol), string(ib.StreamSettings), string(ib.Settings))
  135. }