inbound-link.ts 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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/shared/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 externalProxyPins(value: ExternalProxyEntry['pinnedPeerCertSha256']): string {
  107. if (Array.isArray(value)) return value.filter(Boolean).join(',');
  108. return '';
  109. }
  110. function applyExternalProxyTLSObj(
  111. externalProxy: ExternalProxyEntry | null | undefined,
  112. obj: Record<string, unknown>,
  113. security: string,
  114. ): void {
  115. if (!externalProxy || security !== 'tls') return;
  116. const sni = externalProxy.sni && externalProxy.sni.length > 0 ? externalProxy.sni : externalProxy.dest;
  117. if (sni && sni.length > 0) obj.sni = sni;
  118. if (externalProxy.fingerprint && externalProxy.fingerprint.length > 0) obj.fp = externalProxy.fingerprint;
  119. const alpn = externalProxyAlpn(externalProxy.alpn);
  120. if (alpn.length > 0) obj.alpn = alpn;
  121. const pins = externalProxyPins(externalProxy.pinnedPeerCertSha256);
  122. if (pins.length > 0) obj.pcs = pins;
  123. }
  124. export interface GenVmessLinkInput {
  125. inbound: Inbound;
  126. address: string;
  127. port?: number;
  128. forceTls?: ForceTls;
  129. remark?: string;
  130. clientId: string;
  131. security?: VmessSecurity;
  132. externalProxy?: ExternalProxyEntry | null;
  133. }
  134. // VMess share link: `vmess://` followed by base64-encoded JSON. The JSON
  135. // schema is the v2rayN-compatible "v2" shape. Returns '' if the inbound
  136. // is not vmess so dispatcher code can fall through cleanly.
  137. export function genVmessLink(input: GenVmessLinkInput): string {
  138. const {
  139. inbound,
  140. address,
  141. port = inbound.port,
  142. forceTls = 'same',
  143. remark = '',
  144. clientId,
  145. security,
  146. externalProxy = null,
  147. } = input;
  148. if (inbound.protocol !== 'vmess') return '';
  149. const stream = inbound.streamSettings;
  150. if (!stream) return '';
  151. const tls = forceTls === 'same' ? stream.security : forceTls;
  152. const obj: Record<string, unknown> = {
  153. v: '2',
  154. ps: remark,
  155. add: address,
  156. port,
  157. id: clientId,
  158. scy: security,
  159. net: stream.network,
  160. tls,
  161. };
  162. if (stream.network === 'tcp') {
  163. const tcp = stream.tcpSettings;
  164. const header = tcp.header;
  165. if (header) {
  166. obj.type = header.type;
  167. if (header.type === 'http') {
  168. const request = header.request;
  169. if (request) {
  170. obj.path = request.path.join(',');
  171. const host =
  172. getHeaderValue(header.response?.headers, 'host')
  173. || getHeaderValue(request.headers, 'host');
  174. if (host) obj.host = host;
  175. }
  176. }
  177. } else {
  178. obj.type = 'none';
  179. }
  180. } else if (stream.network === 'kcp') {
  181. const kcp = stream.kcpSettings;
  182. obj.mtu = kcp.mtu;
  183. obj.tti = kcp.tti;
  184. } else if (stream.network === 'ws') {
  185. const ws = stream.wsSettings;
  186. obj.path = ws.path;
  187. obj.host = ws.host.length > 0 ? ws.host : getHeaderValue(ws.headers, 'host');
  188. } else if (stream.network === 'grpc') {
  189. const grpc = stream.grpcSettings;
  190. obj.path = grpc.serviceName;
  191. obj.authority = grpc.authority;
  192. if (grpc.multiMode) obj.type = 'multi';
  193. } else if (stream.network === 'httpupgrade') {
  194. const hu = stream.httpupgradeSettings;
  195. obj.path = hu.path;
  196. obj.host = hu.host.length > 0 ? hu.host : getHeaderValue(hu.headers, 'host');
  197. } else if (stream.network === 'xhttp') {
  198. const xhttp = stream.xhttpSettings;
  199. obj.path = xhttp.path;
  200. obj.host = xhttp.host.length > 0 ? xhttp.host : xhttpHostFallback(xhttp);
  201. obj.type = xhttp.mode;
  202. applyXhttpExtraToObj(xhttp, obj);
  203. }
  204. applyFinalMaskToObj(stream.finalmask, obj);
  205. if (tls === 'tls' && stream.security === 'tls') {
  206. const tlsSettings = stream.tlsSettings;
  207. if (tlsSettings.serverName.length > 0) obj.sni = tlsSettings.serverName;
  208. if (tlsSettings.settings.fingerprint.length > 0) obj.fp = tlsSettings.settings.fingerprint;
  209. if (tlsSettings.alpn.length > 0) obj.alpn = tlsSettings.alpn.join(',');
  210. if (tlsSettings.settings.pinnedPeerCertSha256.length > 0) {
  211. obj.pcs = tlsSettings.settings.pinnedPeerCertSha256.join(',');
  212. }
  213. }
  214. applyExternalProxyTLSObj(externalProxy, obj, tls);
  215. return 'vmess://' + Base64.encode(JSON.stringify(obj, null, 2));
  216. }
  217. // Param-style helpers (vless/trojan/ss/hysteria links). These mirror the
  218. // legacy applyXhttpExtraToParams / applyFinalMaskToParams /
  219. // applyExternalProxyTLSParams but write to a URLSearchParams instance
  220. // directly. Number values get coerced via .toString() on set — same as
  221. // what URLSearchParams does internally so the resulting URL bytes match.
  222. function applyXhttpExtraToParams(xhttp: XHttpStreamSettings | undefined, params: URLSearchParams): void {
  223. if (!xhttp) return;
  224. params.set('path', xhttp.path);
  225. const host = xhttp.host.length > 0 ? xhttp.host : xhttpHostFallback(xhttp);
  226. params.set('host', host);
  227. params.set('mode', xhttp.mode);
  228. if (typeof xhttp.xPaddingBytes === 'string' && xhttp.xPaddingBytes.length > 0) {
  229. params.set('x_padding_bytes', xhttp.xPaddingBytes);
  230. }
  231. const extra = buildXhttpExtra(xhttp);
  232. if (extra) params.set('extra', JSON.stringify(extra));
  233. }
  234. function applyFinalMaskToParams(finalmask: FinalMaskStreamSettings | undefined, params: URLSearchParams): void {
  235. const payload = serializeFinalMask(finalmask);
  236. if (payload.length > 0) params.set('fm', payload);
  237. }
  238. function applyExternalProxyTLSParams(
  239. externalProxy: ExternalProxyEntry | null | undefined,
  240. params: URLSearchParams,
  241. security: string,
  242. ): void {
  243. if (!externalProxy || security !== 'tls') return;
  244. const sni = externalProxy.sni && externalProxy.sni.length > 0 ? externalProxy.sni : externalProxy.dest;
  245. if (sni && sni.length > 0) params.set('sni', sni);
  246. if (externalProxy.fingerprint && externalProxy.fingerprint.length > 0) params.set('fp', externalProxy.fingerprint);
  247. const alpn = externalProxyAlpn(externalProxy.alpn);
  248. if (alpn.length > 0) params.set('alpn', alpn);
  249. const pins = externalProxyPins(externalProxy.pinnedPeerCertSha256);
  250. if (pins.length > 0) params.set('pcs', pins);
  251. }
  252. export interface GenVlessLinkInput {
  253. inbound: Inbound;
  254. address: string;
  255. port?: number;
  256. forceTls?: ForceTls;
  257. remark?: string;
  258. clientId: string;
  259. flow?: VlessClient['flow'];
  260. externalProxy?: ExternalProxyEntry | null;
  261. }
  262. // VLESS share link: vless://<uuid>@<host>:<port>?<query>#<remark>. The
  263. // query carries network type, encryption, network-specific knobs, and
  264. // security-specific knobs (TLS fingerprint/alpn/sni or Reality
  265. // pbk/sid/spx). Returns '' if the inbound isn't vless.
  266. export function genVlessLink(input: GenVlessLinkInput): string {
  267. const {
  268. inbound,
  269. address,
  270. port = inbound.port,
  271. forceTls = 'same',
  272. remark = '',
  273. clientId,
  274. flow = '',
  275. externalProxy = null,
  276. } = input;
  277. if (inbound.protocol !== 'vless') return '';
  278. const stream = inbound.streamSettings;
  279. if (!stream) return '';
  280. const security = forceTls === 'same' ? stream.security : forceTls;
  281. const params = new URLSearchParams();
  282. params.set('type', stream.network);
  283. params.set('encryption', inbound.settings.encryption);
  284. if (stream.network === 'tcp') {
  285. const tcp = stream.tcpSettings;
  286. if (tcp.header?.type === 'http') {
  287. const request = tcp.header.request;
  288. if (request) {
  289. params.set('path', request.path.join(','));
  290. const host =
  291. getHeaderValue(tcp.header.response?.headers, 'host')
  292. || getHeaderValue(request.headers, 'host');
  293. if (host) params.set('host', host);
  294. params.set('headerType', 'http');
  295. }
  296. }
  297. } else if (stream.network === 'kcp') {
  298. const kcp = stream.kcpSettings;
  299. params.set('mtu', String(kcp.mtu));
  300. params.set('tti', String(kcp.tti));
  301. } else if (stream.network === 'ws') {
  302. const ws = stream.wsSettings;
  303. params.set('path', ws.path);
  304. params.set('host', ws.host.length > 0 ? ws.host : getHeaderValue(ws.headers, 'host'));
  305. } else if (stream.network === 'grpc') {
  306. const grpc = stream.grpcSettings;
  307. params.set('serviceName', grpc.serviceName);
  308. params.set('authority', grpc.authority);
  309. if (grpc.multiMode) params.set('mode', 'multi');
  310. } else if (stream.network === 'httpupgrade') {
  311. const hu = stream.httpupgradeSettings;
  312. params.set('path', hu.path);
  313. params.set('host', hu.host.length > 0 ? hu.host : getHeaderValue(hu.headers, 'host'));
  314. } else if (stream.network === 'xhttp') {
  315. applyXhttpExtraToParams(stream.xhttpSettings, params);
  316. }
  317. applyFinalMaskToParams(stream.finalmask, params);
  318. if (security === 'tls') {
  319. params.set('security', 'tls');
  320. if (stream.security === 'tls') {
  321. const tls = stream.tlsSettings;
  322. params.set('fp', tls.settings.fingerprint);
  323. params.set('alpn', tls.alpn.join(','));
  324. if (tls.serverName.length > 0) params.set('sni', tls.serverName);
  325. if (tls.settings.echConfigList.length > 0) params.set('ech', tls.settings.echConfigList);
  326. if (tls.settings.pinnedPeerCertSha256.length > 0) {
  327. params.set('pcs', tls.settings.pinnedPeerCertSha256.join(','));
  328. }
  329. if (stream.network === 'tcp' && flow.length > 0) params.set('flow', flow);
  330. }
  331. applyExternalProxyTLSParams(externalProxy, params, security);
  332. } else if (security === 'reality') {
  333. params.set('security', 'reality');
  334. if (stream.security === 'reality') {
  335. const reality = stream.realitySettings;
  336. params.set('pbk', reality.settings.publicKey);
  337. params.set('fp', reality.settings.fingerprint);
  338. const sni =
  339. reality.settings.serverName ||
  340. reality.serverNames?.[0] ||
  341. reality.target?.split(':')[0];
  342. if (sni && sni.length > 0) params.set('sni', sni);
  343. if (reality.shortIds.length > 0) params.set('sid', reality.shortIds[0]);
  344. if (reality.settings.spiderX.length > 0) params.set('spx', reality.settings.spiderX);
  345. if (reality.settings.mldsa65Verify.length > 0) params.set('pqv', reality.settings.mldsa65Verify);
  346. if (stream.network === 'tcp' && flow.length > 0) params.set('flow', flow);
  347. }
  348. } else {
  349. params.set('security', 'none');
  350. }
  351. const url = new URL(`vless://${clientId}@${address}:${port}`);
  352. for (const [key, value] of params) url.searchParams.set(key, value);
  353. url.hash = encodeURIComponent(remark);
  354. return url.toString();
  355. }
  356. // Shared network-branch writer used by trojan + shadowsocks links.
  357. // VLESS and VMess don't call this because they have minor per-protocol
  358. // quirks inline (vmess maps `multi` differently into obj.type; vless sets
  359. // encryption=none up-front).
  360. function writeNetworkParams(stream: NonNullable<Inbound['streamSettings']>, params: URLSearchParams): void {
  361. if (stream.network === 'tcp') {
  362. const tcp = stream.tcpSettings;
  363. if (tcp.header?.type === 'http') {
  364. const request = tcp.header.request;
  365. if (request) {
  366. params.set('path', request.path.join(','));
  367. const host =
  368. getHeaderValue(tcp.header.response?.headers, 'host')
  369. || getHeaderValue(request.headers, 'host');
  370. if (host) params.set('host', host);
  371. params.set('headerType', 'http');
  372. }
  373. }
  374. } else if (stream.network === 'kcp') {
  375. const kcp = stream.kcpSettings;
  376. params.set('mtu', String(kcp.mtu));
  377. params.set('tti', String(kcp.tti));
  378. } else if (stream.network === 'ws') {
  379. const ws = stream.wsSettings;
  380. params.set('path', ws.path);
  381. params.set('host', ws.host.length > 0 ? ws.host : getHeaderValue(ws.headers, 'host'));
  382. } else if (stream.network === 'grpc') {
  383. const grpc = stream.grpcSettings;
  384. params.set('serviceName', grpc.serviceName);
  385. params.set('authority', grpc.authority);
  386. if (grpc.multiMode) params.set('mode', 'multi');
  387. } else if (stream.network === 'httpupgrade') {
  388. const hu = stream.httpupgradeSettings;
  389. params.set('path', hu.path);
  390. params.set('host', hu.host.length > 0 ? hu.host : getHeaderValue(hu.headers, 'host'));
  391. } else if (stream.network === 'xhttp') {
  392. applyXhttpExtraToParams(stream.xhttpSettings, params);
  393. }
  394. }
  395. function writeTlsParams(stream: NonNullable<Inbound['streamSettings']>, params: URLSearchParams): void {
  396. if (stream.security !== 'tls') return;
  397. const tls = stream.tlsSettings;
  398. params.set('fp', tls.settings.fingerprint);
  399. params.set('alpn', tls.alpn.join(','));
  400. if (tls.settings.echConfigList.length > 0) params.set('ech', tls.settings.echConfigList);
  401. if (tls.serverName.length > 0) params.set('sni', tls.serverName);
  402. if (tls.settings.pinnedPeerCertSha256.length > 0) {
  403. params.set('pcs', tls.settings.pinnedPeerCertSha256.join(','));
  404. }
  405. }
  406. // Reality query-string writer shared by VLESS and Trojan. Preserves the
  407. // legacy SNI-omission quirk (see genVlessLink for the full story).
  408. function writeRealityParams(stream: NonNullable<Inbound['streamSettings']>, params: URLSearchParams): void {
  409. if (stream.security !== 'reality') return;
  410. const reality = stream.realitySettings;
  411. params.set('pbk', reality.settings.publicKey);
  412. params.set('fp', reality.settings.fingerprint);
  413. const sni =
  414. reality.settings.serverName ||
  415. reality.serverNames?.[0] ||
  416. reality.target?.split(':')[0];
  417. if (sni && sni.length > 0) params.set('sni', sni);
  418. if (reality.shortIds.length > 0) params.set('sid', reality.shortIds[0]);
  419. if (reality.settings.spiderX.length > 0) params.set('spx', reality.settings.spiderX);
  420. if (reality.settings.mldsa65Verify.length > 0) params.set('pqv', reality.settings.mldsa65Verify);
  421. }
  422. export interface GenTrojanLinkInput {
  423. inbound: Inbound;
  424. address: string;
  425. port?: number;
  426. forceTls?: ForceTls;
  427. remark?: string;
  428. clientPassword: string;
  429. externalProxy?: ExternalProxyEntry | null;
  430. }
  431. // Trojan share link: trojan://<password>@<host>:<port>?<query>#<remark>.
  432. // Same query-string shape as VLESS minus the `encryption` and `flow`
  433. // fields. Returns '' if the inbound isn't trojan.
  434. export function genTrojanLink(input: GenTrojanLinkInput): string {
  435. const {
  436. inbound,
  437. address,
  438. port = inbound.port,
  439. forceTls = 'same',
  440. remark = '',
  441. clientPassword,
  442. externalProxy = null,
  443. } = input;
  444. if (inbound.protocol !== 'trojan') return '';
  445. const stream = inbound.streamSettings;
  446. if (!stream) return '';
  447. const security = forceTls === 'same' ? stream.security : forceTls;
  448. const params = new URLSearchParams();
  449. params.set('type', stream.network);
  450. writeNetworkParams(stream, params);
  451. applyFinalMaskToParams(stream.finalmask, params);
  452. if (security === 'tls') {
  453. params.set('security', 'tls');
  454. writeTlsParams(stream, params);
  455. applyExternalProxyTLSParams(externalProxy, params, security);
  456. } else if (security === 'reality') {
  457. params.set('security', 'reality');
  458. writeRealityParams(stream, params);
  459. } else {
  460. params.set('security', 'none');
  461. }
  462. const url = new URL(`trojan://${encodeURIComponent(clientPassword)}@${address}:${port}`);
  463. for (const [key, value] of params) url.searchParams.set(key, value);
  464. url.hash = encodeURIComponent(remark);
  465. return url.toString();
  466. }
  467. export interface GenShadowsocksLinkInput {
  468. inbound: Inbound;
  469. address: string;
  470. port?: number;
  471. forceTls?: ForceTls;
  472. remark?: string;
  473. clientPassword?: string;
  474. externalProxy?: ExternalProxyEntry | null;
  475. }
  476. // Shadowsocks 2022 share link. The userinfo portion is base64(method:pw)
  477. // for single-user and base64(method:settingsPw:clientPw) for multi-user
  478. // 2022-blake3. Legacy SS (non-2022) leaves the password out of the
  479. // userinfo entirely — matches the legacy class's password-array logic.
  480. // Note: legacy `isSSMultiUser` returns true for everything except
  481. // 2022-blake3-chacha20-poly1305 (a curious classification, but we
  482. // preserve it for byte-stable parity).
  483. export function genShadowsocksLink(input: GenShadowsocksLinkInput): string {
  484. const {
  485. inbound,
  486. address,
  487. port = inbound.port,
  488. forceTls = 'same',
  489. remark = '',
  490. clientPassword = '',
  491. externalProxy = null,
  492. } = input;
  493. if (inbound.protocol !== 'shadowsocks') return '';
  494. const stream = inbound.streamSettings;
  495. if (!stream) return '';
  496. const settings = inbound.settings;
  497. const security = forceTls === 'same' ? stream.security : forceTls;
  498. const params = new URLSearchParams();
  499. params.set('type', stream.network);
  500. writeNetworkParams(stream, params);
  501. applyFinalMaskToParams(stream.finalmask, params);
  502. if (security === 'tls') {
  503. params.set('security', 'tls');
  504. writeTlsParams(stream, params);
  505. applyExternalProxyTLSParams(externalProxy, params, security);
  506. }
  507. const isSS2022 = settings.method.substring(0, 4) === '2022';
  508. const isSSMultiUser = settings.method !== '2022-blake3-chacha20-poly1305';
  509. const passwords: string[] = [];
  510. if (isSS2022) passwords.push(settings.password);
  511. if (isSSMultiUser) passwords.push(clientPassword);
  512. const userinfo = Base64.encode(`${settings.method}:${passwords.join(':')}`, true);
  513. const url = new URL(`ss://${userinfo}@${address}:${port}`);
  514. for (const [key, value] of params) url.searchParams.set(key, value);
  515. url.hash = encodeURIComponent(remark);
  516. return url.toString();
  517. }
  518. export interface GenHysteriaLinkInput {
  519. inbound: Inbound;
  520. address: string;
  521. port?: number;
  522. remark?: string;
  523. clientAuth: string;
  524. externalProxy?: ExternalProxyEntry | null;
  525. }
  526. // Hysteria2's pinSHA256 must be a 64-char lowercase hex string — Xray-core
  527. // clients hex-decode it and crash on a base64 value. The panel stores pins as
  528. // base64 (xray-core's native TLS format / the generate button) or hex, either
  529. // bare or colon-separated as `openssl x509 -fingerprint -sha256` emits it. Each
  530. // entry is coerced to bare hex. Values that are neither a 32-byte hex nor a
  531. // 32-byte base64 SHA-256 pass through unchanged.
  532. function hysteriaPinHex(pin: string): string {
  533. const stripped = pin.trim().replace(/:/g, '');
  534. if (/^[0-9a-fA-F]{64}$/.test(stripped)) return stripped.toLowerCase();
  535. try {
  536. const binary = atob(pin.trim().replace(/-/g, '+').replace(/_/g, '/'));
  537. if (binary.length !== 32) return pin;
  538. let hex = '';
  539. for (let i = 0; i < binary.length; i++) {
  540. hex += binary.charCodeAt(i).toString(16).padStart(2, '0');
  541. }
  542. return hex;
  543. } catch {
  544. return pin;
  545. }
  546. }
  547. // Hysteria share link: hysteria://<auth>@<host>:<port>?<query>#<remark>.
  548. // The URL scheme is "hysteria2" when settings.version === 2 (hysteria v2
  549. // AKA hysteria2), "hysteria" otherwise. Salamander obfuscation pulls its
  550. // password from finalmask.udp[type=salamander] when present; the broader
  551. // finalmask payload still rides under `fm` like the other links.
  552. //
  553. // Note: legacy genHysteriaLink reads stream.tls.settings.allowInsecure,
  554. // which isn't a field on TlsStreamSettings.Settings — the guard is always
  555. // false. We omit the `insecure` param here to stay byte-stable.
  556. export function genHysteriaLink(input: GenHysteriaLinkInput): string {
  557. const {
  558. inbound,
  559. address,
  560. port = inbound.port,
  561. remark = '',
  562. clientAuth,
  563. externalProxy = null,
  564. } = input;
  565. if (inbound.protocol !== 'hysteria') return '';
  566. const stream = inbound.streamSettings;
  567. if (!stream || stream.security !== 'tls') return '';
  568. const settings = inbound.settings;
  569. const scheme = settings.version === 2 ? 'hysteria2' : 'hysteria';
  570. const params = new URLSearchParams();
  571. params.set('security', 'tls');
  572. const tls = stream.tlsSettings;
  573. if (tls.settings.fingerprint.length > 0) params.set('fp', tls.settings.fingerprint);
  574. if (tls.alpn.length > 0) params.set('alpn', tls.alpn.join(','));
  575. if (tls.settings.echConfigList.length > 0) params.set('ech', tls.settings.echConfigList);
  576. if (tls.serverName.length > 0) params.set('sni', tls.serverName);
  577. if (tls.settings.pinnedPeerCertSha256.length > 0) {
  578. params.set('pinSHA256', tls.settings.pinnedPeerCertSha256.map(hysteriaPinHex).join(','));
  579. }
  580. // An external-proxy entry can pin a different endpoint's certificate.
  581. // Hysteria carries it as hex `pinSHA256` (not the `pcs` other protocols
  582. // use), so coerce each entry through hysteriaPinHex like the main pin.
  583. if (Array.isArray(externalProxy?.pinnedPeerCertSha256)) {
  584. const epPins = externalProxy.pinnedPeerCertSha256.filter(Boolean).map(hysteriaPinHex);
  585. if (epPins.length > 0) params.set('pinSHA256', epPins.join(','));
  586. }
  587. const udpMasks = stream.finalmask?.udp;
  588. if (Array.isArray(udpMasks)) {
  589. const salamander = udpMasks.find((m) => m?.type === 'salamander');
  590. const obfsPassword = salamander?.settings?.password;
  591. if (typeof obfsPassword === 'string' && obfsPassword.length > 0) {
  592. params.set('obfs', 'salamander');
  593. params.set('obfs-password', obfsPassword);
  594. }
  595. }
  596. applyFinalMaskToParams(stream.finalmask, params);
  597. const hopPorts = stream.finalmask?.quicParams?.udpHop?.ports?.trim() ?? '';
  598. if (hopPorts.length > 0) {
  599. params.set('mport', hopPorts);
  600. }
  601. const url = new URL(`${scheme}://${clientAuth}@${address}:${port}`);
  602. for (const [key, value] of params) url.searchParams.set(key, value);
  603. url.hash = encodeURIComponent(remark);
  604. return url.toString();
  605. }
  606. export interface GenWireguardLinkInput {
  607. settings: WireguardInboundSettings;
  608. address: string;
  609. port: number;
  610. remark?: string;
  611. peerIndex: number;
  612. }
  613. // Wireguard share link: wireguard://<peerPrivKey>@<host>:<port>
  614. // ?publickey=<serverPub>&address=<peerAllowedIP>&mtu=<mtu>#<remark>
  615. // pubKey is derived from the server's secretKey via Wireguard.generateKeypair
  616. // at call time (Zod's schema stores secretKey only — pubKey isn't on the
  617. // wire). Returns '' when the peer index is out of bounds.
  618. export function genWireguardLink(input: GenWireguardLinkInput): string {
  619. const { settings, address, port, remark = '', peerIndex } = input;
  620. const peer = settings.peers[peerIndex];
  621. if (!peer) return '';
  622. const url = new URL(`wireguard://${address}:${port}`);
  623. url.username = peer.privateKey ?? '';
  624. const pubKey = settings.secretKey.length > 0
  625. ? Wireguard.generateKeypair(settings.secretKey).publicKey
  626. : '';
  627. if (pubKey.length > 0) url.searchParams.set('publickey', pubKey);
  628. if (peer.allowedIPs.length > 0 && peer.allowedIPs[0]) {
  629. url.searchParams.set('address', peer.allowedIPs[0]);
  630. }
  631. if (typeof settings.mtu === 'number' && settings.mtu > 0) {
  632. url.searchParams.set('mtu', String(settings.mtu));
  633. }
  634. url.hash = encodeURIComponent(remark);
  635. return url.toString();
  636. }
  637. // Plain-text WireGuard client config (.conf format). Mirrors the legacy
  638. // getWireguardTxt — same DNS defaults (1.1.1.1, 1.0.0.1), MTU optional,
  639. // presharedKey + keepAlive only emitted when present on the peer. The
  640. // final newline structure follows the legacy: no newline after Endpoint,
  641. // optional preSharedKey appended with leading \n, keepAlive appended
  642. // with leading \n AND trailing \n.
  643. export function genWireguardConfig(input: GenWireguardLinkInput): string {
  644. const { settings, address, port, remark = '', peerIndex } = input;
  645. const peer = settings.peers[peerIndex];
  646. if (!peer) return '';
  647. const pubKey = settings.secretKey.length > 0
  648. ? Wireguard.generateKeypair(settings.secretKey).publicKey
  649. : '';
  650. let txt = `[Interface]\n`;
  651. txt += `PrivateKey = ${peer.privateKey ?? ''}\n`;
  652. txt += `Address = ${peer.allowedIPs[0] ?? ''}\n`;
  653. txt += `DNS = 1.1.1.1, 1.0.0.1\n`;
  654. if (typeof settings.mtu === 'number' && settings.mtu > 0) {
  655. txt += `MTU = ${settings.mtu}\n`;
  656. }
  657. txt += `\n# ${remark}\n`;
  658. txt += `[Peer]\n`;
  659. txt += `PublicKey = ${pubKey}\n`;
  660. txt += `AllowedIPs = 0.0.0.0/0, ::/0\n`;
  661. txt += `Endpoint = ${address}:${port}`;
  662. if (peer.preSharedKey && peer.preSharedKey.length > 0) {
  663. txt += `\nPresharedKey = ${peer.preSharedKey}`;
  664. }
  665. if (typeof peer.keepAlive === 'number' && peer.keepAlive > 0) {
  666. txt += `\nPersistentKeepalive = ${peer.keepAlive}\n`;
  667. }
  668. return txt;
  669. }
  670. export type { WireguardInboundPeer };
  671. function isUnixSocketListen(listen: string): boolean {
  672. return listen.startsWith('/') || listen.startsWith('@');
  673. }
  674. // Orchestrators.
  675. // resolveAddr picks the host that goes into share/sub links. Order:
  676. // 1. hostOverride (caller supplies node address for node-managed inbounds)
  677. // 2. inbound's bind listen (when it's an explicit reachable address —
  678. // not 0.0.0.0 and not a unix domain socket path)
  679. // 3. fallbackHostname (caller-supplied — typically window.location.hostname
  680. // in the browser; tests pass a fixed value)
  681. export function resolveAddr(inbound: Inbound, hostOverride: string, fallbackHostname: string): string {
  682. if (hostOverride.length > 0) return hostOverride;
  683. if (inbound.listen.length > 0 && inbound.listen !== '0.0.0.0' && !isUnixSocketListen(inbound.listen)) {
  684. return inbound.listen;
  685. }
  686. return fallbackHostname;
  687. }
  688. // A loopback browser host means the panel was reached through a tunnel (e.g.
  689. // SSH-forwarded 127.0.0.1/localhost), so it can never be a shareable link host.
  690. function isLoopbackHost(host: string): boolean {
  691. const h = host.trim().replace(/^\[|\]$/g, '').toLowerCase();
  692. return h === 'localhost' || h === '::1' || h.startsWith('127.');
  693. }
  694. // preferPublicHost is the browser-side analog of the backend's
  695. // configuredPublicHost: when the panel is reached on a loopback host, prefer a
  696. // configured public host (Sub/Web Domain) for share/QR links so they match the
  697. // subscription links instead of leaking localhost. An explicit per-inbound
  698. // listen or node override still wins, since resolveAddr only reaches the
  699. // fallbackHostname after those.
  700. export function preferPublicHost(browserHost: string, publicHost: string): string {
  701. return publicHost && isLoopbackHost(browserHost) ? publicHost : browserHost;
  702. }
  703. // Returns the client array for protocols that have one. SS returns its
  704. // clients only in 2022-blake3 multi-user mode (matches the legacy
  705. // `this.clients` getter, which used isSSMultiUser to gate). Returns null
  706. // for SS single-user, http, mixed, tunnel, wireguard, hysteria2-without-
  707. // clients, and any protocol without a clients array.
  708. type ClientShape = { id?: string; security?: VmessSecurity; flow?: VlessClient['flow']; password?: string; auth?: string; email?: string };
  709. export function getInboundClients(inbound: Inbound): ClientShape[] | null {
  710. switch (inbound.protocol) {
  711. case 'vmess':
  712. return (inbound.settings.clients ?? []) as ClientShape[];
  713. case 'vless':
  714. return (inbound.settings.clients ?? []) as ClientShape[];
  715. case 'trojan':
  716. return (inbound.settings.clients ?? []) as ClientShape[];
  717. case 'hysteria':
  718. return (inbound.settings.clients ?? []) as ClientShape[];
  719. case 'shadowsocks': {
  720. const isMultiUser = inbound.settings.method !== '2022-blake3-chacha20-poly1305';
  721. return isMultiUser ? ((inbound.settings.clients ?? []) as ClientShape[]) : null;
  722. }
  723. default:
  724. return null;
  725. }
  726. }
  727. export interface GenLinkInput {
  728. inbound: Inbound;
  729. address: string;
  730. port?: number;
  731. forceTls?: ForceTls;
  732. remark?: string;
  733. client: ClientShape;
  734. externalProxy?: ExternalProxyEntry | null;
  735. }
  736. // Per-protocol dispatcher matching the legacy `genLink` switch. Returns
  737. // '' for protocols that don't have client-based share links (wireguard
  738. // goes through genWireguardLinks/Configs separately, http/mixed/tunnel
  739. // don't have share URLs).
  740. export function genLink(input: GenLinkInput): string {
  741. const { inbound, address, port = inbound.port, forceTls = 'same', remark = '', client, externalProxy = null } = input;
  742. switch (inbound.protocol) {
  743. case 'vmess':
  744. return genVmessLink({
  745. inbound, address, port, forceTls, remark,
  746. clientId: client.id ?? '',
  747. security: client.security,
  748. externalProxy,
  749. });
  750. case 'vless':
  751. return genVlessLink({
  752. inbound, address, port, forceTls, remark,
  753. clientId: client.id ?? '',
  754. flow: client.flow,
  755. externalProxy,
  756. });
  757. case 'shadowsocks': {
  758. const isMultiUser = inbound.settings.method !== '2022-blake3-chacha20-poly1305';
  759. return genShadowsocksLink({
  760. inbound, address, port, forceTls, remark,
  761. clientPassword: isMultiUser ? (client.password ?? '') : '',
  762. externalProxy,
  763. });
  764. }
  765. case 'trojan':
  766. return genTrojanLink({
  767. inbound, address, port, forceTls, remark,
  768. clientPassword: client.password ?? '',
  769. externalProxy,
  770. });
  771. case 'hysteria':
  772. return genHysteriaLink({
  773. inbound, address, port, remark,
  774. clientAuth: client.auth ?? '',
  775. externalProxy,
  776. });
  777. default:
  778. return '';
  779. }
  780. }
  781. export interface GenAllLinksEntry {
  782. remark: string;
  783. link: string;
  784. }
  785. export interface GenAllLinksInput {
  786. inbound: Inbound;
  787. remark?: string;
  788. remarkModel?: string;
  789. client: ClientShape;
  790. hostOverride?: string;
  791. fallbackHostname: string;
  792. }
  793. // Fans out a single client's link per externalProxy entry, or just one
  794. // link when there are no external proxies. remarkModel is a 4-char
  795. // string: first char is the separator, remaining chars pick which
  796. // pieces to compose into the per-link remark — 'i' = inbound remark,
  797. // 'e' = client email, 'o' = externalProxy remark. Defaults to '-io'
  798. // (dash-separated, inbound + email + proxy).
  799. export function genAllLinks(input: GenAllLinksInput): GenAllLinksEntry[] {
  800. const {
  801. inbound,
  802. remark = '',
  803. remarkModel = '-io',
  804. client,
  805. hostOverride = '',
  806. fallbackHostname,
  807. } = input;
  808. const addr = resolveAddr(inbound, hostOverride, fallbackHostname);
  809. const port = inbound.port;
  810. const separationChar = remarkModel.charAt(0);
  811. const orderChars = remarkModel.slice(1);
  812. const email = client.email ?? '';
  813. const composeRemark = (proxyRemark: string): string => {
  814. const orders: Record<string, string> = { i: remark, e: email, o: proxyRemark };
  815. return orderChars.split('')
  816. .map((c) => orders[c] ?? '')
  817. .filter((x) => x.length > 0)
  818. .join(separationChar);
  819. };
  820. const externals = inbound.streamSettings?.externalProxy;
  821. if (!externals || externals.length === 0) {
  822. const r = composeRemark('');
  823. return [{ remark: r, link: genLink({ inbound, address: addr, port, forceTls: 'same', remark: r, client }) }];
  824. }
  825. return externals.map((ep) => {
  826. const r = composeRemark(ep.remark);
  827. return {
  828. remark: r,
  829. link: genLink({
  830. inbound,
  831. address: ep.dest,
  832. port: ep.port,
  833. forceTls: ep.forceTls,
  834. remark: r,
  835. client,
  836. externalProxy: ep,
  837. }),
  838. };
  839. });
  840. }
  841. export interface GenInboundLinksInput {
  842. inbound: Inbound;
  843. remark?: string;
  844. remarkModel?: string;
  845. hostOverride?: string;
  846. fallbackHostname: string;
  847. }
  848. // Top-level entrypoint that produces the full \r\n-joined block a user
  849. // pastes into a client. Iterates per-client for protocols with clients,
  850. // falls back to a single SS link for single-user 2022-blake3-chacha20,
  851. // and emits per-peer .conf blocks for wireguard. Returns '' for the
  852. // other clientless protocols (http, mixed, tunnel).
  853. export function genInboundLinks(input: GenInboundLinksInput): string {
  854. const {
  855. inbound,
  856. remark = '',
  857. remarkModel = '-io',
  858. hostOverride = '',
  859. fallbackHostname,
  860. } = input;
  861. const addr = resolveAddr(inbound, hostOverride, fallbackHostname);
  862. const clients = getInboundClients(inbound);
  863. if (clients) {
  864. const links: string[] = [];
  865. for (const client of clients) {
  866. const entries = genAllLinks({ inbound, remark, remarkModel, client, hostOverride, fallbackHostname });
  867. for (const e of entries) links.push(e.link);
  868. }
  869. return links.join('\r\n');
  870. }
  871. if (inbound.protocol === 'shadowsocks') {
  872. return genShadowsocksLink({ inbound, address: addr, port: inbound.port, forceTls: 'same', remark });
  873. }
  874. if (inbound.protocol === 'wireguard') {
  875. return genWireguardConfigs({ inbound, remark, remarkModel, hostOverride, fallbackHostname });
  876. }
  877. return '';
  878. }
  879. // Per-peer wireguard fanout. Each peer gets its own link (or .conf
  880. // block) with an index-suffixed remark, joined by \r\n. Matches the
  881. // legacy genWireguardLinks / genWireguardConfigs exactly.
  882. export interface GenWireguardFanoutInput {
  883. inbound: Inbound;
  884. remark?: string;
  885. remarkModel?: string;
  886. hostOverride?: string;
  887. fallbackHostname: string;
  888. }
  889. export function genWireguardLinks(input: GenWireguardFanoutInput): string {
  890. const { inbound, remark = '', remarkModel = '-io', hostOverride = '', fallbackHostname } = input;
  891. if (inbound.protocol !== 'wireguard') return '';
  892. const addr = resolveAddr(inbound, hostOverride, fallbackHostname);
  893. const sep = remarkModel.charAt(0);
  894. return inbound.settings.peers
  895. .map((_p, i) => genWireguardLink({
  896. settings: inbound.settings as WireguardInboundSettings,
  897. address: addr,
  898. port: inbound.port,
  899. remark: `${remark}${sep}${i + 1}`,
  900. peerIndex: i,
  901. }))
  902. .join('\r\n');
  903. }
  904. export function genWireguardConfigs(input: GenWireguardFanoutInput): string {
  905. const { inbound, remark = '', remarkModel = '-io', hostOverride = '', fallbackHostname } = input;
  906. if (inbound.protocol !== 'wireguard') return '';
  907. const addr = resolveAddr(inbound, hostOverride, fallbackHostname);
  908. const sep = remarkModel.charAt(0);
  909. return inbound.settings.peers
  910. .map((_p, i) => genWireguardConfig({
  911. settings: inbound.settings as WireguardInboundSettings,
  912. address: addr,
  913. port: inbound.port,
  914. remark: `${remark}${sep}${i + 1}`,
  915. peerIndex: i,
  916. }))
  917. .join('\r\n');
  918. }
  919. export function isPostQuantumLink(link: string): boolean {
  920. if (/[?&]pqv=/.test(link)) return true;
  921. if (link.includes('mlkem768') || link.includes('mldsa65')) return true;
  922. if (link.includes('ML-KEM-768')) return true;
  923. return false;
  924. }