outbound.js 36 KB

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