inbound-defaults.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import { RandomUtil, Wireguard } from '@/utils';
  2. import type { HttpInboundSettings } from '@/schemas/protocols/inbound/http';
  3. import type { HysteriaClient, HysteriaInboundSettings } from '@/schemas/protocols/inbound/hysteria';
  4. import type { MixedInboundSettings } from '@/schemas/protocols/inbound/mixed';
  5. import type { MtprotoClient, MtprotoInboundSettings } from '@/schemas/protocols/inbound/mtproto';
  6. import type { ShadowsocksClient, ShadowsocksInboundSettings } from '@/schemas/protocols/inbound/shadowsocks';
  7. import type { TrojanClient, TrojanInboundSettings } from '@/schemas/protocols/inbound/trojan';
  8. import type { TunInboundSettings } from '@/schemas/protocols/inbound/tun';
  9. import type { TunnelInboundSettings } from '@/schemas/protocols/inbound/tunnel';
  10. import type { VlessClient, VlessInboundSettings } from '@/schemas/protocols/inbound/vless';
  11. import type { VmessClient, VmessInboundSettings } from '@/schemas/protocols/inbound/vmess';
  12. import type { WireguardInboundSettings } from '@/schemas/protocols/inbound/wireguard';
  13. // Plain-object factories for protocol clients. Each returns a Zod-parsable
  14. // object matching the wire shape. Random fields (id, password, auth,
  15. // email, subId) call RandomUtil at invocation time — pass them in
  16. // `overrides` for deterministic tests or for forms that pre-seed values.
  17. //
  18. // These replace the legacy `new Inbound.<Settings>.<Client>()` constructors
  19. // and the Inbound.ClientBase machinery. Callers no longer carry the
  20. // XrayCommonClass dependency once the swap lands.
  21. interface ClientBaseSeed {
  22. email?: string;
  23. subId?: string;
  24. limitIp?: number;
  25. totalGB?: number;
  26. expiryTime?: number;
  27. enable?: boolean;
  28. tgId?: number;
  29. comment?: string;
  30. reset?: number;
  31. }
  32. interface ClientBase {
  33. email: string;
  34. limitIp: number;
  35. totalGB: number;
  36. expiryTime: number;
  37. enable: boolean;
  38. tgId: number;
  39. subId: string;
  40. comment: string;
  41. reset: number;
  42. }
  43. function clientBase(seed: ClientBaseSeed = {}): ClientBase {
  44. return {
  45. email: seed.email ?? RandomUtil.randomLowerAndNum(10),
  46. limitIp: seed.limitIp ?? 0,
  47. totalGB: seed.totalGB ?? 0,
  48. expiryTime: seed.expiryTime ?? 0,
  49. enable: seed.enable ?? true,
  50. tgId: seed.tgId ?? 0,
  51. subId: seed.subId ?? RandomUtil.randomLowerAndNum(16),
  52. comment: seed.comment ?? '',
  53. reset: seed.reset ?? 0,
  54. };
  55. }
  56. export interface VlessClientSeed extends ClientBaseSeed {
  57. id?: string;
  58. flow?: VlessClient['flow'];
  59. }
  60. export function createDefaultVlessClient(seed: VlessClientSeed = {}): VlessClient {
  61. return {
  62. id: seed.id ?? RandomUtil.randomUUID(),
  63. flow: seed.flow ?? '',
  64. ...clientBase(seed),
  65. };
  66. }
  67. export interface VmessClientSeed extends ClientBaseSeed {
  68. id?: string;
  69. security?: VmessClient['security'];
  70. }
  71. export function createDefaultVmessClient(seed: VmessClientSeed = {}): VmessClient {
  72. return {
  73. id: seed.id ?? RandomUtil.randomUUID(),
  74. security: seed.security ?? 'auto',
  75. alterId: 0,
  76. ...clientBase(seed),
  77. };
  78. }
  79. export interface TrojanClientSeed extends ClientBaseSeed {
  80. password?: string;
  81. }
  82. export function createDefaultTrojanClient(seed: TrojanClientSeed = {}): TrojanClient {
  83. return {
  84. password: seed.password ?? RandomUtil.randomSeq(10),
  85. ...clientBase(seed),
  86. };
  87. }
  88. export interface ShadowsocksClientSeed extends ClientBaseSeed {
  89. method?: string;
  90. password?: string;
  91. ssMethod?: string;
  92. }
  93. // Shadowsocks clients ship with an empty `method` on single-user inbounds
  94. // (the parent inbound's method is authoritative); only 2022-blake3 multi-
  95. // user inbounds use the per-client method. Callers pass `ssMethod` to seed
  96. // a method-specific password length when creating a multi-user client.
  97. export function createDefaultShadowsocksClient(seed: ShadowsocksClientSeed = {}): ShadowsocksClient {
  98. const method = seed.method ?? '';
  99. const password = seed.password ?? RandomUtil.randomShadowsocksPassword(seed.ssMethod ?? '2022-blake3-aes-256-gcm');
  100. return {
  101. method,
  102. password,
  103. ...clientBase(seed),
  104. };
  105. }
  106. export interface HysteriaClientSeed extends ClientBaseSeed {
  107. auth?: string;
  108. }
  109. export function createDefaultHysteriaClient(seed: HysteriaClientSeed = {}): HysteriaClient {
  110. return {
  111. auth: seed.auth ?? RandomUtil.randomSeq(10),
  112. ...clientBase(seed),
  113. };
  114. }
  115. // Inbound-settings factories. Each returns a Zod-parsable wire-shape with
  116. // schema defaults already applied — no class instance, no XrayCommonClass.
  117. // Callers (form modals via Step 4, InboundsPage clone via Step 5) call
  118. // these instead of the legacy `Inbound.Settings.getSettings(protocol)`.
  119. export function createDefaultVlessInboundSettings(): VlessInboundSettings {
  120. return {
  121. clients: [],
  122. decryption: 'none',
  123. encryption: 'none',
  124. fallbacks: [],
  125. };
  126. }
  127. export function createDefaultVmessInboundSettings(): VmessInboundSettings {
  128. return { clients: [] };
  129. }
  130. export function createDefaultTrojanInboundSettings(): TrojanInboundSettings {
  131. return { clients: [], fallbacks: [] };
  132. }
  133. export interface ShadowsocksInboundSeed {
  134. method?: ShadowsocksInboundSettings['method'];
  135. password?: string;
  136. network?: ShadowsocksInboundSettings['network'];
  137. ivCheck?: boolean;
  138. }
  139. export function createDefaultShadowsocksInboundSettings(
  140. seed: ShadowsocksInboundSeed = {},
  141. ): ShadowsocksInboundSettings {
  142. const method = seed.method ?? '2022-blake3-aes-256-gcm';
  143. return {
  144. method,
  145. password: seed.password ?? RandomUtil.randomShadowsocksPassword(method),
  146. network: seed.network ?? 'tcp,udp',
  147. clients: [],
  148. ivCheck: seed.ivCheck ?? false,
  149. };
  150. }
  151. // Hysteria v1 defaults still emit `version: 2` to match the legacy panel
  152. // constructor — the field discriminates v1 vs v2 inside the same settings
  153. // shape. Callers that explicitly want v1 pass `{ version: 1 }`.
  154. export interface HysteriaInboundSeed {
  155. version?: number;
  156. }
  157. export function createDefaultHysteriaInboundSettings(
  158. seed: HysteriaInboundSeed = {},
  159. ): HysteriaInboundSettings {
  160. return {
  161. version: seed.version ?? 2,
  162. clients: [],
  163. };
  164. }
  165. export function createDefaultHttpInboundSettings(): HttpInboundSettings {
  166. return {
  167. accounts: [{ user: RandomUtil.randomLowerAndNum(8), pass: RandomUtil.randomLowerAndNum(12) }],
  168. allowTransparent: false,
  169. };
  170. }
  171. export function createDefaultMixedInboundSettings(): MixedInboundSettings {
  172. return {
  173. auth: 'password',
  174. accounts: [{ user: RandomUtil.randomLowerAndNum(8), pass: RandomUtil.randomLowerAndNum(12) }],
  175. udp: false,
  176. ip: '127.0.0.1',
  177. };
  178. }
  179. function domainToHex(domain: string): string {
  180. return Array.from(new TextEncoder().encode(domain))
  181. .map((b) => b.toString(16).padStart(2, '0'))
  182. .join('');
  183. }
  184. // generateMtprotoSecret builds an "ee" FakeTLS secret: the marker, 16 random
  185. // bytes (32 hex chars), then the domain encoded as hex. Mirrors the Go
  186. // model.GenerateFakeTLSSecret; the backend re-derives it on save so this is
  187. // only for immediate display in the form.
  188. export function generateMtprotoSecret(domain: string): string {
  189. return `ee${RandomUtil.randomSeq(32, { type: 'hex' })}${domainToHex(domain)}`;
  190. }
  191. export function createDefaultMtprotoInboundSettings(): MtprotoInboundSettings {
  192. return {
  193. fakeTlsDomain: 'www.cloudflare.com',
  194. clients: [],
  195. };
  196. }
  197. // createDefaultMtprotoClient seeds a new MTProto client with a fresh FakeTLS
  198. // secret fronting the given domain. Mirrors the WireGuard client default: the
  199. // backend re-derives the secret on save, so this is only for immediate display.
  200. export function createDefaultMtprotoClient(domain: string): Partial<MtprotoClient> {
  201. return {
  202. secret: generateMtprotoSecret(domain || 'www.cloudflare.com'),
  203. };
  204. }
  205. export function createDefaultTunnelInboundSettings(): TunnelInboundSettings {
  206. return {
  207. portMap: {},
  208. allowedNetwork: 'tcp,udp',
  209. followRedirect: false,
  210. };
  211. }
  212. export function createDefaultTunInboundSettings(): TunInboundSettings {
  213. return {
  214. name: 'xray0',
  215. mtu: 1500,
  216. gateway: [],
  217. dns: [],
  218. userLevel: 0,
  219. autoSystemRoutingTable: [],
  220. autoOutboundsInterface: 'auto',
  221. };
  222. }
  223. export interface WireguardInboundSeed {
  224. mtu?: number;
  225. secretKey?: string;
  226. noKernelTun?: boolean;
  227. }
  228. // WireGuard is multi-client now: a new inbound holds only the server identity
  229. // (secretKey/mtu) and starts with no clients. Clients (peers) are added later
  230. // through the client modal, which generates each one's keypair and a unique
  231. // tunnel address. peers stays empty for backward-compatible parsing.
  232. export function createDefaultWireguardInboundSettings(
  233. seed: WireguardInboundSeed = {},
  234. ): WireguardInboundSettings {
  235. return {
  236. mtu: seed.mtu ?? 1420,
  237. secretKey: seed.secretKey ?? Wireguard.generateKeypair().privateKey,
  238. peers: [],
  239. clients: [],
  240. noKernelTun: seed.noKernelTun ?? false,
  241. };
  242. }
  243. // Protocol-aware dispatch over every inbound-settings factory. Mirrors
  244. // the legacy `Inbound.Settings.getSettings(protocol)` dispatcher, but
  245. // returns a plain Zod-parsable object instead of a class instance.
  246. // Callers swapping off the class hierarchy use this in place of
  247. // `getSettings(p)` + `.toJson()`.
  248. export type AnyInboundSettings =
  249. | VlessInboundSettings
  250. | VmessInboundSettings
  251. | TrojanInboundSettings
  252. | ShadowsocksInboundSettings
  253. | HysteriaInboundSettings
  254. | HttpInboundSettings
  255. | MixedInboundSettings
  256. | TunInboundSettings
  257. | TunnelInboundSettings
  258. | WireguardInboundSettings
  259. | MtprotoInboundSettings;
  260. export function createDefaultInboundSettings(protocol: string): AnyInboundSettings | null {
  261. switch (protocol) {
  262. case 'vless': return createDefaultVlessInboundSettings();
  263. case 'vmess': return createDefaultVmessInboundSettings();
  264. case 'trojan': return createDefaultTrojanInboundSettings();
  265. case 'shadowsocks': return createDefaultShadowsocksInboundSettings();
  266. case 'hysteria': return createDefaultHysteriaInboundSettings();
  267. case 'http': return createDefaultHttpInboundSettings();
  268. case 'mixed': return createDefaultMixedInboundSettings();
  269. case 'tunnel': return createDefaultTunnelInboundSettings();
  270. case 'tun': return createDefaultTunInboundSettings();
  271. case 'wireguard': return createDefaultWireguardInboundSettings();
  272. case 'mtproto': return createDefaultMtprotoInboundSettings();
  273. default: return null;
  274. }
  275. }