protocol-capabilities.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Pure-function ports of the legacy Inbound class capability predicates
  2. // (canEnableTls, canEnableReality, canEnableTlsFlow, canEnableStream,
  3. // canEnableVisionSeed, isSS2022, isSSMultiUser). Each accepts the minimal
  4. // slice of an InboundFormValues it needs, so the same predicate can be
  5. // called against a partial-row, a full form value, or a hand-built test
  6. // fixture without the caller projecting a whole object.
  7. const TLS_ELIGIBLE_PROTOCOLS = ['vmess', 'vless', 'trojan', 'shadowsocks'];
  8. const TLS_NETWORKS = ['tcp', 'ws', 'http', 'grpc', 'httpupgrade', 'xhttp'];
  9. const REALITY_ELIGIBLE_PROTOCOLS = ['vless', 'trojan'];
  10. const REALITY_NETWORKS = ['tcp', 'http', 'grpc', 'xhttp'];
  11. const STREAM_PROTOCOLS = [
  12. 'vmess',
  13. 'vless',
  14. 'trojan',
  15. 'shadowsocks',
  16. 'hysteria',
  17. 'wireguard',
  18. 'tunnel',
  19. ];
  20. const VISION_FLOW = 'xtls-rprx-vision';
  21. const SS_2022_PREFIX = '2022';
  22. const SS_BLAKE3_CHACHA20 = '2022-blake3-chacha20-poly1305';
  23. export interface CapabilityProtocolSlice {
  24. protocol: string;
  25. settings?: { encryption?: string; decryption?: string };
  26. streamSettings?: { network?: string; security?: string };
  27. }
  28. export interface CapabilityVlessSlice extends CapabilityProtocolSlice {
  29. settings?: { encryption?: string; decryption?: string; clients?: { flow?: string }[] };
  30. }
  31. export interface CapabilityShadowsocksSlice extends CapabilityProtocolSlice {
  32. settings?: { encryption?: string; method?: string };
  33. }
  34. export function canEnableTls(values: CapabilityProtocolSlice): boolean {
  35. if (values.protocol === 'hysteria') return true;
  36. if (!TLS_ELIGIBLE_PROTOCOLS.includes(values.protocol)) return false;
  37. return TLS_NETWORKS.includes(values.streamSettings?.network ?? '');
  38. }
  39. export function canEnableReality(values: CapabilityProtocolSlice): boolean {
  40. if (!REALITY_ELIGIBLE_PROTOCOLS.includes(values.protocol)) return false;
  41. return REALITY_NETWORKS.includes(values.streamSettings?.network ?? '');
  42. }
  43. // VLESS encryption (vlessenc / ML-KEM) is on when encryption or decryption holds
  44. // a generated value (e.g. "mlkem768x25519plus.native.0rtt.<key>") rather than
  45. // the "none"/"" sentinel. The value is never the literal "vlessenc" (that is the
  46. // `xray vlessenc` subcommand). decryption is the server-side value; encryption is
  47. // stored for link generation — either being set means it is on.
  48. function hasVlessEncryption(settings: CapabilityProtocolSlice['settings']): boolean {
  49. const isSet = (v?: string) => v != null && v !== '' && v !== 'none';
  50. return isSet(settings?.encryption) || isSet(settings?.decryption);
  51. }
  52. export function canEnableTlsFlow(values: CapabilityProtocolSlice): boolean {
  53. if (values.protocol !== 'vless') return false;
  54. const network = values.streamSettings?.network;
  55. const security = values.streamSettings?.security;
  56. // Classic XTLS Vision: raw TCP carried over TLS or REALITY.
  57. if (network === 'tcp' && (security === 'tls' || security === 'reality')) return true;
  58. // vlessenc carries Vision over XHTTP without transport TLS.
  59. if (network === 'xhttp' && hasVlessEncryption(values.settings)) return true;
  60. return false;
  61. }
  62. export function canEnableStream(values: { protocol: string }): boolean {
  63. return STREAM_PROTOCOLS.includes(values.protocol);
  64. }
  65. // mtproto and amneziawg are served by an external process/interface, not
  66. // Xray, so the Xray sniffing block does not apply to either. Every other
  67. // inbound supports sniffing.
  68. export function canEnableSniffing(values: { protocol: string }): boolean {
  69. return values.protocol !== 'mtproto' && values.protocol !== 'amneziawg';
  70. }
  71. // Vision seed applies only when XTLS Vision (TCP/TLS) flow is selected
  72. // AND at least one VLESS client uses the vision flow. Excludes UDP variant.
  73. export function canEnableVisionSeed(values: CapabilityVlessSlice): boolean {
  74. if (!canEnableTlsFlow(values)) return false;
  75. const clients = values.settings?.clients;
  76. if (!Array.isArray(clients)) return false;
  77. return clients.some((c) => c?.flow === VISION_FLOW);
  78. }
  79. // Why: legacy returns true on non-SS protocols too (the method getter
  80. // resolves to "" and "" !== blake3-chacha20-poly1305). Preserved for
  81. // parity with the legacy class; in practice the callers all narrow on
  82. // protocol === shadowsocks before checking.
  83. export function isSSMultiUser(values: CapabilityShadowsocksSlice): boolean {
  84. const method = values.protocol === 'shadowsocks' ? (values.settings?.method ?? '') : '';
  85. return method !== SS_BLAKE3_CHACHA20;
  86. }
  87. export function isSS2022(values: CapabilityShadowsocksSlice): boolean {
  88. const method = values.protocol === 'shadowsocks' ? (values.settings?.method ?? '') : '';
  89. return method.substring(0, 4) === SS_2022_PREFIX;
  90. }