inbound_protocol.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 Inbound.canEnableTlsFlow() from the frontend:
  22. // XTLS Vision is only valid for VLESS on TCP with tls or reality.
  23. func inboundCanEnableTlsFlow(protocol, streamSettings string) bool {
  24. if protocol != string(model.VLESS) {
  25. return false
  26. }
  27. if streamSettings == "" {
  28. return false
  29. }
  30. var stream struct {
  31. Network string `json:"network"`
  32. Security string `json:"security"`
  33. }
  34. if err := json.Unmarshal([]byte(streamSettings), &stream); err != nil {
  35. return false
  36. }
  37. if stream.Network != "tcp" {
  38. return false
  39. }
  40. return stream.Security == "tls" || stream.Security == "reality"
  41. }
  42. // inboundCanHostFallbacks gates the settings.fallbacks injection.
  43. // Xray only honors fallbacks on VLESS and Trojan inbounds carried over
  44. // TCP transport with TLS or Reality security.
  45. func inboundCanHostFallbacks(ib *model.Inbound) bool {
  46. if ib == nil {
  47. return false
  48. }
  49. if ib.Protocol != model.VLESS && ib.Protocol != model.Trojan {
  50. return false
  51. }
  52. return inboundCanEnableTlsFlow(string(ib.Protocol), ib.StreamSettings) ||
  53. (ib.Protocol == model.Trojan && trojanStreamSupportsFallbacks(ib.StreamSettings))
  54. }
  55. // trojanStreamSupportsFallbacks mirrors the Trojan side of the same gate
  56. // (Trojan reuses XTLS-Vision capable streams: tcp + tls or reality).
  57. func trojanStreamSupportsFallbacks(streamSettings string) bool {
  58. if streamSettings == "" {
  59. return false
  60. }
  61. var stream struct {
  62. Network string `json:"network"`
  63. Security string `json:"security"`
  64. }
  65. if err := json.Unmarshal([]byte(streamSettings), &stream); err != nil {
  66. return false
  67. }
  68. if stream.Network != "tcp" {
  69. return false
  70. }
  71. return stream.Security == "tls" || stream.Security == "reality"
  72. }