outbound.js 37 KB

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