xray_bind_conflict.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. bindLeft := bindAddr{listen: listenLeft, v6only: streamV6Only(string(left.StreamSettings))}
  63. bindRight := bindAddr{listen: listenRight, v6only: streamV6Only(string(right.StreamSettings))}
  64. if !listenOverlaps(bindLeft, bindRight) {
  65. continue
  66. }
  67. // One port carrying tcp on one inbound and udp on another is a
  68. // supported deployment (vless/tcp + hysteria2/udp), never a clash.
  69. shared := configTransports(left) & configTransports(right)
  70. if shared == 0 {
  71. continue
  72. }
  73. conflicts = append(conflicts, bindConflict{
  74. tagA: left.Tag,
  75. tagB: right.Tag,
  76. listen: listenLeft,
  77. listenB: listenRight,
  78. port: port,
  79. shared: shared,
  80. })
  81. }
  82. }
  83. }
  84. // Port grouping iterates a map: order the report so the first conflict the
  85. // caller shows is the same on every restart.
  86. slices.SortFunc(conflicts, func(a, b bindConflict) int {
  87. return cmp.Or(cmp.Compare(a.port, b.port), cmp.Compare(a.tagA, b.tagA), cmp.Compare(a.tagB, b.tagB))
  88. })
  89. return conflicts
  90. }
  91. // runningBindPairs is what the core is demonstrably binding right now, keyed the
  92. // way a new config's conflicts are, so only the identical one is excused.
  93. func runningBindPairs(cfg *xray.Config) map[string]struct{} {
  94. conflicts := rawBindConflicts(cfg)
  95. if len(conflicts) == 0 {
  96. return nil
  97. }
  98. pairs := make(map[string]struct{}, len(conflicts))
  99. for _, c := range conflicts {
  100. pairs[bindPairKey(c)] = struct{}{}
  101. }
  102. return pairs
  103. }
  104. // bindPairKey is the socket set an excuse was granted for: the two listens, the
  105. // port and the shared transports, never the tags, which the generator reorders.
  106. func bindPairKey(c bindConflict) string {
  107. left, right := c.listen, c.listenB
  108. if left > right {
  109. left, right = right, left
  110. }
  111. return fmt.Sprintf("%d\x00%s\x00%s\x00%d", c.port, left, right, c.shared)
  112. }
  113. func displayListen(listen string) string {
  114. if isAnyListen(listen) {
  115. return "*"
  116. }
  117. return listen
  118. }
  119. // configListen decodes a generated inbound's listen field: absent or empty means
  120. // every address, which is what the core does with an empty listen too.
  121. func configListen(raw json_util.RawMessage) string {
  122. var listen string
  123. if len(raw) == 0 || json.Unmarshal(raw, &listen) != nil {
  124. return ""
  125. }
  126. return listen
  127. }
  128. // configTransports reads a generated inbound's transports through the same rule
  129. // the save-time guards use, so the two cannot drift apart.
  130. func configTransports(ib *xray.InboundConfig) transportBits {
  131. // "socks" is the panel's own bridge shape (injectAmneziawgnetSocks and
  132. // friends): its udp flag lives in settings like a mixed inbound's.
  133. if ib.Protocol == "socks" {
  134. return inboundTransports(model.Mixed, string(ib.StreamSettings), string(ib.Settings))
  135. }
  136. return inboundTransports(model.Protocol(ib.Protocol), string(ib.StreamSettings), string(ib.Settings))
  137. }