outbound.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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. Object.freeze(Protocols);
  66. Object.freeze(SSMethods);
  67. Object.freeze(TLS_FLOW_CONTROL);
  68. Object.freeze(UTLS_FINGERPRINT);
  69. Object.freeze(ALPN_OPTION);
  70. Object.freeze(OutboundDomainStrategies);
  71. Object.freeze(WireguardDomainStrategy);
  72. class CommonClass {
  73. static toJsonArray(arr) {
  74. return arr.map(obj => obj.toJson());
  75. }
  76. static fromJson() {
  77. return new CommonClass();
  78. }
  79. toJson() {
  80. return this;
  81. }
  82. toString(format = true) {
  83. return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
  84. }
  85. }
  86. class TcpStreamSettings extends CommonClass {
  87. constructor(type = 'none', host, path) {
  88. super();
  89. this.type = type;
  90. this.host = host;
  91. this.path = path;
  92. }
  93. static fromJson(json = {}) {
  94. let header = json.header;
  95. if (!header) return new TcpStreamSettings();
  96. if (header.type == 'http' && header.request) {
  97. return new TcpStreamSettings(
  98. header.type,
  99. header.request.headers.Host.join(','),
  100. header.request.path.join(','),
  101. );
  102. }
  103. return new TcpStreamSettings(header.type, '', '');
  104. }
  105. toJson() {
  106. return {
  107. header: {
  108. type: this.type,
  109. request: this.type === 'http' ? {
  110. headers: {
  111. Host: ObjectUtil.isEmpty(this.host) ? [] : this.host.split(',')
  112. },
  113. path: ObjectUtil.isEmpty(this.path) ? ["/"] : this.path.split(',')
  114. } : undefined,
  115. }
  116. };
  117. }
  118. }
  119. class KcpStreamSettings extends CommonClass {
  120. constructor(
  121. mtu = 1350,
  122. tti = 50,
  123. uplinkCapacity = 5,
  124. downlinkCapacity = 20,
  125. congestion = false,
  126. readBufferSize = 2,
  127. writeBufferSize = 2,
  128. type = 'none',
  129. seed = '',
  130. ) {
  131. super();
  132. this.mtu = mtu;
  133. this.tti = tti;
  134. this.upCap = uplinkCapacity;
  135. this.downCap = downlinkCapacity;
  136. this.congestion = congestion;
  137. this.readBuffer = readBufferSize;
  138. this.writeBuffer = writeBufferSize;
  139. this.type = type;
  140. this.seed = seed;
  141. }
  142. static fromJson(json = {}) {
  143. return new KcpStreamSettings(
  144. json.mtu,
  145. json.tti,
  146. json.uplinkCapacity,
  147. json.downlinkCapacity,
  148. json.congestion,
  149. json.readBufferSize,
  150. json.writeBufferSize,
  151. ObjectUtil.isEmpty(json.header) ? 'none' : json.header.type,
  152. json.seed,
  153. );
  154. }
  155. toJson() {
  156. return {
  157. mtu: this.mtu,
  158. tti: this.tti,
  159. uplinkCapacity: this.upCap,
  160. downlinkCapacity: this.downCap,
  161. congestion: this.congestion,
  162. readBufferSize: this.readBuffer,
  163. writeBufferSize: this.writeBuffer,
  164. header: {
  165. type: this.type,
  166. },
  167. seed: this.seed,
  168. };
  169. }
  170. }
  171. class WsStreamSettings extends CommonClass {
  172. constructor(path = '/', host = '') {
  173. super();
  174. this.path = path;
  175. this.host = host;
  176. }
  177. static fromJson(json = {}) {
  178. return new WsStreamSettings(
  179. json.path,
  180. json.host,
  181. );
  182. }
  183. toJson() {
  184. return {
  185. path: this.path,
  186. host: this.host,
  187. };
  188. }
  189. }
  190. class HttpStreamSettings extends CommonClass {
  191. constructor(path = '/', host = '') {
  192. super();
  193. this.path = path;
  194. this.host = host;
  195. }
  196. static fromJson(json = {}) {
  197. return new HttpStreamSettings(
  198. json.path,
  199. json.host ? json.host.join(',') : '',
  200. );
  201. }
  202. toJson() {
  203. return {
  204. path: this.path,
  205. host: ObjectUtil.isEmpty(this.host) ? [''] : this.host.split(','),
  206. }
  207. }
  208. }
  209. class QuicStreamSettings extends CommonClass {
  210. constructor(
  211. security = 'none',
  212. key = '',
  213. type = 'none'
  214. ) {
  215. super();
  216. this.security = security;
  217. this.key = key;
  218. this.type = type;
  219. }
  220. static fromJson(json = {}) {
  221. return new QuicStreamSettings(
  222. json.security,
  223. json.key,
  224. json.header ? json.header.type : 'none',
  225. );
  226. }
  227. toJson() {
  228. return {
  229. security: this.security,
  230. key: this.key,
  231. header: {
  232. type: this.type,
  233. }
  234. }
  235. }
  236. }
  237. class GrpcStreamSettings extends CommonClass {
  238. constructor(
  239. serviceName = "",
  240. authority = "",
  241. multiMode = false
  242. ) {
  243. super();
  244. this.serviceName = serviceName;
  245. this.authority = authority;
  246. this.multiMode = multiMode;
  247. }
  248. static fromJson(json = {}) {
  249. return new GrpcStreamSettings(json.serviceName, json.authority, json.multiMode);
  250. }
  251. toJson() {
  252. return {
  253. serviceName: this.serviceName,
  254. authority: this.authority,
  255. multiMode: this.multiMode
  256. }
  257. }
  258. }
  259. class HttpUpgradeStreamSettings extends CommonClass {
  260. constructor(path = '/', host = '') {
  261. super();
  262. this.path = path;
  263. this.host = host;
  264. }
  265. static fromJson(json = {}) {
  266. return new HttpUpgradeStreamSettings(
  267. json.path,
  268. json.host,
  269. );
  270. }
  271. toJson() {
  272. return {
  273. path: this.path,
  274. host: this.host,
  275. };
  276. }
  277. }
  278. class SplitHTTPStreamSettings extends CommonClass {
  279. constructor(path = '/', host = '') {
  280. super();
  281. this.path = path;
  282. this.host = host;
  283. }
  284. static fromJson(json = {}) {
  285. return new SplitHTTPStreamSettings(
  286. json.path,
  287. json.host,
  288. );
  289. }
  290. toJson() {
  291. return {
  292. path: this.path,
  293. host: this.host,
  294. };
  295. }
  296. }
  297. class TlsStreamSettings extends CommonClass {
  298. constructor(
  299. serverName = '',
  300. alpn = [],
  301. fingerprint = '',
  302. allowInsecure = false
  303. ) {
  304. super();
  305. this.serverName = serverName;
  306. this.alpn = alpn;
  307. this.fingerprint = fingerprint;
  308. this.allowInsecure = allowInsecure;
  309. }
  310. static fromJson(json = {}) {
  311. return new TlsStreamSettings(
  312. json.serverName,
  313. json.alpn,
  314. json.fingerprint,
  315. json.allowInsecure,
  316. );
  317. }
  318. toJson() {
  319. return {
  320. serverName: this.serverName,
  321. alpn: this.alpn,
  322. fingerprint: this.fingerprint,
  323. allowInsecure: this.allowInsecure,
  324. };
  325. }
  326. }
  327. class RealityStreamSettings extends CommonClass {
  328. constructor(
  329. publicKey = '',
  330. fingerprint = '',
  331. serverName = '',
  332. shortId = '',
  333. spiderX = '/'
  334. ) {
  335. super();
  336. this.publicKey = publicKey;
  337. this.fingerprint = fingerprint;
  338. this.serverName = serverName;
  339. this.shortId = shortId
  340. this.spiderX = spiderX;
  341. }
  342. static fromJson(json = {}) {
  343. return new RealityStreamSettings(
  344. json.publicKey,
  345. json.fingerprint,
  346. json.serverName,
  347. json.shortId,
  348. json.spiderX,
  349. );
  350. }
  351. toJson() {
  352. return {
  353. publicKey: this.publicKey,
  354. fingerprint: this.fingerprint,
  355. serverName: this.serverName,
  356. shortId: this.shortId,
  357. spiderX: this.spiderX,
  358. };
  359. }
  360. };
  361. class SockoptStreamSettings extends CommonClass {
  362. constructor(
  363. dialerProxy = "",
  364. tcpFastOpen = false,
  365. tcpKeepAliveInterval = 0,
  366. tcpMptcp = false,
  367. tcpNoDelay = false
  368. ) {
  369. super();
  370. this.dialerProxy = dialerProxy;
  371. this.tcpFastOpen = tcpFastOpen;
  372. this.tcpKeepAliveInterval = tcpKeepAliveInterval;
  373. this.tcpMptcp = tcpMptcp;
  374. this.tcpNoDelay = tcpNoDelay;
  375. }
  376. static fromJson(json = {}) {
  377. if (Object.keys(json).length === 0) return undefined;
  378. return new SockoptStreamSettings(
  379. json.dialerProxy,
  380. json.tcpFastOpen,
  381. json.tcpKeepAliveInterval,
  382. json.tcpMptcp,
  383. json.tcpNoDelay,
  384. );
  385. }
  386. toJson() {
  387. return {
  388. dialerProxy: this.dialerProxy,
  389. tcpFastOpen: this.tcpFastOpen,
  390. tcpKeepAliveInterval: this.tcpKeepAliveInterval,
  391. tcpMptcp: this.tcpMptcp,
  392. tcpNoDelay: this.tcpNoDelay,
  393. };
  394. }
  395. }
  396. class StreamSettings extends CommonClass {
  397. constructor(
  398. network = 'tcp',
  399. security = 'none',
  400. tlsSettings = new TlsStreamSettings(),
  401. realitySettings = new RealityStreamSettings(),
  402. tcpSettings = new TcpStreamSettings(),
  403. kcpSettings = new KcpStreamSettings(),
  404. wsSettings = new WsStreamSettings(),
  405. httpSettings = new HttpStreamSettings(),
  406. quicSettings = new QuicStreamSettings(),
  407. grpcSettings = new GrpcStreamSettings(),
  408. httpupgradeSettings = new HttpUpgradeStreamSettings(),
  409. splithttpSettings = new SplitHTTPStreamSettings(),
  410. sockopt = undefined,
  411. ) {
  412. super();
  413. this.network = network;
  414. this.security = security;
  415. this.tls = tlsSettings;
  416. this.reality = realitySettings;
  417. this.tcp = tcpSettings;
  418. this.kcp = kcpSettings;
  419. this.ws = wsSettings;
  420. this.http = httpSettings;
  421. this.quic = quicSettings;
  422. this.grpc = grpcSettings;
  423. this.httpupgrade = httpupgradeSettings;
  424. this.splithttp = splithttpSettings;
  425. this.sockopt = sockopt;
  426. }
  427. get isTls() {
  428. return this.security === 'tls';
  429. }
  430. get isReality() {
  431. return this.security === "reality";
  432. }
  433. get sockoptSwitch() {
  434. return this.sockopt != undefined;
  435. }
  436. set sockoptSwitch(value) {
  437. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  438. }
  439. static fromJson(json = {}) {
  440. return new StreamSettings(
  441. json.network,
  442. json.security,
  443. TlsStreamSettings.fromJson(json.tlsSettings),
  444. RealityStreamSettings.fromJson(json.realitySettings),
  445. TcpStreamSettings.fromJson(json.tcpSettings),
  446. KcpStreamSettings.fromJson(json.kcpSettings),
  447. WsStreamSettings.fromJson(json.wsSettings),
  448. HttpStreamSettings.fromJson(json.httpSettings),
  449. QuicStreamSettings.fromJson(json.quicSettings),
  450. GrpcStreamSettings.fromJson(json.grpcSettings),
  451. HttpUpgradeStreamSettings.fromJson(json.httpupgradeSettings),
  452. SplitHTTPStreamSettings.fromJson(json.splithttpSettings),
  453. SockoptStreamSettings.fromJson(json.sockopt),
  454. );
  455. }
  456. toJson() {
  457. const network = this.network;
  458. return {
  459. network: network,
  460. security: this.security,
  461. tlsSettings: this.security == 'tls' ? this.tls.toJson() : undefined,
  462. realitySettings: this.security == 'reality' ? this.reality.toJson() : undefined,
  463. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  464. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  465. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  466. httpSettings: network === 'http' ? this.http.toJson() : undefined,
  467. quicSettings: network === 'quic' ? this.quic.toJson() : undefined,
  468. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  469. httpupgradeSettings: network === 'httpupgrade' ? this.httpupgrade.toJson() : undefined,
  470. splithttpSettings: network === 'splithttp' ? this.splithttp.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.VMess,
  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", "quic", "grpc", "httpupgrade", "splithttp"].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"].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 === 'http' || network == 'h2') {
  627. stream.network = 'http'
  628. stream.http = new HttpStreamSettings(
  629. json.path,
  630. json.host);
  631. } else if (network === 'quic') {
  632. stream.quic = new QuicStreamSettings(
  633. json.host ? json.host : 'none',
  634. json.path,
  635. json.type ? json.type : 'none');
  636. } else if (network === 'grpc') {
  637. stream.grpc = new GrpcStreamSettings(json.path, json.authority, json.type == 'multi');
  638. } else if (network === 'httpupgrade') {
  639. stream.httpupgrade = new HttpUpgradeStreamSettings(json.path, json.host);
  640. } else if (network === 'splithttp') {
  641. stream.splithttp = new SplitHTTPStreamSettings(json.path, json.host);
  642. }
  643. if (json.tls && json.tls == 'tls') {
  644. stream.tls = new TlsStreamSettings(
  645. json.sni,
  646. json.alpn ? json.alpn.split(',') : [],
  647. json.fp,
  648. json.allowInsecure);
  649. }
  650. const port = json.port * 1;
  651. return new Outbound(json.ps, Protocols.VMess, new Outbound.VmessSettings(json.add, port, json.id), stream);
  652. }
  653. static fromParamLink(link) {
  654. const url = new URL(link);
  655. let type = url.searchParams.get('type') ?? 'tcp';
  656. let security = url.searchParams.get('security') ?? 'none';
  657. let stream = new StreamSettings(type, security);
  658. let headerType = url.searchParams.get('headerType') ?? undefined;
  659. let host = url.searchParams.get('host') ?? undefined;
  660. let path = url.searchParams.get('path') ?? undefined;
  661. if (type === 'tcp' || type === 'none') {
  662. stream.tcp = new TcpStreamSettings(headerType ?? 'none', host, path);
  663. } else if (type === 'kcp') {
  664. stream.kcp = new KcpStreamSettings();
  665. stream.kcp.type = headerType ?? 'none';
  666. stream.kcp.seed = path;
  667. } else if (type === 'ws') {
  668. stream.ws = new WsStreamSettings(path, host);
  669. } else if (type === 'http' || type == 'h2') {
  670. stream.http = new HttpStreamSettings(path, host);
  671. } else if (type === 'quic') {
  672. stream.quic = new QuicStreamSettings(
  673. url.searchParams.get('quicSecurity') ?? 'none',
  674. url.searchParams.get('key') ?? '',
  675. headerType ?? 'none');
  676. } else if (type === 'grpc') {
  677. stream.grpc = new GrpcStreamSettings(
  678. url.searchParams.get('serviceName') ?? '',
  679. url.searchParams.get('authority') ?? '',
  680. url.searchParams.get('mode') == 'multi');
  681. } else if (type === 'httpupgrade') {
  682. stream.httpupgrade = new HttpUpgradeStreamSettings(path, host);
  683. } else if (type === 'splithttp') {
  684. stream.splithttp = new SplitHTTPStreamSettings(path, host);
  685. }
  686. if (security == 'tls') {
  687. let fp = url.searchParams.get('fp') ?? 'none';
  688. let alpn = url.searchParams.get('alpn');
  689. let allowInsecure = url.searchParams.get('allowInsecure');
  690. let sni = url.searchParams.get('sni') ?? '';
  691. stream.tls = new TlsStreamSettings(sni, alpn ? alpn.split(',') : [], fp, allowInsecure == 1);
  692. }
  693. if (security == 'reality') {
  694. let pbk = url.searchParams.get('pbk');
  695. let fp = url.searchParams.get('fp');
  696. let sni = url.searchParams.get('sni') ?? '';
  697. let sid = url.searchParams.get('sid') ?? '';
  698. let spx = url.searchParams.get('spx') ?? '';
  699. stream.reality = new RealityStreamSettings(pbk, fp, sni, sid, spx);
  700. }
  701. const regex = /([^@]+):\/\/([^@]+)@(.+):(\d+)(.*)$/;
  702. const match = link.match(regex);
  703. if (!match) return null;
  704. let [, protocol, userData, address, port,] = match;
  705. port *= 1;
  706. if (protocol == 'ss') {
  707. protocol = 'shadowsocks';
  708. userData = atob(userData).split(':');
  709. }
  710. var settings;
  711. switch (protocol) {
  712. case Protocols.VLESS:
  713. settings = new Outbound.VLESSSettings(address, port, userData, url.searchParams.get('flow') ?? '');
  714. break;
  715. case Protocols.Trojan:
  716. settings = new Outbound.TrojanSettings(address, port, userData);
  717. break;
  718. case Protocols.Shadowsocks:
  719. let method = userData.splice(0, 1)[0];
  720. settings = new Outbound.ShadowsocksSettings(address, port, userData.join(":"), method, true);
  721. break;
  722. default:
  723. return null;
  724. }
  725. let remark = decodeURIComponent(url.hash);
  726. // Remove '#' from url.hash
  727. remark = remark.length > 0 ? remark.substring(1) : 'out-' + protocol + '-' + port;
  728. return new Outbound(remark, protocol, settings, stream);
  729. }
  730. }
  731. Outbound.Settings = class extends CommonClass {
  732. constructor(protocol) {
  733. super();
  734. this.protocol = protocol;
  735. }
  736. static getSettings(protocol) {
  737. switch (protocol) {
  738. case Protocols.Freedom: return new Outbound.FreedomSettings();
  739. case Protocols.Blackhole: return new Outbound.BlackholeSettings();
  740. case Protocols.DNS: return new Outbound.DNSSettings();
  741. case Protocols.VMess: return new Outbound.VmessSettings();
  742. case Protocols.VLESS: return new Outbound.VLESSSettings();
  743. case Protocols.Trojan: return new Outbound.TrojanSettings();
  744. case Protocols.Shadowsocks: return new Outbound.ShadowsocksSettings();
  745. case Protocols.Socks: return new Outbound.SocksSettings();
  746. case Protocols.HTTP: return new Outbound.HttpSettings();
  747. case Protocols.Wireguard: return new Outbound.WireguardSettings();
  748. default: return null;
  749. }
  750. }
  751. static fromJson(protocol, json) {
  752. switch (protocol) {
  753. case Protocols.Freedom: return Outbound.FreedomSettings.fromJson(json);
  754. case Protocols.Blackhole: return Outbound.BlackholeSettings.fromJson(json);
  755. case Protocols.DNS: return Outbound.DNSSettings.fromJson(json);
  756. case Protocols.VMess: return Outbound.VmessSettings.fromJson(json);
  757. case Protocols.VLESS: return Outbound.VLESSSettings.fromJson(json);
  758. case Protocols.Trojan: return Outbound.TrojanSettings.fromJson(json);
  759. case Protocols.Shadowsocks: return Outbound.ShadowsocksSettings.fromJson(json);
  760. case Protocols.Socks: return Outbound.SocksSettings.fromJson(json);
  761. case Protocols.HTTP: return Outbound.HttpSettings.fromJson(json);
  762. case Protocols.Wireguard: return Outbound.WireguardSettings.fromJson(json);
  763. default: return null;
  764. }
  765. }
  766. toJson() {
  767. return {};
  768. }
  769. };
  770. Outbound.FreedomSettings = class extends CommonClass {
  771. constructor(domainStrategy = '', fragment = {}) {
  772. super();
  773. this.domainStrategy = domainStrategy;
  774. this.fragment = fragment;
  775. }
  776. static fromJson(json = {}) {
  777. return new Outbound.FreedomSettings(
  778. json.domainStrategy,
  779. json.fragment ? Outbound.FreedomSettings.Fragment.fromJson(json.fragment) : undefined,
  780. );
  781. }
  782. toJson() {
  783. return {
  784. domainStrategy: ObjectUtil.isEmpty(this.domainStrategy) ? undefined : this.domainStrategy,
  785. fragment: Object.keys(this.fragment).length === 0 ? undefined : this.fragment,
  786. };
  787. }
  788. };
  789. Outbound.FreedomSettings.Fragment = class extends CommonClass {
  790. constructor(packets = '1-3', length = '', interval = '') {
  791. super();
  792. this.packets = packets;
  793. this.length = length;
  794. this.interval = interval;
  795. }
  796. static fromJson(json = {}) {
  797. return new Outbound.FreedomSettings.Fragment(
  798. json.packets,
  799. json.length,
  800. json.interval,
  801. );
  802. }
  803. };
  804. Outbound.BlackholeSettings = class extends CommonClass {
  805. constructor(type) {
  806. super();
  807. this.type = type;
  808. }
  809. static fromJson(json = {}) {
  810. return new Outbound.BlackholeSettings(
  811. json.response ? json.response.type : undefined,
  812. );
  813. }
  814. toJson() {
  815. return {
  816. response: ObjectUtil.isEmpty(this.type) ? undefined : { type: this.type },
  817. };
  818. }
  819. };
  820. Outbound.DNSSettings = class extends CommonClass {
  821. constructor(network = 'udp', address = '1.1.1.1', port = 53) {
  822. super();
  823. this.network = network;
  824. this.address = address;
  825. this.port = port;
  826. }
  827. static fromJson(json = {}) {
  828. return new Outbound.DNSSettings(
  829. json.network,
  830. json.address,
  831. json.port,
  832. );
  833. }
  834. };
  835. Outbound.VmessSettings = class extends CommonClass {
  836. constructor(address, port, id) {
  837. super();
  838. this.address = address;
  839. this.port = port;
  840. this.id = id;
  841. }
  842. static fromJson(json = {}) {
  843. if (ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VmessSettings();
  844. return new Outbound.VmessSettings(
  845. json.vnext[0].address,
  846. json.vnext[0].port,
  847. json.vnext[0].users[0].id,
  848. );
  849. }
  850. toJson() {
  851. return {
  852. vnext: [{
  853. address: this.address,
  854. port: this.port,
  855. users: [{ id: this.id }],
  856. }],
  857. };
  858. }
  859. };
  860. Outbound.VLESSSettings = class extends CommonClass {
  861. constructor(address, port, id, flow, encryption = 'none') {
  862. super();
  863. this.address = address;
  864. this.port = port;
  865. this.id = id;
  866. this.flow = flow;
  867. this.encryption = encryption
  868. }
  869. static fromJson(json = {}) {
  870. if (ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VLESSSettings();
  871. return new Outbound.VLESSSettings(
  872. json.vnext[0].address,
  873. json.vnext[0].port,
  874. json.vnext[0].users[0].id,
  875. json.vnext[0].users[0].flow,
  876. json.vnext[0].users[0].encryption,
  877. );
  878. }
  879. toJson() {
  880. return {
  881. vnext: [{
  882. address: this.address,
  883. port: this.port,
  884. users: [{ id: this.id, flow: this.flow, encryption: 'none', }],
  885. }],
  886. };
  887. }
  888. };
  889. Outbound.TrojanSettings = class extends CommonClass {
  890. constructor(address, port, password) {
  891. super();
  892. this.address = address;
  893. this.port = port;
  894. this.password = password;
  895. }
  896. static fromJson(json = {}) {
  897. if (ObjectUtil.isArrEmpty(json.servers)) return new Outbound.TrojanSettings();
  898. return new Outbound.TrojanSettings(
  899. json.servers[0].address,
  900. json.servers[0].port,
  901. json.servers[0].password,
  902. );
  903. }
  904. toJson() {
  905. return {
  906. servers: [{
  907. address: this.address,
  908. port: this.port,
  909. password: this.password,
  910. }],
  911. };
  912. }
  913. };
  914. Outbound.ShadowsocksSettings = class extends CommonClass {
  915. constructor(address, port, password, method, uot, UoTVersion) {
  916. super();
  917. this.address = address;
  918. this.port = port;
  919. this.password = password;
  920. this.method = method;
  921. this.uot = uot;
  922. this.UoTVersion = UoTVersion;
  923. }
  924. static fromJson(json = {}) {
  925. let servers = json.servers;
  926. if (ObjectUtil.isArrEmpty(servers)) servers = [{}];
  927. return new Outbound.ShadowsocksSettings(
  928. servers[0].address,
  929. servers[0].port,
  930. servers[0].password,
  931. servers[0].method,
  932. servers[0].uot,
  933. servers[0].UoTVersion,
  934. );
  935. }
  936. toJson() {
  937. return {
  938. servers: [{
  939. address: this.address,
  940. port: this.port,
  941. password: this.password,
  942. method: this.method,
  943. uot: this.uot,
  944. UoTVersion: this.UoTVersion,
  945. }],
  946. };
  947. }
  948. };
  949. Outbound.SocksSettings = class extends CommonClass {
  950. constructor(address, port, user, pass) {
  951. super();
  952. this.address = address;
  953. this.port = port;
  954. this.user = user;
  955. this.pass = pass;
  956. }
  957. static fromJson(json = {}) {
  958. let servers = json.servers;
  959. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  960. return new Outbound.SocksSettings(
  961. servers[0].address,
  962. servers[0].port,
  963. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  964. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  965. );
  966. }
  967. toJson() {
  968. return {
  969. servers: [{
  970. address: this.address,
  971. port: this.port,
  972. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  973. }],
  974. };
  975. }
  976. };
  977. Outbound.HttpSettings = class extends CommonClass {
  978. constructor(address, port, user, pass) {
  979. super();
  980. this.address = address;
  981. this.port = port;
  982. this.user = user;
  983. this.pass = pass;
  984. }
  985. static fromJson(json = {}) {
  986. let servers = json.servers;
  987. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  988. return new Outbound.HttpSettings(
  989. servers[0].address,
  990. servers[0].port,
  991. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  992. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  993. );
  994. }
  995. toJson() {
  996. return {
  997. servers: [{
  998. address: this.address,
  999. port: this.port,
  1000. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1001. }],
  1002. };
  1003. }
  1004. };
  1005. Outbound.WireguardSettings = class extends CommonClass {
  1006. constructor(
  1007. mtu = 1420,
  1008. secretKey = '',
  1009. address = [''],
  1010. workers = 2,
  1011. domainStrategy = '',
  1012. reserved = '',
  1013. peers = [new Outbound.WireguardSettings.Peer()],
  1014. kernelMode = false
  1015. ) {
  1016. super();
  1017. this.mtu = mtu;
  1018. this.secretKey = secretKey;
  1019. this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  1020. this.address = Array.isArray(address) ? address.join(',') : address;
  1021. this.workers = workers;
  1022. this.domainStrategy = domainStrategy;
  1023. this.reserved = Array.isArray(reserved) ? reserved.join(',') : reserved;
  1024. this.peers = peers;
  1025. this.kernelMode = kernelMode;
  1026. }
  1027. addPeer() {
  1028. this.peers.push(new Outbound.WireguardSettings.Peer());
  1029. }
  1030. delPeer(index) {
  1031. this.peers.splice(index, 1);
  1032. }
  1033. static fromJson(json = {}) {
  1034. return new Outbound.WireguardSettings(
  1035. json.mtu,
  1036. json.secretKey,
  1037. json.address,
  1038. json.workers,
  1039. json.domainStrategy,
  1040. json.reserved,
  1041. json.peers.map(peer => Outbound.WireguardSettings.Peer.fromJson(peer)),
  1042. json.kernelMode,
  1043. );
  1044. }
  1045. toJson() {
  1046. return {
  1047. mtu: this.mtu ?? undefined,
  1048. secretKey: this.secretKey,
  1049. address: this.address ? this.address.split(",") : [],
  1050. workers: this.workers ?? undefined,
  1051. domainStrategy: WireguardDomainStrategy.includes(this.domainStrategy) ? this.domainStrategy : undefined,
  1052. reserved: this.reserved ? this.reserved.split(",").map(Number) : undefined,
  1053. peers: Outbound.WireguardSettings.Peer.toJsonArray(this.peers),
  1054. kernelMode: this.kernelMode,
  1055. };
  1056. }
  1057. };
  1058. Outbound.WireguardSettings.Peer = class extends CommonClass {
  1059. constructor(
  1060. publicKey = '',
  1061. psk = '',
  1062. allowedIPs = ['0.0.0.0/0', '::/0'],
  1063. endpoint = '',
  1064. keepAlive = 0
  1065. ) {
  1066. super();
  1067. this.publicKey = publicKey;
  1068. this.psk = psk;
  1069. this.allowedIPs = allowedIPs;
  1070. this.endpoint = endpoint;
  1071. this.keepAlive = keepAlive;
  1072. }
  1073. static fromJson(json = {}) {
  1074. return new Outbound.WireguardSettings.Peer(
  1075. json.publicKey,
  1076. json.preSharedKey,
  1077. json.allowedIPs,
  1078. json.endpoint,
  1079. json.keepAlive
  1080. );
  1081. }
  1082. toJson() {
  1083. return {
  1084. publicKey: this.publicKey,
  1085. preSharedKey: this.psk.length > 0 ? this.psk : undefined,
  1086. allowedIPs: this.allowedIPs ? this.allowedIPs : undefined,
  1087. endpoint: this.endpoint,
  1088. keepAlive: this.keepAlive ?? undefined,
  1089. };
  1090. }
  1091. };