inbound-defaults.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import { RandomUtil, Wireguard } from '@/utils';
  2. import type { HttpInboundSettings } from '@/schemas/protocols/inbound/http';
  3. import type { Hysteria2InboundSettings } from '@/schemas/protocols/inbound/hysteria2';
  4. import type { HysteriaClient, HysteriaInboundSettings } from '@/schemas/protocols/inbound/hysteria';
  5. import type { MixedInboundSettings } from '@/schemas/protocols/inbound/mixed';
  6. import type { ShadowsocksClient, ShadowsocksInboundSettings } from '@/schemas/protocols/inbound/shadowsocks';
  7. import type { TrojanClient, TrojanInboundSettings } from '@/schemas/protocols/inbound/trojan';
  8. import type { TunnelInboundSettings } from '@/schemas/protocols/inbound/tunnel';
  9. import type { VlessClient, VlessInboundSettings } from '@/schemas/protocols/inbound/vless';
  10. import type { VmessClient, VmessInboundSettings } from '@/schemas/protocols/inbound/vmess';
  11. import type { WireguardInboundSettings } from '@/schemas/protocols/inbound/wireguard';
  12. // Plain-object factories for protocol clients. Each returns a Zod-parsable
  13. // object matching the wire shape. Random fields (id, password, auth,
  14. // email, subId) call RandomUtil at invocation time — pass them in
  15. // `overrides` for deterministic tests or for forms that pre-seed values.
  16. //
  17. // These replace the legacy `new Inbound.<Settings>.<Client>()` constructors
  18. // and the Inbound.ClientBase machinery. Callers no longer carry the
  19. // XrayCommonClass dependency once the swap lands.
  20. interface ClientBaseSeed {
  21. email?: string;
  22. subId?: string;
  23. limitIp?: number;
  24. totalGB?: number;
  25. expiryTime?: number;
  26. enable?: boolean;
  27. tgId?: number;
  28. comment?: string;
  29. reset?: number;
  30. }
  31. interface ClientBase {
  32. email: string;
  33. limitIp: number;
  34. totalGB: number;
  35. expiryTime: number;
  36. enable: boolean;
  37. tgId: number;
  38. subId: string;
  39. comment: string;
  40. reset: number;
  41. }
  42. function clientBase(seed: ClientBaseSeed = {}): ClientBase {
  43. return {
  44. email: seed.email ?? RandomUtil.randomLowerAndNum(8),
  45. limitIp: seed.limitIp ?? 0,
  46. totalGB: seed.totalGB ?? 0,
  47. expiryTime: seed.expiryTime ?? 0,
  48. enable: seed.enable ?? true,
  49. tgId: seed.tgId ?? 0,
  50. subId: seed.subId ?? RandomUtil.randomLowerAndNum(16),
  51. comment: seed.comment ?? '',
  52. reset: seed.reset ?? 0,
  53. };
  54. }
  55. export interface VlessClientSeed extends ClientBaseSeed {
  56. id?: string;
  57. flow?: VlessClient['flow'];
  58. }
  59. export function createDefaultVlessClient(seed: VlessClientSeed = {}): VlessClient {
  60. return {
  61. id: seed.id ?? RandomUtil.randomUUID(),
  62. flow: seed.flow ?? '',
  63. ...clientBase(seed),
  64. };
  65. }
  66. export interface VmessClientSeed extends ClientBaseSeed {
  67. id?: string;
  68. security?: VmessClient['security'];
  69. }
  70. export function createDefaultVmessClient(seed: VmessClientSeed = {}): VmessClient {
  71. return {
  72. id: seed.id ?? RandomUtil.randomUUID(),
  73. security: seed.security ?? 'auto',
  74. ...clientBase(seed),
  75. };
  76. }
  77. export interface TrojanClientSeed extends ClientBaseSeed {
  78. password?: string;
  79. }
  80. export function createDefaultTrojanClient(seed: TrojanClientSeed = {}): TrojanClient {
  81. return {
  82. password: seed.password ?? RandomUtil.randomSeq(10),
  83. ...clientBase(seed),
  84. };
  85. }
  86. export interface ShadowsocksClientSeed extends ClientBaseSeed {
  87. method?: string;
  88. password?: string;
  89. ssMethod?: string;
  90. }
  91. // Shadowsocks clients ship with an empty `method` on single-user inbounds
  92. // (the parent inbound's method is authoritative); only 2022-blake3 multi-
  93. // user inbounds use the per-client method. Callers pass `ssMethod` to seed
  94. // a method-specific password length when creating a multi-user client.
  95. export function createDefaultShadowsocksClient(seed: ShadowsocksClientSeed = {}): ShadowsocksClient {
  96. const method = seed.method ?? '';
  97. const password = seed.password ?? RandomUtil.randomShadowsocksPassword(seed.ssMethod ?? '2022-blake3-aes-256-gcm');
  98. return {
  99. method,
  100. password,
  101. ...clientBase(seed),
  102. };
  103. }
  104. export interface HysteriaClientSeed extends ClientBaseSeed {
  105. auth?: string;
  106. }
  107. export function createDefaultHysteriaClient(seed: HysteriaClientSeed = {}): HysteriaClient {
  108. return {
  109. auth: seed.auth ?? RandomUtil.randomSeq(10),
  110. ...clientBase(seed),
  111. };
  112. }
  113. // Inbound-settings factories. Each returns a Zod-parsable wire-shape with
  114. // schema defaults already applied — no class instance, no XrayCommonClass.
  115. // Callers (form modals via Step 4, InboundsPage clone via Step 5) call
  116. // these instead of the legacy `Inbound.Settings.getSettings(protocol)`.
  117. export function createDefaultVlessInboundSettings(): VlessInboundSettings {
  118. return {
  119. clients: [],
  120. decryption: 'none',
  121. encryption: 'none',
  122. fallbacks: [],
  123. };
  124. }
  125. export function createDefaultVmessInboundSettings(): VmessInboundSettings {
  126. return { clients: [] };
  127. }
  128. export function createDefaultTrojanInboundSettings(): TrojanInboundSettings {
  129. return { clients: [], fallbacks: [] };
  130. }
  131. export interface ShadowsocksInboundSeed {
  132. method?: ShadowsocksInboundSettings['method'];
  133. password?: string;
  134. network?: ShadowsocksInboundSettings['network'];
  135. ivCheck?: boolean;
  136. }
  137. export function createDefaultShadowsocksInboundSettings(
  138. seed: ShadowsocksInboundSeed = {},
  139. ): ShadowsocksInboundSettings {
  140. const method = seed.method ?? '2022-blake3-aes-256-gcm';
  141. return {
  142. method,
  143. password: seed.password ?? RandomUtil.randomShadowsocksPassword(method),
  144. network: seed.network ?? 'tcp',
  145. clients: [],
  146. ivCheck: seed.ivCheck ?? false,
  147. };
  148. }
  149. // Hysteria v1 defaults still emit `version: 2` to match the legacy panel
  150. // constructor — the field discriminates v1 vs v2 inside the same settings
  151. // shape. Callers that explicitly want v1 pass `{ version: 1 }`.
  152. export interface HysteriaInboundSeed {
  153. version?: number;
  154. }
  155. export function createDefaultHysteriaInboundSettings(
  156. seed: HysteriaInboundSeed = {},
  157. ): HysteriaInboundSettings {
  158. return {
  159. version: seed.version ?? 2,
  160. clients: [],
  161. };
  162. }
  163. export function createDefaultHysteria2InboundSettings(): Hysteria2InboundSettings {
  164. return { version: 2, clients: [] };
  165. }
  166. export function createDefaultHttpInboundSettings(): HttpInboundSettings {
  167. return { accounts: [], allowTransparent: false };
  168. }
  169. export function createDefaultMixedInboundSettings(): MixedInboundSettings {
  170. return {
  171. auth: 'password',
  172. accounts: [],
  173. udp: false,
  174. ip: '127.0.0.1',
  175. };
  176. }
  177. export function createDefaultTunnelInboundSettings(): TunnelInboundSettings {
  178. return {
  179. portMap: {},
  180. allowedNetwork: 'tcp,udp',
  181. followRedirect: false,
  182. };
  183. }
  184. export interface WireguardInboundSeed {
  185. mtu?: number;
  186. secretKey?: string;
  187. noKernelTun?: boolean;
  188. }
  189. export function createDefaultWireguardInboundSettings(
  190. seed: WireguardInboundSeed = {},
  191. ): WireguardInboundSettings {
  192. return {
  193. mtu: seed.mtu ?? 1420,
  194. secretKey: seed.secretKey ?? Wireguard.generateKeypair().privateKey,
  195. peers: [],
  196. noKernelTun: seed.noKernelTun ?? false,
  197. };
  198. }
  199. // Protocol-aware dispatch over every inbound-settings factory. Mirrors
  200. // the legacy `Inbound.Settings.getSettings(protocol)` dispatcher, but
  201. // returns a plain Zod-parsable object instead of a class instance.
  202. // Callers swapping off the class hierarchy use this in place of
  203. // `getSettings(p)` + `.toJson()`.
  204. export type AnyInboundSettings =
  205. | VlessInboundSettings
  206. | VmessInboundSettings
  207. | TrojanInboundSettings
  208. | ShadowsocksInboundSettings
  209. | HysteriaInboundSettings
  210. | Hysteria2InboundSettings
  211. | HttpInboundSettings
  212. | MixedInboundSettings
  213. | TunnelInboundSettings
  214. | WireguardInboundSettings;
  215. export function createDefaultInboundSettings(protocol: string): AnyInboundSettings | null {
  216. switch (protocol) {
  217. case 'vless': return createDefaultVlessInboundSettings();
  218. case 'vmess': return createDefaultVmessInboundSettings();
  219. case 'trojan': return createDefaultTrojanInboundSettings();
  220. case 'shadowsocks': return createDefaultShadowsocksInboundSettings();
  221. case 'hysteria': return createDefaultHysteriaInboundSettings();
  222. case 'hysteria2': return createDefaultHysteria2InboundSettings();
  223. case 'http': return createDefaultHttpInboundSettings();
  224. case 'mixed': return createDefaultMixedInboundSettings();
  225. case 'tunnel': return createDefaultTunnelInboundSettings();
  226. case 'wireguard': return createDefaultWireguardInboundSettings();
  227. default: return null;
  228. }
  229. }