outbound.js 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  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. const DNSRuleActions = ['direct', 'drop', 'reject', 'hijack'];
  91. function normalizeDNSRuleField(value) {
  92. if (value === null || value === undefined) {
  93. return '';
  94. }
  95. if (Array.isArray(value)) {
  96. return value.map(item => item.toString().trim()).filter(item => item.length > 0).join(',');
  97. }
  98. return value.toString().trim();
  99. }
  100. function normalizeDNSRuleAction(action) {
  101. action = ObjectUtil.isEmpty(action) ? 'direct' : action.toString().toLowerCase().trim();
  102. return DNSRuleActions.includes(action) ? action : 'direct';
  103. }
  104. function parseLegacyDNSBlockTypes(blockTypes) {
  105. if (blockTypes === null || blockTypes === undefined || blockTypes === '') {
  106. return [];
  107. }
  108. if (Array.isArray(blockTypes)) {
  109. return blockTypes
  110. .map(item => Number(item))
  111. .filter(item => Number.isInteger(item) && item >= 0 && item <= 65535);
  112. }
  113. if (typeof blockTypes === 'number') {
  114. return Number.isInteger(blockTypes) && blockTypes >= 0 && blockTypes <= 65535 ? [blockTypes] : [];
  115. }
  116. return blockTypes
  117. .toString()
  118. .split(',')
  119. .map(item => item.trim())
  120. .filter(item => /^\d+$/.test(item))
  121. .map(item => Number(item))
  122. .filter(item => item >= 0 && item <= 65535);
  123. }
  124. function buildLegacyDNSRules(nonIPQuery, blockTypes) {
  125. const mode = ['reject', 'drop', 'skip'].includes(nonIPQuery) ? nonIPQuery : 'reject';
  126. const rules = [];
  127. const parsedBlockTypes = parseLegacyDNSBlockTypes(blockTypes);
  128. if (parsedBlockTypes.length > 0) {
  129. rules.push(new Outbound.DNSRule(mode === 'reject' ? 'reject' : 'drop', parsedBlockTypes.join(',')));
  130. }
  131. rules.push(new Outbound.DNSRule('hijack', '1,28'));
  132. rules.push(new Outbound.DNSRule(mode === 'skip' ? 'direct' : mode));
  133. return rules;
  134. }
  135. function getDNSRulesFromJson(json = {}) {
  136. if (Array.isArray(json.rules) && json.rules.length > 0) {
  137. return json.rules.map(rule => Outbound.DNSRule.fromJson(rule));
  138. }
  139. if (json.nonIPQuery !== undefined || json.blockTypes !== undefined) {
  140. return buildLegacyDNSRules(json.nonIPQuery, json.blockTypes);
  141. }
  142. return [];
  143. }
  144. Object.freeze(Protocols);
  145. Object.freeze(SSMethods);
  146. Object.freeze(TLS_FLOW_CONTROL);
  147. Object.freeze(UTLS_FINGERPRINT);
  148. Object.freeze(ALPN_OPTION);
  149. Object.freeze(OutboundDomainStrategies);
  150. Object.freeze(WireguardDomainStrategy);
  151. Object.freeze(USERS_SECURITY);
  152. Object.freeze(MODE_OPTION);
  153. Object.freeze(Address_Port_Strategy);
  154. Object.freeze(DNSRuleActions);
  155. class CommonClass {
  156. static toJsonArray(arr) {
  157. return arr.map(obj => obj.toJson());
  158. }
  159. static fromJson() {
  160. return new CommonClass();
  161. }
  162. toJson() {
  163. return this;
  164. }
  165. toString(format = true) {
  166. return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
  167. }
  168. }
  169. class TcpStreamSettings extends CommonClass {
  170. constructor(type = 'none', host, path) {
  171. super();
  172. this.type = type;
  173. this.host = host;
  174. this.path = path;
  175. }
  176. static fromJson(json = {}) {
  177. let header = json.header;
  178. if (!header) return new TcpStreamSettings();
  179. if (header.type == 'http' && header.request) {
  180. return new TcpStreamSettings(
  181. header.type,
  182. header.request.headers.Host.join(','),
  183. header.request.path.join(','),
  184. );
  185. }
  186. return new TcpStreamSettings(header.type, '', '');
  187. }
  188. toJson() {
  189. return {
  190. header: {
  191. type: this.type,
  192. request: this.type === 'http' ? {
  193. headers: {
  194. Host: ObjectUtil.isEmpty(this.host) ? [] : this.host.split(',')
  195. },
  196. path: ObjectUtil.isEmpty(this.path) ? ["/"] : this.path.split(',')
  197. } : undefined,
  198. }
  199. };
  200. }
  201. }
  202. class KcpStreamSettings extends CommonClass {
  203. constructor(
  204. mtu = 1350,
  205. tti = 20,
  206. uplinkCapacity = 5,
  207. downlinkCapacity = 20,
  208. cwndMultiplier = 1,
  209. maxSendingWindow = 1350,
  210. ) {
  211. super();
  212. this.mtu = mtu;
  213. this.tti = tti;
  214. this.upCap = uplinkCapacity;
  215. this.downCap = downlinkCapacity;
  216. this.cwndMultiplier = cwndMultiplier;
  217. this.maxSendingWindow = maxSendingWindow;
  218. }
  219. static fromJson(json = {}) {
  220. return new KcpStreamSettings(
  221. json.mtu,
  222. json.tti,
  223. json.uplinkCapacity,
  224. json.downlinkCapacity,
  225. json.cwndMultiplier,
  226. json.maxSendingWindow,
  227. );
  228. }
  229. toJson() {
  230. return {
  231. mtu: this.mtu,
  232. tti: this.tti,
  233. uplinkCapacity: this.upCap,
  234. downlinkCapacity: this.downCap,
  235. cwndMultiplier: this.cwndMultiplier,
  236. maxSendingWindow: this.maxSendingWindow,
  237. };
  238. }
  239. }
  240. class WsStreamSettings extends CommonClass {
  241. constructor(
  242. path = '/',
  243. host = '',
  244. heartbeatPeriod = 0,
  245. ) {
  246. super();
  247. this.path = path;
  248. this.host = host;
  249. this.heartbeatPeriod = heartbeatPeriod;
  250. }
  251. static fromJson(json = {}) {
  252. return new WsStreamSettings(
  253. json.path,
  254. json.host,
  255. json.heartbeatPeriod,
  256. );
  257. }
  258. toJson() {
  259. return {
  260. path: this.path,
  261. host: this.host,
  262. heartbeatPeriod: this.heartbeatPeriod
  263. };
  264. }
  265. }
  266. class GrpcStreamSettings extends CommonClass {
  267. constructor(
  268. serviceName = "",
  269. authority = "",
  270. multiMode = false
  271. ) {
  272. super();
  273. this.serviceName = serviceName;
  274. this.authority = authority;
  275. this.multiMode = multiMode;
  276. }
  277. static fromJson(json = {}) {
  278. return new GrpcStreamSettings(json.serviceName, json.authority, json.multiMode);
  279. }
  280. toJson() {
  281. return {
  282. serviceName: this.serviceName,
  283. authority: this.authority,
  284. multiMode: this.multiMode
  285. }
  286. }
  287. }
  288. class HttpUpgradeStreamSettings extends CommonClass {
  289. constructor(path = '/', host = '') {
  290. super();
  291. this.path = path;
  292. this.host = host;
  293. }
  294. static fromJson(json = {}) {
  295. return new HttpUpgradeStreamSettings(
  296. json.path,
  297. json.host,
  298. );
  299. }
  300. toJson() {
  301. return {
  302. path: this.path,
  303. host: this.host,
  304. };
  305. }
  306. }
  307. class xHTTPStreamSettings extends CommonClass {
  308. constructor(
  309. path = '/',
  310. host = '',
  311. mode = '',
  312. noGRPCHeader = false,
  313. scMinPostsIntervalMs = "30",
  314. xmux = {
  315. maxConcurrency: "16-32",
  316. maxConnections: 0,
  317. cMaxReuseTimes: 0,
  318. hMaxRequestTimes: "600-900",
  319. hMaxReusableSecs: "1800-3000",
  320. hKeepAlivePeriod: 0,
  321. },
  322. ) {
  323. super();
  324. this.path = path;
  325. this.host = host;
  326. this.mode = mode;
  327. this.noGRPCHeader = noGRPCHeader;
  328. this.scMinPostsIntervalMs = scMinPostsIntervalMs;
  329. this.xmux = xmux;
  330. }
  331. static fromJson(json = {}) {
  332. return new xHTTPStreamSettings(
  333. json.path,
  334. json.host,
  335. json.mode,
  336. json.noGRPCHeader,
  337. json.scMinPostsIntervalMs,
  338. json.xmux
  339. );
  340. }
  341. toJson() {
  342. return {
  343. path: this.path,
  344. host: this.host,
  345. mode: this.mode,
  346. noGRPCHeader: this.noGRPCHeader,
  347. scMinPostsIntervalMs: this.scMinPostsIntervalMs,
  348. xmux: {
  349. maxConcurrency: this.xmux.maxConcurrency,
  350. maxConnections: this.xmux.maxConnections,
  351. cMaxReuseTimes: this.xmux.cMaxReuseTimes,
  352. hMaxRequestTimes: this.xmux.hMaxRequestTimes,
  353. hMaxReusableSecs: this.xmux.hMaxReusableSecs,
  354. hKeepAlivePeriod: this.xmux.hKeepAlivePeriod,
  355. },
  356. };
  357. }
  358. }
  359. class TlsStreamSettings extends CommonClass {
  360. constructor(
  361. serverName = '',
  362. alpn = [],
  363. fingerprint = '',
  364. echConfigList = '',
  365. verifyPeerCertByName = '',
  366. pinnedPeerCertSha256 = '',
  367. ) {
  368. super();
  369. this.serverName = serverName;
  370. this.alpn = alpn;
  371. this.fingerprint = fingerprint;
  372. this.echConfigList = echConfigList;
  373. this.verifyPeerCertByName = verifyPeerCertByName;
  374. this.pinnedPeerCertSha256 = pinnedPeerCertSha256;
  375. }
  376. static fromJson(json = {}) {
  377. return new TlsStreamSettings(
  378. json.serverName,
  379. json.alpn,
  380. json.fingerprint,
  381. json.echConfigList,
  382. json.verifyPeerCertByName,
  383. json.pinnedPeerCertSha256,
  384. );
  385. }
  386. toJson() {
  387. return {
  388. serverName: this.serverName,
  389. alpn: this.alpn,
  390. fingerprint: this.fingerprint,
  391. echConfigList: this.echConfigList,
  392. verifyPeerCertByName: this.verifyPeerCertByName,
  393. pinnedPeerCertSha256: this.pinnedPeerCertSha256
  394. };
  395. }
  396. }
  397. class RealityStreamSettings extends CommonClass {
  398. constructor(
  399. publicKey = '',
  400. fingerprint = '',
  401. serverName = '',
  402. shortId = '',
  403. spiderX = '',
  404. mldsa65Verify = ''
  405. ) {
  406. super();
  407. this.publicKey = publicKey;
  408. this.fingerprint = fingerprint;
  409. this.serverName = serverName;
  410. this.shortId = shortId
  411. this.spiderX = spiderX;
  412. this.mldsa65Verify = mldsa65Verify;
  413. }
  414. static fromJson(json = {}) {
  415. return new RealityStreamSettings(
  416. json.publicKey,
  417. json.fingerprint,
  418. json.serverName,
  419. json.shortId,
  420. json.spiderX,
  421. json.mldsa65Verify
  422. );
  423. }
  424. toJson() {
  425. return {
  426. publicKey: this.publicKey,
  427. fingerprint: this.fingerprint,
  428. serverName: this.serverName,
  429. shortId: this.shortId,
  430. spiderX: this.spiderX,
  431. mldsa65Verify: this.mldsa65Verify
  432. };
  433. }
  434. };
  435. class HysteriaStreamSettings extends CommonClass {
  436. constructor(
  437. version = 2,
  438. auth = '',
  439. congestion = '',
  440. up = '0',
  441. down = '0',
  442. udphopPort = '',
  443. udphopIntervalMin = 30,
  444. udphopIntervalMax = 30,
  445. initStreamReceiveWindow = 8388608,
  446. maxStreamReceiveWindow = 8388608,
  447. initConnectionReceiveWindow = 20971520,
  448. maxConnectionReceiveWindow = 20971520,
  449. maxIdleTimeout = 30,
  450. keepAlivePeriod = 0,
  451. disablePathMTUDiscovery = false
  452. ) {
  453. super();
  454. this.version = version;
  455. this.auth = auth;
  456. this.congestion = congestion;
  457. this.up = up;
  458. this.down = down;
  459. this.udphopPort = udphopPort;
  460. this.udphopIntervalMin = udphopIntervalMin;
  461. this.udphopIntervalMax = udphopIntervalMax;
  462. this.initStreamReceiveWindow = initStreamReceiveWindow;
  463. this.maxStreamReceiveWindow = maxStreamReceiveWindow;
  464. this.initConnectionReceiveWindow = initConnectionReceiveWindow;
  465. this.maxConnectionReceiveWindow = maxConnectionReceiveWindow;
  466. this.maxIdleTimeout = maxIdleTimeout;
  467. this.keepAlivePeriod = keepAlivePeriod;
  468. this.disablePathMTUDiscovery = disablePathMTUDiscovery;
  469. }
  470. static fromJson(json = {}) {
  471. let udphopPort = '';
  472. let udphopIntervalMin = 30;
  473. let udphopIntervalMax = 30;
  474. if (json.udphop) {
  475. udphopPort = json.udphop.port || '';
  476. // Backward compatibility: if old 'interval' exists, use it for both min/max
  477. if (json.udphop.interval !== undefined) {
  478. udphopIntervalMin = json.udphop.interval;
  479. udphopIntervalMax = json.udphop.interval;
  480. } else {
  481. udphopIntervalMin = json.udphop.intervalMin || 30;
  482. udphopIntervalMax = json.udphop.intervalMax || 30;
  483. }
  484. }
  485. return new HysteriaStreamSettings(
  486. json.version,
  487. json.auth,
  488. json.congestion,
  489. json.up,
  490. json.down,
  491. udphopPort,
  492. udphopIntervalMin,
  493. udphopIntervalMax,
  494. json.initStreamReceiveWindow,
  495. json.maxStreamReceiveWindow,
  496. json.initConnectionReceiveWindow,
  497. json.maxConnectionReceiveWindow,
  498. json.maxIdleTimeout,
  499. json.keepAlivePeriod,
  500. json.disablePathMTUDiscovery
  501. );
  502. }
  503. toJson() {
  504. const result = {
  505. version: this.version,
  506. auth: this.auth,
  507. congestion: this.congestion,
  508. up: this.up,
  509. down: this.down,
  510. initStreamReceiveWindow: this.initStreamReceiveWindow,
  511. maxStreamReceiveWindow: this.maxStreamReceiveWindow,
  512. initConnectionReceiveWindow: this.initConnectionReceiveWindow,
  513. maxConnectionReceiveWindow: this.maxConnectionReceiveWindow,
  514. maxIdleTimeout: this.maxIdleTimeout,
  515. keepAlivePeriod: this.keepAlivePeriod,
  516. disablePathMTUDiscovery: this.disablePathMTUDiscovery
  517. };
  518. if (this.udphopPort) {
  519. result.udphop = {
  520. port: this.udphopPort,
  521. intervalMin: this.udphopIntervalMin,
  522. intervalMax: this.udphopIntervalMax
  523. };
  524. }
  525. return result;
  526. }
  527. };
  528. class SockoptStreamSettings extends CommonClass {
  529. constructor(
  530. dialerProxy = "",
  531. tcpFastOpen = false,
  532. tcpKeepAliveInterval = 0,
  533. tcpMptcp = false,
  534. penetrate = false,
  535. addressPortStrategy = Address_Port_Strategy.NONE,
  536. trustedXForwardedFor = [],
  537. ) {
  538. super();
  539. this.dialerProxy = dialerProxy;
  540. this.tcpFastOpen = tcpFastOpen;
  541. this.tcpKeepAliveInterval = tcpKeepAliveInterval;
  542. this.tcpMptcp = tcpMptcp;
  543. this.penetrate = penetrate;
  544. this.addressPortStrategy = addressPortStrategy;
  545. this.trustedXForwardedFor = trustedXForwardedFor;
  546. }
  547. static fromJson(json = {}) {
  548. if (Object.keys(json).length === 0) return undefined;
  549. return new SockoptStreamSettings(
  550. json.dialerProxy,
  551. json.tcpFastOpen,
  552. json.tcpKeepAliveInterval,
  553. json.tcpMptcp,
  554. json.penetrate,
  555. json.addressPortStrategy,
  556. json.trustedXForwardedFor || []
  557. );
  558. }
  559. toJson() {
  560. const result = {
  561. dialerProxy: this.dialerProxy,
  562. tcpFastOpen: this.tcpFastOpen,
  563. tcpKeepAliveInterval: this.tcpKeepAliveInterval,
  564. tcpMptcp: this.tcpMptcp,
  565. penetrate: this.penetrate,
  566. addressPortStrategy: this.addressPortStrategy
  567. };
  568. if (this.trustedXForwardedFor && this.trustedXForwardedFor.length > 0) {
  569. result.trustedXForwardedFor = this.trustedXForwardedFor;
  570. }
  571. return result;
  572. }
  573. }
  574. class UdpMask extends CommonClass {
  575. constructor(type = 'salamander', settings = {}) {
  576. super();
  577. this.type = type;
  578. this.settings = this._getDefaultSettings(type, settings);
  579. }
  580. _getDefaultSettings(type, settings = {}) {
  581. switch (type) {
  582. case 'salamander':
  583. case 'mkcp-aes128gcm':
  584. return { password: settings.password || '' };
  585. case 'header-dns':
  586. return { domain: settings.domain || '' };
  587. case 'xdns':
  588. return { resolvers: Array.isArray(settings.resolvers) ? settings.resolvers : [] };
  589. case 'xicmp':
  590. return { ip: settings.ip || '', id: settings.id ?? 0 };
  591. case 'mkcp-original':
  592. case 'header-dtls':
  593. case 'header-srtp':
  594. case 'header-utp':
  595. case 'header-wechat':
  596. case 'header-wireguard':
  597. return {}; // No settings needed
  598. case 'header-custom':
  599. return {
  600. client: Array.isArray(settings.client) ? settings.client : [],
  601. server: Array.isArray(settings.server) ? settings.server : [],
  602. };
  603. case 'noise':
  604. return {
  605. reset: settings.reset ?? 0,
  606. noise: Array.isArray(settings.noise) ? settings.noise : [],
  607. };
  608. case 'sudoku':
  609. return {
  610. ascii: settings.ascii || '',
  611. customTable: settings.customTable || '',
  612. customTables: Array.isArray(settings.customTables) ? settings.customTables : [],
  613. paddingMin: settings.paddingMin ?? 0,
  614. paddingMax: settings.paddingMax ?? 0
  615. };
  616. default:
  617. return settings;
  618. }
  619. }
  620. static fromJson(json = {}) {
  621. return new UdpMask(
  622. json.type || 'salamander',
  623. json.settings || {}
  624. );
  625. }
  626. toJson() {
  627. const cleanItem = item => {
  628. const out = { ...item };
  629. if (out.type === 'array') {
  630. delete out.packet;
  631. } else {
  632. delete out.rand;
  633. delete out.randRange;
  634. }
  635. return out;
  636. };
  637. let settings = this.settings;
  638. if (this.type === 'noise' && settings && Array.isArray(settings.noise)) {
  639. settings = { ...settings, noise: settings.noise.map(cleanItem) };
  640. } else if (this.type === 'header-custom' && settings) {
  641. settings = {
  642. ...settings,
  643. client: Array.isArray(settings.client) ? settings.client.map(cleanItem) : settings.client,
  644. server: Array.isArray(settings.server) ? settings.server.map(cleanItem) : settings.server,
  645. };
  646. }
  647. return {
  648. type: this.type,
  649. settings: (settings && Object.keys(settings).length > 0) ? settings : undefined
  650. };
  651. }
  652. }
  653. class TcpMask extends CommonClass {
  654. constructor(type = 'fragment', settings = {}) {
  655. super();
  656. this.type = type;
  657. this.settings = this._getDefaultSettings(type, settings);
  658. }
  659. _getDefaultSettings(type, settings = {}) {
  660. switch (type) {
  661. case 'fragment':
  662. return {
  663. packets: settings.packets ?? 'tlshello',
  664. length: settings.length ?? '',
  665. delay: settings.delay ?? '',
  666. maxSplit: settings.maxSplit ?? '',
  667. };
  668. case 'sudoku':
  669. return {
  670. password: settings.password ?? '',
  671. ascii: settings.ascii ?? '',
  672. customTable: settings.customTable ?? '',
  673. customTables: Array.isArray(settings.customTables) ? settings.customTables : [],
  674. paddingMin: settings.paddingMin ?? 0,
  675. paddingMax: settings.paddingMax ?? 0,
  676. };
  677. case 'header-custom':
  678. return {
  679. clients: Array.isArray(settings.clients) ? settings.clients : [],
  680. servers: Array.isArray(settings.servers) ? settings.servers : [],
  681. };
  682. default:
  683. return settings;
  684. }
  685. }
  686. static fromJson(json = {}) {
  687. return new TcpMask(
  688. json.type || 'fragment',
  689. json.settings || {}
  690. );
  691. }
  692. toJson() {
  693. const cleanItem = item => {
  694. const out = { ...item };
  695. if (out.type === 'array') {
  696. delete out.packet;
  697. } else {
  698. delete out.rand;
  699. delete out.randRange;
  700. }
  701. return out;
  702. };
  703. let settings = this.settings;
  704. if (this.type === 'header-custom' && settings) {
  705. const cleanGroup = group => Array.isArray(group) ? group.map(cleanItem) : group;
  706. settings = {
  707. ...settings,
  708. clients: Array.isArray(settings.clients) ? settings.clients.map(cleanGroup) : settings.clients,
  709. servers: Array.isArray(settings.servers) ? settings.servers.map(cleanGroup) : settings.servers,
  710. };
  711. }
  712. return {
  713. type: this.type,
  714. settings: (settings && Object.keys(settings).length > 0) ? settings : undefined
  715. };
  716. }
  717. }
  718. class QuicParams extends CommonClass {
  719. constructor(
  720. congestion = 'bbr',
  721. debug = false,
  722. brutalUp = '',
  723. brutalDown = '',
  724. udpHop = undefined,
  725. ) {
  726. super();
  727. this.congestion = congestion;
  728. this.debug = debug;
  729. this.brutalUp = brutalUp;
  730. this.brutalDown = brutalDown;
  731. this.udpHop = udpHop;
  732. }
  733. get hasUdpHop() {
  734. return this.udpHop != null;
  735. }
  736. set hasUdpHop(value) {
  737. this.udpHop = value ? (this.udpHop || { ports: '20000-50000', interval: '5-10' }) : undefined;
  738. }
  739. static fromJson(json = {}) {
  740. if (!json || Object.keys(json).length === 0) return undefined;
  741. return new QuicParams(
  742. json.congestion,
  743. json.debug,
  744. json.brutalUp,
  745. json.brutalDown,
  746. json.udpHop ? { ports: json.udpHop.ports, interval: json.udpHop.interval } : undefined,
  747. );
  748. }
  749. toJson() {
  750. const result = { congestion: this.congestion };
  751. if (this.debug) result.debug = this.debug;
  752. if (this.brutalUp) result.brutalUp = this.brutalUp;
  753. if (this.brutalDown) result.brutalDown = this.brutalDown;
  754. if (this.udpHop) result.udpHop = { ports: this.udpHop.ports, interval: this.udpHop.interval };
  755. return result;
  756. }
  757. }
  758. class FinalMaskStreamSettings extends CommonClass {
  759. constructor(tcp = [], udp = [], quicParams = undefined) {
  760. super();
  761. this.tcp = Array.isArray(tcp) ? tcp.map(t => t instanceof TcpMask ? t : new TcpMask(t.type, t.settings)) : [];
  762. this.udp = Array.isArray(udp) ? udp.map(u => new UdpMask(u.type, u.settings)) : [new UdpMask(udp.type, udp.settings)];
  763. this.quicParams = quicParams instanceof QuicParams ? quicParams : (quicParams ? QuicParams.fromJson(quicParams) : undefined);
  764. }
  765. get enableQuicParams() {
  766. return this.quicParams != null;
  767. }
  768. set enableQuicParams(value) {
  769. this.quicParams = value ? (this.quicParams || new QuicParams()) : undefined;
  770. }
  771. static fromJson(json = {}) {
  772. return new FinalMaskStreamSettings(
  773. json.tcp || [],
  774. json.udp || [],
  775. json.quicParams ? QuicParams.fromJson(json.quicParams) : undefined,
  776. );
  777. }
  778. toJson() {
  779. const result = {};
  780. if (this.tcp && this.tcp.length > 0) {
  781. result.tcp = this.tcp.map(t => t.toJson());
  782. }
  783. if (this.udp && this.udp.length > 0) {
  784. result.udp = this.udp.map(udp => udp.toJson());
  785. }
  786. if (this.quicParams) {
  787. result.quicParams = this.quicParams.toJson();
  788. }
  789. return result;
  790. }
  791. }
  792. class StreamSettings extends CommonClass {
  793. constructor(
  794. network = 'tcp',
  795. security = 'none',
  796. tlsSettings = new TlsStreamSettings(),
  797. realitySettings = new RealityStreamSettings(),
  798. tcpSettings = new TcpStreamSettings(),
  799. kcpSettings = new KcpStreamSettings(),
  800. wsSettings = new WsStreamSettings(),
  801. grpcSettings = new GrpcStreamSettings(),
  802. httpupgradeSettings = new HttpUpgradeStreamSettings(),
  803. xhttpSettings = new xHTTPStreamSettings(),
  804. hysteriaSettings = new HysteriaStreamSettings(),
  805. finalmask = new FinalMaskStreamSettings(),
  806. sockopt = undefined,
  807. ) {
  808. super();
  809. this.network = network;
  810. this.security = security;
  811. this.tls = tlsSettings;
  812. this.reality = realitySettings;
  813. this.tcp = tcpSettings;
  814. this.kcp = kcpSettings;
  815. this.ws = wsSettings;
  816. this.grpc = grpcSettings;
  817. this.httpupgrade = httpupgradeSettings;
  818. this.xhttp = xhttpSettings;
  819. this.hysteria = hysteriaSettings;
  820. this.finalmask = finalmask;
  821. this.sockopt = sockopt;
  822. }
  823. addTcpMask(type = 'fragment') {
  824. this.finalmask.tcp.push(new TcpMask(type));
  825. }
  826. delTcpMask(index) {
  827. if (this.finalmask.tcp) {
  828. this.finalmask.tcp.splice(index, 1);
  829. }
  830. }
  831. addUdpMask(type = 'salamander') {
  832. this.finalmask.udp.push(new UdpMask(type));
  833. }
  834. delUdpMask(index) {
  835. if (this.finalmask.udp) {
  836. this.finalmask.udp.splice(index, 1);
  837. }
  838. }
  839. get hasFinalMask() {
  840. const hasTcp = this.finalmask.tcp && this.finalmask.tcp.length > 0;
  841. const hasUdp = this.finalmask.udp && this.finalmask.udp.length > 0;
  842. const hasQuicParams = this.finalmask.quicParams != null;
  843. return hasTcp || hasUdp || hasQuicParams;
  844. }
  845. get isTls() {
  846. return this.security === 'tls';
  847. }
  848. get isReality() {
  849. return this.security === "reality";
  850. }
  851. get sockoptSwitch() {
  852. return this.sockopt != undefined;
  853. }
  854. set sockoptSwitch(value) {
  855. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  856. }
  857. static fromJson(json = {}) {
  858. return new StreamSettings(
  859. json.network,
  860. json.security,
  861. TlsStreamSettings.fromJson(json.tlsSettings),
  862. RealityStreamSettings.fromJson(json.realitySettings),
  863. TcpStreamSettings.fromJson(json.tcpSettings),
  864. KcpStreamSettings.fromJson(json.kcpSettings),
  865. WsStreamSettings.fromJson(json.wsSettings),
  866. GrpcStreamSettings.fromJson(json.grpcSettings),
  867. HttpUpgradeStreamSettings.fromJson(json.httpupgradeSettings),
  868. xHTTPStreamSettings.fromJson(json.xhttpSettings),
  869. HysteriaStreamSettings.fromJson(json.hysteriaSettings),
  870. FinalMaskStreamSettings.fromJson(json.finalmask),
  871. SockoptStreamSettings.fromJson(json.sockopt),
  872. );
  873. }
  874. toJson() {
  875. const network = this.network;
  876. return {
  877. network: network,
  878. security: this.security,
  879. tlsSettings: this.security == 'tls' ? this.tls.toJson() : undefined,
  880. realitySettings: this.security == 'reality' ? this.reality.toJson() : undefined,
  881. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  882. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  883. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  884. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  885. httpupgradeSettings: network === 'httpupgrade' ? this.httpupgrade.toJson() : undefined,
  886. xhttpSettings: network === 'xhttp' ? this.xhttp.toJson() : undefined,
  887. hysteriaSettings: network === 'hysteria' ? this.hysteria.toJson() : undefined,
  888. finalmask: this.hasFinalMask ? this.finalmask.toJson() : undefined,
  889. sockopt: this.sockopt != undefined ? this.sockopt.toJson() : undefined,
  890. };
  891. }
  892. }
  893. class Mux extends CommonClass {
  894. constructor(enabled = false, concurrency = 8, xudpConcurrency = 16, xudpProxyUDP443 = "reject") {
  895. super();
  896. this.enabled = enabled;
  897. this.concurrency = concurrency;
  898. this.xudpConcurrency = xudpConcurrency;
  899. this.xudpProxyUDP443 = xudpProxyUDP443;
  900. }
  901. static fromJson(json = {}) {
  902. if (Object.keys(json).length === 0) return undefined;
  903. return new Mux(
  904. json.enabled,
  905. json.concurrency,
  906. json.xudpConcurrency,
  907. json.xudpProxyUDP443,
  908. );
  909. }
  910. toJson() {
  911. return {
  912. enabled: this.enabled,
  913. concurrency: this.concurrency,
  914. xudpConcurrency: this.xudpConcurrency,
  915. xudpProxyUDP443: this.xudpProxyUDP443,
  916. };
  917. }
  918. }
  919. class Outbound extends CommonClass {
  920. constructor(
  921. tag = '',
  922. protocol = Protocols.VLESS,
  923. settings = null,
  924. streamSettings = new StreamSettings(),
  925. sendThrough,
  926. mux = new Mux(),
  927. ) {
  928. super();
  929. this.tag = tag;
  930. this._protocol = protocol;
  931. this.settings = settings == null ? Outbound.Settings.getSettings(protocol) : settings;
  932. this.stream = streamSettings;
  933. this.sendThrough = sendThrough;
  934. this.mux = mux;
  935. }
  936. get protocol() {
  937. return this._protocol;
  938. }
  939. set protocol(protocol) {
  940. this._protocol = protocol;
  941. this.settings = Outbound.Settings.getSettings(protocol);
  942. this.stream = new StreamSettings();
  943. }
  944. canEnableTls() {
  945. if (this.protocol === Protocols.Hysteria) return true;
  946. if (![Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks].includes(this.protocol)) return false;
  947. return ["tcp", "ws", "http", "grpc", "httpupgrade", "xhttp"].includes(this.stream.network);
  948. }
  949. //this is used for xtls-rprx-vision
  950. canEnableTlsFlow() {
  951. if ((this.stream.security != 'none') && (this.stream.network === "tcp")) {
  952. return this.protocol === Protocols.VLESS;
  953. }
  954. return false;
  955. }
  956. // Vision seed applies only when vision flow is selected
  957. canEnableVisionSeed() {
  958. if (!this.canEnableTlsFlow()) return false;
  959. const flow = this.settings?.flow;
  960. return flow === TLS_FLOW_CONTROL.VISION || flow === TLS_FLOW_CONTROL.VISION_UDP443;
  961. }
  962. canEnableReality() {
  963. if (![Protocols.VLESS, Protocols.Trojan].includes(this.protocol)) return false;
  964. return ["tcp", "http", "grpc", "xhttp"].includes(this.stream.network);
  965. }
  966. canEnableStream() {
  967. return [Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks, Protocols.Hysteria].includes(this.protocol);
  968. }
  969. canEnableMux() {
  970. // Disable Mux if flow is set
  971. if (this.settings.flow && this.settings.flow !== '') {
  972. this.mux.enabled = false;
  973. return false;
  974. }
  975. // Disable Mux if network is xhttp
  976. if (this.stream.network === 'xhttp') {
  977. this.mux.enabled = false;
  978. return false;
  979. }
  980. // Allow Mux only for these protocols
  981. return [
  982. Protocols.VMess,
  983. Protocols.VLESS,
  984. Protocols.Trojan,
  985. Protocols.Shadowsocks,
  986. Protocols.HTTP,
  987. Protocols.Socks
  988. ].includes(this.protocol);
  989. }
  990. hasServers() {
  991. return [Protocols.Trojan, Protocols.Shadowsocks, Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  992. }
  993. hasAddressPort() {
  994. return [
  995. Protocols.DNS,
  996. Protocols.VMess,
  997. Protocols.VLESS,
  998. Protocols.Trojan,
  999. Protocols.Shadowsocks,
  1000. Protocols.Socks,
  1001. Protocols.HTTP,
  1002. Protocols.Hysteria
  1003. ].includes(this.protocol);
  1004. }
  1005. hasUsername() {
  1006. return [Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  1007. }
  1008. static fromJson(json = {}) {
  1009. return new Outbound(
  1010. json.tag,
  1011. json.protocol,
  1012. Outbound.Settings.fromJson(json.protocol, json.settings),
  1013. StreamSettings.fromJson(json.streamSettings),
  1014. json.sendThrough,
  1015. Mux.fromJson(json.mux),
  1016. )
  1017. }
  1018. toJson() {
  1019. var stream;
  1020. if (this.canEnableStream()) {
  1021. stream = this.stream.toJson();
  1022. } else {
  1023. if (this.stream?.sockopt)
  1024. stream = { sockopt: this.stream.sockopt.toJson() };
  1025. }
  1026. let settingsOut = this.settings instanceof CommonClass ? this.settings.toJson() : this.settings;
  1027. return {
  1028. protocol: this.protocol,
  1029. settings: settingsOut,
  1030. // Only include tag, streamSettings, sendThrough, mux if present and not empty
  1031. ...(this.tag ? { tag: this.tag } : {}),
  1032. ...(stream ? { streamSettings: stream } : {}),
  1033. ...(this.sendThrough ? { sendThrough: this.sendThrough } : {}),
  1034. ...(this.mux?.enabled ? { mux: this.mux } : {}),
  1035. };
  1036. }
  1037. static fromLink(link) {
  1038. data = link.split('://');
  1039. if (data.length != 2) return null;
  1040. switch (data[0].toLowerCase()) {
  1041. case Protocols.VMess:
  1042. return this.fromVmessLink(JSON.parse(Base64.decode(data[1])));
  1043. case Protocols.VLESS:
  1044. case Protocols.Trojan:
  1045. case 'ss':
  1046. return this.fromParamLink(link);
  1047. case 'hysteria2':
  1048. case Protocols.Hysteria:
  1049. return this.fromHysteriaLink(link);
  1050. default:
  1051. return null;
  1052. }
  1053. }
  1054. static fromVmessLink(json = {}) {
  1055. let stream = new StreamSettings(json.net, json.tls);
  1056. let network = json.net;
  1057. if (network === 'tcp') {
  1058. stream.tcp = new TcpStreamSettings(
  1059. json.type,
  1060. json.host ?? '',
  1061. json.path ?? '');
  1062. } else if (network === 'kcp') {
  1063. stream.kcp = new KcpStreamSettings();
  1064. stream.type = json.type;
  1065. stream.seed = json.path;
  1066. const mtu = Number(json.mtu);
  1067. if (Number.isFinite(mtu) && mtu > 0) stream.kcp.mtu = mtu;
  1068. const tti = Number(json.tti);
  1069. if (Number.isFinite(tti) && tti > 0) stream.kcp.tti = tti;
  1070. } else if (network === 'ws') {
  1071. stream.ws = new WsStreamSettings(json.path, json.host);
  1072. } else if (network === 'grpc') {
  1073. stream.grpc = new GrpcStreamSettings(json.path, json.authority, json.type == 'multi');
  1074. } else if (network === 'httpupgrade') {
  1075. stream.httpupgrade = new HttpUpgradeStreamSettings(json.path, json.host);
  1076. } else if (network === 'xhttp') {
  1077. // xHTTPStreamSettings positional args are (path, host, headers, ..., mode);
  1078. // passing `json.mode` as the 3rd argument used to land in the `headers`
  1079. // slot, dropping the mode on the floor. Build the object and set mode
  1080. // explicitly to avoid that.
  1081. const xh = new xHTTPStreamSettings(json.path, json.host);
  1082. if (json.mode) xh.mode = json.mode;
  1083. stream.xhttp = xh;
  1084. }
  1085. if (json.tls && json.tls == 'tls') {
  1086. stream.tls = new TlsStreamSettings(
  1087. json.sni,
  1088. json.alpn ? json.alpn.split(',') : [],
  1089. json.fp);
  1090. }
  1091. const port = json.port * 1;
  1092. return new Outbound(json.ps, Protocols.VMess, new Outbound.VmessSettings(json.add, port, json.id, json.scy), stream);
  1093. }
  1094. static fromParamLink(link) {
  1095. const url = new URL(link);
  1096. let type = url.searchParams.get('type') ?? 'tcp';
  1097. let security = url.searchParams.get('security') ?? 'none';
  1098. let stream = new StreamSettings(type, security);
  1099. let headerType = url.searchParams.get('headerType') ?? undefined;
  1100. let host = url.searchParams.get('host') ?? undefined;
  1101. let path = url.searchParams.get('path') ?? undefined;
  1102. let seed = url.searchParams.get('seed') ?? path ?? undefined;
  1103. let mode = url.searchParams.get('mode') ?? undefined;
  1104. if (type === 'tcp' || type === 'none') {
  1105. stream.tcp = new TcpStreamSettings(headerType ?? 'none', host, path);
  1106. } else if (type === 'kcp') {
  1107. stream.kcp = new KcpStreamSettings();
  1108. stream.kcp.type = headerType ?? 'none';
  1109. stream.kcp.seed = seed;
  1110. const mtu = Number(url.searchParams.get('mtu'));
  1111. if (Number.isFinite(mtu) && mtu > 0) stream.kcp.mtu = mtu;
  1112. const tti = Number(url.searchParams.get('tti'));
  1113. if (Number.isFinite(tti) && tti > 0) stream.kcp.tti = tti;
  1114. } else if (type === 'ws') {
  1115. stream.ws = new WsStreamSettings(path, host);
  1116. } else if (type === 'grpc') {
  1117. stream.grpc = new GrpcStreamSettings(
  1118. url.searchParams.get('serviceName') ?? '',
  1119. url.searchParams.get('authority') ?? '',
  1120. url.searchParams.get('mode') == 'multi');
  1121. } else if (type === 'httpupgrade') {
  1122. stream.httpupgrade = new HttpUpgradeStreamSettings(path, host);
  1123. } else if (type === 'xhttp') {
  1124. // Same positional bug as in the VMess-JSON branch above:
  1125. // passing `mode` as the 3rd positional arg put it into the
  1126. // `headers` slot. Build explicitly instead.
  1127. const xh = new xHTTPStreamSettings(path, host);
  1128. if (mode) xh.mode = mode;
  1129. const xpb = url.searchParams.get('x_padding_bytes');
  1130. if (xpb) xh.xPaddingBytes = xpb;
  1131. const extraRaw = url.searchParams.get('extra');
  1132. if (extraRaw) {
  1133. try {
  1134. const extra = JSON.parse(extraRaw);
  1135. if (typeof extra.xPaddingBytes === 'string' && extra.xPaddingBytes) xh.xPaddingBytes = extra.xPaddingBytes;
  1136. if (extra.xPaddingObfsMode === true) xh.xPaddingObfsMode = true;
  1137. ["xPaddingKey", "xPaddingHeader", "xPaddingPlacement", "xPaddingMethod"].forEach(k => {
  1138. if (typeof extra[k] === 'string' && extra[k]) xh[k] = extra[k];
  1139. });
  1140. } catch (_) { /* ignore malformed extra */ }
  1141. }
  1142. stream.xhttp = xh;
  1143. }
  1144. if (security == 'tls') {
  1145. let fp = url.searchParams.get('fp') ?? 'none';
  1146. let alpn = url.searchParams.get('alpn');
  1147. let sni = url.searchParams.get('sni') ?? '';
  1148. let ech = url.searchParams.get('ech') ?? '';
  1149. stream.tls = new TlsStreamSettings(sni, alpn ? alpn.split(',') : [], fp, ech);
  1150. }
  1151. if (security == 'reality') {
  1152. let pbk = url.searchParams.get('pbk');
  1153. let fp = url.searchParams.get('fp');
  1154. let sni = url.searchParams.get('sni') ?? '';
  1155. let sid = url.searchParams.get('sid') ?? '';
  1156. let spx = url.searchParams.get('spx') ?? '';
  1157. let pqv = url.searchParams.get('pqv') ?? '';
  1158. stream.reality = new RealityStreamSettings(pbk, fp, sni, sid, spx, pqv);
  1159. }
  1160. const regex = /([^@]+):\/\/([^@]+)@(.+):(\d+)(.*)$/;
  1161. const match = link.match(regex);
  1162. if (!match) return null;
  1163. let [, protocol, userData, address, port,] = match;
  1164. port *= 1;
  1165. if (protocol == 'ss') {
  1166. protocol = 'shadowsocks';
  1167. userData = atob(userData).split(':');
  1168. }
  1169. var settings;
  1170. switch (protocol) {
  1171. case Protocols.VLESS:
  1172. settings = new Outbound.VLESSSettings(address, port, userData, url.searchParams.get('flow') ?? '', url.searchParams.get('encryption') ?? 'none');
  1173. break;
  1174. case Protocols.Trojan:
  1175. settings = new Outbound.TrojanSettings(address, port, userData);
  1176. break;
  1177. case Protocols.Shadowsocks:
  1178. let method = userData.splice(0, 1)[0];
  1179. settings = new Outbound.ShadowsocksSettings(address, port, userData.join(":"), method, true);
  1180. break;
  1181. default:
  1182. return null;
  1183. }
  1184. let remark = decodeURIComponent(url.hash);
  1185. // Remove '#' from url.hash
  1186. remark = remark.length > 0 ? remark.substring(1) : 'out-' + protocol + '-' + port;
  1187. return new Outbound(remark, protocol, settings, stream);
  1188. }
  1189. static fromHysteriaLink(link) {
  1190. // Parse hysteria2://password@address:port[?param1=value1&param2=value2...][#remarks]
  1191. const regex = /^hysteria2?:\/\/([^@]+)@([^:?#]+):(\d+)([^#]*)(#.*)?$/;
  1192. const match = link.match(regex);
  1193. if (!match) return null;
  1194. let [, password, address, port, params, hash] = match;
  1195. port = parseInt(port);
  1196. // Parse URL parameters if present
  1197. let urlParams = new URLSearchParams(params);
  1198. // Create stream settings with hysteria network
  1199. let stream = new StreamSettings('hysteria', 'none');
  1200. // Set hysteria stream settings
  1201. stream.hysteria.auth = password;
  1202. stream.hysteria.congestion = urlParams.get('congestion') ?? '';
  1203. stream.hysteria.up = urlParams.get('up') ?? '0';
  1204. stream.hysteria.down = urlParams.get('down') ?? '0';
  1205. stream.hysteria.udphopPort = urlParams.get('udphopPort') ?? '';
  1206. // Support both old single interval and new min/max range
  1207. if (urlParams.has('udphopInterval')) {
  1208. const interval = parseInt(urlParams.get('udphopInterval'));
  1209. stream.hysteria.udphopIntervalMin = interval;
  1210. stream.hysteria.udphopIntervalMax = interval;
  1211. } else {
  1212. stream.hysteria.udphopIntervalMin = parseInt(urlParams.get('udphopIntervalMin') ?? '30');
  1213. stream.hysteria.udphopIntervalMax = parseInt(urlParams.get('udphopIntervalMax') ?? '30');
  1214. }
  1215. // Optional QUIC parameters
  1216. if (urlParams.has('initStreamReceiveWindow')) {
  1217. stream.hysteria.initStreamReceiveWindow = parseInt(urlParams.get('initStreamReceiveWindow'));
  1218. }
  1219. if (urlParams.has('maxStreamReceiveWindow')) {
  1220. stream.hysteria.maxStreamReceiveWindow = parseInt(urlParams.get('maxStreamReceiveWindow'));
  1221. }
  1222. if (urlParams.has('initConnectionReceiveWindow')) {
  1223. stream.hysteria.initConnectionReceiveWindow = parseInt(urlParams.get('initConnectionReceiveWindow'));
  1224. }
  1225. if (urlParams.has('maxConnectionReceiveWindow')) {
  1226. stream.hysteria.maxConnectionReceiveWindow = parseInt(urlParams.get('maxConnectionReceiveWindow'));
  1227. }
  1228. if (urlParams.has('maxIdleTimeout')) {
  1229. stream.hysteria.maxIdleTimeout = parseInt(urlParams.get('maxIdleTimeout'));
  1230. }
  1231. if (urlParams.has('keepAlivePeriod')) {
  1232. stream.hysteria.keepAlivePeriod = parseInt(urlParams.get('keepAlivePeriod'));
  1233. }
  1234. if (urlParams.has('disablePathMTUDiscovery')) {
  1235. stream.hysteria.disablePathMTUDiscovery = urlParams.get('disablePathMTUDiscovery') === 'true';
  1236. }
  1237. // Create settings
  1238. let settings = new Outbound.HysteriaSettings(address, port, 2);
  1239. // Extract remark from hash
  1240. let remark = hash ? decodeURIComponent(hash.substring(1)) : `out-hysteria-${port}`;
  1241. return new Outbound(remark, Protocols.Hysteria, settings, stream);
  1242. }
  1243. }
  1244. Outbound.Settings = class extends CommonClass {
  1245. constructor(protocol) {
  1246. super();
  1247. this.protocol = protocol;
  1248. }
  1249. static getSettings(protocol) {
  1250. switch (protocol) {
  1251. case Protocols.Freedom: return new Outbound.FreedomSettings();
  1252. case Protocols.Blackhole: return new Outbound.BlackholeSettings();
  1253. case Protocols.DNS: return new Outbound.DNSSettings();
  1254. case Protocols.VMess: return new Outbound.VmessSettings();
  1255. case Protocols.VLESS: return new Outbound.VLESSSettings();
  1256. case Protocols.Trojan: return new Outbound.TrojanSettings();
  1257. case Protocols.Shadowsocks: return new Outbound.ShadowsocksSettings();
  1258. case Protocols.Socks: return new Outbound.SocksSettings();
  1259. case Protocols.HTTP: return new Outbound.HttpSettings();
  1260. case Protocols.Wireguard: return new Outbound.WireguardSettings();
  1261. case Protocols.Hysteria: return new Outbound.HysteriaSettings();
  1262. default: return null;
  1263. }
  1264. }
  1265. static fromJson(protocol, json) {
  1266. switch (protocol) {
  1267. case Protocols.Freedom: return Outbound.FreedomSettings.fromJson(json);
  1268. case Protocols.Blackhole: return Outbound.BlackholeSettings.fromJson(json);
  1269. case Protocols.DNS: return Outbound.DNSSettings.fromJson(json);
  1270. case Protocols.VMess: return Outbound.VmessSettings.fromJson(json);
  1271. case Protocols.VLESS: return Outbound.VLESSSettings.fromJson(json);
  1272. case Protocols.Trojan: return Outbound.TrojanSettings.fromJson(json);
  1273. case Protocols.Shadowsocks: return Outbound.ShadowsocksSettings.fromJson(json);
  1274. case Protocols.Socks: return Outbound.SocksSettings.fromJson(json);
  1275. case Protocols.HTTP: return Outbound.HttpSettings.fromJson(json);
  1276. case Protocols.Wireguard: return Outbound.WireguardSettings.fromJson(json);
  1277. case Protocols.Hysteria: return Outbound.HysteriaSettings.fromJson(json);
  1278. default: return null;
  1279. }
  1280. }
  1281. toJson() {
  1282. return {};
  1283. }
  1284. };
  1285. Outbound.FreedomSettings = class extends CommonClass {
  1286. constructor(
  1287. domainStrategy = '',
  1288. redirect = '',
  1289. fragment = {},
  1290. noises = [],
  1291. ipsBlocked = [],
  1292. ) {
  1293. super();
  1294. this.domainStrategy = domainStrategy;
  1295. this.redirect = redirect;
  1296. this.fragment = fragment || {};
  1297. this.noises = Array.isArray(noises) ? noises : [];
  1298. this.ipsBlocked = Array.isArray(ipsBlocked) ? ipsBlocked : [];
  1299. }
  1300. addNoise() {
  1301. this.noises.push(new Outbound.FreedomSettings.Noise());
  1302. }
  1303. delNoise(index) {
  1304. this.noises.splice(index, 1);
  1305. }
  1306. static fromJson(json = {}) {
  1307. return new Outbound.FreedomSettings(
  1308. json.domainStrategy,
  1309. json.redirect,
  1310. json.fragment ? Outbound.FreedomSettings.Fragment.fromJson(json.fragment) : {},
  1311. json.noises ? json.noises.map(noise => Outbound.FreedomSettings.Noise.fromJson(noise)) : [],
  1312. json.ipsBlocked || [],
  1313. );
  1314. }
  1315. toJson() {
  1316. return {
  1317. domainStrategy: ObjectUtil.isEmpty(this.domainStrategy) ? undefined : this.domainStrategy,
  1318. redirect: ObjectUtil.isEmpty(this.redirect) ? undefined : this.redirect,
  1319. fragment: Object.keys(this.fragment).length === 0 ? undefined : this.fragment,
  1320. noises: this.noises.length === 0 ? undefined : Outbound.FreedomSettings.Noise.toJsonArray(this.noises),
  1321. ipsBlocked: this.ipsBlocked.length === 0 ? undefined : this.ipsBlocked,
  1322. };
  1323. }
  1324. };
  1325. Outbound.FreedomSettings.Fragment = class extends CommonClass {
  1326. constructor(
  1327. packets = '1-3',
  1328. length = '',
  1329. interval = '',
  1330. maxSplit = ''
  1331. ) {
  1332. super();
  1333. this.packets = packets;
  1334. this.length = length;
  1335. this.interval = interval;
  1336. this.maxSplit = maxSplit;
  1337. }
  1338. static fromJson(json = {}) {
  1339. return new Outbound.FreedomSettings.Fragment(
  1340. json.packets,
  1341. json.length,
  1342. json.interval,
  1343. json.maxSplit
  1344. );
  1345. }
  1346. };
  1347. Outbound.FreedomSettings.Noise = class extends CommonClass {
  1348. constructor(
  1349. type = 'rand',
  1350. packet = '10-20',
  1351. delay = '10-16',
  1352. applyTo = 'ip'
  1353. ) {
  1354. super();
  1355. this.type = type;
  1356. this.packet = packet;
  1357. this.delay = delay;
  1358. this.applyTo = applyTo;
  1359. }
  1360. static fromJson(json = {}) {
  1361. return new Outbound.FreedomSettings.Noise(
  1362. json.type,
  1363. json.packet,
  1364. json.delay,
  1365. json.applyTo
  1366. );
  1367. }
  1368. toJson() {
  1369. return {
  1370. type: this.type,
  1371. packet: this.packet,
  1372. delay: this.delay,
  1373. applyTo: this.applyTo
  1374. };
  1375. }
  1376. };
  1377. Outbound.BlackholeSettings = class extends CommonClass {
  1378. constructor(type) {
  1379. super();
  1380. this.type = type;
  1381. }
  1382. static fromJson(json = {}) {
  1383. return new Outbound.BlackholeSettings(
  1384. json.response ? json.response.type : undefined,
  1385. );
  1386. }
  1387. toJson() {
  1388. return {
  1389. response: ObjectUtil.isEmpty(this.type) ? undefined : { type: this.type },
  1390. };
  1391. }
  1392. };
  1393. Outbound.DNSRule = class extends CommonClass {
  1394. constructor(action = 'direct', qtype = '', domain = '') {
  1395. super();
  1396. this.action = action;
  1397. this.qtype = qtype;
  1398. this.domain = domain;
  1399. }
  1400. static fromJson(json = {}) {
  1401. return new Outbound.DNSRule(
  1402. json.action,
  1403. normalizeDNSRuleField(json.qtype),
  1404. normalizeDNSRuleField(json.domain),
  1405. );
  1406. }
  1407. toJson() {
  1408. const rule = {
  1409. action: normalizeDNSRuleAction(this.action),
  1410. };
  1411. const qtype = normalizeDNSRuleField(this.qtype);
  1412. if (!ObjectUtil.isEmpty(qtype)) {
  1413. if (/^\d+$/.test(qtype)) {
  1414. rule.qtype = Number(qtype);
  1415. } else {
  1416. rule.qtype = qtype;
  1417. }
  1418. }
  1419. const domains = normalizeDNSRuleField(this.domain)
  1420. .split(',')
  1421. .map(d => d.trim())
  1422. .filter(d => d.length > 0);
  1423. if (domains.length > 0) {
  1424. rule.domain = domains;
  1425. }
  1426. return rule;
  1427. }
  1428. };
  1429. Outbound.DNSSettings = class extends CommonClass {
  1430. constructor(
  1431. network = 'udp',
  1432. address = '',
  1433. port = 53,
  1434. rules = []
  1435. ) {
  1436. super();
  1437. this.network = network;
  1438. this.address = address;
  1439. this.port = port;
  1440. this.rules = Array.isArray(rules) ? rules.map(rule => rule instanceof Outbound.DNSRule ? rule : Outbound.DNSRule.fromJson(rule)) : [];
  1441. }
  1442. addRule(action = 'direct') {
  1443. this.rules.push(new Outbound.DNSRule(action));
  1444. }
  1445. delRule(index) {
  1446. this.rules.splice(index, 1);
  1447. }
  1448. static fromJson(json = {}) {
  1449. return new Outbound.DNSSettings(
  1450. json.network,
  1451. json.address,
  1452. json.port,
  1453. getDNSRulesFromJson(json),
  1454. );
  1455. }
  1456. toJson() {
  1457. const json = {
  1458. network: this.network,
  1459. address: this.address,
  1460. port: this.port,
  1461. };
  1462. if (this.rules.length > 0) {
  1463. json.rules = Outbound.DNSRule.toJsonArray(this.rules);
  1464. }
  1465. return json;
  1466. }
  1467. };
  1468. Outbound.VmessSettings = class extends CommonClass {
  1469. constructor(address, port, id, security) {
  1470. super();
  1471. this.address = address;
  1472. this.port = port;
  1473. this.id = id;
  1474. this.security = security;
  1475. }
  1476. static fromJson(json = {}) {
  1477. if (!ObjectUtil.isArrEmpty(json.vnext)) {
  1478. const v = json.vnext[0] || {};
  1479. const u = ObjectUtil.isArrEmpty(v.users) ? {} : v.users[0];
  1480. return new Outbound.VmessSettings(
  1481. v.address,
  1482. v.port,
  1483. u.id,
  1484. u.security,
  1485. );
  1486. }
  1487. }
  1488. toJson() {
  1489. return {
  1490. vnext: [{
  1491. address: this.address,
  1492. port: this.port,
  1493. users: [{
  1494. id: this.id,
  1495. security: this.security
  1496. }]
  1497. }]
  1498. };
  1499. }
  1500. };
  1501. Outbound.VLESSSettings = class extends CommonClass {
  1502. constructor(address, port, id, flow, encryption, testpre = 0, testseed = [900, 500, 900, 256]) {
  1503. super();
  1504. this.address = address;
  1505. this.port = port;
  1506. this.id = id;
  1507. this.flow = flow;
  1508. this.encryption = encryption;
  1509. this.testpre = testpre;
  1510. this.testseed = testseed;
  1511. }
  1512. static fromJson(json = {}) {
  1513. if (ObjectUtil.isEmpty(json.address) || ObjectUtil.isEmpty(json.port)) return new Outbound.VLESSSettings();
  1514. return new Outbound.VLESSSettings(
  1515. json.address,
  1516. json.port,
  1517. json.id,
  1518. json.flow,
  1519. json.encryption,
  1520. json.testpre || 0,
  1521. json.testseed && json.testseed.length >= 4 ? json.testseed : [900, 500, 900, 256]
  1522. );
  1523. }
  1524. toJson() {
  1525. const result = {
  1526. address: this.address,
  1527. port: this.port,
  1528. id: this.id,
  1529. flow: this.flow,
  1530. encryption: this.encryption,
  1531. };
  1532. // Only include Vision settings when flow is set
  1533. if (this.flow && this.flow !== '') {
  1534. if (this.testpre > 0) {
  1535. result.testpre = this.testpre;
  1536. }
  1537. if (this.testseed && this.testseed.length >= 4) {
  1538. result.testseed = this.testseed;
  1539. }
  1540. }
  1541. return result;
  1542. }
  1543. };
  1544. Outbound.TrojanSettings = class extends CommonClass {
  1545. constructor(address, port, password) {
  1546. super();
  1547. this.address = address;
  1548. this.port = port;
  1549. this.password = password;
  1550. }
  1551. static fromJson(json = {}) {
  1552. if (ObjectUtil.isArrEmpty(json.servers)) return new Outbound.TrojanSettings();
  1553. return new Outbound.TrojanSettings(
  1554. json.servers[0].address,
  1555. json.servers[0].port,
  1556. json.servers[0].password,
  1557. );
  1558. }
  1559. toJson() {
  1560. return {
  1561. servers: [{
  1562. address: this.address,
  1563. port: this.port,
  1564. password: this.password,
  1565. }],
  1566. };
  1567. }
  1568. };
  1569. Outbound.ShadowsocksSettings = class extends CommonClass {
  1570. constructor(address, port, password, method, uot, UoTVersion) {
  1571. super();
  1572. this.address = address;
  1573. this.port = port;
  1574. this.password = password;
  1575. this.method = method;
  1576. this.uot = uot;
  1577. this.UoTVersion = UoTVersion;
  1578. }
  1579. static fromJson(json = {}) {
  1580. let servers = json.servers;
  1581. if (ObjectUtil.isArrEmpty(servers)) servers = [{}];
  1582. return new Outbound.ShadowsocksSettings(
  1583. servers[0].address,
  1584. servers[0].port,
  1585. servers[0].password,
  1586. servers[0].method,
  1587. servers[0].uot,
  1588. servers[0].UoTVersion,
  1589. );
  1590. }
  1591. toJson() {
  1592. return {
  1593. servers: [{
  1594. address: this.address,
  1595. port: this.port,
  1596. password: this.password,
  1597. method: this.method,
  1598. uot: this.uot,
  1599. UoTVersion: this.UoTVersion,
  1600. }],
  1601. };
  1602. }
  1603. };
  1604. Outbound.SocksSettings = class extends CommonClass {
  1605. constructor(address, port, user, pass) {
  1606. super();
  1607. this.address = address;
  1608. this.port = port;
  1609. this.user = user;
  1610. this.pass = pass;
  1611. }
  1612. static fromJson(json = {}) {
  1613. let servers = json.servers;
  1614. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  1615. return new Outbound.SocksSettings(
  1616. servers[0].address,
  1617. servers[0].port,
  1618. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  1619. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  1620. );
  1621. }
  1622. toJson() {
  1623. return {
  1624. servers: [{
  1625. address: this.address,
  1626. port: this.port,
  1627. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1628. }],
  1629. };
  1630. }
  1631. };
  1632. Outbound.HttpSettings = class extends CommonClass {
  1633. constructor(address, port, user, pass) {
  1634. super();
  1635. this.address = address;
  1636. this.port = port;
  1637. this.user = user;
  1638. this.pass = pass;
  1639. }
  1640. static fromJson(json = {}) {
  1641. let servers = json.servers;
  1642. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  1643. return new Outbound.HttpSettings(
  1644. servers[0].address,
  1645. servers[0].port,
  1646. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  1647. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  1648. );
  1649. }
  1650. toJson() {
  1651. return {
  1652. servers: [{
  1653. address: this.address,
  1654. port: this.port,
  1655. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1656. }],
  1657. };
  1658. }
  1659. };
  1660. Outbound.WireguardSettings = class extends CommonClass {
  1661. constructor(
  1662. mtu = 1420,
  1663. secretKey = '',
  1664. address = [''],
  1665. workers = 2,
  1666. domainStrategy = '',
  1667. reserved = '',
  1668. peers = [new Outbound.WireguardSettings.Peer()],
  1669. noKernelTun = false,
  1670. ) {
  1671. super();
  1672. this.mtu = mtu;
  1673. this.secretKey = secretKey;
  1674. this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  1675. this.address = Array.isArray(address) ? address.join(',') : address;
  1676. this.workers = workers;
  1677. this.domainStrategy = domainStrategy;
  1678. this.reserved = Array.isArray(reserved) ? reserved.join(',') : reserved;
  1679. this.peers = peers;
  1680. this.noKernelTun = noKernelTun;
  1681. }
  1682. addPeer() {
  1683. this.peers.push(new Outbound.WireguardSettings.Peer());
  1684. }
  1685. delPeer(index) {
  1686. this.peers.splice(index, 1);
  1687. }
  1688. static fromJson(json = {}) {
  1689. return new Outbound.WireguardSettings(
  1690. json.mtu,
  1691. json.secretKey,
  1692. json.address,
  1693. json.workers,
  1694. json.domainStrategy,
  1695. json.reserved,
  1696. json.peers.map(peer => Outbound.WireguardSettings.Peer.fromJson(peer)),
  1697. json.noKernelTun,
  1698. );
  1699. }
  1700. toJson() {
  1701. return {
  1702. mtu: this.mtu ?? undefined,
  1703. secretKey: this.secretKey,
  1704. address: this.address ? this.address.split(",") : [],
  1705. workers: this.workers ?? undefined,
  1706. domainStrategy: WireguardDomainStrategy.includes(this.domainStrategy) ? this.domainStrategy : undefined,
  1707. reserved: this.reserved ? this.reserved.split(",").map(Number) : undefined,
  1708. peers: Outbound.WireguardSettings.Peer.toJsonArray(this.peers),
  1709. noKernelTun: this.noKernelTun,
  1710. };
  1711. }
  1712. };
  1713. Outbound.WireguardSettings.Peer = class extends CommonClass {
  1714. constructor(
  1715. publicKey = '',
  1716. psk = '',
  1717. allowedIPs = ['0.0.0.0/0', '::/0'],
  1718. endpoint = '',
  1719. keepAlive = 0
  1720. ) {
  1721. super();
  1722. this.publicKey = publicKey;
  1723. this.psk = psk;
  1724. this.allowedIPs = allowedIPs;
  1725. this.endpoint = endpoint;
  1726. this.keepAlive = keepAlive;
  1727. }
  1728. static fromJson(json = {}) {
  1729. return new Outbound.WireguardSettings.Peer(
  1730. json.publicKey,
  1731. json.preSharedKey,
  1732. json.allowedIPs,
  1733. json.endpoint,
  1734. json.keepAlive
  1735. );
  1736. }
  1737. toJson() {
  1738. return {
  1739. publicKey: this.publicKey,
  1740. preSharedKey: this.psk.length > 0 ? this.psk : undefined,
  1741. allowedIPs: this.allowedIPs ? this.allowedIPs : undefined,
  1742. endpoint: this.endpoint,
  1743. keepAlive: this.keepAlive ?? undefined,
  1744. };
  1745. }
  1746. };
  1747. Outbound.HysteriaSettings = class extends CommonClass {
  1748. constructor(address = '', port = 443, version = 2) {
  1749. super();
  1750. this.address = address;
  1751. this.port = port;
  1752. this.version = version;
  1753. }
  1754. static fromJson(json = {}) {
  1755. if (Object.keys(json).length === 0) return new Outbound.HysteriaSettings();
  1756. return new Outbound.HysteriaSettings(
  1757. json.address,
  1758. json.port,
  1759. json.version
  1760. );
  1761. }
  1762. toJson() {
  1763. return {
  1764. address: this.address,
  1765. port: this.port,
  1766. version: this.version
  1767. };
  1768. }
  1769. };