outbound.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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.grpc = grpcSettings;
  430. this.httpupgrade = httpupgradeSettings;
  431. this.splithttp = splithttpSettings;
  432. this.sockopt = sockopt;
  433. }
  434. get isTls() {
  435. return this.security === 'tls';
  436. }
  437. get isReality() {
  438. return this.security === "reality";
  439. }
  440. get sockoptSwitch() {
  441. return this.sockopt != undefined;
  442. }
  443. set sockoptSwitch(value) {
  444. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  445. }
  446. static fromJson(json = {}) {
  447. return new StreamSettings(
  448. json.network,
  449. json.security,
  450. TlsStreamSettings.fromJson(json.tlsSettings),
  451. RealityStreamSettings.fromJson(json.realitySettings),
  452. TcpStreamSettings.fromJson(json.tcpSettings),
  453. KcpStreamSettings.fromJson(json.kcpSettings),
  454. WsStreamSettings.fromJson(json.wsSettings),
  455. HttpStreamSettings.fromJson(json.httpSettings),
  456. QuicStreamSettings.fromJson(json.quicSettings),
  457. GrpcStreamSettings.fromJson(json.grpcSettings),
  458. HttpUpgradeStreamSettings.fromJson(json.httpupgradeSettings),
  459. SplitHTTPStreamSettings.fromJson(json.splithttpSettings),
  460. SockoptStreamSettings.fromJson(json.sockopt),
  461. );
  462. }
  463. toJson() {
  464. const network = this.network;
  465. return {
  466. network: network,
  467. security: this.security,
  468. tlsSettings: this.security == 'tls' ? this.tls.toJson() : undefined,
  469. realitySettings: this.security == 'reality' ? this.reality.toJson() : undefined,
  470. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  471. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  472. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  473. httpSettings: network === 'http' ? this.http.toJson() : undefined,
  474. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  475. httpupgradeSettings: network === 'httpupgrade' ? this.httpupgrade.toJson() : undefined,
  476. splithttpSettings: network === 'splithttp' ? this.splithttp.toJson() : undefined,
  477. sockopt: this.sockopt != undefined ? this.sockopt.toJson() : undefined,
  478. };
  479. }
  480. }
  481. class Mux extends CommonClass {
  482. constructor(enabled = false, concurrency = 8, xudpConcurrency = 16, xudpProxyUDP443 = "reject") {
  483. super();
  484. this.enabled = enabled;
  485. this.concurrency = concurrency;
  486. this.xudpConcurrency = xudpConcurrency;
  487. this.xudpProxyUDP443 = xudpProxyUDP443;
  488. }
  489. static fromJson(json = {}) {
  490. if (Object.keys(json).length === 0) return undefined;
  491. return new Mux(
  492. json.enabled,
  493. json.concurrency,
  494. json.xudpConcurrency,
  495. json.xudpProxyUDP443,
  496. );
  497. }
  498. toJson() {
  499. return {
  500. enabled: this.enabled,
  501. concurrency: this.concurrency,
  502. xudpConcurrency: this.xudpConcurrency,
  503. xudpProxyUDP443: this.xudpProxyUDP443,
  504. };
  505. }
  506. }
  507. class Outbound extends CommonClass {
  508. constructor(
  509. tag = '',
  510. protocol = Protocols.VMess,
  511. settings = null,
  512. streamSettings = new StreamSettings(),
  513. sendThrough,
  514. mux = new Mux(),
  515. ) {
  516. super();
  517. this.tag = tag;
  518. this._protocol = protocol;
  519. this.settings = settings == null ? Outbound.Settings.getSettings(protocol) : settings;
  520. this.stream = streamSettings;
  521. this.sendThrough = sendThrough;
  522. this.mux = mux;
  523. }
  524. get protocol() {
  525. return this._protocol;
  526. }
  527. set protocol(protocol) {
  528. this._protocol = protocol;
  529. this.settings = Outbound.Settings.getSettings(protocol);
  530. this.stream = new StreamSettings();
  531. }
  532. canEnableTls() {
  533. if (![Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks].includes(this.protocol)) return false;
  534. return ["tcp", "ws", "http", "grpc", "httpupgrade", "splithttp"].includes(this.stream.network);
  535. }
  536. //this is used for xtls-rprx-vision
  537. canEnableTlsFlow() {
  538. if ((this.stream.security != 'none') && (this.stream.network === "tcp")) {
  539. return this.protocol === Protocols.VLESS;
  540. }
  541. return false;
  542. }
  543. canEnableReality() {
  544. if (![Protocols.VLESS, Protocols.Trojan].includes(this.protocol)) return false;
  545. return ["tcp", "http", "grpc"].includes(this.stream.network);
  546. }
  547. canEnableStream() {
  548. return [Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks].includes(this.protocol);
  549. }
  550. canEnableMux() {
  551. if (this.settings.flow && this.settings.flow != '') {
  552. this.mux.enabled = false;
  553. return false;
  554. }
  555. return [Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks, Protocols.HTTP, Protocols.Socks].includes(this.protocol);
  556. }
  557. hasVnext() {
  558. return [Protocols.VMess, Protocols.VLESS].includes(this.protocol);
  559. }
  560. hasServers() {
  561. return [Protocols.Trojan, Protocols.Shadowsocks, Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  562. }
  563. hasAddressPort() {
  564. return [
  565. Protocols.DNS,
  566. Protocols.VMess,
  567. Protocols.VLESS,
  568. Protocols.Trojan,
  569. Protocols.Shadowsocks,
  570. Protocols.Socks,
  571. Protocols.HTTP
  572. ].includes(this.protocol);
  573. }
  574. hasUsername() {
  575. return [Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  576. }
  577. static fromJson(json = {}) {
  578. return new Outbound(
  579. json.tag,
  580. json.protocol,
  581. Outbound.Settings.fromJson(json.protocol, json.settings),
  582. StreamSettings.fromJson(json.streamSettings),
  583. json.sendThrough,
  584. Mux.fromJson(json.mux),
  585. )
  586. }
  587. toJson() {
  588. var stream;
  589. if (this.canEnableStream()) {
  590. stream = this.stream.toJson();
  591. } else {
  592. if (this.stream?.sockopt)
  593. stream = { sockopt: this.stream.sockopt.toJson() };
  594. }
  595. return {
  596. tag: this.tag == '' ? undefined : this.tag,
  597. protocol: this.protocol,
  598. settings: this.settings instanceof CommonClass ? this.settings.toJson() : this.settings,
  599. streamSettings: stream,
  600. sendThrough: this.sendThrough != "" ? this.sendThrough : undefined,
  601. mux: this.mux?.enabled ? this.mux : undefined,
  602. };
  603. }
  604. static fromLink(link) {
  605. data = link.split('://');
  606. if (data.length != 2) return null;
  607. switch (data[0].toLowerCase()) {
  608. case Protocols.VMess:
  609. return this.fromVmessLink(JSON.parse(Base64.decode(data[1])));
  610. case Protocols.VLESS:
  611. case Protocols.Trojan:
  612. case 'ss':
  613. return this.fromParamLink(link);
  614. default:
  615. return null;
  616. }
  617. }
  618. static fromVmessLink(json = {}) {
  619. let stream = new StreamSettings(json.net, json.tls);
  620. let network = json.net;
  621. if (network === 'tcp') {
  622. stream.tcp = new TcpStreamSettings(
  623. json.type,
  624. json.host ?? '',
  625. json.path ?? '');
  626. } else if (network === 'kcp') {
  627. stream.kcp = new KcpStreamSettings();
  628. stream.type = json.type;
  629. stream.seed = json.path;
  630. } else if (network === 'ws') {
  631. stream.ws = new WsStreamSettings(json.path, json.host);
  632. } else if (network === 'http' || network == 'h2') {
  633. stream.network = 'http'
  634. stream.http = new HttpStreamSettings(
  635. json.path,
  636. json.host);
  637. } else if (network === 'grpc') {
  638. stream.grpc = new GrpcStreamSettings(json.path, json.authority, json.type == 'multi');
  639. } else if (network === 'httpupgrade') {
  640. stream.httpupgrade = new HttpUpgradeStreamSettings(json.path, json.host);
  641. } else if (network === 'splithttp') {
  642. stream.splithttp = new SplitHTTPStreamSettings(json.path, json.host);
  643. }
  644. if (json.tls && json.tls == 'tls') {
  645. stream.tls = new TlsStreamSettings(
  646. json.sni,
  647. json.alpn ? json.alpn.split(',') : [],
  648. json.fp,
  649. json.allowInsecure);
  650. }
  651. const port = json.port * 1;
  652. return new Outbound(json.ps, Protocols.VMess, new Outbound.VmessSettings(json.add, port, json.id, json.scy), stream);
  653. }
  654. static fromParamLink(link) {
  655. const url = new URL(link);
  656. let type = url.searchParams.get('type') ?? 'tcp';
  657. let security = url.searchParams.get('security') ?? 'none';
  658. let stream = new StreamSettings(type, security);
  659. let headerType = url.searchParams.get('headerType') ?? undefined;
  660. let host = url.searchParams.get('host') ?? undefined;
  661. let path = url.searchParams.get('path') ?? undefined;
  662. if (type === 'tcp' || type === 'none') {
  663. stream.tcp = new TcpStreamSettings(headerType ?? 'none', host, path);
  664. } else if (type === 'kcp') {
  665. stream.kcp = new KcpStreamSettings();
  666. stream.kcp.type = headerType ?? 'none';
  667. stream.kcp.seed = path;
  668. } else if (type === 'ws') {
  669. stream.ws = new WsStreamSettings(path, host);
  670. } else if (type === 'http' || type == 'h2') {
  671. stream.http = new HttpStreamSettings(path, host);
  672. } else if (type === 'grpc') {
  673. stream.grpc = new GrpcStreamSettings(
  674. url.searchParams.get('serviceName') ?? '',
  675. url.searchParams.get('authority') ?? '',
  676. url.searchParams.get('mode') == 'multi');
  677. } else if (type === 'httpupgrade') {
  678. stream.httpupgrade = new HttpUpgradeStreamSettings(path, host);
  679. } else if (type === 'splithttp') {
  680. stream.splithttp = new SplitHTTPStreamSettings(path, host);
  681. }
  682. if (security == 'tls') {
  683. let fp = url.searchParams.get('fp') ?? 'none';
  684. let alpn = url.searchParams.get('alpn');
  685. let allowInsecure = url.searchParams.get('allowInsecure');
  686. let sni = url.searchParams.get('sni') ?? '';
  687. stream.tls = new TlsStreamSettings(sni, alpn ? alpn.split(',') : [], fp, allowInsecure == 1);
  688. }
  689. if (security == 'reality') {
  690. let pbk = url.searchParams.get('pbk');
  691. let fp = url.searchParams.get('fp');
  692. let sni = url.searchParams.get('sni') ?? '';
  693. let sid = url.searchParams.get('sid') ?? '';
  694. let spx = url.searchParams.get('spx') ?? '';
  695. stream.reality = new RealityStreamSettings(pbk, fp, sni, sid, spx);
  696. }
  697. const regex = /([^@]+):\/\/([^@]+)@(.+):(\d+)(.*)$/;
  698. const match = link.match(regex);
  699. if (!match) return null;
  700. let [, protocol, userData, address, port,] = match;
  701. port *= 1;
  702. if (protocol == 'ss') {
  703. protocol = 'shadowsocks';
  704. userData = atob(userData).split(':');
  705. }
  706. var settings;
  707. switch (protocol) {
  708. case Protocols.VLESS:
  709. settings = new Outbound.VLESSSettings(address, port, userData, url.searchParams.get('flow') ?? '');
  710. break;
  711. case Protocols.Trojan:
  712. settings = new Outbound.TrojanSettings(address, port, userData);
  713. break;
  714. case Protocols.Shadowsocks:
  715. let method = userData.splice(0, 1)[0];
  716. settings = new Outbound.ShadowsocksSettings(address, port, userData.join(":"), method, true);
  717. break;
  718. default:
  719. return null;
  720. }
  721. let remark = decodeURIComponent(url.hash);
  722. // Remove '#' from url.hash
  723. remark = remark.length > 0 ? remark.substring(1) : 'out-' + protocol + '-' + port;
  724. return new Outbound(remark, protocol, settings, stream);
  725. }
  726. }
  727. Outbound.Settings = class extends CommonClass {
  728. constructor(protocol) {
  729. super();
  730. this.protocol = protocol;
  731. }
  732. static getSettings(protocol) {
  733. switch (protocol) {
  734. case Protocols.Freedom: return new Outbound.FreedomSettings();
  735. case Protocols.Blackhole: return new Outbound.BlackholeSettings();
  736. case Protocols.DNS: return new Outbound.DNSSettings();
  737. case Protocols.VMess: return new Outbound.VmessSettings();
  738. case Protocols.VLESS: return new Outbound.VLESSSettings();
  739. case Protocols.Trojan: return new Outbound.TrojanSettings();
  740. case Protocols.Shadowsocks: return new Outbound.ShadowsocksSettings();
  741. case Protocols.Socks: return new Outbound.SocksSettings();
  742. case Protocols.HTTP: return new Outbound.HttpSettings();
  743. case Protocols.Wireguard: return new Outbound.WireguardSettings();
  744. default: return null;
  745. }
  746. }
  747. static fromJson(protocol, json) {
  748. switch (protocol) {
  749. case Protocols.Freedom: return Outbound.FreedomSettings.fromJson(json);
  750. case Protocols.Blackhole: return Outbound.BlackholeSettings.fromJson(json);
  751. case Protocols.DNS: return Outbound.DNSSettings.fromJson(json);
  752. case Protocols.VMess: return Outbound.VmessSettings.fromJson(json);
  753. case Protocols.VLESS: return Outbound.VLESSSettings.fromJson(json);
  754. case Protocols.Trojan: return Outbound.TrojanSettings.fromJson(json);
  755. case Protocols.Shadowsocks: return Outbound.ShadowsocksSettings.fromJson(json);
  756. case Protocols.Socks: return Outbound.SocksSettings.fromJson(json);
  757. case Protocols.HTTP: return Outbound.HttpSettings.fromJson(json);
  758. case Protocols.Wireguard: return Outbound.WireguardSettings.fromJson(json);
  759. default: return null;
  760. }
  761. }
  762. toJson() {
  763. return {};
  764. }
  765. };
  766. Outbound.FreedomSettings = class extends CommonClass {
  767. constructor(
  768. domainStrategy = '',
  769. timeout = 10,
  770. redirect = '',
  771. fragment = {},
  772. noises = []
  773. ) {
  774. super();
  775. this.domainStrategy = domainStrategy;
  776. this.timeout = timeout;
  777. this.redirect = redirect;
  778. this.fragment = fragment;
  779. this.noises = noises;
  780. }
  781. addNoise() {
  782. this.noises.push(new Outbound.FreedomSettings.Noise());
  783. }
  784. delNoise(index) {
  785. this.noises.splice(index, 1);
  786. }
  787. static fromJson(json = {}) {
  788. return new Outbound.FreedomSettings(
  789. json.domainStrategy,
  790. json.timeout,
  791. json.redirect,
  792. json.fragment ? Outbound.FreedomSettings.Fragment.fromJson(json.fragment) : undefined,
  793. json.noises ? json.noises.map(noise => Outbound.FreedomSettings.Noise.fromJson(noise)) : [new Outbound.FreedomSettings.Noise()],
  794. );
  795. }
  796. toJson() {
  797. return {
  798. domainStrategy: ObjectUtil.isEmpty(this.domainStrategy) ? undefined : this.domainStrategy,
  799. timeout: this.timeout,
  800. redirect: this.redirect,
  801. fragment: Object.keys(this.fragment).length === 0 ? undefined : this.fragment,
  802. noises: Outbound.FreedomSettings.Noise.toJsonArray(this.noises),
  803. };
  804. }
  805. };
  806. Outbound.FreedomSettings.Fragment = class extends CommonClass {
  807. constructor(packets = '1-3', length = '', interval = '') {
  808. super();
  809. this.packets = packets;
  810. this.length = length;
  811. this.interval = interval;
  812. }
  813. static fromJson(json = {}) {
  814. return new Outbound.FreedomSettings.Fragment(
  815. json.packets,
  816. json.length,
  817. json.interval,
  818. );
  819. }
  820. };
  821. Outbound.FreedomSettings.Noise = class extends CommonClass {
  822. constructor(
  823. type = 'rand',
  824. packet = '10-20',
  825. delay = '10-16'
  826. ) {
  827. super();
  828. this.type = type;
  829. this.packet = packet;
  830. this.delay = delay;
  831. }
  832. static fromJson(json = {}) {
  833. return new Outbound.FreedomSettings.Noise(
  834. json.type,
  835. json.packet,
  836. json.delay,
  837. );
  838. }
  839. toJson() {
  840. return {
  841. type: this.type,
  842. packet: this.packet,
  843. delay: this.delay,
  844. };
  845. }
  846. static toJsonArray(noises) {
  847. return noises.map(noise => noise.toJson());
  848. }
  849. };
  850. Outbound.BlackholeSettings = class extends CommonClass {
  851. constructor(type) {
  852. super();
  853. this.type = type;
  854. }
  855. static fromJson(json = {}) {
  856. return new Outbound.BlackholeSettings(
  857. json.response ? json.response.type : undefined,
  858. );
  859. }
  860. toJson() {
  861. return {
  862. response: ObjectUtil.isEmpty(this.type) ? undefined : { type: this.type },
  863. };
  864. }
  865. };
  866. Outbound.DNSSettings = class extends CommonClass {
  867. constructor(
  868. network = 'udp',
  869. address = '1.1.1.1',
  870. port = 53,
  871. nonIPQuery = 'drop',
  872. blockTypes = []
  873. ) {
  874. super();
  875. this.network = network;
  876. this.address = address;
  877. this.port = port;
  878. this.nonIPQuery = nonIPQuery;
  879. this.blockTypes = blockTypes;
  880. }
  881. static fromJson(json = {}) {
  882. return new Outbound.DNSSettings(
  883. json.network,
  884. json.address,
  885. json.port,
  886. json.nonIPQuery,
  887. json.blockTypes,
  888. );
  889. }
  890. };
  891. Outbound.VmessSettings = class extends CommonClass {
  892. constructor(address, port, id, security) {
  893. super();
  894. this.address = address;
  895. this.port = port;
  896. this.id = id;
  897. this.security = security;
  898. }
  899. static fromJson(json = {}) {
  900. if (ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VmessSettings();
  901. return new Outbound.VmessSettings(
  902. json.vnext[0].address,
  903. json.vnext[0].port,
  904. json.vnext[0].users[0].id,
  905. json.vnext[0].users[0].security,
  906. );
  907. }
  908. toJson() {
  909. return {
  910. vnext: [{
  911. address: this.address,
  912. port: this.port,
  913. users: [{ id: this.id, security: this.security }],
  914. }],
  915. };
  916. }
  917. };
  918. Outbound.VLESSSettings = class extends CommonClass {
  919. constructor(address, port, id, flow, encryption = 'none') {
  920. super();
  921. this.address = address;
  922. this.port = port;
  923. this.id = id;
  924. this.flow = flow;
  925. this.encryption = encryption
  926. }
  927. static fromJson(json = {}) {
  928. if (ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VLESSSettings();
  929. return new Outbound.VLESSSettings(
  930. json.vnext[0].address,
  931. json.vnext[0].port,
  932. json.vnext[0].users[0].id,
  933. json.vnext[0].users[0].flow,
  934. json.vnext[0].users[0].encryption,
  935. );
  936. }
  937. toJson() {
  938. return {
  939. vnext: [{
  940. address: this.address,
  941. port: this.port,
  942. users: [{ id: this.id, flow: this.flow, encryption: 'none', }],
  943. }],
  944. };
  945. }
  946. };
  947. Outbound.TrojanSettings = class extends CommonClass {
  948. constructor(address, port, password) {
  949. super();
  950. this.address = address;
  951. this.port = port;
  952. this.password = password;
  953. }
  954. static fromJson(json = {}) {
  955. if (ObjectUtil.isArrEmpty(json.servers)) return new Outbound.TrojanSettings();
  956. return new Outbound.TrojanSettings(
  957. json.servers[0].address,
  958. json.servers[0].port,
  959. json.servers[0].password,
  960. );
  961. }
  962. toJson() {
  963. return {
  964. servers: [{
  965. address: this.address,
  966. port: this.port,
  967. password: this.password,
  968. }],
  969. };
  970. }
  971. };
  972. Outbound.ShadowsocksSettings = class extends CommonClass {
  973. constructor(address, port, password, method, uot, UoTVersion) {
  974. super();
  975. this.address = address;
  976. this.port = port;
  977. this.password = password;
  978. this.method = method;
  979. this.uot = uot;
  980. this.UoTVersion = UoTVersion;
  981. }
  982. static fromJson(json = {}) {
  983. let servers = json.servers;
  984. if (ObjectUtil.isArrEmpty(servers)) servers = [{}];
  985. return new Outbound.ShadowsocksSettings(
  986. servers[0].address,
  987. servers[0].port,
  988. servers[0].password,
  989. servers[0].method,
  990. servers[0].uot,
  991. servers[0].UoTVersion,
  992. );
  993. }
  994. toJson() {
  995. return {
  996. servers: [{
  997. address: this.address,
  998. port: this.port,
  999. password: this.password,
  1000. method: this.method,
  1001. uot: this.uot,
  1002. UoTVersion: this.UoTVersion,
  1003. }],
  1004. };
  1005. }
  1006. };
  1007. Outbound.SocksSettings = class extends CommonClass {
  1008. constructor(address, port, user, pass) {
  1009. super();
  1010. this.address = address;
  1011. this.port = port;
  1012. this.user = user;
  1013. this.pass = pass;
  1014. }
  1015. static fromJson(json = {}) {
  1016. let servers = json.servers;
  1017. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  1018. return new Outbound.SocksSettings(
  1019. servers[0].address,
  1020. servers[0].port,
  1021. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  1022. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  1023. );
  1024. }
  1025. toJson() {
  1026. return {
  1027. servers: [{
  1028. address: this.address,
  1029. port: this.port,
  1030. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1031. }],
  1032. };
  1033. }
  1034. };
  1035. Outbound.HttpSettings = class extends CommonClass {
  1036. constructor(address, port, user, pass) {
  1037. super();
  1038. this.address = address;
  1039. this.port = port;
  1040. this.user = user;
  1041. this.pass = pass;
  1042. }
  1043. static fromJson(json = {}) {
  1044. let servers = json.servers;
  1045. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  1046. return new Outbound.HttpSettings(
  1047. servers[0].address,
  1048. servers[0].port,
  1049. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  1050. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  1051. );
  1052. }
  1053. toJson() {
  1054. return {
  1055. servers: [{
  1056. address: this.address,
  1057. port: this.port,
  1058. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1059. }],
  1060. };
  1061. }
  1062. };
  1063. Outbound.WireguardSettings = class extends CommonClass {
  1064. constructor(
  1065. mtu = 1420,
  1066. secretKey = '',
  1067. address = [''],
  1068. workers = 2,
  1069. domainStrategy = '',
  1070. reserved = '',
  1071. peers = [new Outbound.WireguardSettings.Peer()],
  1072. kernelMode = false
  1073. ) {
  1074. super();
  1075. this.mtu = mtu;
  1076. this.secretKey = secretKey;
  1077. this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  1078. this.address = Array.isArray(address) ? address.join(',') : address;
  1079. this.workers = workers;
  1080. this.domainStrategy = domainStrategy;
  1081. this.reserved = Array.isArray(reserved) ? reserved.join(',') : reserved;
  1082. this.peers = peers;
  1083. this.kernelMode = kernelMode;
  1084. }
  1085. addPeer() {
  1086. this.peers.push(new Outbound.WireguardSettings.Peer());
  1087. }
  1088. delPeer(index) {
  1089. this.peers.splice(index, 1);
  1090. }
  1091. static fromJson(json = {}) {
  1092. return new Outbound.WireguardSettings(
  1093. json.mtu,
  1094. json.secretKey,
  1095. json.address,
  1096. json.workers,
  1097. json.domainStrategy,
  1098. json.reserved,
  1099. json.peers.map(peer => Outbound.WireguardSettings.Peer.fromJson(peer)),
  1100. json.kernelMode,
  1101. );
  1102. }
  1103. toJson() {
  1104. return {
  1105. mtu: this.mtu ?? undefined,
  1106. secretKey: this.secretKey,
  1107. address: this.address ? this.address.split(",") : [],
  1108. workers: this.workers ?? undefined,
  1109. domainStrategy: WireguardDomainStrategy.includes(this.domainStrategy) ? this.domainStrategy : undefined,
  1110. reserved: this.reserved ? this.reserved.split(",").map(Number) : undefined,
  1111. peers: Outbound.WireguardSettings.Peer.toJsonArray(this.peers),
  1112. kernelMode: this.kernelMode,
  1113. };
  1114. }
  1115. };
  1116. Outbound.WireguardSettings.Peer = class extends CommonClass {
  1117. constructor(
  1118. publicKey = '',
  1119. psk = '',
  1120. allowedIPs = ['0.0.0.0/0', '::/0'],
  1121. endpoint = '',
  1122. keepAlive = 0
  1123. ) {
  1124. super();
  1125. this.publicKey = publicKey;
  1126. this.psk = psk;
  1127. this.allowedIPs = allowedIPs;
  1128. this.endpoint = endpoint;
  1129. this.keepAlive = keepAlive;
  1130. }
  1131. static fromJson(json = {}) {
  1132. return new Outbound.WireguardSettings.Peer(
  1133. json.publicKey,
  1134. json.preSharedKey,
  1135. json.allowedIPs,
  1136. json.endpoint,
  1137. json.keepAlive
  1138. );
  1139. }
  1140. toJson() {
  1141. return {
  1142. publicKey: this.publicKey,
  1143. preSharedKey: this.psk.length > 0 ? this.psk : undefined,
  1144. allowedIPs: this.allowedIPs ? this.allowedIPs : undefined,
  1145. endpoint: this.endpoint,
  1146. keepAlive: this.keepAlive ?? undefined,
  1147. };
  1148. }
  1149. };