outbound.js 36 KB

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