outbound.js 50 KB

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