outbound.js 36 KB

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