outbound.js 36 KB

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