outbound.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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. Hysteria: "hysteria"
  13. };
  14. const SSMethods = {
  15. AES_256_GCM: 'aes-256-gcm',
  16. AES_128_GCM: 'aes-128-gcm',
  17. CHACHA20_POLY1305: 'chacha20-poly1305',
  18. CHACHA20_IETF_POLY1305: 'chacha20-ietf-poly1305',
  19. XCHACHA20_POLY1305: 'xchacha20-poly1305',
  20. XCHACHA20_IETF_POLY1305: 'xchacha20-ietf-poly1305',
  21. BLAKE3_AES_128_GCM: '2022-blake3-aes-128-gcm',
  22. BLAKE3_AES_256_GCM: '2022-blake3-aes-256-gcm',
  23. BLAKE3_CHACHA20_POLY1305: '2022-blake3-chacha20-poly1305',
  24. };
  25. const TLS_FLOW_CONTROL = {
  26. VISION: "xtls-rprx-vision",
  27. VISION_UDP443: "xtls-rprx-vision-udp443",
  28. };
  29. const UTLS_FINGERPRINT = {
  30. UTLS_CHROME: "chrome",
  31. UTLS_FIREFOX: "firefox",
  32. UTLS_SAFARI: "safari",
  33. UTLS_IOS: "ios",
  34. UTLS_android: "android",
  35. UTLS_EDGE: "edge",
  36. UTLS_360: "360",
  37. UTLS_QQ: "qq",
  38. UTLS_RANDOM: "random",
  39. UTLS_RANDOMIZED: "randomized",
  40. UTLS_RONDOMIZEDNOALPN: "randomizednoalpn",
  41. UTLS_UNSAFE: "unsafe",
  42. };
  43. const ALPN_OPTION = {
  44. H3: "h3",
  45. H2: "h2",
  46. HTTP1: "http/1.1",
  47. };
  48. const OutboundDomainStrategies = [
  49. "AsIs",
  50. "UseIP",
  51. "UseIPv4",
  52. "UseIPv6",
  53. "UseIPv6v4",
  54. "UseIPv4v6",
  55. "ForceIP",
  56. "ForceIPv6v4",
  57. "ForceIPv6",
  58. "ForceIPv4v6",
  59. "ForceIPv4"
  60. ];
  61. const WireguardDomainStrategy = [
  62. "ForceIP",
  63. "ForceIPv4",
  64. "ForceIPv4v6",
  65. "ForceIPv6",
  66. "ForceIPv6v4"
  67. ];
  68. const USERS_SECURITY = {
  69. AES_128_GCM: "aes-128-gcm",
  70. CHACHA20_POLY1305: "chacha20-poly1305",
  71. AUTO: "auto",
  72. NONE: "none",
  73. ZERO: "zero",
  74. };
  75. const MODE_OPTION = {
  76. AUTO: "auto",
  77. PACKET_UP: "packet-up",
  78. STREAM_UP: "stream-up",
  79. STREAM_ONE: "stream-one",
  80. };
  81. const Address_Port_Strategy = {
  82. NONE: "none",
  83. SrvPortOnly: "srvportonly",
  84. SrvAddressOnly: "srvaddressonly",
  85. SrvPortAndAddress: "srvportandaddress",
  86. TxtPortOnly: "txtportonly",
  87. TxtAddressOnly: "txtaddressonly",
  88. TxtPortAndAddress: "txtportandaddress"
  89. };
  90. Object.freeze(Protocols);
  91. Object.freeze(SSMethods);
  92. Object.freeze(TLS_FLOW_CONTROL);
  93. Object.freeze(UTLS_FINGERPRINT);
  94. Object.freeze(ALPN_OPTION);
  95. Object.freeze(OutboundDomainStrategies);
  96. Object.freeze(WireguardDomainStrategy);
  97. Object.freeze(USERS_SECURITY);
  98. Object.freeze(MODE_OPTION);
  99. Object.freeze(Address_Port_Strategy);
  100. class CommonClass {
  101. static toJsonArray(arr) {
  102. return arr.map(obj => obj.toJson());
  103. }
  104. static fromJson() {
  105. return new CommonClass();
  106. }
  107. toJson() {
  108. return this;
  109. }
  110. toString(format = true) {
  111. return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
  112. }
  113. }
  114. class TcpStreamSettings extends CommonClass {
  115. constructor(type = 'none', host, path) {
  116. super();
  117. this.type = type;
  118. this.host = host;
  119. this.path = path;
  120. }
  121. static fromJson(json = {}) {
  122. let header = json.header;
  123. if (!header) return new TcpStreamSettings();
  124. if (header.type == 'http' && header.request) {
  125. return new TcpStreamSettings(
  126. header.type,
  127. header.request.headers.Host.join(','),
  128. header.request.path.join(','),
  129. );
  130. }
  131. return new TcpStreamSettings(header.type, '', '');
  132. }
  133. toJson() {
  134. return {
  135. header: {
  136. type: this.type,
  137. request: this.type === 'http' ? {
  138. headers: {
  139. Host: ObjectUtil.isEmpty(this.host) ? [] : this.host.split(',')
  140. },
  141. path: ObjectUtil.isEmpty(this.path) ? ["/"] : this.path.split(',')
  142. } : undefined,
  143. }
  144. };
  145. }
  146. }
  147. class KcpStreamSettings extends CommonClass {
  148. constructor(
  149. mtu = 1250,
  150. tti = 50,
  151. uplinkCapacity = 5,
  152. downlinkCapacity = 20,
  153. congestion = false,
  154. readBufferSize = 2,
  155. writeBufferSize = 2,
  156. type = 'none',
  157. seed = '',
  158. ) {
  159. super();
  160. this.mtu = mtu;
  161. this.tti = tti;
  162. this.upCap = uplinkCapacity;
  163. this.downCap = downlinkCapacity;
  164. this.congestion = congestion;
  165. this.readBuffer = readBufferSize;
  166. this.writeBuffer = writeBufferSize;
  167. this.type = type;
  168. this.seed = seed;
  169. }
  170. static fromJson(json = {}) {
  171. return new KcpStreamSettings(
  172. json.mtu,
  173. json.tti,
  174. json.uplinkCapacity,
  175. json.downlinkCapacity,
  176. json.congestion,
  177. json.readBufferSize,
  178. json.writeBufferSize,
  179. ObjectUtil.isEmpty(json.header) ? 'none' : json.header.type,
  180. json.seed,
  181. );
  182. }
  183. toJson() {
  184. return {
  185. mtu: this.mtu,
  186. tti: this.tti,
  187. uplinkCapacity: this.upCap,
  188. downlinkCapacity: this.downCap,
  189. congestion: this.congestion,
  190. readBufferSize: this.readBuffer,
  191. writeBufferSize: this.writeBuffer,
  192. header: {
  193. type: this.type,
  194. },
  195. seed: this.seed,
  196. };
  197. }
  198. }
  199. class WsStreamSettings extends CommonClass {
  200. constructor(
  201. path = '/',
  202. host = '',
  203. heartbeatPeriod = 0,
  204. ) {
  205. super();
  206. this.path = path;
  207. this.host = host;
  208. this.heartbeatPeriod = heartbeatPeriod;
  209. }
  210. static fromJson(json = {}) {
  211. return new WsStreamSettings(
  212. json.path,
  213. json.host,
  214. json.heartbeatPeriod,
  215. );
  216. }
  217. toJson() {
  218. return {
  219. path: this.path,
  220. host: this.host,
  221. heartbeatPeriod: this.heartbeatPeriod
  222. };
  223. }
  224. }
  225. class GrpcStreamSettings extends CommonClass {
  226. constructor(
  227. serviceName = "",
  228. authority = "",
  229. multiMode = false
  230. ) {
  231. super();
  232. this.serviceName = serviceName;
  233. this.authority = authority;
  234. this.multiMode = multiMode;
  235. }
  236. static fromJson(json = {}) {
  237. return new GrpcStreamSettings(json.serviceName, json.authority, json.multiMode);
  238. }
  239. toJson() {
  240. return {
  241. serviceName: this.serviceName,
  242. authority: this.authority,
  243. multiMode: this.multiMode
  244. }
  245. }
  246. }
  247. class HttpUpgradeStreamSettings extends CommonClass {
  248. constructor(path = '/', host = '') {
  249. super();
  250. this.path = path;
  251. this.host = host;
  252. }
  253. static fromJson(json = {}) {
  254. return new HttpUpgradeStreamSettings(
  255. json.path,
  256. json.host,
  257. );
  258. }
  259. toJson() {
  260. return {
  261. path: this.path,
  262. host: this.host,
  263. };
  264. }
  265. }
  266. class xHTTPStreamSettings extends CommonClass {
  267. constructor(
  268. path = '/',
  269. host = '',
  270. mode = '',
  271. noGRPCHeader = false,
  272. scMinPostsIntervalMs = "30",
  273. xmux = {
  274. maxConcurrency: "16-32",
  275. maxConnections: 0,
  276. cMaxReuseTimes: 0,
  277. hMaxRequestTimes: "600-900",
  278. hMaxReusableSecs: "1800-3000",
  279. hKeepAlivePeriod: 0,
  280. },
  281. ) {
  282. super();
  283. this.path = path;
  284. this.host = host;
  285. this.mode = mode;
  286. this.noGRPCHeader = noGRPCHeader;
  287. this.scMinPostsIntervalMs = scMinPostsIntervalMs;
  288. this.xmux = xmux;
  289. }
  290. static fromJson(json = {}) {
  291. return new xHTTPStreamSettings(
  292. json.path,
  293. json.host,
  294. json.mode,
  295. json.noGRPCHeader,
  296. json.scMinPostsIntervalMs,
  297. json.xmux
  298. );
  299. }
  300. toJson() {
  301. return {
  302. path: this.path,
  303. host: this.host,
  304. mode: this.mode,
  305. noGRPCHeader: this.noGRPCHeader,
  306. scMinPostsIntervalMs: this.scMinPostsIntervalMs,
  307. xmux: {
  308. maxConcurrency: this.xmux.maxConcurrency,
  309. maxConnections: this.xmux.maxConnections,
  310. cMaxReuseTimes: this.xmux.cMaxReuseTimes,
  311. hMaxRequestTimes: this.xmux.hMaxRequestTimes,
  312. hMaxReusableSecs: this.xmux.hMaxReusableSecs,
  313. hKeepAlivePeriod: this.xmux.hKeepAlivePeriod,
  314. },
  315. };
  316. }
  317. }
  318. class TlsStreamSettings extends CommonClass {
  319. constructor(
  320. serverName = '',
  321. alpn = [],
  322. fingerprint = '',
  323. allowInsecure = false,
  324. echConfigList = '',
  325. ) {
  326. super();
  327. this.serverName = serverName;
  328. this.alpn = alpn;
  329. this.fingerprint = fingerprint;
  330. this.allowInsecure = allowInsecure;
  331. this.echConfigList = echConfigList;
  332. }
  333. static fromJson(json = {}) {
  334. return new TlsStreamSettings(
  335. json.serverName,
  336. json.alpn,
  337. json.fingerprint,
  338. json.allowInsecure,
  339. json.echConfigList,
  340. );
  341. }
  342. toJson() {
  343. return {
  344. serverName: this.serverName,
  345. alpn: this.alpn,
  346. fingerprint: this.fingerprint,
  347. allowInsecure: this.allowInsecure,
  348. echConfigList: this.echConfigList
  349. };
  350. }
  351. }
  352. class RealityStreamSettings extends CommonClass {
  353. constructor(
  354. publicKey = '',
  355. fingerprint = '',
  356. serverName = '',
  357. shortId = '',
  358. spiderX = '',
  359. mldsa65Verify = ''
  360. ) {
  361. super();
  362. this.publicKey = publicKey;
  363. this.fingerprint = fingerprint;
  364. this.serverName = serverName;
  365. this.shortId = shortId
  366. this.spiderX = spiderX;
  367. this.mldsa65Verify = mldsa65Verify;
  368. }
  369. static fromJson(json = {}) {
  370. return new RealityStreamSettings(
  371. json.publicKey,
  372. json.fingerprint,
  373. json.serverName,
  374. json.shortId,
  375. json.spiderX,
  376. json.mldsa65Verify
  377. );
  378. }
  379. toJson() {
  380. return {
  381. publicKey: this.publicKey,
  382. fingerprint: this.fingerprint,
  383. serverName: this.serverName,
  384. shortId: this.shortId,
  385. spiderX: this.spiderX,
  386. mldsa65Verify: this.mldsa65Verify
  387. };
  388. }
  389. };
  390. class HysteriaStreamSettings extends CommonClass {
  391. constructor(
  392. version = 2,
  393. auth = '',
  394. up = '0',
  395. down = '0',
  396. udphopPort = '',
  397. udphopInterval = 30,
  398. initStreamReceiveWindow = 8388608,
  399. maxStreamReceiveWindow = 8388608,
  400. initConnectionReceiveWindow = 20971520,
  401. maxConnectionReceiveWindow = 20971520,
  402. maxIdleTimeout = 30,
  403. keepAlivePeriod = 0,
  404. disablePathMTUDiscovery = false
  405. ) {
  406. super();
  407. this.version = version;
  408. this.auth = auth;
  409. this.up = up;
  410. this.down = down;
  411. this.udphopPort = udphopPort;
  412. this.udphopInterval = udphopInterval;
  413. this.initStreamReceiveWindow = initStreamReceiveWindow;
  414. this.maxStreamReceiveWindow = maxStreamReceiveWindow;
  415. this.initConnectionReceiveWindow = initConnectionReceiveWindow;
  416. this.maxConnectionReceiveWindow = maxConnectionReceiveWindow;
  417. this.maxIdleTimeout = maxIdleTimeout;
  418. this.keepAlivePeriod = keepAlivePeriod;
  419. this.disablePathMTUDiscovery = disablePathMTUDiscovery;
  420. }
  421. static fromJson(json = {}) {
  422. let udphopPort = '';
  423. let udphopInterval = 30;
  424. if (json.udphop) {
  425. udphopPort = json.udphop.port || '';
  426. udphopInterval = json.udphop.interval || 30;
  427. }
  428. return new HysteriaStreamSettings(
  429. json.version,
  430. json.auth,
  431. json.up,
  432. json.down,
  433. udphopPort,
  434. udphopInterval,
  435. json.initStreamReceiveWindow,
  436. json.maxStreamReceiveWindow,
  437. json.initConnectionReceiveWindow,
  438. json.maxConnectionReceiveWindow,
  439. json.maxIdleTimeout,
  440. json.keepAlivePeriod,
  441. json.disablePathMTUDiscovery
  442. );
  443. }
  444. toJson() {
  445. const result = {
  446. version: this.version,
  447. auth: this.auth,
  448. up: this.up,
  449. down: this.down,
  450. initStreamReceiveWindow: this.initStreamReceiveWindow,
  451. maxStreamReceiveWindow: this.maxStreamReceiveWindow,
  452. initConnectionReceiveWindow: this.initConnectionReceiveWindow,
  453. maxConnectionReceiveWindow: this.maxConnectionReceiveWindow,
  454. maxIdleTimeout: this.maxIdleTimeout,
  455. keepAlivePeriod: this.keepAlivePeriod,
  456. disablePathMTUDiscovery: this.disablePathMTUDiscovery
  457. };
  458. if (this.udphopPort) {
  459. result.udphop = {
  460. port: this.udphopPort,
  461. interval: this.udphopInterval
  462. };
  463. }
  464. return result;
  465. }
  466. };
  467. class SockoptStreamSettings extends CommonClass {
  468. constructor(
  469. dialerProxy = "",
  470. tcpFastOpen = false,
  471. tcpKeepAliveInterval = 0,
  472. tcpMptcp = false,
  473. penetrate = false,
  474. addressPortStrategy = Address_Port_Strategy.NONE,
  475. trustedXForwardedFor = [],
  476. ) {
  477. super();
  478. this.dialerProxy = dialerProxy;
  479. this.tcpFastOpen = tcpFastOpen;
  480. this.tcpKeepAliveInterval = tcpKeepAliveInterval;
  481. this.tcpMptcp = tcpMptcp;
  482. this.penetrate = penetrate;
  483. this.addressPortStrategy = addressPortStrategy;
  484. this.trustedXForwardedFor = trustedXForwardedFor;
  485. }
  486. static fromJson(json = {}) {
  487. if (Object.keys(json).length === 0) return undefined;
  488. return new SockoptStreamSettings(
  489. json.dialerProxy,
  490. json.tcpFastOpen,
  491. json.tcpKeepAliveInterval,
  492. json.tcpMptcp,
  493. json.penetrate,
  494. json.addressPortStrategy,
  495. json.trustedXForwardedFor || []
  496. );
  497. }
  498. toJson() {
  499. const result = {
  500. dialerProxy: this.dialerProxy,
  501. tcpFastOpen: this.tcpFastOpen,
  502. tcpKeepAliveInterval: this.tcpKeepAliveInterval,
  503. tcpMptcp: this.tcpMptcp,
  504. penetrate: this.penetrate,
  505. addressPortStrategy: this.addressPortStrategy
  506. };
  507. if (this.trustedXForwardedFor && this.trustedXForwardedFor.length > 0) {
  508. result.trustedXForwardedFor = this.trustedXForwardedFor;
  509. }
  510. return result;
  511. }
  512. }
  513. class StreamSettings extends CommonClass {
  514. constructor(
  515. network = 'tcp',
  516. security = 'none',
  517. tlsSettings = new TlsStreamSettings(),
  518. realitySettings = new RealityStreamSettings(),
  519. tcpSettings = new TcpStreamSettings(),
  520. kcpSettings = new KcpStreamSettings(),
  521. wsSettings = new WsStreamSettings(),
  522. grpcSettings = new GrpcStreamSettings(),
  523. httpupgradeSettings = new HttpUpgradeStreamSettings(),
  524. xhttpSettings = new xHTTPStreamSettings(),
  525. hysteriaSettings = new HysteriaStreamSettings(),
  526. sockopt = undefined,
  527. ) {
  528. super();
  529. this.network = network;
  530. this.security = security;
  531. this.tls = tlsSettings;
  532. this.reality = realitySettings;
  533. this.tcp = tcpSettings;
  534. this.kcp = kcpSettings;
  535. this.ws = wsSettings;
  536. this.grpc = grpcSettings;
  537. this.httpupgrade = httpupgradeSettings;
  538. this.xhttp = xhttpSettings;
  539. this.hysteria = hysteriaSettings;
  540. this.sockopt = sockopt;
  541. }
  542. get isTls() {
  543. return this.security === 'tls';
  544. }
  545. get isReality() {
  546. return this.security === "reality";
  547. }
  548. get sockoptSwitch() {
  549. return this.sockopt != undefined;
  550. }
  551. set sockoptSwitch(value) {
  552. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  553. }
  554. static fromJson(json = {}) {
  555. return new StreamSettings(
  556. json.network,
  557. json.security,
  558. TlsStreamSettings.fromJson(json.tlsSettings),
  559. RealityStreamSettings.fromJson(json.realitySettings),
  560. TcpStreamSettings.fromJson(json.tcpSettings),
  561. KcpStreamSettings.fromJson(json.kcpSettings),
  562. WsStreamSettings.fromJson(json.wsSettings),
  563. GrpcStreamSettings.fromJson(json.grpcSettings),
  564. HttpUpgradeStreamSettings.fromJson(json.httpupgradeSettings),
  565. xHTTPStreamSettings.fromJson(json.xhttpSettings),
  566. HysteriaStreamSettings.fromJson(json.hysteriaSettings),
  567. SockoptStreamSettings.fromJson(json.sockopt),
  568. );
  569. }
  570. toJson() {
  571. const network = this.network;
  572. return {
  573. network: network,
  574. security: this.security,
  575. tlsSettings: this.security == 'tls' ? this.tls.toJson() : undefined,
  576. realitySettings: this.security == 'reality' ? this.reality.toJson() : undefined,
  577. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  578. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  579. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  580. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  581. httpupgradeSettings: network === 'httpupgrade' ? this.httpupgrade.toJson() : undefined,
  582. xhttpSettings: network === 'xhttp' ? this.xhttp.toJson() : undefined,
  583. hysteriaSettings: network === 'hysteria' ? this.hysteria.toJson() : undefined,
  584. sockopt: this.sockopt != undefined ? this.sockopt.toJson() : undefined,
  585. };
  586. }
  587. }
  588. class Mux extends CommonClass {
  589. constructor(enabled = false, concurrency = 8, xudpConcurrency = 16, xudpProxyUDP443 = "reject") {
  590. super();
  591. this.enabled = enabled;
  592. this.concurrency = concurrency;
  593. this.xudpConcurrency = xudpConcurrency;
  594. this.xudpProxyUDP443 = xudpProxyUDP443;
  595. }
  596. static fromJson(json = {}) {
  597. if (Object.keys(json).length === 0) return undefined;
  598. return new Mux(
  599. json.enabled,
  600. json.concurrency,
  601. json.xudpConcurrency,
  602. json.xudpProxyUDP443,
  603. );
  604. }
  605. toJson() {
  606. return {
  607. enabled: this.enabled,
  608. concurrency: this.concurrency,
  609. xudpConcurrency: this.xudpConcurrency,
  610. xudpProxyUDP443: this.xudpProxyUDP443,
  611. };
  612. }
  613. }
  614. class Outbound extends CommonClass {
  615. constructor(
  616. tag = '',
  617. protocol = Protocols.VLESS,
  618. settings = null,
  619. streamSettings = new StreamSettings(),
  620. sendThrough,
  621. mux = new Mux(),
  622. ) {
  623. super();
  624. this.tag = tag;
  625. this._protocol = protocol;
  626. this.settings = settings == null ? Outbound.Settings.getSettings(protocol) : settings;
  627. this.stream = streamSettings;
  628. this.sendThrough = sendThrough;
  629. this.mux = mux;
  630. }
  631. get protocol() {
  632. return this._protocol;
  633. }
  634. set protocol(protocol) {
  635. this._protocol = protocol;
  636. this.settings = Outbound.Settings.getSettings(protocol);
  637. this.stream = new StreamSettings();
  638. }
  639. canEnableTls() {
  640. if (![Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks].includes(this.protocol)) return false;
  641. return ["tcp", "ws", "http", "grpc", "httpupgrade", "xhttp"].includes(this.stream.network);
  642. }
  643. //this is used for xtls-rprx-vision
  644. canEnableTlsFlow() {
  645. if ((this.stream.security != 'none') && (this.stream.network === "tcp")) {
  646. return this.protocol === Protocols.VLESS;
  647. }
  648. return false;
  649. }
  650. // Vision seed applies only when vision flow is selected
  651. canEnableVisionSeed() {
  652. if (!this.canEnableTlsFlow()) return false;
  653. const flow = this.settings?.flow;
  654. return flow === TLS_FLOW_CONTROL.VISION || flow === TLS_FLOW_CONTROL.VISION_UDP443;
  655. }
  656. canEnableReality() {
  657. if (![Protocols.VLESS, Protocols.Trojan].includes(this.protocol)) return false;
  658. return ["tcp", "http", "grpc", "xhttp"].includes(this.stream.network);
  659. }
  660. canEnableStream() {
  661. return [Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks, Protocols.Hysteria].includes(this.protocol);
  662. }
  663. canEnableMux() {
  664. // Disable Mux if flow is set
  665. if (this.settings.flow && this.settings.flow !== '') {
  666. this.mux.enabled = false;
  667. return false;
  668. }
  669. // Disable Mux if network is xhttp
  670. if (this.stream.network === 'xhttp') {
  671. this.mux.enabled = false;
  672. return false;
  673. }
  674. // Allow Mux only for these protocols
  675. return [
  676. Protocols.VMess,
  677. Protocols.VLESS,
  678. Protocols.Trojan,
  679. Protocols.Shadowsocks,
  680. Protocols.HTTP,
  681. Protocols.Socks
  682. ].includes(this.protocol);
  683. }
  684. hasServers() {
  685. return [Protocols.Trojan, Protocols.Shadowsocks, Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  686. }
  687. hasAddressPort() {
  688. return [
  689. Protocols.DNS,
  690. Protocols.VMess,
  691. Protocols.VLESS,
  692. Protocols.Trojan,
  693. Protocols.Shadowsocks,
  694. Protocols.Socks,
  695. Protocols.HTTP,
  696. Protocols.Hysteria
  697. ].includes(this.protocol);
  698. }
  699. hasUsername() {
  700. return [Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  701. }
  702. static fromJson(json = {}) {
  703. return new Outbound(
  704. json.tag,
  705. json.protocol,
  706. Outbound.Settings.fromJson(json.protocol, json.settings),
  707. StreamSettings.fromJson(json.streamSettings),
  708. json.sendThrough,
  709. Mux.fromJson(json.mux),
  710. )
  711. }
  712. toJson() {
  713. var stream;
  714. if (this.canEnableStream()) {
  715. stream = this.stream.toJson();
  716. } else {
  717. if (this.stream?.sockopt)
  718. stream = { sockopt: this.stream.sockopt.toJson() };
  719. }
  720. let settingsOut = this.settings instanceof CommonClass ? this.settings.toJson() : this.settings;
  721. return {
  722. protocol: this.protocol,
  723. settings: settingsOut,
  724. // Only include tag, streamSettings, sendThrough, mux if present and not empty
  725. ...(this.tag ? { tag: this.tag } : {}),
  726. ...(stream ? { streamSettings: stream } : {}),
  727. ...(this.sendThrough ? { sendThrough: this.sendThrough } : {}),
  728. ...(this.mux?.enabled ? { mux: this.mux } : {}),
  729. };
  730. }
  731. static fromLink(link) {
  732. data = link.split('://');
  733. if (data.length != 2) return null;
  734. switch (data[0].toLowerCase()) {
  735. case Protocols.VMess:
  736. return this.fromVmessLink(JSON.parse(Base64.decode(data[1])));
  737. case Protocols.VLESS:
  738. case Protocols.Trojan:
  739. case 'ss':
  740. return this.fromParamLink(link);
  741. case 'hysteria2':
  742. case Protocols.Hysteria:
  743. return this.fromHysteriaLink(link);
  744. default:
  745. return null;
  746. }
  747. }
  748. static fromVmessLink(json = {}) {
  749. let stream = new StreamSettings(json.net, json.tls);
  750. let network = json.net;
  751. if (network === 'tcp') {
  752. stream.tcp = new TcpStreamSettings(
  753. json.type,
  754. json.host ?? '',
  755. json.path ?? '');
  756. } else if (network === 'kcp') {
  757. stream.kcp = new KcpStreamSettings();
  758. stream.type = json.type;
  759. stream.seed = json.path;
  760. } else if (network === 'ws') {
  761. stream.ws = new WsStreamSettings(json.path, json.host);
  762. } else if (network === 'grpc') {
  763. stream.grpc = new GrpcStreamSettings(json.path, json.authority, json.type == 'multi');
  764. } else if (network === 'httpupgrade') {
  765. stream.httpupgrade = new HttpUpgradeStreamSettings(json.path, json.host);
  766. } else if (network === 'xhttp') {
  767. stream.xhttp = new xHTTPStreamSettings(json.path, json.host, json.mode);
  768. }
  769. if (json.tls && json.tls == 'tls') {
  770. stream.tls = new TlsStreamSettings(
  771. json.sni,
  772. json.alpn ? json.alpn.split(',') : [],
  773. json.fp,
  774. json.allowInsecure);
  775. }
  776. const port = json.port * 1;
  777. return new Outbound(json.ps, Protocols.VMess, new Outbound.VmessSettings(json.add, port, json.id, json.scy), stream);
  778. }
  779. static fromParamLink(link) {
  780. const url = new URL(link);
  781. let type = url.searchParams.get('type') ?? 'tcp';
  782. let security = url.searchParams.get('security') ?? 'none';
  783. let stream = new StreamSettings(type, security);
  784. let headerType = url.searchParams.get('headerType') ?? undefined;
  785. let host = url.searchParams.get('host') ?? undefined;
  786. let path = url.searchParams.get('path') ?? undefined;
  787. let mode = url.searchParams.get('mode') ?? undefined;
  788. if (type === 'tcp' || type === 'none') {
  789. stream.tcp = new TcpStreamSettings(headerType ?? 'none', host, path);
  790. } else if (type === 'kcp') {
  791. stream.kcp = new KcpStreamSettings();
  792. stream.kcp.type = headerType ?? 'none';
  793. stream.kcp.seed = path;
  794. } else if (type === 'ws') {
  795. stream.ws = new WsStreamSettings(path, host);
  796. } else if (type === 'grpc') {
  797. stream.grpc = new GrpcStreamSettings(
  798. url.searchParams.get('serviceName') ?? '',
  799. url.searchParams.get('authority') ?? '',
  800. url.searchParams.get('mode') == 'multi');
  801. } else if (type === 'httpupgrade') {
  802. stream.httpupgrade = new HttpUpgradeStreamSettings(path, host);
  803. } else if (type === 'xhttp') {
  804. stream.xhttp = new xHTTPStreamSettings(path, host, mode);
  805. }
  806. if (security == 'tls') {
  807. let fp = url.searchParams.get('fp') ?? 'none';
  808. let alpn = url.searchParams.get('alpn');
  809. let allowInsecure = url.searchParams.get('allowInsecure');
  810. let sni = url.searchParams.get('sni') ?? '';
  811. let ech = url.searchParams.get('ech') ?? '';
  812. stream.tls = new TlsStreamSettings(sni, alpn ? alpn.split(',') : [], fp, allowInsecure == 1, ech);
  813. }
  814. if (security == 'reality') {
  815. let pbk = url.searchParams.get('pbk');
  816. let fp = url.searchParams.get('fp');
  817. let sni = url.searchParams.get('sni') ?? '';
  818. let sid = url.searchParams.get('sid') ?? '';
  819. let spx = url.searchParams.get('spx') ?? '';
  820. let pqv = url.searchParams.get('pqv') ?? '';
  821. stream.reality = new RealityStreamSettings(pbk, fp, sni, sid, spx, pqv);
  822. }
  823. const regex = /([^@]+):\/\/([^@]+)@(.+):(\d+)(.*)$/;
  824. const match = link.match(regex);
  825. if (!match) return null;
  826. let [, protocol, userData, address, port,] = match;
  827. port *= 1;
  828. if (protocol == 'ss') {
  829. protocol = 'shadowsocks';
  830. userData = atob(userData).split(':');
  831. }
  832. var settings;
  833. switch (protocol) {
  834. case Protocols.VLESS:
  835. settings = new Outbound.VLESSSettings(address, port, userData, url.searchParams.get('flow') ?? '', url.searchParams.get('encryption') ?? 'none');
  836. break;
  837. case Protocols.Trojan:
  838. settings = new Outbound.TrojanSettings(address, port, userData);
  839. break;
  840. case Protocols.Shadowsocks:
  841. let method = userData.splice(0, 1)[0];
  842. settings = new Outbound.ShadowsocksSettings(address, port, userData.join(":"), method, true);
  843. break;
  844. default:
  845. return null;
  846. }
  847. let remark = decodeURIComponent(url.hash);
  848. // Remove '#' from url.hash
  849. remark = remark.length > 0 ? remark.substring(1) : 'out-' + protocol + '-' + port;
  850. return new Outbound(remark, protocol, settings, stream);
  851. }
  852. static fromHysteriaLink(link) {
  853. // Parse hysteria2://password@address:port[?param1=value1&param2=value2...][#remarks]
  854. const regex = /^hysteria2?:\/\/([^@]+)@([^:?#]+):(\d+)([^#]*)(#.*)?$/;
  855. const match = link.match(regex);
  856. if (!match) return null;
  857. let [, password, address, port, params, hash] = match;
  858. port = parseInt(port);
  859. // Parse URL parameters if present
  860. let urlParams = new URLSearchParams(params);
  861. // Create stream settings with hysteria network
  862. let stream = new StreamSettings('hysteria', 'none');
  863. // Set hysteria stream settings
  864. stream.hysteria.auth = password;
  865. stream.hysteria.up = urlParams.get('up') ?? '0';
  866. stream.hysteria.down = urlParams.get('down') ?? '0';
  867. stream.hysteria.udphopPort = urlParams.get('udphopPort') ?? '';
  868. stream.hysteria.udphopInterval = parseInt(urlParams.get('udphopInterval') ?? '30');
  869. // Optional QUIC parameters
  870. if (urlParams.has('initStreamReceiveWindow')) {
  871. stream.hysteria.initStreamReceiveWindow = parseInt(urlParams.get('initStreamReceiveWindow'));
  872. }
  873. if (urlParams.has('maxStreamReceiveWindow')) {
  874. stream.hysteria.maxStreamReceiveWindow = parseInt(urlParams.get('maxStreamReceiveWindow'));
  875. }
  876. if (urlParams.has('initConnectionReceiveWindow')) {
  877. stream.hysteria.initConnectionReceiveWindow = parseInt(urlParams.get('initConnectionReceiveWindow'));
  878. }
  879. if (urlParams.has('maxConnectionReceiveWindow')) {
  880. stream.hysteria.maxConnectionReceiveWindow = parseInt(urlParams.get('maxConnectionReceiveWindow'));
  881. }
  882. if (urlParams.has('maxIdleTimeout')) {
  883. stream.hysteria.maxIdleTimeout = parseInt(urlParams.get('maxIdleTimeout'));
  884. }
  885. if (urlParams.has('keepAlivePeriod')) {
  886. stream.hysteria.keepAlivePeriod = parseInt(urlParams.get('keepAlivePeriod'));
  887. }
  888. if (urlParams.has('disablePathMTUDiscovery')) {
  889. stream.hysteria.disablePathMTUDiscovery = urlParams.get('disablePathMTUDiscovery') === 'true';
  890. }
  891. // Create settings
  892. let settings = new Outbound.HysteriaSettings(address, port, 2);
  893. // Extract remark from hash
  894. let remark = hash ? decodeURIComponent(hash.substring(1)) : `out-hysteria-${port}`;
  895. return new Outbound(remark, Protocols.Hysteria, settings, stream);
  896. }
  897. }
  898. Outbound.Settings = class extends CommonClass {
  899. constructor(protocol) {
  900. super();
  901. this.protocol = protocol;
  902. }
  903. static getSettings(protocol) {
  904. switch (protocol) {
  905. case Protocols.Freedom: return new Outbound.FreedomSettings();
  906. case Protocols.Blackhole: return new Outbound.BlackholeSettings();
  907. case Protocols.DNS: return new Outbound.DNSSettings();
  908. case Protocols.VMess: return new Outbound.VmessSettings();
  909. case Protocols.VLESS: return new Outbound.VLESSSettings();
  910. case Protocols.Trojan: return new Outbound.TrojanSettings();
  911. case Protocols.Shadowsocks: return new Outbound.ShadowsocksSettings();
  912. case Protocols.Socks: return new Outbound.SocksSettings();
  913. case Protocols.HTTP: return new Outbound.HttpSettings();
  914. case Protocols.Wireguard: return new Outbound.WireguardSettings();
  915. case Protocols.Hysteria: return new Outbound.HysteriaSettings();
  916. default: return null;
  917. }
  918. }
  919. static fromJson(protocol, json) {
  920. switch (protocol) {
  921. case Protocols.Freedom: return Outbound.FreedomSettings.fromJson(json);
  922. case Protocols.Blackhole: return Outbound.BlackholeSettings.fromJson(json);
  923. case Protocols.DNS: return Outbound.DNSSettings.fromJson(json);
  924. case Protocols.VMess: return Outbound.VmessSettings.fromJson(json);
  925. case Protocols.VLESS: return Outbound.VLESSSettings.fromJson(json);
  926. case Protocols.Trojan: return Outbound.TrojanSettings.fromJson(json);
  927. case Protocols.Shadowsocks: return Outbound.ShadowsocksSettings.fromJson(json);
  928. case Protocols.Socks: return Outbound.SocksSettings.fromJson(json);
  929. case Protocols.HTTP: return Outbound.HttpSettings.fromJson(json);
  930. case Protocols.Wireguard: return Outbound.WireguardSettings.fromJson(json);
  931. case Protocols.Hysteria: return Outbound.HysteriaSettings.fromJson(json);
  932. default: return null;
  933. }
  934. }
  935. toJson() {
  936. return {};
  937. }
  938. };
  939. Outbound.FreedomSettings = class extends CommonClass {
  940. constructor(
  941. domainStrategy = '',
  942. redirect = '',
  943. fragment = {},
  944. noises = []
  945. ) {
  946. super();
  947. this.domainStrategy = domainStrategy;
  948. this.redirect = redirect;
  949. this.fragment = fragment;
  950. this.noises = noises;
  951. }
  952. addNoise() {
  953. this.noises.push(new Outbound.FreedomSettings.Noise());
  954. }
  955. delNoise(index) {
  956. this.noises.splice(index, 1);
  957. }
  958. static fromJson(json = {}) {
  959. return new Outbound.FreedomSettings(
  960. json.domainStrategy,
  961. json.redirect,
  962. json.fragment ? Outbound.FreedomSettings.Fragment.fromJson(json.fragment) : undefined,
  963. json.noises ? json.noises.map(noise => Outbound.FreedomSettings.Noise.fromJson(noise)) : undefined,
  964. );
  965. }
  966. toJson() {
  967. return {
  968. domainStrategy: ObjectUtil.isEmpty(this.domainStrategy) ? undefined : this.domainStrategy,
  969. redirect: ObjectUtil.isEmpty(this.redirect) ? undefined : this.redirect,
  970. fragment: Object.keys(this.fragment).length === 0 ? undefined : this.fragment,
  971. noises: this.noises.length === 0 ? undefined : Outbound.FreedomSettings.Noise.toJsonArray(this.noises),
  972. };
  973. }
  974. };
  975. Outbound.FreedomSettings.Fragment = class extends CommonClass {
  976. constructor(
  977. packets = '1-3',
  978. length = '',
  979. interval = '',
  980. maxSplit = ''
  981. ) {
  982. super();
  983. this.packets = packets;
  984. this.length = length;
  985. this.interval = interval;
  986. this.maxSplit = maxSplit;
  987. }
  988. static fromJson(json = {}) {
  989. return new Outbound.FreedomSettings.Fragment(
  990. json.packets,
  991. json.length,
  992. json.interval,
  993. json.maxSplit
  994. );
  995. }
  996. };
  997. Outbound.FreedomSettings.Noise = class extends CommonClass {
  998. constructor(
  999. type = 'rand',
  1000. packet = '10-20',
  1001. delay = '10-16',
  1002. applyTo = 'ip'
  1003. ) {
  1004. super();
  1005. this.type = type;
  1006. this.packet = packet;
  1007. this.delay = delay;
  1008. this.applyTo = applyTo;
  1009. }
  1010. static fromJson(json = {}) {
  1011. return new Outbound.FreedomSettings.Noise(
  1012. json.type,
  1013. json.packet,
  1014. json.delay,
  1015. json.applyTo
  1016. );
  1017. }
  1018. toJson() {
  1019. return {
  1020. type: this.type,
  1021. packet: this.packet,
  1022. delay: this.delay,
  1023. applyTo: this.applyTo
  1024. };
  1025. }
  1026. };
  1027. Outbound.BlackholeSettings = class extends CommonClass {
  1028. constructor(type) {
  1029. super();
  1030. this.type = type;
  1031. }
  1032. static fromJson(json = {}) {
  1033. return new Outbound.BlackholeSettings(
  1034. json.response ? json.response.type : undefined,
  1035. );
  1036. }
  1037. toJson() {
  1038. return {
  1039. response: ObjectUtil.isEmpty(this.type) ? undefined : { type: this.type },
  1040. };
  1041. }
  1042. };
  1043. Outbound.DNSSettings = class extends CommonClass {
  1044. constructor(
  1045. network = 'udp',
  1046. address = '',
  1047. port = 53,
  1048. nonIPQuery = 'reject',
  1049. blockTypes = []
  1050. ) {
  1051. super();
  1052. this.network = network;
  1053. this.address = address;
  1054. this.port = port;
  1055. this.nonIPQuery = nonIPQuery;
  1056. this.blockTypes = blockTypes;
  1057. }
  1058. static fromJson(json = {}) {
  1059. return new Outbound.DNSSettings(
  1060. json.network,
  1061. json.address,
  1062. json.port,
  1063. json.nonIPQuery,
  1064. json.blockTypes,
  1065. );
  1066. }
  1067. };
  1068. Outbound.VmessSettings = class extends CommonClass {
  1069. constructor(address, port, id, security) {
  1070. super();
  1071. this.address = address;
  1072. this.port = port;
  1073. this.id = id;
  1074. this.security = security;
  1075. }
  1076. static fromJson(json = {}) {
  1077. if (!ObjectUtil.isArrEmpty(json.vnext)) {
  1078. const v = json.vnext[0] || {};
  1079. const u = ObjectUtil.isArrEmpty(v.users) ? {} : v.users[0];
  1080. return new Outbound.VmessSettings(
  1081. v.address,
  1082. v.port,
  1083. u.id,
  1084. u.security,
  1085. );
  1086. }
  1087. }
  1088. toJson() {
  1089. return {
  1090. vnext: [{
  1091. address: this.address,
  1092. port: this.port,
  1093. users: [{
  1094. id: this.id,
  1095. security: this.security
  1096. }]
  1097. }]
  1098. };
  1099. }
  1100. };
  1101. Outbound.VLESSSettings = class extends CommonClass {
  1102. constructor(address, port, id, flow, encryption, testpre = 0, testseed = [900, 500, 900, 256]) {
  1103. super();
  1104. this.address = address;
  1105. this.port = port;
  1106. this.id = id;
  1107. this.flow = flow;
  1108. this.encryption = encryption;
  1109. this.testpre = testpre;
  1110. this.testseed = testseed;
  1111. }
  1112. static fromJson(json = {}) {
  1113. if (ObjectUtil.isEmpty(json.address) || ObjectUtil.isEmpty(json.port)) return new Outbound.VLESSSettings();
  1114. return new Outbound.VLESSSettings(
  1115. json.address,
  1116. json.port,
  1117. json.id,
  1118. json.flow,
  1119. json.encryption,
  1120. json.testpre || 0,
  1121. json.testseed && json.testseed.length >= 4 ? json.testseed : [900, 500, 900, 256]
  1122. );
  1123. }
  1124. toJson() {
  1125. const result = {
  1126. address: this.address,
  1127. port: this.port,
  1128. id: this.id,
  1129. flow: this.flow,
  1130. encryption: this.encryption,
  1131. };
  1132. if (this.testpre > 0) {
  1133. result.testpre = this.testpre;
  1134. }
  1135. if (this.testseed && this.testseed.length >= 4) {
  1136. result.testseed = this.testseed;
  1137. }
  1138. return result;
  1139. }
  1140. };
  1141. Outbound.TrojanSettings = class extends CommonClass {
  1142. constructor(address, port, password) {
  1143. super();
  1144. this.address = address;
  1145. this.port = port;
  1146. this.password = password;
  1147. }
  1148. static fromJson(json = {}) {
  1149. if (ObjectUtil.isArrEmpty(json.servers)) return new Outbound.TrojanSettings();
  1150. return new Outbound.TrojanSettings(
  1151. json.servers[0].address,
  1152. json.servers[0].port,
  1153. json.servers[0].password,
  1154. );
  1155. }
  1156. toJson() {
  1157. return {
  1158. servers: [{
  1159. address: this.address,
  1160. port: this.port,
  1161. password: this.password,
  1162. }],
  1163. };
  1164. }
  1165. };
  1166. Outbound.ShadowsocksSettings = class extends CommonClass {
  1167. constructor(address, port, password, method, uot, UoTVersion) {
  1168. super();
  1169. this.address = address;
  1170. this.port = port;
  1171. this.password = password;
  1172. this.method = method;
  1173. this.uot = uot;
  1174. this.UoTVersion = UoTVersion;
  1175. }
  1176. static fromJson(json = {}) {
  1177. let servers = json.servers;
  1178. if (ObjectUtil.isArrEmpty(servers)) servers = [{}];
  1179. return new Outbound.ShadowsocksSettings(
  1180. servers[0].address,
  1181. servers[0].port,
  1182. servers[0].password,
  1183. servers[0].method,
  1184. servers[0].uot,
  1185. servers[0].UoTVersion,
  1186. );
  1187. }
  1188. toJson() {
  1189. return {
  1190. servers: [{
  1191. address: this.address,
  1192. port: this.port,
  1193. password: this.password,
  1194. method: this.method,
  1195. uot: this.uot,
  1196. UoTVersion: this.UoTVersion,
  1197. }],
  1198. };
  1199. }
  1200. };
  1201. Outbound.SocksSettings = class extends CommonClass {
  1202. constructor(address, port, user, pass) {
  1203. super();
  1204. this.address = address;
  1205. this.port = port;
  1206. this.user = user;
  1207. this.pass = pass;
  1208. }
  1209. static fromJson(json = {}) {
  1210. let servers = json.servers;
  1211. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  1212. return new Outbound.SocksSettings(
  1213. servers[0].address,
  1214. servers[0].port,
  1215. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  1216. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  1217. );
  1218. }
  1219. toJson() {
  1220. return {
  1221. servers: [{
  1222. address: this.address,
  1223. port: this.port,
  1224. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1225. }],
  1226. };
  1227. }
  1228. };
  1229. Outbound.HttpSettings = class extends CommonClass {
  1230. constructor(address, port, user, pass) {
  1231. super();
  1232. this.address = address;
  1233. this.port = port;
  1234. this.user = user;
  1235. this.pass = pass;
  1236. }
  1237. static fromJson(json = {}) {
  1238. let servers = json.servers;
  1239. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  1240. return new Outbound.HttpSettings(
  1241. servers[0].address,
  1242. servers[0].port,
  1243. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  1244. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  1245. );
  1246. }
  1247. toJson() {
  1248. return {
  1249. servers: [{
  1250. address: this.address,
  1251. port: this.port,
  1252. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1253. }],
  1254. };
  1255. }
  1256. };
  1257. Outbound.WireguardSettings = class extends CommonClass {
  1258. constructor(
  1259. mtu = 1250,
  1260. secretKey = '',
  1261. address = [''],
  1262. workers = 2,
  1263. domainStrategy = '',
  1264. reserved = '',
  1265. peers = [new Outbound.WireguardSettings.Peer()],
  1266. noKernelTun = false,
  1267. ) {
  1268. super();
  1269. this.mtu = mtu;
  1270. this.secretKey = secretKey;
  1271. this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  1272. this.address = Array.isArray(address) ? address.join(',') : address;
  1273. this.workers = workers;
  1274. this.domainStrategy = domainStrategy;
  1275. this.reserved = Array.isArray(reserved) ? reserved.join(',') : reserved;
  1276. this.peers = peers;
  1277. this.noKernelTun = noKernelTun;
  1278. }
  1279. addPeer() {
  1280. this.peers.push(new Outbound.WireguardSettings.Peer());
  1281. }
  1282. delPeer(index) {
  1283. this.peers.splice(index, 1);
  1284. }
  1285. static fromJson(json = {}) {
  1286. return new Outbound.WireguardSettings(
  1287. json.mtu,
  1288. json.secretKey,
  1289. json.address,
  1290. json.workers,
  1291. json.domainStrategy,
  1292. json.reserved,
  1293. json.peers.map(peer => Outbound.WireguardSettings.Peer.fromJson(peer)),
  1294. json.noKernelTun,
  1295. );
  1296. }
  1297. toJson() {
  1298. return {
  1299. mtu: this.mtu ?? undefined,
  1300. secretKey: this.secretKey,
  1301. address: this.address ? this.address.split(",") : [],
  1302. workers: this.workers ?? undefined,
  1303. domainStrategy: WireguardDomainStrategy.includes(this.domainStrategy) ? this.domainStrategy : undefined,
  1304. reserved: this.reserved ? this.reserved.split(",").map(Number) : undefined,
  1305. peers: Outbound.WireguardSettings.Peer.toJsonArray(this.peers),
  1306. noKernelTun: this.noKernelTun,
  1307. };
  1308. }
  1309. };
  1310. Outbound.WireguardSettings.Peer = class extends CommonClass {
  1311. constructor(
  1312. publicKey = '',
  1313. psk = '',
  1314. allowedIPs = ['0.0.0.0/0', '::/0'],
  1315. endpoint = '',
  1316. keepAlive = 0
  1317. ) {
  1318. super();
  1319. this.publicKey = publicKey;
  1320. this.psk = psk;
  1321. this.allowedIPs = allowedIPs;
  1322. this.endpoint = endpoint;
  1323. this.keepAlive = keepAlive;
  1324. }
  1325. static fromJson(json = {}) {
  1326. return new Outbound.WireguardSettings.Peer(
  1327. json.publicKey,
  1328. json.preSharedKey,
  1329. json.allowedIPs,
  1330. json.endpoint,
  1331. json.keepAlive
  1332. );
  1333. }
  1334. toJson() {
  1335. return {
  1336. publicKey: this.publicKey,
  1337. preSharedKey: this.psk.length > 0 ? this.psk : undefined,
  1338. allowedIPs: this.allowedIPs ? this.allowedIPs : undefined,
  1339. endpoint: this.endpoint,
  1340. keepAlive: this.keepAlive ?? undefined,
  1341. };
  1342. }
  1343. };
  1344. Outbound.HysteriaSettings = class extends CommonClass {
  1345. constructor(address = '', port = 443, version = 2) {
  1346. super();
  1347. this.address = address;
  1348. this.port = port;
  1349. this.version = version;
  1350. }
  1351. static fromJson(json = {}) {
  1352. if (Object.keys(json).length === 0) return new Outbound.HysteriaSettings();
  1353. return new Outbound.HysteriaSettings(
  1354. json.address,
  1355. json.port,
  1356. json.version
  1357. );
  1358. }
  1359. toJson() {
  1360. return {
  1361. address: this.address,
  1362. port: this.port,
  1363. version: this.version
  1364. };
  1365. }
  1366. };