outbound.js 48 KB

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