outbound.js 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  1. const Protocols = {
  2. Freedom: "freedom",
  3. Blackhole: "blackhole",
  4. DNS: "dns",
  5. VMess: "vmess",
  6. VLESS: "vless",
  7. Trojan: "trojan",
  8. Shadowsocks: "shadowsocks",
  9. Socks: "socks",
  10. HTTP: "http",
  11. Wireguard: "wireguard",
  12. Hysteria: "hysteria"
  13. };
  14. const SSMethods = {
  15. AES_256_GCM: 'aes-256-gcm',
  16. AES_128_GCM: 'aes-128-gcm',
  17. CHACHA20_POLY1305: 'chacha20-poly1305',
  18. CHACHA20_IETF_POLY1305: 'chacha20-ietf-poly1305',
  19. XCHACHA20_POLY1305: 'xchacha20-poly1305',
  20. XCHACHA20_IETF_POLY1305: 'xchacha20-ietf-poly1305',
  21. BLAKE3_AES_128_GCM: '2022-blake3-aes-128-gcm',
  22. BLAKE3_AES_256_GCM: '2022-blake3-aes-256-gcm',
  23. BLAKE3_CHACHA20_POLY1305: '2022-blake3-chacha20-poly1305',
  24. };
  25. const TLS_FLOW_CONTROL = {
  26. VISION: "xtls-rprx-vision",
  27. VISION_UDP443: "xtls-rprx-vision-udp443",
  28. };
  29. const UTLS_FINGERPRINT = {
  30. UTLS_CHROME: "chrome",
  31. UTLS_FIREFOX: "firefox",
  32. UTLS_SAFARI: "safari",
  33. UTLS_IOS: "ios",
  34. UTLS_android: "android",
  35. UTLS_EDGE: "edge",
  36. UTLS_360: "360",
  37. UTLS_QQ: "qq",
  38. UTLS_RANDOM: "random",
  39. UTLS_RANDOMIZED: "randomized",
  40. UTLS_RONDOMIZEDNOALPN: "randomizednoalpn",
  41. UTLS_UNSAFE: "unsafe",
  42. };
  43. const ALPN_OPTION = {
  44. H3: "h3",
  45. H2: "h2",
  46. HTTP1: "http/1.1",
  47. };
  48. const OutboundDomainStrategies = [
  49. "AsIs",
  50. "UseIP",
  51. "UseIPv4",
  52. "UseIPv6",
  53. "UseIPv6v4",
  54. "UseIPv4v6",
  55. "ForceIP",
  56. "ForceIPv6v4",
  57. "ForceIPv6",
  58. "ForceIPv4v6",
  59. "ForceIPv4"
  60. ];
  61. const WireguardDomainStrategy = [
  62. "ForceIP",
  63. "ForceIPv4",
  64. "ForceIPv4v6",
  65. "ForceIPv6",
  66. "ForceIPv6v4"
  67. ];
  68. const USERS_SECURITY = {
  69. AES_128_GCM: "aes-128-gcm",
  70. CHACHA20_POLY1305: "chacha20-poly1305",
  71. AUTO: "auto",
  72. NONE: "none",
  73. ZERO: "zero",
  74. };
  75. const MODE_OPTION = {
  76. AUTO: "auto",
  77. PACKET_UP: "packet-up",
  78. STREAM_UP: "stream-up",
  79. STREAM_ONE: "stream-one",
  80. };
  81. const Address_Port_Strategy = {
  82. NONE: "none",
  83. SrvPortOnly: "srvportonly",
  84. SrvAddressOnly: "srvaddressonly",
  85. SrvPortAndAddress: "srvportandaddress",
  86. TxtPortOnly: "txtportonly",
  87. TxtAddressOnly: "txtaddressonly",
  88. TxtPortAndAddress: "txtportandaddress"
  89. };
  90. Object.freeze(Protocols);
  91. Object.freeze(SSMethods);
  92. Object.freeze(TLS_FLOW_CONTROL);
  93. Object.freeze(UTLS_FINGERPRINT);
  94. Object.freeze(ALPN_OPTION);
  95. Object.freeze(OutboundDomainStrategies);
  96. Object.freeze(WireguardDomainStrategy);
  97. Object.freeze(USERS_SECURITY);
  98. Object.freeze(MODE_OPTION);
  99. Object.freeze(Address_Port_Strategy);
  100. class CommonClass {
  101. static toJsonArray(arr) {
  102. return arr.map(obj => obj.toJson());
  103. }
  104. static fromJson() {
  105. return new CommonClass();
  106. }
  107. toJson() {
  108. return this;
  109. }
  110. toString(format = true) {
  111. return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
  112. }
  113. }
  114. class TcpStreamSettings extends CommonClass {
  115. constructor(type = 'none', host, path) {
  116. super();
  117. this.type = type;
  118. this.host = host;
  119. this.path = path;
  120. }
  121. static fromJson(json = {}) {
  122. let header = json.header;
  123. if (!header) return new TcpStreamSettings();
  124. if (header.type == 'http' && header.request) {
  125. return new TcpStreamSettings(
  126. header.type,
  127. header.request.headers.Host.join(','),
  128. header.request.path.join(','),
  129. );
  130. }
  131. return new TcpStreamSettings(header.type, '', '');
  132. }
  133. toJson() {
  134. return {
  135. header: {
  136. type: this.type,
  137. request: this.type === 'http' ? {
  138. headers: {
  139. Host: ObjectUtil.isEmpty(this.host) ? [] : this.host.split(',')
  140. },
  141. path: ObjectUtil.isEmpty(this.path) ? ["/"] : this.path.split(',')
  142. } : undefined,
  143. }
  144. };
  145. }
  146. }
  147. class KcpStreamSettings extends CommonClass {
  148. constructor(
  149. mtu = 1350,
  150. tti = 20,
  151. uplinkCapacity = 5,
  152. downlinkCapacity = 20,
  153. cwndMultiplier = 0,
  154. maxSendingWindow = 0,
  155. ) {
  156. super();
  157. this.mtu = mtu;
  158. this.tti = tti;
  159. this.upCap = uplinkCapacity;
  160. this.downCap = downlinkCapacity;
  161. this.cwndMultiplier = cwndMultiplier;
  162. this.maxSendingWindow = maxSendingWindow;
  163. }
  164. static fromJson(json = {}) {
  165. return new KcpStreamSettings(
  166. json.mtu,
  167. json.tti,
  168. json.uplinkCapacity,
  169. json.downlinkCapacity,
  170. json.cwndMultiplier,
  171. json.maxSendingWindow,
  172. );
  173. }
  174. toJson() {
  175. return {
  176. mtu: this.mtu,
  177. tti: this.tti,
  178. uplinkCapacity: this.upCap,
  179. downlinkCapacity: this.downCap,
  180. cwndMultiplier: this.cwndMultiplier,
  181. maxSendingWindow: this.maxSendingWindow,
  182. };
  183. }
  184. }
  185. class WsStreamSettings extends CommonClass {
  186. constructor(
  187. path = '/',
  188. host = '',
  189. heartbeatPeriod = 0,
  190. ) {
  191. super();
  192. this.path = path;
  193. this.host = host;
  194. this.heartbeatPeriod = heartbeatPeriod;
  195. }
  196. static fromJson(json = {}) {
  197. return new WsStreamSettings(
  198. json.path,
  199. json.host,
  200. json.heartbeatPeriod,
  201. );
  202. }
  203. toJson() {
  204. return {
  205. path: this.path,
  206. host: this.host,
  207. heartbeatPeriod: this.heartbeatPeriod
  208. };
  209. }
  210. }
  211. class GrpcStreamSettings extends CommonClass {
  212. constructor(
  213. serviceName = "",
  214. authority = "",
  215. multiMode = false
  216. ) {
  217. super();
  218. this.serviceName = serviceName;
  219. this.authority = authority;
  220. this.multiMode = multiMode;
  221. }
  222. static fromJson(json = {}) {
  223. return new GrpcStreamSettings(json.serviceName, json.authority, json.multiMode);
  224. }
  225. toJson() {
  226. return {
  227. serviceName: this.serviceName,
  228. authority: this.authority,
  229. multiMode: this.multiMode
  230. }
  231. }
  232. }
  233. class HttpUpgradeStreamSettings extends CommonClass {
  234. constructor(path = '/', host = '') {
  235. super();
  236. this.path = path;
  237. this.host = host;
  238. }
  239. static fromJson(json = {}) {
  240. return new HttpUpgradeStreamSettings(
  241. json.path,
  242. json.host,
  243. );
  244. }
  245. toJson() {
  246. return {
  247. path: this.path,
  248. host: this.host,
  249. };
  250. }
  251. }
  252. class xHTTPStreamSettings extends CommonClass {
  253. constructor(
  254. path = '/',
  255. host = '',
  256. mode = '',
  257. noGRPCHeader = false,
  258. scMinPostsIntervalMs = "30",
  259. xmux = {
  260. maxConcurrency: "16-32",
  261. maxConnections: 0,
  262. cMaxReuseTimes: 0,
  263. hMaxRequestTimes: "600-900",
  264. hMaxReusableSecs: "1800-3000",
  265. hKeepAlivePeriod: 0,
  266. },
  267. ) {
  268. super();
  269. this.path = path;
  270. this.host = host;
  271. this.mode = mode;
  272. this.noGRPCHeader = noGRPCHeader;
  273. this.scMinPostsIntervalMs = scMinPostsIntervalMs;
  274. this.xmux = xmux;
  275. }
  276. static fromJson(json = {}) {
  277. return new xHTTPStreamSettings(
  278. json.path,
  279. json.host,
  280. json.mode,
  281. json.noGRPCHeader,
  282. json.scMinPostsIntervalMs,
  283. json.xmux
  284. );
  285. }
  286. toJson() {
  287. return {
  288. path: this.path,
  289. host: this.host,
  290. mode: this.mode,
  291. noGRPCHeader: this.noGRPCHeader,
  292. scMinPostsIntervalMs: this.scMinPostsIntervalMs,
  293. xmux: {
  294. maxConcurrency: this.xmux.maxConcurrency,
  295. maxConnections: this.xmux.maxConnections,
  296. cMaxReuseTimes: this.xmux.cMaxReuseTimes,
  297. hMaxRequestTimes: this.xmux.hMaxRequestTimes,
  298. hMaxReusableSecs: this.xmux.hMaxReusableSecs,
  299. hKeepAlivePeriod: this.xmux.hKeepAlivePeriod,
  300. },
  301. };
  302. }
  303. }
  304. class TlsStreamSettings extends CommonClass {
  305. constructor(
  306. serverName = '',
  307. alpn = [],
  308. fingerprint = '',
  309. echConfigList = '',
  310. verifyPeerCertByName = '',
  311. pinnedPeerCertSha256 = '',
  312. ) {
  313. super();
  314. this.serverName = serverName;
  315. this.alpn = alpn;
  316. this.fingerprint = fingerprint;
  317. this.echConfigList = echConfigList;
  318. this.verifyPeerCertByName = verifyPeerCertByName;
  319. this.pinnedPeerCertSha256 = pinnedPeerCertSha256;
  320. }
  321. static fromJson(json = {}) {
  322. return new TlsStreamSettings(
  323. json.serverName,
  324. json.alpn,
  325. json.fingerprint,
  326. json.echConfigList,
  327. json.verifyPeerCertByName,
  328. json.pinnedPeerCertSha256,
  329. );
  330. }
  331. toJson() {
  332. return {
  333. serverName: this.serverName,
  334. alpn: this.alpn,
  335. fingerprint: this.fingerprint,
  336. echConfigList: this.echConfigList,
  337. verifyPeerCertByName: this.verifyPeerCertByName,
  338. pinnedPeerCertSha256: this.pinnedPeerCertSha256
  339. };
  340. }
  341. }
  342. class RealityStreamSettings extends CommonClass {
  343. constructor(
  344. publicKey = '',
  345. fingerprint = '',
  346. serverName = '',
  347. shortId = '',
  348. spiderX = '',
  349. mldsa65Verify = ''
  350. ) {
  351. super();
  352. this.publicKey = publicKey;
  353. this.fingerprint = fingerprint;
  354. this.serverName = serverName;
  355. this.shortId = shortId
  356. this.spiderX = spiderX;
  357. this.mldsa65Verify = mldsa65Verify;
  358. }
  359. static fromJson(json = {}) {
  360. return new RealityStreamSettings(
  361. json.publicKey,
  362. json.fingerprint,
  363. json.serverName,
  364. json.shortId,
  365. json.spiderX,
  366. json.mldsa65Verify
  367. );
  368. }
  369. toJson() {
  370. return {
  371. publicKey: this.publicKey,
  372. fingerprint: this.fingerprint,
  373. serverName: this.serverName,
  374. shortId: this.shortId,
  375. spiderX: this.spiderX,
  376. mldsa65Verify: this.mldsa65Verify
  377. };
  378. }
  379. };
  380. class HysteriaStreamSettings extends CommonClass {
  381. constructor(
  382. version = 2,
  383. auth = '',
  384. congestion = '',
  385. up = '0',
  386. down = '0',
  387. udphopPort = '',
  388. udphopIntervalMin = 30,
  389. udphopIntervalMax = 30,
  390. initStreamReceiveWindow = 8388608,
  391. maxStreamReceiveWindow = 8388608,
  392. initConnectionReceiveWindow = 20971520,
  393. maxConnectionReceiveWindow = 20971520,
  394. maxIdleTimeout = 30,
  395. keepAlivePeriod = 0,
  396. disablePathMTUDiscovery = false
  397. ) {
  398. super();
  399. this.version = version;
  400. this.auth = auth;
  401. this.congestion = congestion;
  402. this.up = up;
  403. this.down = down;
  404. this.udphopPort = udphopPort;
  405. this.udphopIntervalMin = udphopIntervalMin;
  406. this.udphopIntervalMax = udphopIntervalMax;
  407. this.initStreamReceiveWindow = initStreamReceiveWindow;
  408. this.maxStreamReceiveWindow = maxStreamReceiveWindow;
  409. this.initConnectionReceiveWindow = initConnectionReceiveWindow;
  410. this.maxConnectionReceiveWindow = maxConnectionReceiveWindow;
  411. this.maxIdleTimeout = maxIdleTimeout;
  412. this.keepAlivePeriod = keepAlivePeriod;
  413. this.disablePathMTUDiscovery = disablePathMTUDiscovery;
  414. }
  415. static fromJson(json = {}) {
  416. let udphopPort = '';
  417. let udphopIntervalMin = 30;
  418. let udphopIntervalMax = 30;
  419. if (json.udphop) {
  420. udphopPort = json.udphop.port || '';
  421. // Backward compatibility: if old 'interval' exists, use it for both min/max
  422. if (json.udphop.interval !== undefined) {
  423. udphopIntervalMin = json.udphop.interval;
  424. udphopIntervalMax = json.udphop.interval;
  425. } else {
  426. udphopIntervalMin = json.udphop.intervalMin || 30;
  427. udphopIntervalMax = json.udphop.intervalMax || 30;
  428. }
  429. }
  430. return new HysteriaStreamSettings(
  431. json.version,
  432. json.auth,
  433. json.congestion,
  434. json.up,
  435. json.down,
  436. udphopPort,
  437. udphopIntervalMin,
  438. udphopIntervalMax,
  439. json.initStreamReceiveWindow,
  440. json.maxStreamReceiveWindow,
  441. json.initConnectionReceiveWindow,
  442. json.maxConnectionReceiveWindow,
  443. json.maxIdleTimeout,
  444. json.keepAlivePeriod,
  445. json.disablePathMTUDiscovery
  446. );
  447. }
  448. toJson() {
  449. const result = {
  450. version: this.version,
  451. auth: this.auth,
  452. congestion: this.congestion,
  453. up: this.up,
  454. down: this.down,
  455. initStreamReceiveWindow: this.initStreamReceiveWindow,
  456. maxStreamReceiveWindow: this.maxStreamReceiveWindow,
  457. initConnectionReceiveWindow: this.initConnectionReceiveWindow,
  458. maxConnectionReceiveWindow: this.maxConnectionReceiveWindow,
  459. maxIdleTimeout: this.maxIdleTimeout,
  460. keepAlivePeriod: this.keepAlivePeriod,
  461. disablePathMTUDiscovery: this.disablePathMTUDiscovery
  462. };
  463. if (this.udphopPort) {
  464. result.udphop = {
  465. port: this.udphopPort,
  466. intervalMin: this.udphopIntervalMin,
  467. intervalMax: this.udphopIntervalMax
  468. };
  469. }
  470. return result;
  471. }
  472. };
  473. class SockoptStreamSettings extends CommonClass {
  474. constructor(
  475. dialerProxy = "",
  476. tcpFastOpen = false,
  477. tcpKeepAliveInterval = 0,
  478. tcpMptcp = false,
  479. penetrate = false,
  480. addressPortStrategy = Address_Port_Strategy.NONE,
  481. trustedXForwardedFor = [],
  482. ) {
  483. super();
  484. this.dialerProxy = dialerProxy;
  485. this.tcpFastOpen = tcpFastOpen;
  486. this.tcpKeepAliveInterval = tcpKeepAliveInterval;
  487. this.tcpMptcp = tcpMptcp;
  488. this.penetrate = penetrate;
  489. this.addressPortStrategy = addressPortStrategy;
  490. this.trustedXForwardedFor = trustedXForwardedFor;
  491. }
  492. static fromJson(json = {}) {
  493. if (Object.keys(json).length === 0) return undefined;
  494. return new SockoptStreamSettings(
  495. json.dialerProxy,
  496. json.tcpFastOpen,
  497. json.tcpKeepAliveInterval,
  498. json.tcpMptcp,
  499. json.penetrate,
  500. json.addressPortStrategy,
  501. json.trustedXForwardedFor || []
  502. );
  503. }
  504. toJson() {
  505. const result = {
  506. dialerProxy: this.dialerProxy,
  507. tcpFastOpen: this.tcpFastOpen,
  508. tcpKeepAliveInterval: this.tcpKeepAliveInterval,
  509. tcpMptcp: this.tcpMptcp,
  510. penetrate: this.penetrate,
  511. addressPortStrategy: this.addressPortStrategy
  512. };
  513. if (this.trustedXForwardedFor && this.trustedXForwardedFor.length > 0) {
  514. result.trustedXForwardedFor = this.trustedXForwardedFor;
  515. }
  516. return result;
  517. }
  518. }
  519. class UdpMask extends CommonClass {
  520. constructor(type = 'salamander', settings = {}) {
  521. super();
  522. this.type = type;
  523. this.settings = this._getDefaultSettings(type, settings);
  524. }
  525. _getDefaultSettings(type, settings = {}) {
  526. switch (type) {
  527. case 'salamander':
  528. case 'mkcp-aes128gcm':
  529. return { password: settings.password || '' };
  530. case 'header-dns':
  531. case 'xdns':
  532. return { domain: settings.domain || '' };
  533. case 'xicmp':
  534. return { ip: settings.ip || '', id: settings.id ?? 0 };
  535. case 'mkcp-original':
  536. case 'header-dtls':
  537. case 'header-srtp':
  538. case 'header-utp':
  539. case 'header-wechat':
  540. case 'header-wireguard':
  541. return {}; // No settings needed
  542. case 'header-custom':
  543. return { client: [], server: [] };
  544. case 'noise':
  545. return { reset: 0, noise: [] };
  546. case 'sudoku':
  547. return { ascii: '', customTable: '', customTables: [], paddingMin: 0, paddingMax: 0 };
  548. default:
  549. return settings;
  550. }
  551. }
  552. static fromJson(json = {}) {
  553. return new UdpMask(
  554. json.type || 'salamander',
  555. json.settings || {}
  556. );
  557. }
  558. toJson() {
  559. return {
  560. type: this.type,
  561. settings: (this.settings && Object.keys(this.settings).length > 0) ? this.settings : undefined
  562. };
  563. }
  564. }
  565. class FinalMaskStreamSettings extends CommonClass {
  566. constructor(udp = []) {
  567. super();
  568. this.udp = Array.isArray(udp) ? udp.map(u => new UdpMask(u.type, u.settings)) : [new UdpMask(udp.type, udp.settings)];
  569. }
  570. static fromJson(json = {}) {
  571. return new FinalMaskStreamSettings(json.udp || []);
  572. }
  573. toJson() {
  574. return {
  575. udp: this.udp.map(udp => udp.toJson())
  576. };
  577. }
  578. }
  579. class StreamSettings extends CommonClass {
  580. constructor(
  581. network = 'tcp',
  582. security = 'none',
  583. tlsSettings = new TlsStreamSettings(),
  584. realitySettings = new RealityStreamSettings(),
  585. tcpSettings = new TcpStreamSettings(),
  586. kcpSettings = new KcpStreamSettings(),
  587. wsSettings = new WsStreamSettings(),
  588. grpcSettings = new GrpcStreamSettings(),
  589. httpupgradeSettings = new HttpUpgradeStreamSettings(),
  590. xhttpSettings = new xHTTPStreamSettings(),
  591. hysteriaSettings = new HysteriaStreamSettings(),
  592. finalmask = new FinalMaskStreamSettings(),
  593. sockopt = undefined,
  594. ) {
  595. super();
  596. this.network = network;
  597. this.security = security;
  598. this.tls = tlsSettings;
  599. this.reality = realitySettings;
  600. this.tcp = tcpSettings;
  601. this.kcp = kcpSettings;
  602. this.ws = wsSettings;
  603. this.grpc = grpcSettings;
  604. this.httpupgrade = httpupgradeSettings;
  605. this.xhttp = xhttpSettings;
  606. this.hysteria = hysteriaSettings;
  607. this.finalmask = finalmask;
  608. this.sockopt = sockopt;
  609. }
  610. addUdpMask(type = 'salamander') {
  611. this.finalmask.udp.push(new UdpMask(type));
  612. }
  613. delUdpMask(index) {
  614. if (this.finalmask.udp) {
  615. this.finalmask.udp.splice(index, 1);
  616. }
  617. }
  618. get hasFinalMask() {
  619. return this.finalmask.udp && this.finalmask.udp.length > 0;
  620. }
  621. get isTls() {
  622. return this.security === 'tls';
  623. }
  624. get isReality() {
  625. return this.security === "reality";
  626. }
  627. get sockoptSwitch() {
  628. return this.sockopt != undefined;
  629. }
  630. set sockoptSwitch(value) {
  631. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  632. }
  633. static fromJson(json = {}) {
  634. return new StreamSettings(
  635. json.network,
  636. json.security,
  637. TlsStreamSettings.fromJson(json.tlsSettings),
  638. RealityStreamSettings.fromJson(json.realitySettings),
  639. TcpStreamSettings.fromJson(json.tcpSettings),
  640. KcpStreamSettings.fromJson(json.kcpSettings),
  641. WsStreamSettings.fromJson(json.wsSettings),
  642. GrpcStreamSettings.fromJson(json.grpcSettings),
  643. HttpUpgradeStreamSettings.fromJson(json.httpupgradeSettings),
  644. xHTTPStreamSettings.fromJson(json.xhttpSettings),
  645. HysteriaStreamSettings.fromJson(json.hysteriaSettings),
  646. FinalMaskStreamSettings.fromJson(json.finalmask),
  647. SockoptStreamSettings.fromJson(json.sockopt),
  648. );
  649. }
  650. toJson() {
  651. const network = this.network;
  652. return {
  653. network: network,
  654. security: this.security,
  655. tlsSettings: this.security == 'tls' ? this.tls.toJson() : undefined,
  656. realitySettings: this.security == 'reality' ? this.reality.toJson() : undefined,
  657. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  658. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  659. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  660. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  661. httpupgradeSettings: network === 'httpupgrade' ? this.httpupgrade.toJson() : undefined,
  662. xhttpSettings: network === 'xhttp' ? this.xhttp.toJson() : undefined,
  663. hysteriaSettings: network === 'hysteria' ? this.hysteria.toJson() : undefined,
  664. finalmask: this.hasFinalMask ? this.finalmask.toJson() : undefined,
  665. sockopt: this.sockopt != undefined ? this.sockopt.toJson() : undefined,
  666. };
  667. }
  668. }
  669. class Mux extends CommonClass {
  670. constructor(enabled = false, concurrency = 8, xudpConcurrency = 16, xudpProxyUDP443 = "reject") {
  671. super();
  672. this.enabled = enabled;
  673. this.concurrency = concurrency;
  674. this.xudpConcurrency = xudpConcurrency;
  675. this.xudpProxyUDP443 = xudpProxyUDP443;
  676. }
  677. static fromJson(json = {}) {
  678. if (Object.keys(json).length === 0) return undefined;
  679. return new Mux(
  680. json.enabled,
  681. json.concurrency,
  682. json.xudpConcurrency,
  683. json.xudpProxyUDP443,
  684. );
  685. }
  686. toJson() {
  687. return {
  688. enabled: this.enabled,
  689. concurrency: this.concurrency,
  690. xudpConcurrency: this.xudpConcurrency,
  691. xudpProxyUDP443: this.xudpProxyUDP443,
  692. };
  693. }
  694. }
  695. class Outbound extends CommonClass {
  696. constructor(
  697. tag = '',
  698. protocol = Protocols.VLESS,
  699. settings = null,
  700. streamSettings = new StreamSettings(),
  701. sendThrough,
  702. mux = new Mux(),
  703. ) {
  704. super();
  705. this.tag = tag;
  706. this._protocol = protocol;
  707. this.settings = settings == null ? Outbound.Settings.getSettings(protocol) : settings;
  708. this.stream = streamSettings;
  709. this.sendThrough = sendThrough;
  710. this.mux = mux;
  711. }
  712. get protocol() {
  713. return this._protocol;
  714. }
  715. set protocol(protocol) {
  716. this._protocol = protocol;
  717. this.settings = Outbound.Settings.getSettings(protocol);
  718. this.stream = new StreamSettings();
  719. }
  720. canEnableTls() {
  721. if (this.protocol === Protocols.Hysteria) return true;
  722. if (![Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks].includes(this.protocol)) return false;
  723. return ["tcp", "ws", "http", "grpc", "httpupgrade", "xhttp"].includes(this.stream.network);
  724. }
  725. //this is used for xtls-rprx-vision
  726. canEnableTlsFlow() {
  727. if ((this.stream.security != 'none') && (this.stream.network === "tcp")) {
  728. return this.protocol === Protocols.VLESS;
  729. }
  730. return false;
  731. }
  732. // Vision seed applies only when vision flow is selected
  733. canEnableVisionSeed() {
  734. if (!this.canEnableTlsFlow()) return false;
  735. const flow = this.settings?.flow;
  736. return flow === TLS_FLOW_CONTROL.VISION || flow === TLS_FLOW_CONTROL.VISION_UDP443;
  737. }
  738. canEnableReality() {
  739. if (![Protocols.VLESS, Protocols.Trojan].includes(this.protocol)) return false;
  740. return ["tcp", "http", "grpc", "xhttp"].includes(this.stream.network);
  741. }
  742. canEnableStream() {
  743. return [Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks, Protocols.Hysteria].includes(this.protocol);
  744. }
  745. canEnableMux() {
  746. // Disable Mux if flow is set
  747. if (this.settings.flow && this.settings.flow !== '') {
  748. this.mux.enabled = false;
  749. return false;
  750. }
  751. // Disable Mux if network is xhttp
  752. if (this.stream.network === 'xhttp') {
  753. this.mux.enabled = false;
  754. return false;
  755. }
  756. // Allow Mux only for these protocols
  757. return [
  758. Protocols.VMess,
  759. Protocols.VLESS,
  760. Protocols.Trojan,
  761. Protocols.Shadowsocks,
  762. Protocols.HTTP,
  763. Protocols.Socks
  764. ].includes(this.protocol);
  765. }
  766. hasServers() {
  767. return [Protocols.Trojan, Protocols.Shadowsocks, Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  768. }
  769. hasAddressPort() {
  770. return [
  771. Protocols.DNS,
  772. Protocols.VMess,
  773. Protocols.VLESS,
  774. Protocols.Trojan,
  775. Protocols.Shadowsocks,
  776. Protocols.Socks,
  777. Protocols.HTTP,
  778. Protocols.Hysteria
  779. ].includes(this.protocol);
  780. }
  781. hasUsername() {
  782. return [Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  783. }
  784. static fromJson(json = {}) {
  785. return new Outbound(
  786. json.tag,
  787. json.protocol,
  788. Outbound.Settings.fromJson(json.protocol, json.settings),
  789. StreamSettings.fromJson(json.streamSettings),
  790. json.sendThrough,
  791. Mux.fromJson(json.mux),
  792. )
  793. }
  794. toJson() {
  795. var stream;
  796. if (this.canEnableStream()) {
  797. stream = this.stream.toJson();
  798. } else {
  799. if (this.stream?.sockopt)
  800. stream = { sockopt: this.stream.sockopt.toJson() };
  801. }
  802. let settingsOut = this.settings instanceof CommonClass ? this.settings.toJson() : this.settings;
  803. return {
  804. protocol: this.protocol,
  805. settings: settingsOut,
  806. // Only include tag, streamSettings, sendThrough, mux if present and not empty
  807. ...(this.tag ? { tag: this.tag } : {}),
  808. ...(stream ? { streamSettings: stream } : {}),
  809. ...(this.sendThrough ? { sendThrough: this.sendThrough } : {}),
  810. ...(this.mux?.enabled ? { mux: this.mux } : {}),
  811. };
  812. }
  813. static fromLink(link) {
  814. data = link.split('://');
  815. if (data.length != 2) return null;
  816. switch (data[0].toLowerCase()) {
  817. case Protocols.VMess:
  818. return this.fromVmessLink(JSON.parse(Base64.decode(data[1])));
  819. case Protocols.VLESS:
  820. case Protocols.Trojan:
  821. case 'ss':
  822. return this.fromParamLink(link);
  823. case 'hysteria2':
  824. case Protocols.Hysteria:
  825. return this.fromHysteriaLink(link);
  826. default:
  827. return null;
  828. }
  829. }
  830. static fromVmessLink(json = {}) {
  831. let stream = new StreamSettings(json.net, json.tls);
  832. let network = json.net;
  833. if (network === 'tcp') {
  834. stream.tcp = new TcpStreamSettings(
  835. json.type,
  836. json.host ?? '',
  837. json.path ?? '');
  838. } else if (network === 'kcp') {
  839. stream.kcp = new KcpStreamSettings();
  840. stream.type = json.type;
  841. stream.seed = json.path;
  842. } else if (network === 'ws') {
  843. stream.ws = new WsStreamSettings(json.path, json.host);
  844. } else if (network === 'grpc') {
  845. stream.grpc = new GrpcStreamSettings(json.path, json.authority, json.type == 'multi');
  846. } else if (network === 'httpupgrade') {
  847. stream.httpupgrade = new HttpUpgradeStreamSettings(json.path, json.host);
  848. } else if (network === 'xhttp') {
  849. stream.xhttp = new xHTTPStreamSettings(json.path, json.host, json.mode);
  850. }
  851. if (json.tls && json.tls == 'tls') {
  852. stream.tls = new TlsStreamSettings(
  853. json.sni,
  854. json.alpn ? json.alpn.split(',') : [],
  855. json.fp);
  856. }
  857. const port = json.port * 1;
  858. return new Outbound(json.ps, Protocols.VMess, new Outbound.VmessSettings(json.add, port, json.id, json.scy), stream);
  859. }
  860. static fromParamLink(link) {
  861. const url = new URL(link);
  862. let type = url.searchParams.get('type') ?? 'tcp';
  863. let security = url.searchParams.get('security') ?? 'none';
  864. let stream = new StreamSettings(type, security);
  865. let headerType = url.searchParams.get('headerType') ?? undefined;
  866. let host = url.searchParams.get('host') ?? undefined;
  867. let path = url.searchParams.get('path') ?? undefined;
  868. let mode = url.searchParams.get('mode') ?? undefined;
  869. if (type === 'tcp' || type === 'none') {
  870. stream.tcp = new TcpStreamSettings(headerType ?? 'none', host, path);
  871. } else if (type === 'kcp') {
  872. stream.kcp = new KcpStreamSettings();
  873. stream.kcp.type = headerType ?? 'none';
  874. stream.kcp.seed = path;
  875. } else if (type === 'ws') {
  876. stream.ws = new WsStreamSettings(path, host);
  877. } else if (type === 'grpc') {
  878. stream.grpc = new GrpcStreamSettings(
  879. url.searchParams.get('serviceName') ?? '',
  880. url.searchParams.get('authority') ?? '',
  881. url.searchParams.get('mode') == 'multi');
  882. } else if (type === 'httpupgrade') {
  883. stream.httpupgrade = new HttpUpgradeStreamSettings(path, host);
  884. } else if (type === 'xhttp') {
  885. stream.xhttp = new xHTTPStreamSettings(path, host, mode);
  886. }
  887. if (security == 'tls') {
  888. let fp = url.searchParams.get('fp') ?? 'none';
  889. let alpn = url.searchParams.get('alpn');
  890. let sni = url.searchParams.get('sni') ?? '';
  891. let ech = url.searchParams.get('ech') ?? '';
  892. stream.tls = new TlsStreamSettings(sni, alpn ? alpn.split(',') : [], fp, ech);
  893. }
  894. if (security == 'reality') {
  895. let pbk = url.searchParams.get('pbk');
  896. let fp = url.searchParams.get('fp');
  897. let sni = url.searchParams.get('sni') ?? '';
  898. let sid = url.searchParams.get('sid') ?? '';
  899. let spx = url.searchParams.get('spx') ?? '';
  900. let pqv = url.searchParams.get('pqv') ?? '';
  901. stream.reality = new RealityStreamSettings(pbk, fp, sni, sid, spx, pqv);
  902. }
  903. const regex = /([^@]+):\/\/([^@]+)@(.+):(\d+)(.*)$/;
  904. const match = link.match(regex);
  905. if (!match) return null;
  906. let [, protocol, userData, address, port,] = match;
  907. port *= 1;
  908. if (protocol == 'ss') {
  909. protocol = 'shadowsocks';
  910. userData = atob(userData).split(':');
  911. }
  912. var settings;
  913. switch (protocol) {
  914. case Protocols.VLESS:
  915. settings = new Outbound.VLESSSettings(address, port, userData, url.searchParams.get('flow') ?? '', url.searchParams.get('encryption') ?? 'none');
  916. break;
  917. case Protocols.Trojan:
  918. settings = new Outbound.TrojanSettings(address, port, userData);
  919. break;
  920. case Protocols.Shadowsocks:
  921. let method = userData.splice(0, 1)[0];
  922. settings = new Outbound.ShadowsocksSettings(address, port, userData.join(":"), method, true);
  923. break;
  924. default:
  925. return null;
  926. }
  927. let remark = decodeURIComponent(url.hash);
  928. // Remove '#' from url.hash
  929. remark = remark.length > 0 ? remark.substring(1) : 'out-' + protocol + '-' + port;
  930. return new Outbound(remark, protocol, settings, stream);
  931. }
  932. static fromHysteriaLink(link) {
  933. // Parse hysteria2://password@address:port[?param1=value1&param2=value2...][#remarks]
  934. const regex = /^hysteria2?:\/\/([^@]+)@([^:?#]+):(\d+)([^#]*)(#.*)?$/;
  935. const match = link.match(regex);
  936. if (!match) return null;
  937. let [, password, address, port, params, hash] = match;
  938. port = parseInt(port);
  939. // Parse URL parameters if present
  940. let urlParams = new URLSearchParams(params);
  941. // Create stream settings with hysteria network
  942. let stream = new StreamSettings('hysteria', 'none');
  943. // Set hysteria stream settings
  944. stream.hysteria.auth = password;
  945. stream.hysteria.congestion = urlParams.get('congestion') ?? '';
  946. stream.hysteria.up = urlParams.get('up') ?? '0';
  947. stream.hysteria.down = urlParams.get('down') ?? '0';
  948. stream.hysteria.udphopPort = urlParams.get('udphopPort') ?? '';
  949. // Support both old single interval and new min/max range
  950. if (urlParams.has('udphopInterval')) {
  951. const interval = parseInt(urlParams.get('udphopInterval'));
  952. stream.hysteria.udphopIntervalMin = interval;
  953. stream.hysteria.udphopIntervalMax = interval;
  954. } else {
  955. stream.hysteria.udphopIntervalMin = parseInt(urlParams.get('udphopIntervalMin') ?? '30');
  956. stream.hysteria.udphopIntervalMax = parseInt(urlParams.get('udphopIntervalMax') ?? '30');
  957. }
  958. // Optional QUIC parameters
  959. if (urlParams.has('initStreamReceiveWindow')) {
  960. stream.hysteria.initStreamReceiveWindow = parseInt(urlParams.get('initStreamReceiveWindow'));
  961. }
  962. if (urlParams.has('maxStreamReceiveWindow')) {
  963. stream.hysteria.maxStreamReceiveWindow = parseInt(urlParams.get('maxStreamReceiveWindow'));
  964. }
  965. if (urlParams.has('initConnectionReceiveWindow')) {
  966. stream.hysteria.initConnectionReceiveWindow = parseInt(urlParams.get('initConnectionReceiveWindow'));
  967. }
  968. if (urlParams.has('maxConnectionReceiveWindow')) {
  969. stream.hysteria.maxConnectionReceiveWindow = parseInt(urlParams.get('maxConnectionReceiveWindow'));
  970. }
  971. if (urlParams.has('maxIdleTimeout')) {
  972. stream.hysteria.maxIdleTimeout = parseInt(urlParams.get('maxIdleTimeout'));
  973. }
  974. if (urlParams.has('keepAlivePeriod')) {
  975. stream.hysteria.keepAlivePeriod = parseInt(urlParams.get('keepAlivePeriod'));
  976. }
  977. if (urlParams.has('disablePathMTUDiscovery')) {
  978. stream.hysteria.disablePathMTUDiscovery = urlParams.get('disablePathMTUDiscovery') === 'true';
  979. }
  980. // Create settings
  981. let settings = new Outbound.HysteriaSettings(address, port, 2);
  982. // Extract remark from hash
  983. let remark = hash ? decodeURIComponent(hash.substring(1)) : `out-hysteria-${port}`;
  984. return new Outbound(remark, Protocols.Hysteria, settings, stream);
  985. }
  986. }
  987. Outbound.Settings = class extends CommonClass {
  988. constructor(protocol) {
  989. super();
  990. this.protocol = protocol;
  991. }
  992. static getSettings(protocol) {
  993. switch (protocol) {
  994. case Protocols.Freedom: return new Outbound.FreedomSettings();
  995. case Protocols.Blackhole: return new Outbound.BlackholeSettings();
  996. case Protocols.DNS: return new Outbound.DNSSettings();
  997. case Protocols.VMess: return new Outbound.VmessSettings();
  998. case Protocols.VLESS: return new Outbound.VLESSSettings();
  999. case Protocols.Trojan: return new Outbound.TrojanSettings();
  1000. case Protocols.Shadowsocks: return new Outbound.ShadowsocksSettings();
  1001. case Protocols.Socks: return new Outbound.SocksSettings();
  1002. case Protocols.HTTP: return new Outbound.HttpSettings();
  1003. case Protocols.Wireguard: return new Outbound.WireguardSettings();
  1004. case Protocols.Hysteria: return new Outbound.HysteriaSettings();
  1005. default: return null;
  1006. }
  1007. }
  1008. static fromJson(protocol, json) {
  1009. switch (protocol) {
  1010. case Protocols.Freedom: return Outbound.FreedomSettings.fromJson(json);
  1011. case Protocols.Blackhole: return Outbound.BlackholeSettings.fromJson(json);
  1012. case Protocols.DNS: return Outbound.DNSSettings.fromJson(json);
  1013. case Protocols.VMess: return Outbound.VmessSettings.fromJson(json);
  1014. case Protocols.VLESS: return Outbound.VLESSSettings.fromJson(json);
  1015. case Protocols.Trojan: return Outbound.TrojanSettings.fromJson(json);
  1016. case Protocols.Shadowsocks: return Outbound.ShadowsocksSettings.fromJson(json);
  1017. case Protocols.Socks: return Outbound.SocksSettings.fromJson(json);
  1018. case Protocols.HTTP: return Outbound.HttpSettings.fromJson(json);
  1019. case Protocols.Wireguard: return Outbound.WireguardSettings.fromJson(json);
  1020. case Protocols.Hysteria: return Outbound.HysteriaSettings.fromJson(json);
  1021. default: return null;
  1022. }
  1023. }
  1024. toJson() {
  1025. return {};
  1026. }
  1027. };
  1028. Outbound.FreedomSettings = class extends CommonClass {
  1029. constructor(
  1030. domainStrategy = '',
  1031. redirect = '',
  1032. fragment = {},
  1033. noises = [],
  1034. ipsBlocked = [],
  1035. ) {
  1036. super();
  1037. this.domainStrategy = domainStrategy;
  1038. this.redirect = redirect;
  1039. this.fragment = fragment || {};
  1040. this.noises = Array.isArray(noises) ? noises : [];
  1041. this.ipsBlocked = Array.isArray(ipsBlocked) ? ipsBlocked : [];
  1042. }
  1043. addNoise() {
  1044. this.noises.push(new Outbound.FreedomSettings.Noise());
  1045. }
  1046. delNoise(index) {
  1047. this.noises.splice(index, 1);
  1048. }
  1049. static fromJson(json = {}) {
  1050. return new Outbound.FreedomSettings(
  1051. json.domainStrategy,
  1052. json.redirect,
  1053. json.fragment ? Outbound.FreedomSettings.Fragment.fromJson(json.fragment) : {},
  1054. json.noises ? json.noises.map(noise => Outbound.FreedomSettings.Noise.fromJson(noise)) : [],
  1055. json.ipsBlocked || [],
  1056. );
  1057. }
  1058. toJson() {
  1059. return {
  1060. domainStrategy: ObjectUtil.isEmpty(this.domainStrategy) ? undefined : this.domainStrategy,
  1061. redirect: ObjectUtil.isEmpty(this.redirect) ? undefined : this.redirect,
  1062. fragment: Object.keys(this.fragment).length === 0 ? undefined : this.fragment,
  1063. noises: this.noises.length === 0 ? undefined : Outbound.FreedomSettings.Noise.toJsonArray(this.noises),
  1064. ipsBlocked: this.ipsBlocked.length === 0 ? undefined : this.ipsBlocked,
  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. };