outbound.js 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  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 FinalMask 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 FinalMask(
  554. json.type || 'salamander',
  555. json.settings || {}
  556. );
  557. }
  558. toJson() {
  559. const result = {
  560. type: this.type
  561. };
  562. // Only include settings if they exist and are not empty
  563. if (this.settings && Object.keys(this.settings).length > 0) {
  564. result.settings = this.settings;
  565. }
  566. return result;
  567. }
  568. }
  569. class StreamSettings extends CommonClass {
  570. constructor(
  571. network = 'tcp',
  572. security = 'none',
  573. tlsSettings = new TlsStreamSettings(),
  574. realitySettings = new RealityStreamSettings(),
  575. tcpSettings = new TcpStreamSettings(),
  576. kcpSettings = new KcpStreamSettings(),
  577. wsSettings = new WsStreamSettings(),
  578. grpcSettings = new GrpcStreamSettings(),
  579. httpupgradeSettings = new HttpUpgradeStreamSettings(),
  580. xhttpSettings = new xHTTPStreamSettings(),
  581. hysteriaSettings = new HysteriaStreamSettings(),
  582. finalmask = { udp: [] },
  583. sockopt = undefined,
  584. ) {
  585. super();
  586. this.network = network;
  587. this.security = security;
  588. this.tls = tlsSettings;
  589. this.reality = realitySettings;
  590. this.tcp = tcpSettings;
  591. this.kcp = kcpSettings;
  592. this.ws = wsSettings;
  593. this.grpc = grpcSettings;
  594. this.httpupgrade = httpupgradeSettings;
  595. this.xhttp = xhttpSettings;
  596. this.hysteria = hysteriaSettings;
  597. this.finalmask = finalmask;
  598. this.sockopt = sockopt;
  599. }
  600. addUdpMask(type = 'salamander') {
  601. if (!this.finalmask.udp) {
  602. this.finalmask.udp = [];
  603. }
  604. this.finalmask.udp.push(new FinalMask(type));
  605. }
  606. delUdpMask(index) {
  607. if (this.finalmask.udp) {
  608. this.finalmask.udp.splice(index, 1);
  609. }
  610. }
  611. get isTls() {
  612. return this.security === 'tls';
  613. }
  614. get isReality() {
  615. return this.security === "reality";
  616. }
  617. get sockoptSwitch() {
  618. return this.sockopt != undefined;
  619. }
  620. set sockoptSwitch(value) {
  621. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  622. }
  623. static fromJson(json = {}) {
  624. let finalmask = { udp: [] };
  625. if (json.finalmask) {
  626. if (Array.isArray(json.finalmask)) {
  627. // Legacy format: direct array (backward compatibility)
  628. finalmask.udp = json.finalmask.map(mask => FinalMask.fromJson(mask));
  629. } else if (json.finalmask.udp) {
  630. // New format: object with udp array
  631. finalmask.udp = json.finalmask.udp.map(mask => FinalMask.fromJson(mask));
  632. }
  633. }
  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. 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.finalmask.udp && this.finalmask.udp.length > 0) ? {
  665. udp: this.finalmask.udp.map(mask => mask.toJson())
  666. } : undefined,
  667. sockopt: this.sockopt != undefined ? this.sockopt.toJson() : undefined,
  668. };
  669. }
  670. }
  671. class Mux extends CommonClass {
  672. constructor(enabled = false, concurrency = 8, xudpConcurrency = 16, xudpProxyUDP443 = "reject") {
  673. super();
  674. this.enabled = enabled;
  675. this.concurrency = concurrency;
  676. this.xudpConcurrency = xudpConcurrency;
  677. this.xudpProxyUDP443 = xudpProxyUDP443;
  678. }
  679. static fromJson(json = {}) {
  680. if (Object.keys(json).length === 0) return undefined;
  681. return new Mux(
  682. json.enabled,
  683. json.concurrency,
  684. json.xudpConcurrency,
  685. json.xudpProxyUDP443,
  686. );
  687. }
  688. toJson() {
  689. return {
  690. enabled: this.enabled,
  691. concurrency: this.concurrency,
  692. xudpConcurrency: this.xudpConcurrency,
  693. xudpProxyUDP443: this.xudpProxyUDP443,
  694. };
  695. }
  696. }
  697. class Outbound extends CommonClass {
  698. constructor(
  699. tag = '',
  700. protocol = Protocols.VLESS,
  701. settings = null,
  702. streamSettings = new StreamSettings(),
  703. sendThrough,
  704. mux = new Mux(),
  705. ) {
  706. super();
  707. this.tag = tag;
  708. this._protocol = protocol;
  709. this.settings = settings == null ? Outbound.Settings.getSettings(protocol) : settings;
  710. this.stream = streamSettings;
  711. this.sendThrough = sendThrough;
  712. this.mux = mux;
  713. }
  714. get protocol() {
  715. return this._protocol;
  716. }
  717. set protocol(protocol) {
  718. this._protocol = protocol;
  719. this.settings = Outbound.Settings.getSettings(protocol);
  720. this.stream = new StreamSettings();
  721. }
  722. canEnableTls() {
  723. if (![Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks, Protocols.Hysteria].includes(this.protocol)) return false;
  724. if (this.protocol === Protocols.Hysteria) return this.stream.network === 'hysteria';
  725. return ["tcp", "ws", "http", "grpc", "httpupgrade", "xhttp"].includes(this.stream.network);
  726. }
  727. //this is used for xtls-rprx-vision
  728. canEnableTlsFlow() {
  729. if ((this.stream.security != 'none') && (this.stream.network === "tcp")) {
  730. return this.protocol === Protocols.VLESS;
  731. }
  732. return false;
  733. }
  734. // Vision seed applies only when vision flow is selected
  735. canEnableVisionSeed() {
  736. if (!this.canEnableTlsFlow()) return false;
  737. const flow = this.settings?.flow;
  738. return flow === TLS_FLOW_CONTROL.VISION || flow === TLS_FLOW_CONTROL.VISION_UDP443;
  739. }
  740. canEnableReality() {
  741. if (![Protocols.VLESS, Protocols.Trojan].includes(this.protocol)) return false;
  742. return ["tcp", "http", "grpc", "xhttp"].includes(this.stream.network);
  743. }
  744. canEnableStream() {
  745. return [Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks, Protocols.Hysteria].includes(this.protocol);
  746. }
  747. canEnableMux() {
  748. // Disable Mux if flow is set
  749. if (this.settings.flow && this.settings.flow !== '') {
  750. this.mux.enabled = false;
  751. return false;
  752. }
  753. // Disable Mux if network is xhttp
  754. if (this.stream.network === 'xhttp') {
  755. this.mux.enabled = false;
  756. return false;
  757. }
  758. // Allow Mux only for these protocols
  759. return [
  760. Protocols.VMess,
  761. Protocols.VLESS,
  762. Protocols.Trojan,
  763. Protocols.Shadowsocks,
  764. Protocols.HTTP,
  765. Protocols.Socks
  766. ].includes(this.protocol);
  767. }
  768. hasServers() {
  769. return [Protocols.Trojan, Protocols.Shadowsocks, Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  770. }
  771. hasAddressPort() {
  772. return [
  773. Protocols.DNS,
  774. Protocols.VMess,
  775. Protocols.VLESS,
  776. Protocols.Trojan,
  777. Protocols.Shadowsocks,
  778. Protocols.Socks,
  779. Protocols.HTTP,
  780. Protocols.Hysteria
  781. ].includes(this.protocol);
  782. }
  783. hasUsername() {
  784. return [Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  785. }
  786. static fromJson(json = {}) {
  787. return new Outbound(
  788. json.tag,
  789. json.protocol,
  790. Outbound.Settings.fromJson(json.protocol, json.settings),
  791. StreamSettings.fromJson(json.streamSettings),
  792. json.sendThrough,
  793. Mux.fromJson(json.mux),
  794. )
  795. }
  796. toJson() {
  797. var stream;
  798. if (this.canEnableStream()) {
  799. stream = this.stream.toJson();
  800. } else {
  801. if (this.stream?.sockopt)
  802. stream = { sockopt: this.stream.sockopt.toJson() };
  803. }
  804. let settingsOut = this.settings instanceof CommonClass ? this.settings.toJson() : this.settings;
  805. return {
  806. protocol: this.protocol,
  807. settings: settingsOut,
  808. // Only include tag, streamSettings, sendThrough, mux if present and not empty
  809. ...(this.tag ? { tag: this.tag } : {}),
  810. ...(stream ? { streamSettings: stream } : {}),
  811. ...(this.sendThrough ? { sendThrough: this.sendThrough } : {}),
  812. ...(this.mux?.enabled ? { mux: this.mux } : {}),
  813. };
  814. }
  815. static fromLink(link) {
  816. data = link.split('://');
  817. if (data.length != 2) return null;
  818. switch (data[0].toLowerCase()) {
  819. case Protocols.VMess:
  820. return this.fromVmessLink(JSON.parse(Base64.decode(data[1])));
  821. case Protocols.VLESS:
  822. case Protocols.Trojan:
  823. case 'ss':
  824. return this.fromParamLink(link);
  825. case 'hysteria2':
  826. case Protocols.Hysteria:
  827. return this.fromHysteriaLink(link);
  828. default:
  829. return null;
  830. }
  831. }
  832. static fromVmessLink(json = {}) {
  833. let stream = new StreamSettings(json.net, json.tls);
  834. let network = json.net;
  835. if (network === 'tcp') {
  836. stream.tcp = new TcpStreamSettings(
  837. json.type,
  838. json.host ?? '',
  839. json.path ?? '');
  840. } else if (network === 'kcp') {
  841. stream.kcp = new KcpStreamSettings();
  842. stream.type = json.type;
  843. stream.seed = json.path;
  844. } else if (network === 'ws') {
  845. stream.ws = new WsStreamSettings(json.path, json.host);
  846. } else if (network === 'grpc') {
  847. stream.grpc = new GrpcStreamSettings(json.path, json.authority, json.type == 'multi');
  848. } else if (network === 'httpupgrade') {
  849. stream.httpupgrade = new HttpUpgradeStreamSettings(json.path, json.host);
  850. } else if (network === 'xhttp') {
  851. stream.xhttp = new xHTTPStreamSettings(json.path, json.host, json.mode);
  852. }
  853. if (json.tls && json.tls == 'tls') {
  854. stream.tls = new TlsStreamSettings(
  855. json.sni,
  856. json.alpn ? json.alpn.split(',') : [],
  857. json.fp,
  858. json.allowInsecure);
  859. }
  860. const port = json.port * 1;
  861. return new Outbound(json.ps, Protocols.VMess, new Outbound.VmessSettings(json.add, port, json.id, json.scy), stream);
  862. }
  863. static fromParamLink(link) {
  864. const url = new URL(link);
  865. let type = url.searchParams.get('type') ?? 'tcp';
  866. let security = url.searchParams.get('security') ?? 'none';
  867. let stream = new StreamSettings(type, security);
  868. let headerType = url.searchParams.get('headerType') ?? undefined;
  869. let host = url.searchParams.get('host') ?? undefined;
  870. let path = url.searchParams.get('path') ?? undefined;
  871. let mode = url.searchParams.get('mode') ?? undefined;
  872. if (type === 'tcp' || type === 'none') {
  873. stream.tcp = new TcpStreamSettings(headerType ?? 'none', host, path);
  874. } else if (type === 'kcp') {
  875. stream.kcp = new KcpStreamSettings();
  876. stream.kcp.type = headerType ?? 'none';
  877. stream.kcp.seed = path;
  878. } else if (type === 'ws') {
  879. stream.ws = new WsStreamSettings(path, host);
  880. } else if (type === 'grpc') {
  881. stream.grpc = new GrpcStreamSettings(
  882. url.searchParams.get('serviceName') ?? '',
  883. url.searchParams.get('authority') ?? '',
  884. url.searchParams.get('mode') == 'multi');
  885. } else if (type === 'httpupgrade') {
  886. stream.httpupgrade = new HttpUpgradeStreamSettings(path, host);
  887. } else if (type === 'xhttp') {
  888. stream.xhttp = new xHTTPStreamSettings(path, host, mode);
  889. }
  890. if (security == 'tls') {
  891. let fp = url.searchParams.get('fp') ?? 'none';
  892. let alpn = url.searchParams.get('alpn');
  893. let allowInsecure = url.searchParams.get('allowInsecure');
  894. let sni = url.searchParams.get('sni') ?? '';
  895. let ech = url.searchParams.get('ech') ?? '';
  896. stream.tls = new TlsStreamSettings(sni, alpn ? alpn.split(',') : [], fp, allowInsecure == 1, ech);
  897. }
  898. if (security == 'reality') {
  899. let pbk = url.searchParams.get('pbk');
  900. let fp = url.searchParams.get('fp');
  901. let sni = url.searchParams.get('sni') ?? '';
  902. let sid = url.searchParams.get('sid') ?? '';
  903. let spx = url.searchParams.get('spx') ?? '';
  904. let pqv = url.searchParams.get('pqv') ?? '';
  905. stream.reality = new RealityStreamSettings(pbk, fp, sni, sid, spx, pqv);
  906. }
  907. const regex = /([^@]+):\/\/([^@]+)@(.+):(\d+)(.*)$/;
  908. const match = link.match(regex);
  909. if (!match) return null;
  910. let [, protocol, userData, address, port,] = match;
  911. port *= 1;
  912. if (protocol == 'ss') {
  913. protocol = 'shadowsocks';
  914. userData = atob(userData).split(':');
  915. }
  916. var settings;
  917. switch (protocol) {
  918. case Protocols.VLESS:
  919. settings = new Outbound.VLESSSettings(address, port, userData, url.searchParams.get('flow') ?? '', url.searchParams.get('encryption') ?? 'none');
  920. break;
  921. case Protocols.Trojan:
  922. settings = new Outbound.TrojanSettings(address, port, userData);
  923. break;
  924. case Protocols.Shadowsocks:
  925. let method = userData.splice(0, 1)[0];
  926. settings = new Outbound.ShadowsocksSettings(address, port, userData.join(":"), method, true);
  927. break;
  928. default:
  929. return null;
  930. }
  931. let remark = decodeURIComponent(url.hash);
  932. // Remove '#' from url.hash
  933. remark = remark.length > 0 ? remark.substring(1) : 'out-' + protocol + '-' + port;
  934. return new Outbound(remark, protocol, settings, stream);
  935. }
  936. static fromHysteriaLink(link) {
  937. // Parse hysteria2://password@address:port[?param1=value1&param2=value2...][#remarks]
  938. const regex = /^hysteria2?:\/\/([^@]+)@([^:?#]+):(\d+)([^#]*)(#.*)?$/;
  939. const match = link.match(regex);
  940. if (!match) return null;
  941. let [, password, address, port, params, hash] = match;
  942. port = parseInt(port);
  943. // Parse URL parameters if present
  944. let urlParams = new URLSearchParams(params);
  945. // Create stream settings with hysteria network
  946. let stream = new StreamSettings('hysteria', 'none');
  947. // Set hysteria stream settings
  948. stream.hysteria.auth = password;
  949. stream.hysteria.congestion = urlParams.get('congestion') ?? '';
  950. stream.hysteria.up = urlParams.get('up') ?? '0';
  951. stream.hysteria.down = urlParams.get('down') ?? '0';
  952. stream.hysteria.udphopPort = urlParams.get('udphopPort') ?? '';
  953. // Support both old single interval and new min/max range
  954. if (urlParams.has('udphopInterval')) {
  955. const interval = parseInt(urlParams.get('udphopInterval'));
  956. stream.hysteria.udphopIntervalMin = interval;
  957. stream.hysteria.udphopIntervalMax = interval;
  958. } else {
  959. stream.hysteria.udphopIntervalMin = parseInt(urlParams.get('udphopIntervalMin') ?? '30');
  960. stream.hysteria.udphopIntervalMax = parseInt(urlParams.get('udphopIntervalMax') ?? '30');
  961. }
  962. // Optional QUIC parameters
  963. if (urlParams.has('initStreamReceiveWindow')) {
  964. stream.hysteria.initStreamReceiveWindow = parseInt(urlParams.get('initStreamReceiveWindow'));
  965. }
  966. if (urlParams.has('maxStreamReceiveWindow')) {
  967. stream.hysteria.maxStreamReceiveWindow = parseInt(urlParams.get('maxStreamReceiveWindow'));
  968. }
  969. if (urlParams.has('initConnectionReceiveWindow')) {
  970. stream.hysteria.initConnectionReceiveWindow = parseInt(urlParams.get('initConnectionReceiveWindow'));
  971. }
  972. if (urlParams.has('maxConnectionReceiveWindow')) {
  973. stream.hysteria.maxConnectionReceiveWindow = parseInt(urlParams.get('maxConnectionReceiveWindow'));
  974. }
  975. if (urlParams.has('maxIdleTimeout')) {
  976. stream.hysteria.maxIdleTimeout = parseInt(urlParams.get('maxIdleTimeout'));
  977. }
  978. if (urlParams.has('keepAlivePeriod')) {
  979. stream.hysteria.keepAlivePeriod = parseInt(urlParams.get('keepAlivePeriod'));
  980. }
  981. if (urlParams.has('disablePathMTUDiscovery')) {
  982. stream.hysteria.disablePathMTUDiscovery = urlParams.get('disablePathMTUDiscovery') === 'true';
  983. }
  984. // Create settings
  985. let settings = new Outbound.HysteriaSettings(address, port, 2);
  986. // Extract remark from hash
  987. let remark = hash ? decodeURIComponent(hash.substring(1)) : `out-hysteria-${port}`;
  988. return new Outbound(remark, Protocols.Hysteria, settings, stream);
  989. }
  990. }
  991. Outbound.Settings = class extends CommonClass {
  992. constructor(protocol) {
  993. super();
  994. this.protocol = protocol;
  995. }
  996. static getSettings(protocol) {
  997. switch (protocol) {
  998. case Protocols.Freedom: return new Outbound.FreedomSettings();
  999. case Protocols.Blackhole: return new Outbound.BlackholeSettings();
  1000. case Protocols.DNS: return new Outbound.DNSSettings();
  1001. case Protocols.VMess: return new Outbound.VmessSettings();
  1002. case Protocols.VLESS: return new Outbound.VLESSSettings();
  1003. case Protocols.Trojan: return new Outbound.TrojanSettings();
  1004. case Protocols.Shadowsocks: return new Outbound.ShadowsocksSettings();
  1005. case Protocols.Socks: return new Outbound.SocksSettings();
  1006. case Protocols.HTTP: return new Outbound.HttpSettings();
  1007. case Protocols.Wireguard: return new Outbound.WireguardSettings();
  1008. case Protocols.Hysteria: return new Outbound.HysteriaSettings();
  1009. default: return null;
  1010. }
  1011. }
  1012. static fromJson(protocol, json) {
  1013. switch (protocol) {
  1014. case Protocols.Freedom: return Outbound.FreedomSettings.fromJson(json);
  1015. case Protocols.Blackhole: return Outbound.BlackholeSettings.fromJson(json);
  1016. case Protocols.DNS: return Outbound.DNSSettings.fromJson(json);
  1017. case Protocols.VMess: return Outbound.VmessSettings.fromJson(json);
  1018. case Protocols.VLESS: return Outbound.VLESSSettings.fromJson(json);
  1019. case Protocols.Trojan: return Outbound.TrojanSettings.fromJson(json);
  1020. case Protocols.Shadowsocks: return Outbound.ShadowsocksSettings.fromJson(json);
  1021. case Protocols.Socks: return Outbound.SocksSettings.fromJson(json);
  1022. case Protocols.HTTP: return Outbound.HttpSettings.fromJson(json);
  1023. case Protocols.Wireguard: return Outbound.WireguardSettings.fromJson(json);
  1024. case Protocols.Hysteria: return Outbound.HysteriaSettings.fromJson(json);
  1025. default: return null;
  1026. }
  1027. }
  1028. toJson() {
  1029. return {};
  1030. }
  1031. };
  1032. Outbound.FreedomSettings = class extends CommonClass {
  1033. constructor(
  1034. domainStrategy = '',
  1035. redirect = '',
  1036. fragment = {},
  1037. noises = []
  1038. ) {
  1039. super();
  1040. this.domainStrategy = domainStrategy;
  1041. this.redirect = redirect;
  1042. this.fragment = fragment;
  1043. this.noises = noises;
  1044. }
  1045. addNoise() {
  1046. this.noises.push(new Outbound.FreedomSettings.Noise());
  1047. }
  1048. delNoise(index) {
  1049. this.noises.splice(index, 1);
  1050. }
  1051. static fromJson(json = {}) {
  1052. return new Outbound.FreedomSettings(
  1053. json.domainStrategy,
  1054. json.redirect,
  1055. json.fragment ? Outbound.FreedomSettings.Fragment.fromJson(json.fragment) : undefined,
  1056. json.noises ? json.noises.map(noise => Outbound.FreedomSettings.Noise.fromJson(noise)) : undefined,
  1057. );
  1058. }
  1059. toJson() {
  1060. return {
  1061. domainStrategy: ObjectUtil.isEmpty(this.domainStrategy) ? undefined : this.domainStrategy,
  1062. redirect: ObjectUtil.isEmpty(this.redirect) ? undefined : this.redirect,
  1063. fragment: Object.keys(this.fragment).length === 0 ? undefined : this.fragment,
  1064. noises: this.noises.length === 0 ? undefined : Outbound.FreedomSettings.Noise.toJsonArray(this.noises),
  1065. };
  1066. }
  1067. };
  1068. Outbound.FreedomSettings.Fragment = class extends CommonClass {
  1069. constructor(
  1070. packets = '1-3',
  1071. length = '',
  1072. interval = '',
  1073. maxSplit = ''
  1074. ) {
  1075. super();
  1076. this.packets = packets;
  1077. this.length = length;
  1078. this.interval = interval;
  1079. this.maxSplit = maxSplit;
  1080. }
  1081. static fromJson(json = {}) {
  1082. return new Outbound.FreedomSettings.Fragment(
  1083. json.packets,
  1084. json.length,
  1085. json.interval,
  1086. json.maxSplit
  1087. );
  1088. }
  1089. };
  1090. Outbound.FreedomSettings.Noise = class extends CommonClass {
  1091. constructor(
  1092. type = 'rand',
  1093. packet = '10-20',
  1094. delay = '10-16',
  1095. applyTo = 'ip'
  1096. ) {
  1097. super();
  1098. this.type = type;
  1099. this.packet = packet;
  1100. this.delay = delay;
  1101. this.applyTo = applyTo;
  1102. }
  1103. static fromJson(json = {}) {
  1104. return new Outbound.FreedomSettings.Noise(
  1105. json.type,
  1106. json.packet,
  1107. json.delay,
  1108. json.applyTo
  1109. );
  1110. }
  1111. toJson() {
  1112. return {
  1113. type: this.type,
  1114. packet: this.packet,
  1115. delay: this.delay,
  1116. applyTo: this.applyTo
  1117. };
  1118. }
  1119. };
  1120. Outbound.BlackholeSettings = class extends CommonClass {
  1121. constructor(type) {
  1122. super();
  1123. this.type = type;
  1124. }
  1125. static fromJson(json = {}) {
  1126. return new Outbound.BlackholeSettings(
  1127. json.response ? json.response.type : undefined,
  1128. );
  1129. }
  1130. toJson() {
  1131. return {
  1132. response: ObjectUtil.isEmpty(this.type) ? undefined : { type: this.type },
  1133. };
  1134. }
  1135. };
  1136. Outbound.DNSSettings = class extends CommonClass {
  1137. constructor(
  1138. network = 'udp',
  1139. address = '',
  1140. port = 53,
  1141. nonIPQuery = 'reject',
  1142. blockTypes = []
  1143. ) {
  1144. super();
  1145. this.network = network;
  1146. this.address = address;
  1147. this.port = port;
  1148. this.nonIPQuery = nonIPQuery;
  1149. this.blockTypes = blockTypes;
  1150. }
  1151. static fromJson(json = {}) {
  1152. return new Outbound.DNSSettings(
  1153. json.network,
  1154. json.address,
  1155. json.port,
  1156. json.nonIPQuery,
  1157. json.blockTypes,
  1158. );
  1159. }
  1160. };
  1161. Outbound.VmessSettings = class extends CommonClass {
  1162. constructor(address, port, id, security) {
  1163. super();
  1164. this.address = address;
  1165. this.port = port;
  1166. this.id = id;
  1167. this.security = security;
  1168. }
  1169. static fromJson(json = {}) {
  1170. if (!ObjectUtil.isArrEmpty(json.vnext)) {
  1171. const v = json.vnext[0] || {};
  1172. const u = ObjectUtil.isArrEmpty(v.users) ? {} : v.users[0];
  1173. return new Outbound.VmessSettings(
  1174. v.address,
  1175. v.port,
  1176. u.id,
  1177. u.security,
  1178. );
  1179. }
  1180. }
  1181. toJson() {
  1182. return {
  1183. vnext: [{
  1184. address: this.address,
  1185. port: this.port,
  1186. users: [{
  1187. id: this.id,
  1188. security: this.security
  1189. }]
  1190. }]
  1191. };
  1192. }
  1193. };
  1194. Outbound.VLESSSettings = class extends CommonClass {
  1195. constructor(address, port, id, flow, encryption, testpre = 0, testseed = [900, 500, 900, 256]) {
  1196. super();
  1197. this.address = address;
  1198. this.port = port;
  1199. this.id = id;
  1200. this.flow = flow;
  1201. this.encryption = encryption;
  1202. this.testpre = testpre;
  1203. this.testseed = testseed;
  1204. }
  1205. static fromJson(json = {}) {
  1206. if (ObjectUtil.isEmpty(json.address) || ObjectUtil.isEmpty(json.port)) return new Outbound.VLESSSettings();
  1207. return new Outbound.VLESSSettings(
  1208. json.address,
  1209. json.port,
  1210. json.id,
  1211. json.flow,
  1212. json.encryption,
  1213. json.testpre || 0,
  1214. json.testseed && json.testseed.length >= 4 ? json.testseed : [900, 500, 900, 256]
  1215. );
  1216. }
  1217. toJson() {
  1218. const result = {
  1219. address: this.address,
  1220. port: this.port,
  1221. id: this.id,
  1222. flow: this.flow,
  1223. encryption: this.encryption,
  1224. };
  1225. // Only include Vision settings when flow is set
  1226. if (this.flow && this.flow !== '') {
  1227. if (this.testpre > 0) {
  1228. result.testpre = this.testpre;
  1229. }
  1230. if (this.testseed && this.testseed.length >= 4) {
  1231. result.testseed = this.testseed;
  1232. }
  1233. }
  1234. return result;
  1235. }
  1236. };
  1237. Outbound.TrojanSettings = class extends CommonClass {
  1238. constructor(address, port, password) {
  1239. super();
  1240. this.address = address;
  1241. this.port = port;
  1242. this.password = password;
  1243. }
  1244. static fromJson(json = {}) {
  1245. if (ObjectUtil.isArrEmpty(json.servers)) return new Outbound.TrojanSettings();
  1246. return new Outbound.TrojanSettings(
  1247. json.servers[0].address,
  1248. json.servers[0].port,
  1249. json.servers[0].password,
  1250. );
  1251. }
  1252. toJson() {
  1253. return {
  1254. servers: [{
  1255. address: this.address,
  1256. port: this.port,
  1257. password: this.password,
  1258. }],
  1259. };
  1260. }
  1261. };
  1262. Outbound.ShadowsocksSettings = class extends CommonClass {
  1263. constructor(address, port, password, method, uot, UoTVersion) {
  1264. super();
  1265. this.address = address;
  1266. this.port = port;
  1267. this.password = password;
  1268. this.method = method;
  1269. this.uot = uot;
  1270. this.UoTVersion = UoTVersion;
  1271. }
  1272. static fromJson(json = {}) {
  1273. let servers = json.servers;
  1274. if (ObjectUtil.isArrEmpty(servers)) servers = [{}];
  1275. return new Outbound.ShadowsocksSettings(
  1276. servers[0].address,
  1277. servers[0].port,
  1278. servers[0].password,
  1279. servers[0].method,
  1280. servers[0].uot,
  1281. servers[0].UoTVersion,
  1282. );
  1283. }
  1284. toJson() {
  1285. return {
  1286. servers: [{
  1287. address: this.address,
  1288. port: this.port,
  1289. password: this.password,
  1290. method: this.method,
  1291. uot: this.uot,
  1292. UoTVersion: this.UoTVersion,
  1293. }],
  1294. };
  1295. }
  1296. };
  1297. Outbound.SocksSettings = class extends CommonClass {
  1298. constructor(address, port, user, pass) {
  1299. super();
  1300. this.address = address;
  1301. this.port = port;
  1302. this.user = user;
  1303. this.pass = pass;
  1304. }
  1305. static fromJson(json = {}) {
  1306. let servers = json.servers;
  1307. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  1308. return new Outbound.SocksSettings(
  1309. servers[0].address,
  1310. servers[0].port,
  1311. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  1312. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  1313. );
  1314. }
  1315. toJson() {
  1316. return {
  1317. servers: [{
  1318. address: this.address,
  1319. port: this.port,
  1320. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1321. }],
  1322. };
  1323. }
  1324. };
  1325. Outbound.HttpSettings = class extends CommonClass {
  1326. constructor(address, port, user, pass) {
  1327. super();
  1328. this.address = address;
  1329. this.port = port;
  1330. this.user = user;
  1331. this.pass = pass;
  1332. }
  1333. static fromJson(json = {}) {
  1334. let servers = json.servers;
  1335. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  1336. return new Outbound.HttpSettings(
  1337. servers[0].address,
  1338. servers[0].port,
  1339. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  1340. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  1341. );
  1342. }
  1343. toJson() {
  1344. return {
  1345. servers: [{
  1346. address: this.address,
  1347. port: this.port,
  1348. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1349. }],
  1350. };
  1351. }
  1352. };
  1353. Outbound.WireguardSettings = class extends CommonClass {
  1354. constructor(
  1355. mtu = 1420,
  1356. secretKey = '',
  1357. address = [''],
  1358. workers = 2,
  1359. domainStrategy = '',
  1360. reserved = '',
  1361. peers = [new Outbound.WireguardSettings.Peer()],
  1362. noKernelTun = false,
  1363. ) {
  1364. super();
  1365. this.mtu = mtu;
  1366. this.secretKey = secretKey;
  1367. this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  1368. this.address = Array.isArray(address) ? address.join(',') : address;
  1369. this.workers = workers;
  1370. this.domainStrategy = domainStrategy;
  1371. this.reserved = Array.isArray(reserved) ? reserved.join(',') : reserved;
  1372. this.peers = peers;
  1373. this.noKernelTun = noKernelTun;
  1374. }
  1375. addPeer() {
  1376. this.peers.push(new Outbound.WireguardSettings.Peer());
  1377. }
  1378. delPeer(index) {
  1379. this.peers.splice(index, 1);
  1380. }
  1381. static fromJson(json = {}) {
  1382. return new Outbound.WireguardSettings(
  1383. json.mtu,
  1384. json.secretKey,
  1385. json.address,
  1386. json.workers,
  1387. json.domainStrategy,
  1388. json.reserved,
  1389. json.peers.map(peer => Outbound.WireguardSettings.Peer.fromJson(peer)),
  1390. json.noKernelTun,
  1391. );
  1392. }
  1393. toJson() {
  1394. return {
  1395. mtu: this.mtu ?? undefined,
  1396. secretKey: this.secretKey,
  1397. address: this.address ? this.address.split(",") : [],
  1398. workers: this.workers ?? undefined,
  1399. domainStrategy: WireguardDomainStrategy.includes(this.domainStrategy) ? this.domainStrategy : undefined,
  1400. reserved: this.reserved ? this.reserved.split(",").map(Number) : undefined,
  1401. peers: Outbound.WireguardSettings.Peer.toJsonArray(this.peers),
  1402. noKernelTun: this.noKernelTun,
  1403. };
  1404. }
  1405. };
  1406. Outbound.WireguardSettings.Peer = class extends CommonClass {
  1407. constructor(
  1408. publicKey = '',
  1409. psk = '',
  1410. allowedIPs = ['0.0.0.0/0', '::/0'],
  1411. endpoint = '',
  1412. keepAlive = 0
  1413. ) {
  1414. super();
  1415. this.publicKey = publicKey;
  1416. this.psk = psk;
  1417. this.allowedIPs = allowedIPs;
  1418. this.endpoint = endpoint;
  1419. this.keepAlive = keepAlive;
  1420. }
  1421. static fromJson(json = {}) {
  1422. return new Outbound.WireguardSettings.Peer(
  1423. json.publicKey,
  1424. json.preSharedKey,
  1425. json.allowedIPs,
  1426. json.endpoint,
  1427. json.keepAlive
  1428. );
  1429. }
  1430. toJson() {
  1431. return {
  1432. publicKey: this.publicKey,
  1433. preSharedKey: this.psk.length > 0 ? this.psk : undefined,
  1434. allowedIPs: this.allowedIPs ? this.allowedIPs : undefined,
  1435. endpoint: this.endpoint,
  1436. keepAlive: this.keepAlive ?? undefined,
  1437. };
  1438. }
  1439. };
  1440. Outbound.HysteriaSettings = class extends CommonClass {
  1441. constructor(address = '', port = 443, version = 2) {
  1442. super();
  1443. this.address = address;
  1444. this.port = port;
  1445. this.version = version;
  1446. }
  1447. static fromJson(json = {}) {
  1448. if (Object.keys(json).length === 0) return new Outbound.HysteriaSettings();
  1449. return new Outbound.HysteriaSettings(
  1450. json.address,
  1451. json.port,
  1452. json.version
  1453. );
  1454. }
  1455. toJson() {
  1456. return {
  1457. address: this.address,
  1458. port: this.port,
  1459. version: this.version
  1460. };
  1461. }
  1462. };