check.mjs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. import fs from "fs-extra";
  2. import zlib from "zlib";
  3. import tar from "tar";
  4. import path from "path";
  5. import AdmZip from "adm-zip";
  6. import fetch from "node-fetch";
  7. import proxyAgent from "https-proxy-agent";
  8. import { execSync } from "child_process";
  9. const cwd = process.cwd();
  10. const TEMP_DIR = path.join(cwd, "node_modules/.verge");
  11. const FORCE = process.argv.includes("--force");
  12. const PLATFORM_MAP = {
  13. "x86_64-pc-windows-msvc": "win32",
  14. "i686-pc-windows-msvc": "win32",
  15. "aarch64-pc-windows-msvc": "win32",
  16. "x86_64-apple-darwin": "darwin",
  17. "aarch64-apple-darwin": "darwin",
  18. "x86_64-unknown-linux-gnu": "linux",
  19. "i686-unknown-linux-gnu": "linux",
  20. "aarch64-unknown-linux-gnu": "linux",
  21. "armv7-unknown-linux-gnueabihf": "linux",
  22. "riscv64gc-unknown-linux-gnu": "linux",
  23. "loongarch64-unknown-linux-gnu": "linux",
  24. };
  25. const ARCH_MAP = {
  26. "x86_64-pc-windows-msvc": "x64",
  27. "i686-pc-windows-msvc": "ia32",
  28. "aarch64-pc-windows-msvc": "arm64",
  29. "x86_64-apple-darwin": "x64",
  30. "aarch64-apple-darwin": "arm64",
  31. "x86_64-unknown-linux-gnu": "x64",
  32. "i686-unknown-linux-gnu": "ia32",
  33. "aarch64-unknown-linux-gnu": "arm64",
  34. "armv7-unknown-linux-gnueabihf": "arm",
  35. "riscv64gc-unknown-linux-gnu": "riscv64",
  36. "loongarch64-unknown-linux-gnu": "loong64",
  37. };
  38. const arg1 = process.argv.slice(2)[0];
  39. const arg2 = process.argv.slice(2)[1];
  40. const target = arg1 === "--force" ? arg2 : arg1;
  41. const { platform, arch } = target
  42. ? { platform: PLATFORM_MAP[target], arch: ARCH_MAP[target] }
  43. : process;
  44. const SIDECAR_HOST = target
  45. ? target
  46. : execSync("rustc -vV")
  47. .toString()
  48. .match(/(?<=host: ).+(?=\s*)/g)[0];
  49. /* ======= clash meta alpha======= */
  50. const META_ALPHA_VERSION_URL =
  51. "https://github.com/MetaCubeX/mihomo/releases/download/Prerelease-Alpha/version.txt";
  52. const META_ALPHA_URL_PREFIX = `https://github.com/MetaCubeX/mihomo/releases/download/Prerelease-Alpha`;
  53. let META_ALPHA_VERSION;
  54. const META_ALPHA_MAP = {
  55. "win32-x64": "mihomo-windows-amd64-compatible",
  56. "win32-ia32": "mihomo-windows-386",
  57. "win32-arm64": "mihomo-windows-arm64",
  58. "darwin-x64": "mihomo-darwin-amd64-compatible",
  59. "darwin-arm64": "mihomo-darwin-arm64",
  60. "linux-x64": "mihomo-linux-amd64-compatible",
  61. "linux-ia32": "mihomo-linux-386",
  62. "linux-arm64": "mihomo-linux-arm64",
  63. "linux-arm": "mihomo-linux-armv7",
  64. "linux-riscv64": "mihomo-linux-riscv64",
  65. "linux-loong64": "mihomo-linux-loong64",
  66. };
  67. // Fetch the latest alpha release version from the version.txt file
  68. async function getLatestAlphaVersion() {
  69. const options = {};
  70. const httpProxy =
  71. process.env.HTTP_PROXY ||
  72. process.env.http_proxy ||
  73. process.env.HTTPS_PROXY ||
  74. process.env.https_proxy;
  75. if (httpProxy) {
  76. options.agent = proxyAgent(httpProxy);
  77. }
  78. try {
  79. const response = await fetch(META_ALPHA_VERSION_URL, {
  80. ...options,
  81. method: "GET",
  82. });
  83. let v = await response.text();
  84. META_ALPHA_VERSION = v.trim(); // Trim to remove extra whitespaces
  85. console.log(`Latest alpha version: ${META_ALPHA_VERSION}`);
  86. } catch (error) {
  87. console.error("Error fetching latest alpha version:", error.message);
  88. process.exit(1);
  89. }
  90. }
  91. /* ======= clash meta stable ======= */
  92. const META_VERSION_URL =
  93. "https://github.com/MetaCubeX/mihomo/releases/latest/download/version.txt";
  94. const META_URL_PREFIX = `https://github.com/MetaCubeX/mihomo/releases/download`;
  95. let META_VERSION;
  96. const META_MAP = {
  97. "win32-x64": "mihomo-windows-amd64-compatible",
  98. "win32-ia32": "mihomo-windows-386",
  99. "win32-arm64": "mihomo-windows-arm64",
  100. "darwin-x64": "mihomo-darwin-amd64-compatible",
  101. "darwin-arm64": "mihomo-darwin-arm64",
  102. "linux-x64": "mihomo-linux-amd64-compatible",
  103. "linux-ia32": "mihomo-linux-386",
  104. "linux-arm64": "mihomo-linux-arm64",
  105. "linux-arm": "mihomo-linux-armv7",
  106. "linux-riscv64": "mihomo-linux-riscv64",
  107. "linux-loong64": "mihomo-linux-loong64",
  108. };
  109. // Fetch the latest release version from the version.txt file
  110. async function getLatestReleaseVersion() {
  111. const options = {};
  112. const httpProxy =
  113. process.env.HTTP_PROXY ||
  114. process.env.http_proxy ||
  115. process.env.HTTPS_PROXY ||
  116. process.env.https_proxy;
  117. if (httpProxy) {
  118. options.agent = proxyAgent(httpProxy);
  119. }
  120. try {
  121. const response = await fetch(META_VERSION_URL, {
  122. ...options,
  123. method: "GET",
  124. });
  125. let v = await response.text();
  126. META_VERSION = v.trim(); // Trim to remove extra whitespaces
  127. console.log(`Latest release version: ${META_VERSION}`);
  128. } catch (error) {
  129. console.error("Error fetching latest release version:", error.message);
  130. process.exit(1);
  131. }
  132. }
  133. /*
  134. * check available
  135. */
  136. if (!META_MAP[`${platform}-${arch}`]) {
  137. throw new Error(
  138. `clash meta alpha unsupported platform "${platform}-${arch}"`
  139. );
  140. }
  141. if (!META_ALPHA_MAP[`${platform}-${arch}`]) {
  142. throw new Error(
  143. `clash meta alpha unsupported platform "${platform}-${arch}"`
  144. );
  145. }
  146. /**
  147. * core info
  148. */
  149. function clashMetaAlpha() {
  150. const name = META_ALPHA_MAP[`${platform}-${arch}`];
  151. const isWin = platform === "win32";
  152. const urlExt = isWin ? "zip" : "gz";
  153. const downloadURL = `${META_ALPHA_URL_PREFIX}/${name}-${META_ALPHA_VERSION}.${urlExt}`;
  154. const exeFile = `${name}${isWin ? ".exe" : ""}`;
  155. const zipFile = `${name}-${META_ALPHA_VERSION}.${urlExt}`;
  156. return {
  157. name: "verge-mihomo-alpha",
  158. targetFile: `verge-mihomo-alpha-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
  159. exeFile,
  160. zipFile,
  161. downloadURL,
  162. };
  163. }
  164. function clashMeta() {
  165. const name = META_MAP[`${platform}-${arch}`];
  166. const isWin = platform === "win32";
  167. const urlExt = isWin ? "zip" : "gz";
  168. const downloadURL = `${META_URL_PREFIX}/${META_VERSION}/${name}-${META_VERSION}.${urlExt}`;
  169. const exeFile = `${name}${isWin ? ".exe" : ""}`;
  170. const zipFile = `${name}-${META_VERSION}.${urlExt}`;
  171. return {
  172. name: "verge-mihomo",
  173. targetFile: `verge-mihomo-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
  174. exeFile,
  175. zipFile,
  176. downloadURL,
  177. };
  178. }
  179. /**
  180. * download sidecar and rename
  181. */
  182. async function resolveSidecar(binInfo) {
  183. const { name, targetFile, zipFile, exeFile, downloadURL } = binInfo;
  184. const sidecarDir = path.join(cwd, "src-tauri", "sidecar");
  185. const sidecarPath = path.join(sidecarDir, targetFile);
  186. await fs.mkdirp(sidecarDir);
  187. if (!FORCE && (await fs.pathExists(sidecarPath))) return;
  188. const tempDir = path.join(TEMP_DIR, name);
  189. const tempZip = path.join(tempDir, zipFile);
  190. const tempExe = path.join(tempDir, exeFile);
  191. await fs.mkdirp(tempDir);
  192. try {
  193. if (!(await fs.pathExists(tempZip))) {
  194. await downloadFile(downloadURL, tempZip);
  195. }
  196. if (zipFile.endsWith(".zip")) {
  197. const zip = new AdmZip(tempZip);
  198. zip.getEntries().forEach((entry) => {
  199. console.log(`[DEBUG]: "${name}" entry name`, entry.entryName);
  200. });
  201. zip.extractAllTo(tempDir, true);
  202. await fs.rename(tempExe, sidecarPath);
  203. console.log(`[INFO]: "${name}" unzip finished`);
  204. } else if (zipFile.endsWith(".tgz")) {
  205. // tgz
  206. await fs.mkdirp(tempDir);
  207. await tar.extract({
  208. cwd: tempDir,
  209. file: tempZip,
  210. //strip: 1, // 可能需要根据实际的 .tgz 文件结构调整
  211. });
  212. const files = await fs.readdir(tempDir);
  213. console.log(`[DEBUG]: "${name}" files in tempDir:`, files);
  214. const extractedFile = files.find((file) => file.startsWith("虚空终端-"));
  215. if (extractedFile) {
  216. const extractedFilePath = path.join(tempDir, extractedFile);
  217. await fs.rename(extractedFilePath, sidecarPath);
  218. console.log(`[INFO]: "${name}" file renamed to "${sidecarPath}"`);
  219. execSync(`chmod 755 ${sidecarPath}`);
  220. console.log(`[INFO]: "${name}" chmod binary finished`);
  221. } else {
  222. throw new Error(`Expected file not found in ${tempDir}`);
  223. }
  224. } else {
  225. // gz
  226. const readStream = fs.createReadStream(tempZip);
  227. const writeStream = fs.createWriteStream(sidecarPath);
  228. await new Promise((resolve, reject) => {
  229. const onError = (error) => {
  230. console.error(`[ERROR]: "${name}" gz failed:`, error.message);
  231. reject(error);
  232. };
  233. readStream
  234. .pipe(zlib.createGunzip().on("error", onError))
  235. .pipe(writeStream)
  236. .on("finish", () => {
  237. console.log(`[INFO]: "${name}" gunzip finished`);
  238. execSync(`chmod 755 ${sidecarPath}`);
  239. console.log(`[INFO]: "${name}" chmod binary finished`);
  240. resolve();
  241. })
  242. .on("error", onError);
  243. });
  244. }
  245. } catch (err) {
  246. // 需要删除文件
  247. await fs.remove(sidecarPath);
  248. throw err;
  249. } finally {
  250. // delete temp dir
  251. await fs.remove(tempDir);
  252. }
  253. }
  254. /**
  255. * download the file to the resources dir
  256. */
  257. async function resolveResource(binInfo) {
  258. const { file, downloadURL } = binInfo;
  259. const resDir = path.join(cwd, "src-tauri/resources");
  260. const targetPath = path.join(resDir, file);
  261. if (!FORCE && (await fs.pathExists(targetPath))) return;
  262. await fs.mkdirp(resDir);
  263. await downloadFile(downloadURL, targetPath);
  264. console.log(`[INFO]: ${file} finished`);
  265. }
  266. /**
  267. * download file and save to `path`
  268. */
  269. async function downloadFile(url, path) {
  270. const options = {};
  271. const httpProxy =
  272. process.env.HTTP_PROXY ||
  273. process.env.http_proxy ||
  274. process.env.HTTPS_PROXY ||
  275. process.env.https_proxy;
  276. if (httpProxy) {
  277. options.agent = proxyAgent(httpProxy);
  278. }
  279. const response = await fetch(url, {
  280. ...options,
  281. method: "GET",
  282. headers: { "Content-Type": "application/octet-stream" },
  283. });
  284. const buffer = await response.arrayBuffer();
  285. await fs.writeFile(path, new Uint8Array(buffer));
  286. console.log(`[INFO]: download finished "${url}"`);
  287. }
  288. // SimpleSC.dll
  289. const resolvePlugin = async () => {
  290. const url =
  291. "https://nsis.sourceforge.io/mediawiki/images/e/ef/NSIS_Simple_Service_Plugin_Unicode_1.30.zip";
  292. const tempDir = path.join(TEMP_DIR, "SimpleSC");
  293. const tempZip = path.join(
  294. tempDir,
  295. "NSIS_Simple_Service_Plugin_Unicode_1.30.zip"
  296. );
  297. const tempDll = path.join(tempDir, "SimpleSC.dll");
  298. const pluginDir = path.join(process.env.APPDATA, "Local/NSIS");
  299. const pluginPath = path.join(pluginDir, "SimpleSC.dll");
  300. await fs.mkdirp(pluginDir);
  301. await fs.mkdirp(tempDir);
  302. if (!FORCE && (await fs.pathExists(pluginPath))) return;
  303. try {
  304. if (!(await fs.pathExists(tempZip))) {
  305. await downloadFile(url, tempZip);
  306. }
  307. const zip = new AdmZip(tempZip);
  308. zip.getEntries().forEach((entry) => {
  309. console.log(`[DEBUG]: "SimpleSC" entry name`, entry.entryName);
  310. });
  311. zip.extractAllTo(tempDir, true);
  312. await fs.copyFile(tempDll, pluginPath);
  313. console.log(`[INFO]: "SimpleSC" unzip finished`);
  314. } finally {
  315. await fs.remove(tempDir);
  316. }
  317. };
  318. // service chmod
  319. const resolveServicePermission = async () => {
  320. const serviceExecutables = [
  321. "clash-verge-service",
  322. "install-service",
  323. "uninstall-service",
  324. ];
  325. const resDir = path.join(cwd, "src-tauri/resources");
  326. for (let f of serviceExecutables) {
  327. const targetPath = path.join(resDir, f);
  328. if (await fs.pathExists(targetPath)) {
  329. execSync(`chmod 755 ${targetPath}`);
  330. console.log(`[INFO]: "${targetPath}" chmod finished`);
  331. }
  332. }
  333. };
  334. /**
  335. * main
  336. */
  337. const SERVICE_URL = `https://github.com/clash-verge-rev/clash-verge-service/releases/download/${SIDECAR_HOST}`;
  338. const resolveService = () => {
  339. let ext = platform === "win32" ? ".exe" : "";
  340. resolveResource({
  341. file: "clash-verge-service" + ext,
  342. downloadURL: `${SERVICE_URL}/clash-verge-service${ext}`,
  343. });
  344. };
  345. const resolveInstall = () => {
  346. let ext = platform === "win32" ? ".exe" : "";
  347. resolveResource({
  348. file: "install-service" + ext,
  349. downloadURL: `${SERVICE_URL}/install-service${ext}`,
  350. });
  351. };
  352. const resolveUninstall = () => {
  353. let ext = platform === "win32" ? ".exe" : "";
  354. resolveResource({
  355. file: "uninstall-service" + ext,
  356. downloadURL: `${SERVICE_URL}/uninstall-service${ext}`,
  357. });
  358. };
  359. const resolveMmdb = () =>
  360. resolveResource({
  361. file: "Country.mmdb",
  362. downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/country.mmdb`,
  363. });
  364. const resolveGeosite = () =>
  365. resolveResource({
  366. file: "geosite.dat",
  367. downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat`,
  368. });
  369. const resolveGeoIP = () =>
  370. resolveResource({
  371. file: "geoip.dat",
  372. downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip.dat`,
  373. });
  374. const resolveEnableLoopback = () =>
  375. resolveResource({
  376. file: "enableLoopback.exe",
  377. downloadURL: `https://github.com/Kuingsmile/uwp-tool/releases/download/latest/enableLoopback.exe`,
  378. });
  379. const tasks = [
  380. // { name: "clash", func: resolveClash, retry: 5 },
  381. {
  382. name: "verge-mihomo-alpha",
  383. func: () =>
  384. getLatestAlphaVersion().then(() => resolveSidecar(clashMetaAlpha())),
  385. retry: 5,
  386. },
  387. {
  388. name: "verge-mihomo",
  389. func: () =>
  390. getLatestReleaseVersion().then(() => resolveSidecar(clashMeta())),
  391. retry: 5,
  392. },
  393. { name: "plugin", func: resolvePlugin, retry: 5, winOnly: true },
  394. { name: "service", func: resolveService, retry: 5 },
  395. { name: "install", func: resolveInstall, retry: 5 },
  396. { name: "uninstall", func: resolveUninstall, retry: 5 },
  397. { name: "mmdb", func: resolveMmdb, retry: 5 },
  398. { name: "geosite", func: resolveGeosite, retry: 5 },
  399. { name: "geoip", func: resolveGeoIP, retry: 5 },
  400. {
  401. name: "enableLoopback",
  402. func: resolveEnableLoopback,
  403. retry: 5,
  404. winOnly: true,
  405. },
  406. {
  407. name: "service_chmod",
  408. func: resolveServicePermission,
  409. retry: 1,
  410. unixOnly: true,
  411. },
  412. ];
  413. async function runTask() {
  414. const task = tasks.shift();
  415. if (!task) return;
  416. if (task.winOnly && platform !== "win32") return runTask();
  417. if (task.linuxOnly && platform !== "linux") return runTask();
  418. if (task.unixOnly && platform === "win32") return runTask();
  419. for (let i = 0; i < task.retry; i++) {
  420. try {
  421. await task.func();
  422. break;
  423. } catch (err) {
  424. console.error(`[ERROR]: task::${task.name} try ${i} ==`, err.message);
  425. if (i === task.retry - 1) throw err;
  426. }
  427. }
  428. return runTask();
  429. }
  430. runTask();
  431. runTask();