outbound.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. const Protocols = {
  2. Freedom: "freedom",
  3. Blackhole: "blackhole",
  4. DNS: "dns",
  5. VMess: "vmess",
  6. VLESS: "vless",
  7. Trojan: "trojan",
  8. Shadowsocks: "shadowsocks",
  9. Socks: "socks",
  10. HTTP: "http",
  11. Wireguard: "wireguard"
  12. };
  13. const SSMethods = {
  14. AES_256_GCM: 'aes-256-gcm',
  15. AES_128_GCM: 'aes-128-gcm',
  16. CHACHA20_POLY1305: 'chacha20-poly1305',
  17. CHACHA20_IETF_POLY1305: 'chacha20-ietf-poly1305',
  18. XCHACHA20_POLY1305: 'xchacha20-poly1305',
  19. XCHACHA20_IETF_POLY1305: 'xchacha20-ietf-poly1305',
  20. BLAKE3_AES_128_GCM: '2022-blake3-aes-128-gcm',
  21. BLAKE3_AES_256_GCM: '2022-blake3-aes-256-gcm',
  22. BLAKE3_CHACHA20_POLY1305: '2022-blake3-chacha20-poly1305',
  23. };
  24. const TLS_FLOW_CONTROL = {
  25. VISION: "xtls-rprx-vision",
  26. VISION_UDP443: "xtls-rprx-vision-udp443",
  27. };
  28. const UTLS_FINGERPRINT = {
  29. UTLS_CHROME: "chrome",
  30. UTLS_FIREFOX: "firefox",
  31. UTLS_SAFARI: "safari",
  32. UTLS_IOS: "ios",
  33. UTLS_android: "android",
  34. UTLS_EDGE: "edge",
  35. UTLS_360: "360",
  36. UTLS_QQ: "qq",
  37. UTLS_RANDOM: "random",
  38. UTLS_RANDOMIZED: "randomized",
  39. };
  40. const ALPN_OPTION = {
  41. H3: "h3",
  42. H2: "h2",
  43. HTTP1: "http/1.1",
  44. };
  45. const OutboundDomainStrategies = [
  46. "AsIs",
  47. "UseIP",
  48. "UseIPv4",
  49. "UseIPv6",
  50. "UseIPv6v4",
  51. "UseIPv4v6",
  52. "ForceIP",
  53. "ForceIPv6v4",
  54. "ForceIPv6",
  55. "ForceIPv4v6",
  56. "ForceIPv4"
  57. ];
  58. const WireguardDomainStrategy = [
  59. "ForceIP",
  60. "ForceIPv4",
  61. "ForceIPv4v6",
  62. "ForceIPv6",
  63. "ForceIPv6v4"
  64. ];
  65. const USERS_SECURITY = {
  66. AES_128_GCM: "aes-128-gcm",
  67. CHACHA20_POLY1305: "chacha20-poly1305",
  68. AUTO: "auto",
  69. NONE: "none",
  70. ZERO: "zero",
  71. };
  72. const MODE_OPTION = {
  73. AUTO: "auto",
  74. PACKET_UP: "packet-up",
  75. STREAM_UP: "stream-up",
  76. STREAM_ONE: "stream-one",
  77. };
  78. Object.freeze(Protocols);
  79. Object.freeze(SSMethods);
  80. Object.freeze(TLS_FLOW_CONTROL);
  81. Object.freeze(UTLS_FINGERPRINT);
  82. Object.freeze(ALPN_OPTION);
  83. Object.freeze(OutboundDomainStrategies);
  84. Object.freeze(WireguardDomainStrategy);
  85. Object.freeze(USERS_SECURITY);
  86. Object.freeze(MODE_OPTION);
  87. class CommonClass {
  88. static toJsonArray(arr) {
  89. return arr.map(obj => obj.toJson());
  90. }
  91. static fromJson() {
  92. return new CommonClass();
  93. }
  94. toJson() {
  95. return this;
  96. }
  97. toString(format = true) {
  98. return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
  99. }
  100. }
  101. class TcpStreamSettings extends CommonClass {
  102. constructor(type = 'none', host, path) {
  103. super();
  104. this.type = type;
  105. this.host = host;
  106. this.path = path;
  107. }
  108. static fromJson(json = {}) {
  109. let header = json.header;
  110. if (!header) return new TcpStreamSettings();
  111. if (header.type == 'http' && header.request) {
  112. return new TcpStreamSettings(
  113. header.type,
  114. header.request.headers.Host.join(','),
  115. header.request.path.join(','),
  116. );
  117. }
  118. return new TcpStreamSettings(header.type, '', '');
  119. }
  120. toJson() {
  121. return {
  122. header: {
  123. type: this.type,
  124. request: this.type === 'http' ? {
  125. headers: {
  126. Host: ObjectUtil.isEmpty(this.host) ? [] : this.host.split(',')
  127. },
  128. path: ObjectUtil.isEmpty(this.path) ? ["/"] : this.path.split(',')
  129. } : undefined,
  130. }
  131. };
  132. }
  133. }
  134. class KcpStreamSettings extends CommonClass {
  135. constructor(
  136. mtu = 1350,
  137. tti = 50,
  138. uplinkCapacity = 5,
  139. downlinkCapacity = 20,
  140. congestion = false,
  141. readBufferSize = 2,
  142. writeBufferSize = 2,
  143. type = 'none',
  144. seed = '',
  145. ) {
  146. super();
  147. this.mtu = mtu;
  148. this.tti = tti;
  149. this.upCap = uplinkCapacity;
  150. this.downCap = downlinkCapacity;
  151. this.congestion = congestion;
  152. this.readBuffer = readBufferSize;
  153. this.writeBuffer = writeBufferSize;
  154. this.type = type;
  155. this.seed = seed;
  156. }
  157. static fromJson(json = {}) {
  158. return new KcpStreamSettings(
  159. json.mtu,
  160. json.tti,
  161. json.uplinkCapacity,
  162. json.downlinkCapacity,
  163. json.congestion,
  164. json.readBufferSize,
  165. json.writeBufferSize,
  166. ObjectUtil.isEmpty(json.header) ? 'none' : json.header.type,
  167. json.seed,
  168. );
  169. }
  170. toJson() {
  171. return {
  172. mtu: this.mtu,
  173. tti: this.tti,
  174. uplinkCapacity: this.upCap,
  175. downlinkCapacity: this.downCap,
  176. congestion: this.congestion,
  177. readBufferSize: this.readBuffer,
  178. writeBufferSize: this.writeBuffer,
  179. header: {
  180. type: this.type,
  181. },
  182. seed: this.seed,
  183. };
  184. }
  185. }
  186. class WsStreamSettings extends CommonClass {
  187. constructor(
  188. path = '/',
  189. host = '',
  190. heartbeatPeriod = 0,
  191. ) {
  192. super();
  193. this.path = path;
  194. this.host = host;
  195. this.heartbeatPeriod = heartbeatPeriod;
  196. }
  197. static fromJson(json = {}) {
  198. return new WsStreamSettings(
  199. json.path,
  200. json.host,
  201. json.heartbeatPeriod,
  202. );
  203. }
  204. toJson() {
  205. return {
  206. path: this.path,
  207. host: this.host,
  208. heartbeatPeriod: this.heartbeatPeriod
  209. };
  210. }
  211. }
  212. class GrpcStreamSettings extends CommonClass {
  213. constructor(
  214. serviceName = "",
  215. authority = "",
  216. multiMode = false
  217. ) {
  218. super();
  219. this.serviceName = serviceName;
  220. this.authority = authority;
  221. this.multiMode = multiMode;
  222. }
  223. static fromJson(json = {}) {
  224. return new GrpcStreamSettings(json.serviceName, json.authority, json.multiMode);
  225. }
  226. toJson() {
  227. return {
  228. serviceName: this.serviceName,
  229. authority: this.authority,
  230. multiMode: this.multiMode
  231. }
  232. }
  233. }
  234. class HttpUpgradeStreamSettings extends CommonClass {
  235. constructor(path = '/', host = '') {
  236. super();
  237. this.path = path;
  238. this.host = host;
  239. }
  240. static fromJson(json = {}) {
  241. return new HttpUpgradeStreamSettings(
  242. json.path,
  243. json.host,
  244. );
  245. }
  246. toJson() {
  247. return {
  248. path: this.path,
  249. host: this.host,
  250. };
  251. }
  252. }
  253. class xHTTPStreamSettings extends CommonClass {
  254. constructor(
  255. path = '/',
  256. host = '',
  257. mode = '',
  258. ) {
  259. super();
  260. this.path = path;
  261. this.host = host;
  262. this.mode = mode;
  263. }
  264. static fromJson(json = {}) {
  265. return new xHTTPStreamSettings(
  266. json.path,
  267. json.host,
  268. json.mode,
  269. );
  270. }
  271. toJson() {
  272. return {
  273. path: this.path,
  274. host: this.host,
  275. mode: this.mode,
  276. };
  277. }
  278. }
  279. class TlsStreamSettings extends CommonClass {
  280. constructor(
  281. serverName = '',
  282. alpn = [],
  283. fingerprint = '',
  284. allowInsecure = false
  285. ) {
  286. super();
  287. this.serverName = serverName;
  288. this.alpn = alpn;
  289. this.fingerprint = fingerprint;
  290. this.allowInsecure = allowInsecure;
  291. }
  292. static fromJson(json = {}) {
  293. return new TlsStreamSettings(
  294. json.serverName,
  295. json.alpn,
  296. json.fingerprint,
  297. json.allowInsecure,
  298. );
  299. }
  300. toJson() {
  301. return {
  302. serverName: this.serverName,
  303. alpn: this.alpn,
  304. fingerprint: this.fingerprint,
  305. allowInsecure: this.allowInsecure,
  306. };
  307. }
  308. }
  309. class RealityStreamSettings extends CommonClass {
  310. constructor(
  311. publicKey = '',
  312. fingerprint = '',
  313. serverName = '',
  314. shortId = '',
  315. spiderX = '/'
  316. ) {
  317. super();
  318. this.publicKey = publicKey;
  319. this.fingerprint = fingerprint;
  320. this.serverName = serverName;
  321. this.shortId = shortId
  322. this.spiderX = spiderX;
  323. }
  324. static fromJson(json = {}) {
  325. return new RealityStreamSettings(
  326. json.publicKey,
  327. json.fingerprint,
  328. json.serverName,
  329. json.shortId,
  330. json.spiderX,
  331. );
  332. }
  333. toJson() {
  334. return {
  335. publicKey: this.publicKey,
  336. fingerprint: this.fingerprint,
  337. serverName: this.serverName,
  338. shortId: this.shortId,
  339. spiderX: this.spiderX,
  340. };
  341. }
  342. };
  343. class SockoptStreamSettings extends CommonClass {
  344. constructor(
  345. dialerProxy = "",
  346. tcpFastOpen = false,
  347. tcpKeepAliveInterval = 0,
  348. tcpMptcp = false,
  349. tcpNoDelay = false
  350. ) {
  351. super();
  352. this.dialerProxy = dialerProxy;
  353. this.tcpFastOpen = tcpFastOpen;
  354. this.tcpKeepAliveInterval = tcpKeepAliveInterval;
  355. this.tcpMptcp = tcpMptcp;
  356. this.tcpNoDelay = tcpNoDelay;
  357. }
  358. static fromJson(json = {}) {
  359. if (Object.keys(json).length === 0) return undefined;
  360. return new SockoptStreamSettings(
  361. json.dialerProxy,
  362. json.tcpFastOpen,
  363. json.tcpKeepAliveInterval,
  364. json.tcpMptcp,
  365. json.tcpNoDelay,
  366. );
  367. }
  368. toJson() {
  369. return {
  370. dialerProxy: this.dialerProxy,
  371. tcpFastOpen: this.tcpFastOpen,
  372. tcpKeepAliveInterval: this.tcpKeepAliveInterval,
  373. tcpMptcp: this.tcpMptcp,
  374. tcpNoDelay: this.tcpNoDelay,
  375. };
  376. }
  377. }
  378. class StreamSettings extends CommonClass {
  379. constructor(
  380. network = 'tcp',
  381. security = 'none',
  382. tlsSettings = new TlsStreamSettings(),
  383. realitySettings = new RealityStreamSettings(),
  384. tcpSettings = new TcpStreamSettings(),
  385. kcpSettings = new KcpStreamSettings(),
  386. wsSettings = new WsStreamSettings(),
  387. grpcSettings = new GrpcStreamSettings(),
  388. httpupgradeSettings = new HttpUpgradeStreamSettings(),
  389. xhttpSettings = new xHTTPStreamSettings(),
  390. sockopt = undefined,
  391. ) {
  392. super();
  393. this.network = network;
  394. this.security = security;
  395. this.tls = tlsSettings;
  396. this.reality = realitySettings;
  397. this.tcp = tcpSettings;
  398. this.kcp = kcpSettings;
  399. this.ws = wsSettings;
  400. this.grpc = grpcSettings;
  401. this.httpupgrade = httpupgradeSettings;
  402. this.xhttp = xhttpSettings;
  403. this.sockopt = sockopt;
  404. }
  405. get isTls() {
  406. return this.security === 'tls';
  407. }
  408. get isReality() {
  409. return this.security === "reality";
  410. }
  411. get sockoptSwitch() {
  412. return this.sockopt != undefined;
  413. }
  414. set sockoptSwitch(value) {
  415. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  416. }
  417. static fromJson(json = {}) {
  418. return new StreamSettings(
  419. json.network,
  420. json.security,
  421. TlsStreamSettings.fromJson(json.tlsSettings),
  422. RealityStreamSettings.fromJson(json.realitySettings),
  423. TcpStreamSettings.fromJson(json.tcpSettings),
  424. KcpStreamSettings.fromJson(json.kcpSettings),
  425. WsStreamSettings.fromJson(json.wsSettings),
  426. GrpcStreamSettings.fromJson(json.grpcSettings),
  427. HttpUpgradeStreamSettings.fromJson(json.httpupgradeSettings),
  428. xHTTPStreamSettings.fromJson(json.xhttpSettings),
  429. SockoptStreamSettings.fromJson(json.sockopt),
  430. );
  431. }
  432. toJson() {
  433. const network = this.network;
  434. return {
  435. network: network,
  436. security: this.security,
  437. tlsSettings: this.security == 'tls' ? this.tls.toJson() : undefined,
  438. realitySettings: this.security == 'reality' ? this.reality.toJson() : undefined,
  439. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  440. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  441. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  442. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  443. httpupgradeSettings: network === 'httpupgrade' ? this.httpupgrade.toJson() : undefined,
  444. xhttpSettings: network === 'xhttp' ? this.xhttp.toJson() : undefined,
  445. sockopt: this.sockopt != undefined ? this.sockopt.toJson() : undefined,
  446. };
  447. }
  448. }
  449. class Mux extends CommonClass {
  450. constructor(enabled = false, concurrency = 8, xudpConcurrency = 16, xudpProxyUDP443 = "reject") {
  451. super();
  452. this.enabled = enabled;
  453. this.concurrency = concurrency;
  454. this.xudpConcurrency = xudpConcurrency;
  455. this.xudpProxyUDP443 = xudpProxyUDP443;
  456. }
  457. static fromJson(json = {}) {
  458. if (Object.keys(json).length === 0) return undefined;
  459. return new Mux(
  460. json.enabled,
  461. json.concurrency,
  462. json.xudpConcurrency,
  463. json.xudpProxyUDP443,
  464. );
  465. }
  466. toJson() {
  467. return {
  468. enabled: this.enabled,
  469. concurrency: this.concurrency,
  470. xudpConcurrency: this.xudpConcurrency,
  471. xudpProxyUDP443: this.xudpProxyUDP443,
  472. };
  473. }
  474. }
  475. class Outbound extends CommonClass {
  476. constructor(
  477. tag = '',
  478. protocol = Protocols.VLESS,
  479. settings = null,
  480. streamSettings = new StreamSettings(),
  481. sendThrough,
  482. mux = new Mux(),
  483. ) {
  484. super();
  485. this.tag = tag;
  486. this._protocol = protocol;
  487. this.settings = settings == null ? Outbound.Settings.getSettings(protocol) : settings;
  488. this.stream = streamSettings;
  489. this.sendThrough = sendThrough;
  490. this.mux = mux;
  491. }
  492. get protocol() {
  493. return this._protocol;
  494. }
  495. set protocol(protocol) {
  496. this._protocol = protocol;
  497. this.settings = Outbound.Settings.getSettings(protocol);
  498. this.stream = new StreamSettings();
  499. }
  500. canEnableTls() {
  501. if (![Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks].includes(this.protocol)) return false;
  502. return ["tcp", "ws", "http", "grpc", "httpupgrade", "xhttp"].includes(this.stream.network);
  503. }
  504. //this is used for xtls-rprx-vision
  505. canEnableTlsFlow() {
  506. if ((this.stream.security != 'none') && (this.stream.network === "tcp")) {
  507. return this.protocol === Protocols.VLESS;
  508. }
  509. return false;
  510. }
  511. canEnableReality() {
  512. if (![Protocols.VLESS, Protocols.Trojan].includes(this.protocol)) return false;
  513. return ["tcp", "http", "grpc", "xhttp"].includes(this.stream.network);
  514. }
  515. canEnableStream() {
  516. return [Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks].includes(this.protocol);
  517. }
  518. canEnableMux() {
  519. if (this.settings.flow && this.settings.flow != '') {
  520. this.mux.enabled = false;
  521. return false;
  522. }
  523. return [Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks, Protocols.HTTP, Protocols.Socks].includes(this.protocol);
  524. }
  525. hasVnext() {
  526. return [Protocols.VMess, Protocols.VLESS].includes(this.protocol);
  527. }
  528. hasServers() {
  529. return [Protocols.Trojan, Protocols.Shadowsocks, Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  530. }
  531. hasAddressPort() {
  532. return [
  533. Protocols.DNS,
  534. Protocols.VMess,
  535. Protocols.VLESS,
  536. Protocols.Trojan,
  537. Protocols.Shadowsocks,
  538. Protocols.Socks,
  539. Protocols.HTTP
  540. ].includes(this.protocol);
  541. }
  542. hasUsername() {
  543. return [Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  544. }
  545. static fromJson(json = {}) {
  546. return new Outbound(
  547. json.tag,
  548. json.protocol,
  549. Outbound.Settings.fromJson(json.protocol, json.settings),
  550. StreamSettings.fromJson(json.streamSettings),
  551. json.sendThrough,
  552. Mux.fromJson(json.mux),
  553. )
  554. }
  555. toJson() {
  556. var stream;
  557. if (this.canEnableStream()) {
  558. stream = this.stream.toJson();
  559. } else {
  560. if (this.stream?.sockopt)
  561. stream = { sockopt: this.stream.sockopt.toJson() };
  562. }
  563. return {
  564. tag: this.tag == '' ? undefined : this.tag,
  565. protocol: this.protocol,
  566. settings: this.settings instanceof CommonClass ? this.settings.toJson() : this.settings,
  567. streamSettings: stream,
  568. sendThrough: this.sendThrough != "" ? this.sendThrough : undefined,
  569. mux: this.mux?.enabled ? this.mux : undefined,
  570. };
  571. }
  572. static fromLink(link) {
  573. data = link.split('://');
  574. if (data.length != 2) return null;
  575. switch (data[0].toLowerCase()) {
  576. case Protocols.VMess:
  577. return this.fromVmessLink(JSON.parse(Base64.decode(data[1])));
  578. case Protocols.VLESS:
  579. case Protocols.Trojan:
  580. case 'ss':
  581. return this.fromParamLink(link);
  582. default:
  583. return null;
  584. }
  585. }
  586. static fromVmessLink(json = {}) {
  587. let stream = new StreamSettings(json.net, json.tls);
  588. let network = json.net;
  589. if (network === 'tcp') {
  590. stream.tcp = new TcpStreamSettings(
  591. json.type,
  592. json.host ?? '',
  593. json.path ?? '');
  594. } else if (network === 'kcp') {
  595. stream.kcp = new KcpStreamSettings();
  596. stream.type = json.type;
  597. stream.seed = json.path;
  598. } else if (network === 'ws') {
  599. stream.ws = new WsStreamSettings(json.path, json.host);
  600. } else if (network === 'grpc') {
  601. stream.grpc = new GrpcStreamSettings(json.path, json.authority, json.type == 'multi');
  602. } else if (network === 'httpupgrade') {
  603. stream.httpupgrade = new HttpUpgradeStreamSettings(json.path, json.host);
  604. } else if (network === 'xhttp') {
  605. stream.xhttp = new xHTTPStreamSettings(json.path, json.host, json.mode);
  606. }
  607. if (json.tls && json.tls == 'tls') {
  608. stream.tls = new TlsStreamSettings(
  609. json.sni,
  610. json.alpn ? json.alpn.split(',') : [],
  611. json.fp,
  612. json.allowInsecure);
  613. }
  614. const port = json.port * 1;
  615. return new Outbound(json.ps, Protocols.VMess, new Outbound.VmessSettings(json.add, port, json.id, json.scy), stream);
  616. }
  617. static fromParamLink(link) {
  618. const url = new URL(link);
  619. let type = url.searchParams.get('type') ?? 'tcp';
  620. let security = url.searchParams.get('security') ?? 'none';
  621. let stream = new StreamSettings(type, security);
  622. let headerType = url.searchParams.get('headerType') ?? undefined;
  623. let host = url.searchParams.get('host') ?? undefined;
  624. let path = url.searchParams.get('path') ?? undefined;
  625. if (type === 'tcp' || type === 'none') {
  626. stream.tcp = new TcpStreamSettings(headerType ?? 'none', host, path);
  627. } else if (type === 'kcp') {
  628. stream.kcp = new KcpStreamSettings();
  629. stream.kcp.type = headerType ?? 'none';
  630. stream.kcp.seed = path;
  631. } else if (type === 'ws') {
  632. stream.ws = new WsStreamSettings(path, host);
  633. } else if (type === 'grpc') {
  634. stream.grpc = new GrpcStreamSettings(
  635. url.searchParams.get('serviceName') ?? '',
  636. url.searchParams.get('authority') ?? '',
  637. url.searchParams.get('mode') == 'multi');
  638. } else if (type === 'httpupgrade') {
  639. stream.httpupgrade = new HttpUpgradeStreamSettings(path, host);
  640. } else if (type === 'xhttp') {
  641. stream.xhttp = new xHTTPStreamSettings(path, host, mode);
  642. }
  643. if (security == 'tls') {
  644. let fp = url.searchParams.get('fp') ?? 'none';
  645. let alpn = url.searchParams.get('alpn');
  646. let allowInsecure = url.searchParams.get('allowInsecure');
  647. let sni = url.searchParams.get('sni') ?? '';
  648. stream.tls = new TlsStreamSettings(sni, alpn ? alpn.split(',') : [], fp, allowInsecure == 1);
  649. }
  650. if (security == 'reality') {
  651. let pbk = url.searchParams.get('pbk');
  652. let fp = url.searchParams.get('fp');
  653. let sni = url.searchParams.get('sni') ?? '';
  654. let sid = url.searchParams.get('sid') ?? '';
  655. let spx = url.searchParams.get('spx') ?? '';
  656. stream.reality = new RealityStreamSettings(pbk, fp, sni, sid, spx);
  657. }
  658. const regex = /([^@]+):\/\/([^@]+)@(.+):(\d+)(.*)$/;
  659. const match = link.match(regex);
  660. if (!match) return null;
  661. let [, protocol, userData, address, port,] = match;
  662. port *= 1;
  663. if (protocol == 'ss') {
  664. protocol = 'shadowsocks';
  665. userData = atob(userData).split(':');
  666. }
  667. var settings;
  668. switch (protocol) {
  669. case Protocols.VLESS:
  670. settings = new Outbound.VLESSSettings(address, port, userData, url.searchParams.get('flow') ?? '');
  671. break;
  672. case Protocols.Trojan:
  673. settings = new Outbound.TrojanSettings(address, port, userData);
  674. break;
  675. case Protocols.Shadowsocks:
  676. let method = userData.splice(0, 1)[0];
  677. settings = new Outbound.ShadowsocksSettings(address, port, userData.join(":"), method, true);
  678. break;
  679. default:
  680. return null;
  681. }
  682. let remark = decodeURIComponent(url.hash);
  683. // Remove '#' from url.hash
  684. remark = remark.length > 0 ? remark.substring(1) : 'out-' + protocol + '-' + port;
  685. return new Outbound(remark, protocol, settings, stream);
  686. }
  687. }
  688. Outbound.Settings = class extends CommonClass {
  689. constructor(protocol) {
  690. super();
  691. this.protocol = protocol;
  692. }
  693. static getSettings(protocol) {
  694. switch (protocol) {
  695. case Protocols.Freedom: return new Outbound.FreedomSettings();
  696. case Protocols.Blackhole: return new Outbound.BlackholeSettings();
  697. case Protocols.DNS: return new Outbound.DNSSettings();
  698. case Protocols.VMess: return new Outbound.VmessSettings();
  699. case Protocols.VLESS: return new Outbound.VLESSSettings();
  700. case Protocols.Trojan: return new Outbound.TrojanSettings();
  701. case Protocols.Shadowsocks: return new Outbound.ShadowsocksSettings();
  702. case Protocols.Socks: return new Outbound.SocksSettings();
  703. case Protocols.HTTP: return new Outbound.HttpSettings();
  704. case Protocols.Wireguard: return new Outbound.WireguardSettings();
  705. default: return null;
  706. }
  707. }
  708. static fromJson(protocol, json) {
  709. switch (protocol) {
  710. case Protocols.Freedom: return Outbound.FreedomSettings.fromJson(json);
  711. case Protocols.Blackhole: return Outbound.BlackholeSettings.fromJson(json);
  712. case Protocols.DNS: return Outbound.DNSSettings.fromJson(json);
  713. case Protocols.VMess: return Outbound.VmessSettings.fromJson(json);
  714. case Protocols.VLESS: return Outbound.VLESSSettings.fromJson(json);
  715. case Protocols.Trojan: return Outbound.TrojanSettings.fromJson(json);
  716. case Protocols.Shadowsocks: return Outbound.ShadowsocksSettings.fromJson(json);
  717. case Protocols.Socks: return Outbound.SocksSettings.fromJson(json);
  718. case Protocols.HTTP: return Outbound.HttpSettings.fromJson(json);
  719. case Protocols.Wireguard: return Outbound.WireguardSettings.fromJson(json);
  720. default: return null;
  721. }
  722. }
  723. toJson() {
  724. return {};
  725. }
  726. };
  727. Outbound.FreedomSettings = class extends CommonClass {
  728. constructor(
  729. domainStrategy = '',
  730. redirect = '',
  731. fragment = {},
  732. noises = []
  733. ) {
  734. super();
  735. this.domainStrategy = domainStrategy;
  736. this.redirect = redirect;
  737. this.fragment = fragment;
  738. this.noises = noises;
  739. }
  740. addNoise() {
  741. this.noises.push(new Outbound.FreedomSettings.Noise());
  742. }
  743. delNoise(index) {
  744. this.noises.splice(index, 1);
  745. }
  746. static fromJson(json = {}) {
  747. return new Outbound.FreedomSettings(
  748. json.domainStrategy,
  749. json.redirect,
  750. json.fragment ? Outbound.FreedomSettings.Fragment.fromJson(json.fragment) : undefined,
  751. json.noises ? json.noises.map(noise => Outbound.FreedomSettings.Noise.fromJson(noise)) : undefined,
  752. );
  753. }
  754. toJson() {
  755. return {
  756. domainStrategy: ObjectUtil.isEmpty(this.domainStrategy) ? undefined : this.domainStrategy,
  757. redirect: ObjectUtil.isEmpty(this.redirect) ? undefined: this.redirect,
  758. fragment: Object.keys(this.fragment).length === 0 ? undefined : this.fragment,
  759. noises: this.noises.length === 0 ? undefined : Outbound.FreedomSettings.Noise.toJsonArray(this.noises),
  760. };
  761. }
  762. };
  763. Outbound.FreedomSettings.Fragment = class extends CommonClass {
  764. constructor(
  765. packets = '1-3',
  766. length = '',
  767. interval = ''
  768. ) {
  769. super();
  770. this.packets = packets;
  771. this.length = length;
  772. this.interval = interval;
  773. }
  774. static fromJson(json = {}) {
  775. return new Outbound.FreedomSettings.Fragment(
  776. json.packets,
  777. json.length,
  778. json.interval,
  779. );
  780. }
  781. };
  782. Outbound.FreedomSettings.Noise = class extends CommonClass {
  783. constructor(
  784. type = 'rand',
  785. packet = '10-20',
  786. delay = '10-16'
  787. ) {
  788. super();
  789. this.type = type;
  790. this.packet = packet;
  791. this.delay = delay;
  792. }
  793. static fromJson(json = {}) {
  794. return new Outbound.FreedomSettings.Noise(
  795. json.type,
  796. json.packet,
  797. json.delay,
  798. );
  799. }
  800. toJson() {
  801. return {
  802. type: this.type,
  803. packet: this.packet,
  804. delay: this.delay,
  805. };
  806. }
  807. };
  808. Outbound.BlackholeSettings = class extends CommonClass {
  809. constructor(type) {
  810. super();
  811. this.type = type;
  812. }
  813. static fromJson(json = {}) {
  814. return new Outbound.BlackholeSettings(
  815. json.response ? json.response.type : undefined,
  816. );
  817. }
  818. toJson() {
  819. return {
  820. response: ObjectUtil.isEmpty(this.type) ? undefined : { type: this.type },
  821. };
  822. }
  823. };
  824. Outbound.DNSSettings = class extends CommonClass {
  825. constructor(
  826. network = 'udp',
  827. address = '',
  828. port = 53,
  829. nonIPQuery = 'drop',
  830. blockTypes = []
  831. ) {
  832. super();
  833. this.network = network;
  834. this.address = address;
  835. this.port = port;
  836. this.nonIPQuery = nonIPQuery;
  837. this.blockTypes = blockTypes;
  838. }
  839. static fromJson(json = {}) {
  840. return new Outbound.DNSSettings(
  841. json.network,
  842. json.address,
  843. json.port,
  844. json.nonIPQuery,
  845. json.blockTypes,
  846. );
  847. }
  848. };
  849. Outbound.VmessSettings = class extends CommonClass {
  850. constructor(address, port, id, security) {
  851. super();
  852. this.address = address;
  853. this.port = port;
  854. this.id = id;
  855. this.security = security;
  856. }
  857. static fromJson(json = {}) {
  858. if (ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VmessSettings();
  859. return new Outbound.VmessSettings(
  860. json.vnext[0].address,
  861. json.vnext[0].port,
  862. json.vnext[0].users[0].id,
  863. json.vnext[0].users[0].security,
  864. );
  865. }
  866. toJson() {
  867. return {
  868. vnext: [{
  869. address: this.address,
  870. port: this.port,
  871. users: [{ id: this.id, security: this.security }],
  872. }],
  873. };
  874. }
  875. };
  876. Outbound.VLESSSettings = class extends CommonClass {
  877. constructor(address, port, id, flow, encryption = 'none') {
  878. super();
  879. this.address = address;
  880. this.port = port;
  881. this.id = id;
  882. this.flow = flow;
  883. this.encryption = encryption
  884. }
  885. static fromJson(json = {}) {
  886. if (ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VLESSSettings();
  887. return new Outbound.VLESSSettings(
  888. json.vnext[0].address,
  889. json.vnext[0].port,
  890. json.vnext[0].users[0].id,
  891. json.vnext[0].users[0].flow,
  892. json.vnext[0].users[0].encryption,
  893. );
  894. }
  895. toJson() {
  896. return {
  897. vnext: [{
  898. address: this.address,
  899. port: this.port,
  900. users: [{ id: this.id, flow: this.flow, encryption: 'none', }],
  901. }],
  902. };
  903. }
  904. };
  905. Outbound.TrojanSettings = class extends CommonClass {
  906. constructor(address, port, password) {
  907. super();
  908. this.address = address;
  909. this.port = port;
  910. this.password = password;
  911. }
  912. static fromJson(json = {}) {
  913. if (ObjectUtil.isArrEmpty(json.servers)) return new Outbound.TrojanSettings();
  914. return new Outbound.TrojanSettings(
  915. json.servers[0].address,
  916. json.servers[0].port,
  917. json.servers[0].password,
  918. );
  919. }
  920. toJson() {
  921. return {
  922. servers: [{
  923. address: this.address,
  924. port: this.port,
  925. password: this.password,
  926. }],
  927. };
  928. }
  929. };
  930. Outbound.ShadowsocksSettings = class extends CommonClass {
  931. constructor(address, port, password, method, uot, UoTVersion) {
  932. super();
  933. this.address = address;
  934. this.port = port;
  935. this.password = password;
  936. this.method = method;
  937. this.uot = uot;
  938. this.UoTVersion = UoTVersion;
  939. }
  940. static fromJson(json = {}) {
  941. let servers = json.servers;
  942. if (ObjectUtil.isArrEmpty(servers)) servers = [{}];
  943. return new Outbound.ShadowsocksSettings(
  944. servers[0].address,
  945. servers[0].port,
  946. servers[0].password,
  947. servers[0].method,
  948. servers[0].uot,
  949. servers[0].UoTVersion,
  950. );
  951. }
  952. toJson() {
  953. return {
  954. servers: [{
  955. address: this.address,
  956. port: this.port,
  957. password: this.password,
  958. method: this.method,
  959. uot: this.uot,
  960. UoTVersion: this.UoTVersion,
  961. }],
  962. };
  963. }
  964. };
  965. Outbound.SocksSettings = class extends CommonClass {
  966. constructor(address, port, user, pass) {
  967. super();
  968. this.address = address;
  969. this.port = port;
  970. this.user = user;
  971. this.pass = pass;
  972. }
  973. static fromJson(json = {}) {
  974. let servers = json.servers;
  975. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  976. return new Outbound.SocksSettings(
  977. servers[0].address,
  978. servers[0].port,
  979. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  980. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  981. );
  982. }
  983. toJson() {
  984. return {
  985. servers: [{
  986. address: this.address,
  987. port: this.port,
  988. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  989. }],
  990. };
  991. }
  992. };
  993. Outbound.HttpSettings = class extends CommonClass {
  994. constructor(address, port, user, pass) {
  995. super();
  996. this.address = address;
  997. this.port = port;
  998. this.user = user;
  999. this.pass = pass;
  1000. }
  1001. static fromJson(json = {}) {
  1002. let servers = json.servers;
  1003. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  1004. return new Outbound.HttpSettings(
  1005. servers[0].address,
  1006. servers[0].port,
  1007. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  1008. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  1009. );
  1010. }
  1011. toJson() {
  1012. return {
  1013. servers: [{
  1014. address: this.address,
  1015. port: this.port,
  1016. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1017. }],
  1018. };
  1019. }
  1020. };
  1021. Outbound.WireguardSettings = class extends CommonClass {
  1022. constructor(
  1023. mtu = 1420,
  1024. secretKey = '',
  1025. address = [''],
  1026. workers = 2,
  1027. domainStrategy = '',
  1028. reserved = '',
  1029. peers = [new Outbound.WireguardSettings.Peer()],
  1030. noKernelTun = false,
  1031. ) {
  1032. super();
  1033. this.mtu = mtu;
  1034. this.secretKey = secretKey;
  1035. this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  1036. this.address = Array.isArray(address) ? address.join(',') : address;
  1037. this.workers = workers;
  1038. this.domainStrategy = domainStrategy;
  1039. this.reserved = Array.isArray(reserved) ? reserved.join(',') : reserved;
  1040. this.peers = peers;
  1041. this.noKernelTun = noKernelTun;
  1042. }
  1043. addPeer() {
  1044. this.peers.push(new Outbound.WireguardSettings.Peer());
  1045. }
  1046. delPeer(index) {
  1047. this.peers.splice(index, 1);
  1048. }
  1049. static fromJson(json = {}) {
  1050. return new Outbound.WireguardSettings(
  1051. json.mtu,
  1052. json.secretKey,
  1053. json.address,
  1054. json.workers,
  1055. json.domainStrategy,
  1056. json.reserved,
  1057. json.peers.map(peer => Outbound.WireguardSettings.Peer.fromJson(peer)),
  1058. json.noKernelTun,
  1059. );
  1060. }
  1061. toJson() {
  1062. return {
  1063. mtu: this.mtu ?? undefined,
  1064. secretKey: this.secretKey,
  1065. address: this.address ? this.address.split(",") : [],
  1066. workers: this.workers ?? undefined,
  1067. domainStrategy: WireguardDomainStrategy.includes(this.domainStrategy) ? this.domainStrategy : undefined,
  1068. reserved: this.reserved ? this.reserved.split(",").map(Number) : undefined,
  1069. peers: Outbound.WireguardSettings.Peer.toJsonArray(this.peers),
  1070. noKernelTun: this.noKernelTun,
  1071. };
  1072. }
  1073. };
  1074. Outbound.WireguardSettings.Peer = class extends CommonClass {
  1075. constructor(
  1076. publicKey = '',
  1077. psk = '',
  1078. allowedIPs = ['0.0.0.0/0', '::/0'],
  1079. endpoint = '',
  1080. keepAlive = 0
  1081. ) {
  1082. super();
  1083. this.publicKey = publicKey;
  1084. this.psk = psk;
  1085. this.allowedIPs = allowedIPs;
  1086. this.endpoint = endpoint;
  1087. this.keepAlive = keepAlive;
  1088. }
  1089. static fromJson(json = {}) {
  1090. return new Outbound.WireguardSettings.Peer(
  1091. json.publicKey,
  1092. json.preSharedKey,
  1093. json.allowedIPs,
  1094. json.endpoint,
  1095. json.keepAlive
  1096. );
  1097. }
  1098. toJson() {
  1099. return {
  1100. publicKey: this.publicKey,
  1101. preSharedKey: this.psk.length > 0 ? this.psk : undefined,
  1102. allowedIPs: this.allowedIPs ? this.allowedIPs : undefined,
  1103. endpoint: this.endpoint,
  1104. keepAlive: this.keepAlive ?? undefined,
  1105. };
  1106. }
  1107. };