outbound.js 37 KB

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