inbound-link.ts 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. import { Base64, Wireguard } from '@/utils';
  2. import type { Inbound } from '@/schemas/api/inbound';
  3. import type { VlessClient } from '@/schemas/protocols/inbound/vless';
  4. import type { VmessSecurity } from '@/schemas/protocols/inbound/vmess';
  5. import type {
  6. WireguardInboundPeer,
  7. WireguardInboundSettings,
  8. } from '@/schemas/protocols/inbound/wireguard';
  9. import type { ExternalProxyEntry } from '@/schemas/protocols/stream/external-proxy';
  10. import type { FinalMaskStreamSettings } from '@/schemas/protocols/stream/finalmask';
  11. import type { XHttpStreamSettings } from '@/schemas/protocols/stream/xhttp';
  12. import { getHeaderValue } from './headers';
  13. // Share-link generators. Each per-protocol fn takes a typed inbound plus
  14. // client overrides and returns a URL (or '' when the protocol doesn't
  15. // support shareable links). The helpers below were previously static
  16. // methods on the Inbound class; extracting them removes the
  17. // XrayCommonClass dependency and lets these run against Zod-parsed data
  18. // directly.
  19. type ForceTls = 'same' | 'tls' | 'none';
  20. // xHTTP headers ship as Record<string, string> on the wire (Zod schema)
  21. // rather than the legacy class's HeaderEntry[]. Lookup by case-folded key.
  22. function xhttpHostFallback(xhttp: XHttpStreamSettings | undefined): string {
  23. return getHeaderValue(xhttp?.headers, 'host');
  24. }
  25. // Pull the bidirectional SplitHTTPConfig fields out of xhttp into a
  26. // compact extra payload. Server-only fields (noSSEHeader, scMaxBufferedPosts,
  27. // scStreamUpServerSecs, serverMaxHeaderBytes) are excluded — the client
  28. // reading the share link wouldn't honor them. Mirrors the legacy
  29. // Inbound.buildXhttpExtra exactly so the shadow link snapshots line up.
  30. function buildXhttpExtra(xhttp: XHttpStreamSettings | undefined): Record<string, unknown> | null {
  31. if (!xhttp) return null;
  32. const extra: Record<string, unknown> = {};
  33. if (typeof xhttp.xPaddingBytes === 'string' && xhttp.xPaddingBytes.length > 0) {
  34. extra.xPaddingBytes = xhttp.xPaddingBytes;
  35. }
  36. if (xhttp.xPaddingObfsMode === true) {
  37. extra.xPaddingObfsMode = true;
  38. for (const k of ['xPaddingKey', 'xPaddingHeader', 'xPaddingPlacement', 'xPaddingMethod'] as const) {
  39. const v = xhttp[k];
  40. if (typeof v === 'string' && v.length > 0) extra[k] = v;
  41. }
  42. }
  43. const stringFields = [
  44. 'uplinkHTTPMethod',
  45. 'sessionPlacement',
  46. 'sessionKey',
  47. 'seqPlacement',
  48. 'seqKey',
  49. 'uplinkDataPlacement',
  50. 'uplinkDataKey',
  51. 'scMaxEachPostBytes',
  52. ] as const;
  53. for (const k of stringFields) {
  54. const v = xhttp[k];
  55. if (typeof v === 'string' && v.length > 0) extra[k] = v;
  56. }
  57. // Headers on the wire are a record; emit them as a map upstream's
  58. // SplitHTTPConfig.headers expects, dropping Host (already on the URL).
  59. if (xhttp.headers && Object.keys(xhttp.headers).length > 0) {
  60. const headersMap: Record<string, string> = {};
  61. for (const [name, value] of Object.entries(xhttp.headers)) {
  62. if (name.toLowerCase() === 'host') continue;
  63. headersMap[name] = value;
  64. }
  65. if (Object.keys(headersMap).length > 0) extra.headers = headersMap;
  66. }
  67. return Object.keys(extra).length > 0 ? extra : null;
  68. }
  69. function applyXhttpExtraToObj(xhttp: XHttpStreamSettings | undefined, obj: Record<string, unknown>): void {
  70. if (!xhttp) return;
  71. if (typeof xhttp.xPaddingBytes === 'string' && xhttp.xPaddingBytes.length > 0) {
  72. obj.x_padding_bytes = xhttp.xPaddingBytes;
  73. }
  74. const extra = buildXhttpExtra(xhttp);
  75. if (!extra) return;
  76. for (const [k, v] of Object.entries(extra)) obj[k] = v;
  77. }
  78. // Recursively checks whether a finalmask payload has any non-empty
  79. // content. Empty arrays / empty objects / empty strings all return false;
  80. // any truthy primitive returns true. Used to decide whether the link
  81. // should carry an `fm` blob at all.
  82. function hasShareableFinalMaskValue(value: unknown): boolean {
  83. if (value == null) return false;
  84. if (Array.isArray(value)) return value.some(hasShareableFinalMaskValue);
  85. if (typeof value === 'object') {
  86. return Object.values(value as Record<string, unknown>).some(hasShareableFinalMaskValue);
  87. }
  88. if (typeof value === 'string') return value.length > 0;
  89. return true;
  90. }
  91. function serializeFinalMask(finalmask: FinalMaskStreamSettings | undefined): string {
  92. if (!finalmask) return '';
  93. return hasShareableFinalMaskValue(finalmask) ? JSON.stringify(finalmask) : '';
  94. }
  95. function applyFinalMaskToObj(
  96. finalmask: FinalMaskStreamSettings | undefined,
  97. obj: Record<string, unknown>,
  98. ): void {
  99. const payload = serializeFinalMask(finalmask);
  100. if (payload.length > 0) obj.fm = payload;
  101. }
  102. function externalProxyAlpn(value: ExternalProxyEntry['alpn']): string {
  103. if (Array.isArray(value)) return value.filter(Boolean).join(',');
  104. return '';
  105. }
  106. function applyExternalProxyTLSObj(
  107. externalProxy: ExternalProxyEntry | null | undefined,
  108. obj: Record<string, unknown>,
  109. security: string,
  110. ): void {
  111. if (!externalProxy || security !== 'tls') return;
  112. const sni = externalProxy.sni && externalProxy.sni.length > 0 ? externalProxy.sni : externalProxy.dest;
  113. if (sni && sni.length > 0) obj.sni = sni;
  114. if (externalProxy.fingerprint && externalProxy.fingerprint.length > 0) obj.fp = externalProxy.fingerprint;
  115. const alpn = externalProxyAlpn(externalProxy.alpn);
  116. if (alpn.length > 0) obj.alpn = alpn;
  117. }
  118. export interface GenVmessLinkInput {
  119. inbound: Inbound;
  120. address: string;
  121. port?: number;
  122. forceTls?: ForceTls;
  123. remark?: string;
  124. clientId: string;
  125. security?: VmessSecurity;
  126. externalProxy?: ExternalProxyEntry | null;
  127. }
  128. // VMess share link: `vmess://` followed by base64-encoded JSON. The JSON
  129. // schema is the v2rayN-compatible "v2" shape. Returns '' if the inbound
  130. // is not vmess so dispatcher code can fall through cleanly.
  131. export function genVmessLink(input: GenVmessLinkInput): string {
  132. const {
  133. inbound,
  134. address,
  135. port = inbound.port,
  136. forceTls = 'same',
  137. remark = '',
  138. clientId,
  139. security,
  140. externalProxy = null,
  141. } = input;
  142. if (inbound.protocol !== 'vmess') return '';
  143. const stream = inbound.streamSettings;
  144. if (!stream) return '';
  145. const tls = forceTls === 'same' ? stream.security : forceTls;
  146. const obj: Record<string, unknown> = {
  147. v: '2',
  148. ps: remark,
  149. add: address,
  150. port,
  151. id: clientId,
  152. scy: security,
  153. net: stream.network,
  154. tls,
  155. };
  156. if (stream.network === 'tcp') {
  157. const tcp = stream.tcpSettings;
  158. const header = tcp.header;
  159. if (header) {
  160. obj.type = header.type;
  161. if (header.type === 'http') {
  162. const request = header.request;
  163. if (request) {
  164. obj.path = request.path.join(',');
  165. const host =
  166. getHeaderValue(header.response?.headers, 'host')
  167. || getHeaderValue(request.headers, 'host');
  168. if (host) obj.host = host;
  169. }
  170. }
  171. } else {
  172. obj.type = 'none';
  173. }
  174. } else if (stream.network === 'kcp') {
  175. const kcp = stream.kcpSettings;
  176. obj.mtu = kcp.mtu;
  177. obj.tti = kcp.tti;
  178. } else if (stream.network === 'ws') {
  179. const ws = stream.wsSettings;
  180. obj.path = ws.path;
  181. obj.host = ws.host.length > 0 ? ws.host : getHeaderValue(ws.headers, 'host');
  182. } else if (stream.network === 'grpc') {
  183. const grpc = stream.grpcSettings;
  184. obj.path = grpc.serviceName;
  185. obj.authority = grpc.authority;
  186. if (grpc.multiMode) obj.type = 'multi';
  187. } else if (stream.network === 'httpupgrade') {
  188. const hu = stream.httpupgradeSettings;
  189. obj.path = hu.path;
  190. obj.host = hu.host.length > 0 ? hu.host : getHeaderValue(hu.headers, 'host');
  191. } else if (stream.network === 'xhttp') {
  192. const xhttp = stream.xhttpSettings;
  193. obj.path = xhttp.path;
  194. obj.host = xhttp.host.length > 0 ? xhttp.host : xhttpHostFallback(xhttp);
  195. obj.type = xhttp.mode;
  196. applyXhttpExtraToObj(xhttp, obj);
  197. }
  198. applyFinalMaskToObj(stream.finalmask, obj);
  199. if (tls === 'tls' && stream.security === 'tls') {
  200. const tlsSettings = stream.tlsSettings;
  201. if (tlsSettings.serverName.length > 0) obj.sni = tlsSettings.serverName;
  202. if (tlsSettings.settings.fingerprint.length > 0) obj.fp = tlsSettings.settings.fingerprint;
  203. if (tlsSettings.alpn.length > 0) obj.alpn = tlsSettings.alpn.join(',');
  204. if (tlsSettings.settings.pinnedPeerCertSha256.length > 0) {
  205. obj.pcs = tlsSettings.settings.pinnedPeerCertSha256.join(',');
  206. }
  207. }
  208. applyExternalProxyTLSObj(externalProxy, obj, tls);
  209. return 'vmess://' + Base64.encode(JSON.stringify(obj, null, 2));
  210. }
  211. // Param-style helpers (vless/trojan/ss/hysteria links). These mirror the
  212. // legacy applyXhttpExtraToParams / applyFinalMaskToParams /
  213. // applyExternalProxyTLSParams but write to a URLSearchParams instance
  214. // directly. Number values get coerced via .toString() on set — same as
  215. // what URLSearchParams does internally so the resulting URL bytes match.
  216. function applyXhttpExtraToParams(xhttp: XHttpStreamSettings | undefined, params: URLSearchParams): void {
  217. if (!xhttp) return;
  218. params.set('path', xhttp.path);
  219. const host = xhttp.host.length > 0 ? xhttp.host : xhttpHostFallback(xhttp);
  220. params.set('host', host);
  221. params.set('mode', xhttp.mode);
  222. if (typeof xhttp.xPaddingBytes === 'string' && xhttp.xPaddingBytes.length > 0) {
  223. params.set('x_padding_bytes', xhttp.xPaddingBytes);
  224. }
  225. const extra = buildXhttpExtra(xhttp);
  226. if (extra) params.set('extra', JSON.stringify(extra));
  227. }
  228. function applyFinalMaskToParams(finalmask: FinalMaskStreamSettings | undefined, params: URLSearchParams): void {
  229. const payload = serializeFinalMask(finalmask);
  230. if (payload.length > 0) params.set('fm', payload);
  231. }
  232. function applyExternalProxyTLSParams(
  233. externalProxy: ExternalProxyEntry | null | undefined,
  234. params: URLSearchParams,
  235. security: string,
  236. ): void {
  237. if (!externalProxy || security !== 'tls') return;
  238. const sni = externalProxy.sni && externalProxy.sni.length > 0 ? externalProxy.sni : externalProxy.dest;
  239. if (sni && sni.length > 0) params.set('sni', sni);
  240. if (externalProxy.fingerprint && externalProxy.fingerprint.length > 0) params.set('fp', externalProxy.fingerprint);
  241. const alpn = externalProxyAlpn(externalProxy.alpn);
  242. if (alpn.length > 0) params.set('alpn', alpn);
  243. }
  244. export interface GenVlessLinkInput {
  245. inbound: Inbound;
  246. address: string;
  247. port?: number;
  248. forceTls?: ForceTls;
  249. remark?: string;
  250. clientId: string;
  251. flow?: VlessClient['flow'];
  252. externalProxy?: ExternalProxyEntry | null;
  253. }
  254. // VLESS share link: vless://<uuid>@<host>:<port>?<query>#<remark>. The
  255. // query carries network type, encryption, network-specific knobs, and
  256. // security-specific knobs (TLS fingerprint/alpn/sni or Reality
  257. // pbk/sid/spx). Returns '' if the inbound isn't vless.
  258. export function genVlessLink(input: GenVlessLinkInput): string {
  259. const {
  260. inbound,
  261. address,
  262. port = inbound.port,
  263. forceTls = 'same',
  264. remark = '',
  265. clientId,
  266. flow = '',
  267. externalProxy = null,
  268. } = input;
  269. if (inbound.protocol !== 'vless') return '';
  270. const stream = inbound.streamSettings;
  271. if (!stream) return '';
  272. const security = forceTls === 'same' ? stream.security : forceTls;
  273. const params = new URLSearchParams();
  274. params.set('type', stream.network);
  275. params.set('encryption', inbound.settings.encryption);
  276. if (stream.network === 'tcp') {
  277. const tcp = stream.tcpSettings;
  278. if (tcp.header?.type === 'http') {
  279. const request = tcp.header.request;
  280. if (request) {
  281. params.set('path', request.path.join(','));
  282. const host =
  283. getHeaderValue(tcp.header.response?.headers, 'host')
  284. || getHeaderValue(request.headers, 'host');
  285. if (host) params.set('host', host);
  286. params.set('headerType', 'http');
  287. }
  288. }
  289. } else if (stream.network === 'kcp') {
  290. const kcp = stream.kcpSettings;
  291. params.set('mtu', String(kcp.mtu));
  292. params.set('tti', String(kcp.tti));
  293. } else if (stream.network === 'ws') {
  294. const ws = stream.wsSettings;
  295. params.set('path', ws.path);
  296. params.set('host', ws.host.length > 0 ? ws.host : getHeaderValue(ws.headers, 'host'));
  297. } else if (stream.network === 'grpc') {
  298. const grpc = stream.grpcSettings;
  299. params.set('serviceName', grpc.serviceName);
  300. params.set('authority', grpc.authority);
  301. if (grpc.multiMode) params.set('mode', 'multi');
  302. } else if (stream.network === 'httpupgrade') {
  303. const hu = stream.httpupgradeSettings;
  304. params.set('path', hu.path);
  305. params.set('host', hu.host.length > 0 ? hu.host : getHeaderValue(hu.headers, 'host'));
  306. } else if (stream.network === 'xhttp') {
  307. applyXhttpExtraToParams(stream.xhttpSettings, params);
  308. }
  309. applyFinalMaskToParams(stream.finalmask, params);
  310. if (security === 'tls') {
  311. params.set('security', 'tls');
  312. if (stream.security === 'tls') {
  313. const tls = stream.tlsSettings;
  314. params.set('fp', tls.settings.fingerprint);
  315. params.set('alpn', tls.alpn.join(','));
  316. if (tls.serverName.length > 0) params.set('sni', tls.serverName);
  317. if (tls.settings.echConfigList.length > 0) params.set('ech', tls.settings.echConfigList);
  318. if (tls.settings.pinnedPeerCertSha256.length > 0) {
  319. params.set('pcs', tls.settings.pinnedPeerCertSha256.join(','));
  320. }
  321. if (stream.network === 'tcp' && flow.length > 0) params.set('flow', flow);
  322. }
  323. applyExternalProxyTLSParams(externalProxy, params, security);
  324. } else if (security === 'reality') {
  325. params.set('security', 'reality');
  326. if (stream.security === 'reality') {
  327. const reality = stream.realitySettings;
  328. params.set('pbk', reality.settings.publicKey);
  329. params.set('fp', reality.settings.fingerprint);
  330. const sni =
  331. reality.settings.serverName ||
  332. reality.serverNames?.[0] ||
  333. reality.target?.split(':')[0];
  334. if (sni && sni.length > 0) params.set('sni', sni);
  335. if (reality.shortIds.length > 0) params.set('sid', reality.shortIds[0]);
  336. if (reality.settings.spiderX.length > 0) params.set('spx', reality.settings.spiderX);
  337. if (reality.settings.mldsa65Verify.length > 0) params.set('pqv', reality.settings.mldsa65Verify);
  338. if (stream.network === 'tcp' && flow.length > 0) params.set('flow', flow);
  339. }
  340. } else {
  341. params.set('security', 'none');
  342. }
  343. const url = new URL(`vless://${clientId}@${address}:${port}`);
  344. for (const [key, value] of params) url.searchParams.set(key, value);
  345. url.hash = encodeURIComponent(remark);
  346. return url.toString();
  347. }
  348. // Shared network-branch writer used by trojan + shadowsocks links.
  349. // VLESS and VMess don't call this because they have minor per-protocol
  350. // quirks inline (vmess maps `multi` differently into obj.type; vless sets
  351. // encryption=none up-front).
  352. function writeNetworkParams(stream: NonNullable<Inbound['streamSettings']>, params: URLSearchParams): void {
  353. if (stream.network === 'tcp') {
  354. const tcp = stream.tcpSettings;
  355. if (tcp.header?.type === 'http') {
  356. const request = tcp.header.request;
  357. if (request) {
  358. params.set('path', request.path.join(','));
  359. const host =
  360. getHeaderValue(tcp.header.response?.headers, 'host')
  361. || getHeaderValue(request.headers, 'host');
  362. if (host) params.set('host', host);
  363. params.set('headerType', 'http');
  364. }
  365. }
  366. } else if (stream.network === 'kcp') {
  367. const kcp = stream.kcpSettings;
  368. params.set('mtu', String(kcp.mtu));
  369. params.set('tti', String(kcp.tti));
  370. } else if (stream.network === 'ws') {
  371. const ws = stream.wsSettings;
  372. params.set('path', ws.path);
  373. params.set('host', ws.host.length > 0 ? ws.host : getHeaderValue(ws.headers, 'host'));
  374. } else if (stream.network === 'grpc') {
  375. const grpc = stream.grpcSettings;
  376. params.set('serviceName', grpc.serviceName);
  377. params.set('authority', grpc.authority);
  378. if (grpc.multiMode) params.set('mode', 'multi');
  379. } else if (stream.network === 'httpupgrade') {
  380. const hu = stream.httpupgradeSettings;
  381. params.set('path', hu.path);
  382. params.set('host', hu.host.length > 0 ? hu.host : getHeaderValue(hu.headers, 'host'));
  383. } else if (stream.network === 'xhttp') {
  384. applyXhttpExtraToParams(stream.xhttpSettings, params);
  385. }
  386. }
  387. function writeTlsParams(stream: NonNullable<Inbound['streamSettings']>, params: URLSearchParams): void {
  388. if (stream.security !== 'tls') return;
  389. const tls = stream.tlsSettings;
  390. params.set('fp', tls.settings.fingerprint);
  391. params.set('alpn', tls.alpn.join(','));
  392. if (tls.settings.echConfigList.length > 0) params.set('ech', tls.settings.echConfigList);
  393. if (tls.serverName.length > 0) params.set('sni', tls.serverName);
  394. if (tls.settings.pinnedPeerCertSha256.length > 0) {
  395. params.set('pcs', tls.settings.pinnedPeerCertSha256.join(','));
  396. }
  397. }
  398. // Reality query-string writer shared by VLESS and Trojan. Preserves the
  399. // legacy SNI-omission quirk (see genVlessLink for the full story).
  400. function writeRealityParams(stream: NonNullable<Inbound['streamSettings']>, params: URLSearchParams): void {
  401. if (stream.security !== 'reality') return;
  402. const reality = stream.realitySettings;
  403. params.set('pbk', reality.settings.publicKey);
  404. params.set('fp', reality.settings.fingerprint);
  405. const sni =
  406. reality.settings.serverName ||
  407. reality.serverNames?.[0] ||
  408. reality.target?.split(':')[0];
  409. if (sni && sni.length > 0) params.set('sni', sni);
  410. if (reality.shortIds.length > 0) params.set('sid', reality.shortIds[0]);
  411. if (reality.settings.spiderX.length > 0) params.set('spx', reality.settings.spiderX);
  412. if (reality.settings.mldsa65Verify.length > 0) params.set('pqv', reality.settings.mldsa65Verify);
  413. }
  414. export interface GenTrojanLinkInput {
  415. inbound: Inbound;
  416. address: string;
  417. port?: number;
  418. forceTls?: ForceTls;
  419. remark?: string;
  420. clientPassword: string;
  421. externalProxy?: ExternalProxyEntry | null;
  422. }
  423. // Trojan share link: trojan://<password>@<host>:<port>?<query>#<remark>.
  424. // Same query-string shape as VLESS minus the `encryption` and `flow`
  425. // fields. Returns '' if the inbound isn't trojan.
  426. export function genTrojanLink(input: GenTrojanLinkInput): string {
  427. const {
  428. inbound,
  429. address,
  430. port = inbound.port,
  431. forceTls = 'same',
  432. remark = '',
  433. clientPassword,
  434. externalProxy = null,
  435. } = input;
  436. if (inbound.protocol !== 'trojan') return '';
  437. const stream = inbound.streamSettings;
  438. if (!stream) return '';
  439. const security = forceTls === 'same' ? stream.security : forceTls;
  440. const params = new URLSearchParams();
  441. params.set('type', stream.network);
  442. writeNetworkParams(stream, params);
  443. applyFinalMaskToParams(stream.finalmask, params);
  444. if (security === 'tls') {
  445. params.set('security', 'tls');
  446. writeTlsParams(stream, params);
  447. applyExternalProxyTLSParams(externalProxy, params, security);
  448. } else if (security === 'reality') {
  449. params.set('security', 'reality');
  450. writeRealityParams(stream, params);
  451. } else {
  452. params.set('security', 'none');
  453. }
  454. const url = new URL(`trojan://${encodeURIComponent(clientPassword)}@${address}:${port}`);
  455. for (const [key, value] of params) url.searchParams.set(key, value);
  456. url.hash = encodeURIComponent(remark);
  457. return url.toString();
  458. }
  459. export interface GenShadowsocksLinkInput {
  460. inbound: Inbound;
  461. address: string;
  462. port?: number;
  463. forceTls?: ForceTls;
  464. remark?: string;
  465. clientPassword?: string;
  466. externalProxy?: ExternalProxyEntry | null;
  467. }
  468. // Shadowsocks 2022 share link. The userinfo portion is base64(method:pw)
  469. // for single-user and base64(method:settingsPw:clientPw) for multi-user
  470. // 2022-blake3. Legacy SS (non-2022) leaves the password out of the
  471. // userinfo entirely — matches the legacy class's password-array logic.
  472. // Note: legacy `isSSMultiUser` returns true for everything except
  473. // 2022-blake3-chacha20-poly1305 (a curious classification, but we
  474. // preserve it for byte-stable parity).
  475. export function genShadowsocksLink(input: GenShadowsocksLinkInput): string {
  476. const {
  477. inbound,
  478. address,
  479. port = inbound.port,
  480. forceTls = 'same',
  481. remark = '',
  482. clientPassword = '',
  483. externalProxy = null,
  484. } = input;
  485. if (inbound.protocol !== 'shadowsocks') return '';
  486. const stream = inbound.streamSettings;
  487. if (!stream) return '';
  488. const settings = inbound.settings;
  489. const security = forceTls === 'same' ? stream.security : forceTls;
  490. const params = new URLSearchParams();
  491. params.set('type', stream.network);
  492. writeNetworkParams(stream, params);
  493. applyFinalMaskToParams(stream.finalmask, params);
  494. if (security === 'tls') {
  495. params.set('security', 'tls');
  496. writeTlsParams(stream, params);
  497. applyExternalProxyTLSParams(externalProxy, params, security);
  498. }
  499. const isSS2022 = settings.method.substring(0, 4) === '2022';
  500. const isSSMultiUser = settings.method !== '2022-blake3-chacha20-poly1305';
  501. const passwords: string[] = [];
  502. if (isSS2022) passwords.push(settings.password);
  503. if (isSSMultiUser) passwords.push(clientPassword);
  504. const userinfo = Base64.encode(`${settings.method}:${passwords.join(':')}`, true);
  505. const url = new URL(`ss://${userinfo}@${address}:${port}`);
  506. for (const [key, value] of params) url.searchParams.set(key, value);
  507. url.hash = encodeURIComponent(remark);
  508. return url.toString();
  509. }
  510. export interface GenHysteriaLinkInput {
  511. inbound: Inbound;
  512. address: string;
  513. port?: number;
  514. remark?: string;
  515. clientAuth: string;
  516. }
  517. // Hysteria share link: hysteria://<auth>@<host>:<port>?<query>#<remark>.
  518. // The URL scheme is "hysteria2" when settings.version === 2 (hysteria v2
  519. // AKA hysteria2), "hysteria" otherwise. Salamander obfuscation pulls its
  520. // password from finalmask.udp[type=salamander] when present; the broader
  521. // finalmask payload still rides under `fm` like the other links.
  522. //
  523. // Note: legacy genHysteriaLink reads stream.tls.settings.allowInsecure,
  524. // which isn't a field on TlsStreamSettings.Settings — the guard is always
  525. // false. We omit the `insecure` param here to stay byte-stable.
  526. export function genHysteriaLink(input: GenHysteriaLinkInput): string {
  527. const {
  528. inbound,
  529. address,
  530. port = inbound.port,
  531. remark = '',
  532. clientAuth,
  533. } = input;
  534. if (inbound.protocol !== 'hysteria') return '';
  535. const stream = inbound.streamSettings;
  536. if (!stream || stream.security !== 'tls') return '';
  537. const settings = inbound.settings;
  538. const scheme = settings.version === 2 ? 'hysteria2' : 'hysteria';
  539. const params = new URLSearchParams();
  540. params.set('security', 'tls');
  541. const tls = stream.tlsSettings;
  542. if (tls.settings.fingerprint.length > 0) params.set('fp', tls.settings.fingerprint);
  543. if (tls.alpn.length > 0) params.set('alpn', tls.alpn.join(','));
  544. if (tls.settings.echConfigList.length > 0) params.set('ech', tls.settings.echConfigList);
  545. if (tls.serverName.length > 0) params.set('sni', tls.serverName);
  546. const udpMasks = stream.finalmask?.udp;
  547. if (Array.isArray(udpMasks)) {
  548. const salamander = udpMasks.find((m) => m?.type === 'salamander');
  549. const obfsPassword = salamander?.settings?.password;
  550. if (typeof obfsPassword === 'string' && obfsPassword.length > 0) {
  551. params.set('obfs', 'salamander');
  552. params.set('obfs-password', obfsPassword);
  553. }
  554. }
  555. applyFinalMaskToParams(stream.finalmask, params);
  556. const url = new URL(`${scheme}://${clientAuth}@${address}:${port}`);
  557. for (const [key, value] of params) url.searchParams.set(key, value);
  558. url.hash = encodeURIComponent(remark);
  559. return url.toString();
  560. }
  561. export interface GenWireguardLinkInput {
  562. settings: WireguardInboundSettings;
  563. address: string;
  564. port: number;
  565. remark?: string;
  566. peerIndex: number;
  567. }
  568. // Wireguard share link: wireguard://<peerPrivKey>@<host>:<port>
  569. // ?publickey=<serverPub>&address=<peerAllowedIP>&mtu=<mtu>#<remark>
  570. // pubKey is derived from the server's secretKey via Wireguard.generateKeypair
  571. // at call time (Zod's schema stores secretKey only — pubKey isn't on the
  572. // wire). Returns '' when the peer index is out of bounds.
  573. export function genWireguardLink(input: GenWireguardLinkInput): string {
  574. const { settings, address, port, remark = '', peerIndex } = input;
  575. const peer = settings.peers[peerIndex];
  576. if (!peer) return '';
  577. const url = new URL(`wireguard://${address}:${port}`);
  578. url.username = peer.privateKey ?? '';
  579. const pubKey = settings.secretKey.length > 0
  580. ? Wireguard.generateKeypair(settings.secretKey).publicKey
  581. : '';
  582. if (pubKey.length > 0) url.searchParams.set('publickey', pubKey);
  583. if (peer.allowedIPs.length > 0 && peer.allowedIPs[0]) {
  584. url.searchParams.set('address', peer.allowedIPs[0]);
  585. }
  586. if (typeof settings.mtu === 'number' && settings.mtu > 0) {
  587. url.searchParams.set('mtu', String(settings.mtu));
  588. }
  589. url.hash = encodeURIComponent(remark);
  590. return url.toString();
  591. }
  592. // Plain-text WireGuard client config (.conf format). Mirrors the legacy
  593. // getWireguardTxt — same DNS defaults (1.1.1.1, 1.0.0.1), MTU optional,
  594. // presharedKey + keepAlive only emitted when present on the peer. The
  595. // final newline structure follows the legacy: no newline after Endpoint,
  596. // optional preSharedKey appended with leading \n, keepAlive appended
  597. // with leading \n AND trailing \n.
  598. export function genWireguardConfig(input: GenWireguardLinkInput): string {
  599. const { settings, address, port, remark = '', peerIndex } = input;
  600. const peer = settings.peers[peerIndex];
  601. if (!peer) return '';
  602. const pubKey = settings.secretKey.length > 0
  603. ? Wireguard.generateKeypair(settings.secretKey).publicKey
  604. : '';
  605. let txt = `[Interface]\n`;
  606. txt += `PrivateKey = ${peer.privateKey ?? ''}\n`;
  607. txt += `Address = ${peer.allowedIPs[0] ?? ''}\n`;
  608. txt += `DNS = 1.1.1.1, 1.0.0.1\n`;
  609. if (typeof settings.mtu === 'number' && settings.mtu > 0) {
  610. txt += `MTU = ${settings.mtu}\n`;
  611. }
  612. txt += `\n# ${remark}\n`;
  613. txt += `[Peer]\n`;
  614. txt += `PublicKey = ${pubKey}\n`;
  615. txt += `AllowedIPs = 0.0.0.0/0, ::/0\n`;
  616. txt += `Endpoint = ${address}:${port}`;
  617. if (peer.preSharedKey && peer.preSharedKey.length > 0) {
  618. txt += `\nPresharedKey = ${peer.preSharedKey}`;
  619. }
  620. if (typeof peer.keepAlive === 'number' && peer.keepAlive > 0) {
  621. txt += `\nPersistentKeepalive = ${peer.keepAlive}\n`;
  622. }
  623. return txt;
  624. }
  625. export type { WireguardInboundPeer };
  626. // Orchestrators.
  627. // resolveAddr picks the host that goes into share/sub links. Order:
  628. // 1. hostOverride (caller supplies node address for node-managed inbounds)
  629. // 2. inbound's bind listen (when explicit, not 0.0.0.0)
  630. // 3. fallbackHostname (caller-supplied — typically window.location.hostname
  631. // in the browser; tests pass a fixed value)
  632. export function resolveAddr(inbound: Inbound, hostOverride: string, fallbackHostname: string): string {
  633. if (hostOverride.length > 0) return hostOverride;
  634. if (inbound.listen.length > 0 && inbound.listen !== '0.0.0.0') return inbound.listen;
  635. return fallbackHostname;
  636. }
  637. // Returns the client array for protocols that have one. SS returns its
  638. // clients only in 2022-blake3 multi-user mode (matches the legacy
  639. // `this.clients` getter, which used isSSMultiUser to gate). Returns null
  640. // for SS single-user, http, mixed, tunnel, wireguard, hysteria2-without-
  641. // clients, and any protocol without a clients array.
  642. type ClientShape = { id?: string; security?: VmessSecurity; flow?: VlessClient['flow']; password?: string; auth?: string; email?: string };
  643. export function getInboundClients(inbound: Inbound): ClientShape[] | null {
  644. switch (inbound.protocol) {
  645. case 'vmess':
  646. return (inbound.settings.clients ?? []) as ClientShape[];
  647. case 'vless':
  648. return (inbound.settings.clients ?? []) as ClientShape[];
  649. case 'trojan':
  650. return (inbound.settings.clients ?? []) as ClientShape[];
  651. case 'hysteria':
  652. return (inbound.settings.clients ?? []) as ClientShape[];
  653. case 'shadowsocks': {
  654. const isMultiUser = inbound.settings.method !== '2022-blake3-chacha20-poly1305';
  655. return isMultiUser ? ((inbound.settings.clients ?? []) as ClientShape[]) : null;
  656. }
  657. default:
  658. return null;
  659. }
  660. }
  661. export interface GenLinkInput {
  662. inbound: Inbound;
  663. address: string;
  664. port?: number;
  665. forceTls?: ForceTls;
  666. remark?: string;
  667. client: ClientShape;
  668. externalProxy?: ExternalProxyEntry | null;
  669. }
  670. // Per-protocol dispatcher matching the legacy `genLink` switch. Returns
  671. // '' for protocols that don't have client-based share links (wireguard
  672. // goes through genWireguardLinks/Configs separately, http/mixed/tunnel
  673. // don't have share URLs).
  674. export function genLink(input: GenLinkInput): string {
  675. const { inbound, address, port = inbound.port, forceTls = 'same', remark = '', client, externalProxy = null } = input;
  676. switch (inbound.protocol) {
  677. case 'vmess':
  678. return genVmessLink({
  679. inbound, address, port, forceTls, remark,
  680. clientId: client.id ?? '',
  681. security: client.security,
  682. externalProxy,
  683. });
  684. case 'vless':
  685. return genVlessLink({
  686. inbound, address, port, forceTls, remark,
  687. clientId: client.id ?? '',
  688. flow: client.flow,
  689. externalProxy,
  690. });
  691. case 'shadowsocks': {
  692. const isMultiUser = inbound.settings.method !== '2022-blake3-chacha20-poly1305';
  693. return genShadowsocksLink({
  694. inbound, address, port, forceTls, remark,
  695. clientPassword: isMultiUser ? (client.password ?? '') : '',
  696. externalProxy,
  697. });
  698. }
  699. case 'trojan':
  700. return genTrojanLink({
  701. inbound, address, port, forceTls, remark,
  702. clientPassword: client.password ?? '',
  703. externalProxy,
  704. });
  705. case 'hysteria':
  706. return genHysteriaLink({
  707. inbound, address, port, remark,
  708. clientAuth: client.auth ?? '',
  709. });
  710. default:
  711. return '';
  712. }
  713. }
  714. export interface GenAllLinksEntry {
  715. remark: string;
  716. link: string;
  717. }
  718. export interface GenAllLinksInput {
  719. inbound: Inbound;
  720. remark?: string;
  721. remarkModel?: string;
  722. client: ClientShape;
  723. hostOverride?: string;
  724. fallbackHostname: string;
  725. }
  726. // Fans out a single client's link per externalProxy entry, or just one
  727. // link when there are no external proxies. remarkModel is a 4-char
  728. // string: first char is the separator, remaining chars pick which
  729. // pieces to compose into the per-link remark — 'i' = inbound remark,
  730. // 'e' = client email, 'o' = externalProxy remark. Defaults to '-io'
  731. // (dash-separated, inbound + email + proxy).
  732. export function genAllLinks(input: GenAllLinksInput): GenAllLinksEntry[] {
  733. const {
  734. inbound,
  735. remark = '',
  736. remarkModel = '-io',
  737. client,
  738. hostOverride = '',
  739. fallbackHostname,
  740. } = input;
  741. const addr = resolveAddr(inbound, hostOverride, fallbackHostname);
  742. const port = inbound.port;
  743. const separationChar = remarkModel.charAt(0);
  744. const orderChars = remarkModel.slice(1);
  745. const email = client.email ?? '';
  746. const composeRemark = (proxyRemark: string): string => {
  747. const orders: Record<string, string> = { i: remark, e: email, o: proxyRemark };
  748. return orderChars.split('')
  749. .map((c) => orders[c] ?? '')
  750. .filter((x) => x.length > 0)
  751. .join(separationChar);
  752. };
  753. const externals = inbound.streamSettings?.externalProxy;
  754. if (!externals || externals.length === 0) {
  755. const r = composeRemark('');
  756. return [{ remark: r, link: genLink({ inbound, address: addr, port, forceTls: 'same', remark: r, client }) }];
  757. }
  758. return externals.map((ep) => {
  759. const r = composeRemark(ep.remark);
  760. return {
  761. remark: r,
  762. link: genLink({
  763. inbound,
  764. address: ep.dest,
  765. port: ep.port,
  766. forceTls: ep.forceTls,
  767. remark: r,
  768. client,
  769. externalProxy: ep,
  770. }),
  771. };
  772. });
  773. }
  774. export interface GenInboundLinksInput {
  775. inbound: Inbound;
  776. remark?: string;
  777. remarkModel?: string;
  778. hostOverride?: string;
  779. fallbackHostname: string;
  780. }
  781. // Top-level entrypoint that produces the full \r\n-joined block a user
  782. // pastes into a client. Iterates per-client for protocols with clients,
  783. // falls back to a single SS link for single-user 2022-blake3-chacha20,
  784. // and emits per-peer .conf blocks for wireguard. Returns '' for the
  785. // other clientless protocols (http, mixed, tunnel).
  786. export function genInboundLinks(input: GenInboundLinksInput): string {
  787. const {
  788. inbound,
  789. remark = '',
  790. remarkModel = '-io',
  791. hostOverride = '',
  792. fallbackHostname,
  793. } = input;
  794. const addr = resolveAddr(inbound, hostOverride, fallbackHostname);
  795. const clients = getInboundClients(inbound);
  796. if (clients) {
  797. const links: string[] = [];
  798. for (const client of clients) {
  799. const entries = genAllLinks({ inbound, remark, remarkModel, client, hostOverride, fallbackHostname });
  800. for (const e of entries) links.push(e.link);
  801. }
  802. return links.join('\r\n');
  803. }
  804. if (inbound.protocol === 'shadowsocks') {
  805. return genShadowsocksLink({ inbound, address: addr, port: inbound.port, forceTls: 'same', remark });
  806. }
  807. if (inbound.protocol === 'wireguard') {
  808. return genWireguardConfigs({ inbound, remark, remarkModel, hostOverride, fallbackHostname });
  809. }
  810. return '';
  811. }
  812. // Per-peer wireguard fanout. Each peer gets its own link (or .conf
  813. // block) with an index-suffixed remark, joined by \r\n. Matches the
  814. // legacy genWireguardLinks / genWireguardConfigs exactly.
  815. export interface GenWireguardFanoutInput {
  816. inbound: Inbound;
  817. remark?: string;
  818. remarkModel?: string;
  819. hostOverride?: string;
  820. fallbackHostname: string;
  821. }
  822. export function genWireguardLinks(input: GenWireguardFanoutInput): string {
  823. const { inbound, remark = '', remarkModel = '-io', hostOverride = '', fallbackHostname } = input;
  824. if (inbound.protocol !== 'wireguard') return '';
  825. const addr = resolveAddr(inbound, hostOverride, fallbackHostname);
  826. const sep = remarkModel.charAt(0);
  827. return inbound.settings.peers
  828. .map((_p, i) => genWireguardLink({
  829. settings: inbound.settings as WireguardInboundSettings,
  830. address: addr,
  831. port: inbound.port,
  832. remark: `${remark}${sep}${i + 1}`,
  833. peerIndex: i,
  834. }))
  835. .join('\r\n');
  836. }
  837. export function genWireguardConfigs(input: GenWireguardFanoutInput): string {
  838. const { inbound, remark = '', remarkModel = '-io', hostOverride = '', fallbackHostname } = input;
  839. if (inbound.protocol !== 'wireguard') return '';
  840. const addr = resolveAddr(inbound, hostOverride, fallbackHostname);
  841. const sep = remarkModel.charAt(0);
  842. return inbound.settings.peers
  843. .map((_p, i) => genWireguardConfig({
  844. settings: inbound.settings as WireguardInboundSettings,
  845. address: addr,
  846. port: inbound.port,
  847. remark: `${remark}${sep}${i + 1}`,
  848. peerIndex: i,
  849. }))
  850. .join('\r\n');
  851. }
  852. export function isPostQuantumLink(link: string): boolean {
  853. if (/[?&]pqv=/.test(link)) return true;
  854. if (link.includes('mlkem768') || link.includes('mldsa65')) return true;
  855. if (link.includes('ML-KEM-768')) return true;
  856. return false;
  857. }