outbound.js 49 KB

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