inbound-defaults.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 { 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. // mtprotoSecretForDomain rewrites only the domain suffix of an existing secret,
  192. // preserving its 16-byte random middle when valid (generating one otherwise).
  193. // Mirrors the Go model.HealMtprotoSecret so editing the FakeTLS domain doesn't
  194. // needlessly rotate the secret's identity.
  195. export function mtprotoSecretForDomain(currentSecret: string, domain: string): string {
  196. let body = currentSecret;
  197. if (body.startsWith('ee') || body.startsWith('dd')) {
  198. body = body.slice(2);
  199. }
  200. const middle = /^[0-9a-f]{32}/i.test(body)
  201. ? body.slice(0, 32)
  202. : RandomUtil.randomSeq(32, { type: 'hex' });
  203. return `ee${middle}${domainToHex(domain)}`;
  204. }
  205. export function createDefaultMtprotoInboundSettings(): MtprotoInboundSettings {
  206. const fakeTlsDomain = 'www.cloudflare.com';
  207. return {
  208. fakeTlsDomain,
  209. secret: generateMtprotoSecret(fakeTlsDomain),
  210. };
  211. }
  212. export function createDefaultTunnelInboundSettings(): TunnelInboundSettings {
  213. return {
  214. portMap: {},
  215. allowedNetwork: 'tcp,udp',
  216. followRedirect: false,
  217. };
  218. }
  219. export function createDefaultTunInboundSettings(): TunInboundSettings {
  220. return {
  221. name: 'xray0',
  222. mtu: 1500,
  223. gateway: [],
  224. dns: [],
  225. userLevel: 0,
  226. autoSystemRoutingTable: [],
  227. autoOutboundsInterface: 'auto',
  228. };
  229. }
  230. export interface WireguardInboundSeed {
  231. mtu?: number;
  232. secretKey?: string;
  233. noKernelTun?: boolean;
  234. peerPrivateKey?: string;
  235. }
  236. export function createDefaultWireguardInboundSettings(
  237. seed: WireguardInboundSeed = {},
  238. ): WireguardInboundSettings {
  239. const peerKp = seed.peerPrivateKey
  240. ? { privateKey: seed.peerPrivateKey, publicKey: Wireguard.generateKeypair(seed.peerPrivateKey).publicKey }
  241. : Wireguard.generateKeypair();
  242. return {
  243. mtu: seed.mtu ?? 1420,
  244. secretKey: seed.secretKey ?? Wireguard.generateKeypair().privateKey,
  245. peers: [{
  246. privateKey: peerKp.privateKey,
  247. publicKey: peerKp.publicKey,
  248. allowedIPs: ['10.0.0.2/32'],
  249. keepAlive: 0,
  250. }],
  251. noKernelTun: seed.noKernelTun ?? false,
  252. };
  253. }
  254. // Protocol-aware dispatch over every inbound-settings factory. Mirrors
  255. // the legacy `Inbound.Settings.getSettings(protocol)` dispatcher, but
  256. // returns a plain Zod-parsable object instead of a class instance.
  257. // Callers swapping off the class hierarchy use this in place of
  258. // `getSettings(p)` + `.toJson()`.
  259. export type AnyInboundSettings =
  260. | VlessInboundSettings
  261. | VmessInboundSettings
  262. | TrojanInboundSettings
  263. | ShadowsocksInboundSettings
  264. | HysteriaInboundSettings
  265. | HttpInboundSettings
  266. | MixedInboundSettings
  267. | TunInboundSettings
  268. | TunnelInboundSettings
  269. | WireguardInboundSettings
  270. | MtprotoInboundSettings;
  271. export function createDefaultInboundSettings(protocol: string): AnyInboundSettings | null {
  272. switch (protocol) {
  273. case 'vless': return createDefaultVlessInboundSettings();
  274. case 'vmess': return createDefaultVmessInboundSettings();
  275. case 'trojan': return createDefaultTrojanInboundSettings();
  276. case 'shadowsocks': return createDefaultShadowsocksInboundSettings();
  277. case 'hysteria': return createDefaultHysteriaInboundSettings();
  278. case 'http': return createDefaultHttpInboundSettings();
  279. case 'mixed': return createDefaultMixedInboundSettings();
  280. case 'tunnel': return createDefaultTunnelInboundSettings();
  281. case 'tun': return createDefaultTunInboundSettings();
  282. case 'wireguard': return createDefaultWireguardInboundSettings();
  283. case 'mtproto': return createDefaultMtprotoInboundSettings();
  284. default: return null;
  285. }
  286. }