outbound.js 36 KB

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