1
0

inbound_protocol.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package service
  2. import (
  3. "encoding/json"
  4. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  5. )
  6. // inboundShadowsocksMethod extracts settings.method for Shadowsocks inbounds so
  7. // the client UI can generate a valid PSK (base64 of the method's key length)
  8. // for Shadowsocks 2022 ciphers. Returns "" for non-Shadowsocks inbounds.
  9. func inboundShadowsocksMethod(protocol, settings string) string {
  10. if protocol != string(model.Shadowsocks) || settings == "" {
  11. return ""
  12. }
  13. var s struct {
  14. Method string `json:"method"`
  15. }
  16. if err := json.Unmarshal([]byte(settings), &s); err != nil {
  17. return ""
  18. }
  19. return s.Method
  20. }
  21. // inboundCanEnableTlsFlow mirrors canEnableTlsFlow() from the frontend
  22. // (frontend/src/lib/xray/protocol-capabilities.ts). XTLS Vision is valid for
  23. // VLESS on TCP with tls or reality (classic), and on XHTTP when VLESS encryption
  24. // (vlessenc / ML-KEM) is enabled — there the post-quantum, VLESS-level
  25. // encryption stands in for the transport TLS that Vision relies on. settings is
  26. // the inbound's raw settings JSON, which carries the encryption value
  27. // (streamSettings does not).
  28. func inboundCanEnableTlsFlow(protocol, streamSettings, settings string) bool {
  29. if protocol != string(model.VLESS) {
  30. return false
  31. }
  32. if streamSettings == "" {
  33. return false
  34. }
  35. var stream struct {
  36. Network string `json:"network"`
  37. Security string `json:"security"`
  38. }
  39. if err := json.Unmarshal([]byte(streamSettings), &stream); err != nil {
  40. return false
  41. }
  42. switch stream.Network {
  43. case "tcp":
  44. return stream.Security == "tls" || stream.Security == "reality"
  45. case "xhttp":
  46. return vlessEncryptionEnabled(settings)
  47. default:
  48. return false
  49. }
  50. }
  51. // vlessEncryptionEnabled reports whether a VLESS inbound has VLESS-level
  52. // encryption (vlessenc / ML-KEM) configured. When enabled these fields hold a
  53. // generated dotted string (e.g. "mlkem768x25519plus.native.0rtt.<key>"); "none"
  54. // or empty means off. The value is never the literal "vlessenc" — that is the
  55. // name of the `xray vlessenc` CLI subcommand, not a stored value.
  56. //
  57. // Both fields are checked: decryption is the authoritative server-side value
  58. // xray-core reads, while encryption is stored by the panel for link generation.
  59. // The ML-KEM/X25519 buttons set both, but accepting either keeps the gate
  60. // working for inbounds configured via the API or raw JSON.
  61. func vlessEncryptionEnabled(settings string) bool {
  62. if settings == "" {
  63. return false
  64. }
  65. var s struct {
  66. Encryption string `json:"encryption"`
  67. Decryption string `json:"decryption"`
  68. }
  69. if err := json.Unmarshal([]byte(settings), &s); err != nil {
  70. return false
  71. }
  72. return vlessEncValueSet(s.Encryption) || vlessEncValueSet(s.Decryption)
  73. }
  74. // vlessEncValueSet reports whether a VLESS encryption/decryption field holds a
  75. // real (generated) value rather than the "none"/empty sentinel.
  76. func vlessEncValueSet(v string) bool {
  77. return v != "" && v != "none"
  78. }
  79. // inboundCanHostFallbacks gates the settings.fallbacks injection.
  80. // Xray only honors fallbacks on VLESS and Trojan inbounds carried over
  81. // TCP transport with TLS or Reality security. This is intentionally stricter
  82. // than inboundCanEnableTlsFlow (which also accepts XHTTP+vlessenc): fallbacks
  83. // are a raw-TCP-only feature.
  84. func inboundCanHostFallbacks(ib *model.Inbound) bool {
  85. if ib == nil {
  86. return false
  87. }
  88. if ib.Protocol != model.VLESS && ib.Protocol != model.Trojan {
  89. return false
  90. }
  91. return streamSupportsFallbacks(ib.StreamSettings)
  92. }
  93. // streamSupportsFallbacks reports whether the stream is raw TCP carried over
  94. // TLS or REALITY — the only transport Xray honors inbound fallbacks on (and the
  95. // classic requirement for XTLS Vision before vlessenc).
  96. func streamSupportsFallbacks(streamSettings string) bool {
  97. if streamSettings == "" {
  98. return false
  99. }
  100. var stream struct {
  101. Network string `json:"network"`
  102. Security string `json:"security"`
  103. }
  104. if err := json.Unmarshal([]byte(streamSettings), &stream); err != nil {
  105. return false
  106. }
  107. if stream.Network != "tcp" {
  108. return false
  109. }
  110. return stream.Security == "tls" || stream.Security == "reality"
  111. }