inbound_protocol.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package service
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  7. "github.com/mhsanaei/3x-ui/v3/internal/util/version"
  8. "gorm.io/gorm"
  9. )
  10. // inboundShadowsocksMethod extracts settings.method for Shadowsocks inbounds so
  11. // the client UI can generate a valid PSK (base64 of the method's key length)
  12. // for Shadowsocks 2022 ciphers. Returns "" for non-Shadowsocks inbounds.
  13. func inboundShadowsocksMethod(protocol, settings string) string {
  14. if protocol != string(model.Shadowsocks) || settings == "" {
  15. return ""
  16. }
  17. var s struct {
  18. Method string `json:"method"`
  19. }
  20. if err := json.Unmarshal([]byte(settings), &s); err != nil {
  21. return ""
  22. }
  23. return s.Method
  24. }
  25. // inboundCanEnableTlsFlow mirrors the frontend's canEnableTlsFlow: xray-core runs Vision over
  26. // TLS/REALITY on raw TCP, or inside VLESS encryption (encryption.CommonConn) on any transport.
  27. func inboundCanEnableTlsFlow(protocol, streamSettings, settings string) bool {
  28. if protocol != string(model.VLESS) {
  29. return false
  30. }
  31. var stream struct {
  32. Network string `json:"network"`
  33. Security string `json:"security"`
  34. }
  35. if json.Unmarshal([]byte(streamSettings), &stream) == nil && stream.Network == "tcp" &&
  36. (stream.Security == "tls" || stream.Security == "reality") {
  37. return true
  38. }
  39. return vlessEncryptionEnabled(settings)
  40. }
  41. // nodeEligibleProtocols mirrors the frontend's NODE_ELIGIBLE_PROTOCOLS. A sidecar
  42. // protocol's row is local on the node it is pushed to, so that panel runs it.
  43. var nodeEligibleProtocols = map[model.Protocol]bool{
  44. model.VLESS: true,
  45. model.VMESS: true,
  46. model.Trojan: true,
  47. model.Shadowsocks: true,
  48. model.Hysteria: true,
  49. model.WireGuard: true,
  50. model.MTProto: true,
  51. model.AmneziaWG: true,
  52. model.TUIC: true,
  53. model.MASQUE: true,
  54. }
  55. // nodeProtocolFirstRelease is the panel release that introduced each protocol
  56. // newer than node support itself; an older node would hand it to Xray as-is.
  57. var nodeProtocolFirstRelease = map[model.Protocol]string{
  58. model.MTProto: "v3.5.0",
  59. model.AmneziaWG: "v3.7.0",
  60. model.TUIC: "v3.8.0",
  61. model.MASQUE: "v3.9.1",
  62. }
  63. // checkNodeCanHostProtocol refuses assigning protocol to nodeID unless the
  64. // protocol may live on a node and that node's panel is new enough to run it.
  65. func checkNodeCanHostProtocol(db *gorm.DB, nodeID int, protocol model.Protocol) error {
  66. if !nodeEligibleProtocols[protocol] {
  67. return common.NewErrorf("%s inbounds cannot be assigned to a node", protocol)
  68. }
  69. firstRelease, ok := nodeProtocolFirstRelease[protocol]
  70. if !ok {
  71. return nil
  72. }
  73. var node model.Node
  74. if err := db.Select("id", "name", "panel_version").First(&node, nodeID).Error; err != nil {
  75. return err
  76. }
  77. if strings.TrimSpace(node.PanelVersion) == "" {
  78. return common.NewErrorf("node %q has not reported its panel version yet; %s inbounds need %s or newer",
  79. node.Name, protocol, firstRelease)
  80. }
  81. // A dev build reports "dev+<sha>": it tracks main, which carries every protocol.
  82. if cmp, ok := version.Compare(node.PanelVersion, firstRelease); ok && cmp < 0 {
  83. return common.NewErrorf("node %q runs panel %s; %s inbounds need %s or newer",
  84. node.Name, node.PanelVersion, protocol, firstRelease)
  85. }
  86. return nil
  87. }
  88. // vlessEncryptionEnabled reports whether a VLESS inbound has VLESS-level
  89. // encryption (vlessenc / ML-KEM) configured. When enabled these fields hold a
  90. // generated dotted string (e.g. "mlkem768x25519plus.native.0rtt.<key>"); "none"
  91. // or empty means off. The value is never the literal "vlessenc" — that is the
  92. // name of the `xray vlessenc` CLI subcommand, not a stored value.
  93. //
  94. // Both fields are checked: decryption is the authoritative server-side value
  95. // xray-core reads, while encryption is stored by the panel for link generation.
  96. // The ML-KEM/X25519 buttons set both, but accepting either keeps the gate
  97. // working for inbounds configured via the API or raw JSON.
  98. func vlessEncryptionEnabled(settings string) bool {
  99. if settings == "" {
  100. return false
  101. }
  102. var s struct {
  103. Encryption string `json:"encryption"`
  104. Decryption string `json:"decryption"`
  105. }
  106. if err := json.Unmarshal([]byte(settings), &s); err != nil {
  107. return false
  108. }
  109. return vlessEncValueSet(s.Encryption) || vlessEncValueSet(s.Decryption)
  110. }
  111. // vlessEncValueSet reports whether a VLESS encryption/decryption field holds a
  112. // real (generated) value rather than the "none"/empty sentinel.
  113. func vlessEncValueSet(v string) bool {
  114. return v != "" && v != "none"
  115. }
  116. // inboundCanHostFallbacks gates the settings.fallbacks injection.
  117. // Xray only honors fallbacks on VLESS and Trojan inbounds carried over
  118. // TCP transport with TLS or Reality security. This is intentionally stricter
  119. // than inboundCanEnableTlsFlow (which also accepts any vlessenc stream): fallbacks
  120. // are a raw-TCP-only feature.
  121. func inboundCanHostFallbacks(ib *model.Inbound) bool {
  122. if ib == nil {
  123. return false
  124. }
  125. if ib.Protocol != model.VLESS && ib.Protocol != model.Trojan {
  126. return false
  127. }
  128. return streamSupportsFallbacks(ib.StreamSettings)
  129. }
  130. // streamSupportsFallbacks reports whether the stream is raw TCP carried over
  131. // TLS or REALITY — the only transport Xray honors inbound fallbacks on (and the
  132. // classic requirement for XTLS Vision before vlessenc).
  133. func streamSupportsFallbacks(streamSettings string) bool {
  134. if streamSettings == "" {
  135. return false
  136. }
  137. var stream struct {
  138. Network string `json:"network"`
  139. Security string `json:"security"`
  140. }
  141. if err := json.Unmarshal([]byte(streamSettings), &stream); err != nil {
  142. return false
  143. }
  144. if stream.Network != "tcp" {
  145. return false
  146. }
  147. return stream.Security == "tls" || stream.Security == "reality"
  148. }