inbound_protocol_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package service
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  5. )
  6. // A representative vlessenc/ML-KEM encryption value as produced by `xray
  7. // vlessenc` — a dotted string, never the literal "vlessenc".
  8. const vlessEncValue = "mlkem768x25519plus.native.0rtt.G3cdPSd1-NnlpTbWNSM5vHsT5VNzWfFzYSKwbUMnV1Y"
  9. func TestInboundCanEnableTlsFlow(t *testing.T) {
  10. cases := []struct {
  11. name string
  12. protocol string
  13. streamSettings string
  14. settings string
  15. want bool
  16. }{
  17. {"vless tcp tls", string(model.VLESS), `{"network":"tcp","security":"tls"}`, "", true},
  18. {"vless tcp reality", string(model.VLESS), `{"network":"tcp","security":"reality"}`, "", true},
  19. {"vless tcp none no enc", string(model.VLESS), `{"network":"tcp","security":"none"}`, "", false},
  20. {"vless ws tls", string(model.VLESS), `{"network":"ws","security":"tls"}`, "", false},
  21. {"vless grpc reality", string(model.VLESS), `{"network":"grpc","security":"reality"}`, "", false},
  22. {"vmess tcp tls", string(model.VMESS), `{"network":"tcp","security":"tls"}`, "", false},
  23. {"empty stream", string(model.VLESS), "", "", false},
  24. // vlessenc is gated to XHTTP only. TCP without tls/reality is NOT
  25. // Vision-capable even with vlessenc set — the combination only works on
  26. // XHTTP in practice.
  27. {"vless tcp vlessenc not capable", string(model.VLESS), `{"network":"tcp","security":"none"}`, `{"decryption":"mlkem768x25519plus.native.600s.mMFxPe7lz5xoq2qBk22cQYefu5fpc_2dGR8lMOKem0E","encryption":"mlkem768x25519plus.native.0rtt.hT4AY_tPWY9NVuKR3BIXxXq6zx9DqN2X86QPYW09XEM"}`, false},
  28. // ws is a framed transport — vlessenc never enables Vision there.
  29. {"vless ws vlessenc still off", string(model.VLESS), `{"network":"ws","security":"none"}`, `{"encryption":"` + vlessEncValue + `"}`, false},
  30. // XHTTP + VLESS encryption (the #5157 case).
  31. {"vless xhttp vlessenc", string(model.VLESS), `{"network":"xhttp","security":"none"}`, `{"encryption":"` + vlessEncValue + `"}`, true},
  32. {"vless xhttp encryption none", string(model.VLESS), `{"network":"xhttp","security":"none"}`, `{"encryption":"none"}`, false},
  33. {"vless xhttp no settings", string(model.VLESS), `{"network":"xhttp","security":"none"}`, "", false},
  34. // Regression for PR #5185: the gate is "any non-none encryption", NOT an
  35. // equality check against the literal "vlessenc" (which the buggy PR used
  36. // and which never matches a real, generated encryption value). An x25519
  37. // auth value must enable it just like the ML-KEM value above.
  38. {"vless xhttp x25519 enc", string(model.VLESS), `{"network":"xhttp","security":"none"}`, `{"encryption":"native.0rtt.121s-180s.xRMUYYjQctqYO1pSyffM-w"}`, true},
  39. // Server-side configs (API/JSON) may carry only decryption; that alone
  40. // must also enable the flow gate.
  41. {"vless xhttp decryption only", string(model.VLESS), `{"network":"xhttp","security":"none"}`, `{"decryption":"` + vlessEncValue + `","encryption":"none"}`, true},
  42. // XHTTP without encryption stays off even with tls (Vision over XHTTP is
  43. // gated on vlessenc, not transport security).
  44. {"vless xhttp tls no encryption", string(model.VLESS), `{"network":"xhttp","security":"tls"}`, `{"encryption":"none"}`, false},
  45. }
  46. for _, tc := range cases {
  47. t.Run(tc.name, func(t *testing.T) {
  48. got := inboundCanEnableTlsFlow(tc.protocol, tc.streamSettings, tc.settings)
  49. if got != tc.want {
  50. t.Errorf("inboundCanEnableTlsFlow(%q, %q, %q) = %v, want %v",
  51. tc.protocol, tc.streamSettings, tc.settings, got, tc.want)
  52. }
  53. })
  54. }
  55. }
  56. // Fallbacks must remain raw-TCP-only and must NOT follow the broadened flow gate
  57. // onto XHTTP+vlessenc.
  58. func TestInboundCanHostFallbacks_StaysTcpOnly(t *testing.T) {
  59. cases := []struct {
  60. name string
  61. protocol model.Protocol
  62. streamSettings string
  63. settings string
  64. want bool
  65. }{
  66. {"vless tcp tls", model.VLESS, `{"network":"tcp","security":"tls"}`, "", true},
  67. {"trojan tcp reality", model.Trojan, `{"network":"tcp","security":"reality"}`, "", true},
  68. {"vless xhttp vlessenc not fallback-capable", model.VLESS, `{"network":"xhttp","security":"none"}`, `{"encryption":"` + vlessEncValue + `"}`, false},
  69. {"vmess tcp tls not fallback-capable", model.VMESS, `{"network":"tcp","security":"tls"}`, "", false},
  70. {"nil-ish empty stream", model.VLESS, "", "", false},
  71. }
  72. for _, tc := range cases {
  73. t.Run(tc.name, func(t *testing.T) {
  74. ib := &model.Inbound{Protocol: tc.protocol, StreamSettings: tc.streamSettings, Settings: tc.settings}
  75. if got := inboundCanHostFallbacks(ib); got != tc.want {
  76. t.Errorf("inboundCanHostFallbacks = %v, want %v", got, tc.want)
  77. }
  78. })
  79. }
  80. if inboundCanHostFallbacks(nil) {
  81. t.Errorf("inboundCanHostFallbacks(nil) = true, want false")
  82. }
  83. }