check.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. };
  23. const ARCH_MAP = {
  24. "x86_64-pc-windows-msvc": "x64",
  25. "i686-pc-windows-msvc": "ia32",
  26. "aarch64-pc-windows-msvc": "arm64",
  27. "x86_64-apple-darwin": "x64",
  28. "aarch64-apple-darwin": "arm64",
  29. "x86_64-unknown-linux-gnu": "x64",
  30. "i686-unknown-linux-gnu": "ia32",
  31. "aarch64-unknown-linux-gnu": "arm64",
  32. "armv7-unknown-linux-gnueabihf": "arm",
  33. };
  34. const arg1 = process.argv.slice(2)[0];
  35. const arg2 = process.argv.slice(2)[1];
  36. const target = arg1 === "--force" ? arg2 : arg1;
  37. const { platform, arch } = target
  38. ? { platform: PLATFORM_MAP[target], arch: ARCH_MAP[target] }
  39. : process;
  40. const SIDECAR_HOST = target
  41. ? target
  42. : execSync("rustc -vV")
  43. .toString()
  44. .match(/(?<=host: ).+(?=\s*)/g)[0];
  45. /* ======= clash meta alpha======= */
  46. const VERSION_URL =
  47. "https://github.com/MetaCubeX/mihomo/releases/download/Prerelease-Alpha/version.txt";
  48. const META_ALPHA_URL_PREFIX = `https://github.com/MetaCubeX/mihomo/releases/download/Prerelease-Alpha`;
  49. let META_ALPHA_VERSION;
  50. const META_ALPHA_MAP = {
  51. "win32-x64": "mihomo-windows-amd64-compatible",
  52. "win32-ia32": "mihomo-windows-386",
  53. "win32-arm64": "mihomo-windows-arm64",
  54. "darwin-x64": "mihomo-darwin-amd64",
  55. "darwin-arm64": "mihomo-darwin-arm64",
  56. "linux-x64": "mihomo-linux-amd64-compatible",
  57. "linux-ia32": "mihomo-linux-386",
  58. "linux-arm64": "mihomo-linux-arm64",
  59. "linux-arm": "mihomo-linux-armv7",
  60. };
  61. // Fetch the latest release version from the version.txt file
  62. async function getLatestVersion() {
  63. try {
  64. const response = await fetch(VERSION_URL, { method: "GET" });
  65. let v = await response.text();
  66. META_ALPHA_VERSION = v.trim(); // Trim to remove extra whitespaces
  67. console.log(`Latest release version: ${META_ALPHA_VERSION}`);
  68. } catch (error) {
  69. console.error("Error fetching latest release version:", error.message);
  70. process.exit(1);
  71. }
  72. }
  73. /* ======= clash meta stable ======= */
  74. const META_URL_PREFIX = `https://github.com/MetaCubeX/mihomo/releases/download`;
  75. let META_VERSION = "v1.17.0";
  76. const META_MAP = {
  77. "win32-x64": "mihomo-windows-amd64-compatible",
  78. "win32-ia32": "mihomo-windows-386",
  79. "win32-arm64": "mihomo-windows-arm64",
  80. "darwin-x64": "mihomo-darwin-amd64",
  81. "darwin-arm64": "mihomo-darwin-arm64",
  82. "linux-x64": "mihomo-linux-amd64-compatible",
  83. "linux-ia32": "mihomo-linux-386",
  84. "linux-arm64": "mihomo-linux-arm64",
  85. "linux-arm": "mihomo-linux-armv7",
  86. };
  87. /*
  88. * check available
  89. */
  90. if (!META_MAP[`${platform}-${arch}`]) {
  91. throw new Error(
  92. `clash meta alpha unsupported platform "${platform}-${arch}"`
  93. );
  94. }
  95. if (!META_ALPHA_MAP[`${platform}-${arch}`]) {
  96. throw new Error(
  97. `clash meta alpha unsupported platform "${platform}-${arch}"`
  98. );
  99. }
  100. /**
  101. * core info
  102. */
  103. function clashMetaAlpha() {
  104. const name = META_ALPHA_MAP[`${platform}-${arch}`];
  105. const isWin = platform === "win32";
  106. const urlExt = isWin ? "zip" : "gz";
  107. const downloadURL = `${META_ALPHA_URL_PREFIX}/${name}-${META_ALPHA_VERSION}.${urlExt}`;
  108. const exeFile = `${name}${isWin ? ".exe" : ""}`;
  109. const zipFile = `${name}-${META_ALPHA_VERSION}.${urlExt}`;
  110. return {
  111. name: "clash-meta-alpha",
  112. targetFile: `clash-meta-alpha-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
  113. exeFile,
  114. zipFile,
  115. downloadURL,
  116. };
  117. }
  118. function clashMeta() {
  119. const name = META_MAP[`${platform}-${arch}`];
  120. const isWin = platform === "win32";
  121. const urlExt = isWin ? "zip" : "gz";
  122. const downloadURL = `${META_URL_PREFIX}/${META_VERSION}/${name}-${META_VERSION}.${urlExt}`;
  123. const exeFile = `${name}${isWin ? ".exe" : ""}`;
  124. const zipFile = `${name}-${META_VERSION}.${urlExt}`;
  125. return {
  126. name: "clash-meta",
  127. targetFile: `clash-meta-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
  128. exeFile,
  129. zipFile,
  130. downloadURL,
  131. };
  132. }
  133. /**
  134. * download sidecar and rename
  135. */
  136. async function resolveSidecar(binInfo) {
  137. const { name, targetFile, zipFile, exeFile, downloadURL } = binInfo;
  138. const sidecarDir = path.join(cwd, "src-tauri", "sidecar");
  139. const sidecarPath = path.join(sidecarDir, targetFile);
  140. await fs.mkdirp(sidecarDir);
  141. if (!FORCE && (await fs.pathExists(sidecarPath))) return;
  142. const tempDir = path.join(TEMP_DIR, name);
  143. const tempZip = path.join(tempDir, zipFile);
  144. const tempExe = path.join(tempDir, exeFile);
  145. await fs.mkdirp(tempDir);
  146. try {
  147. if (!(await fs.pathExists(tempZip))) {
  148. await downloadFile(downloadURL, tempZip);
  149. }
  150. if (zipFile.endsWith(".zip")) {
  151. const zip = new AdmZip(tempZip);
  152. zip.getEntries().forEach((entry) => {
  153. console.log(`[DEBUG]: "${name}" entry name`, entry.entryName);
  154. });
  155. zip.extractAllTo(tempDir, true);
  156. await fs.rename(tempExe, sidecarPath);
  157. console.log(`[INFO]: "${name}" unzip finished`);
  158. } else if (zipFile.endsWith(".tgz")) {
  159. // tgz
  160. await fs.mkdirp(tempDir);
  161. await tar.extract({
  162. cwd: tempDir,
  163. file: tempZip,
  164. //strip: 1, // 可能需要根据实际的 .tgz 文件结构调整
  165. });
  166. const files = await fs.readdir(tempDir);
  167. console.log(`[DEBUG]: "${name}" files in tempDir:`, files);
  168. const extractedFile = files.find((file) => file.startsWith("虚空终端-"));
  169. if (extractedFile) {
  170. const extractedFilePath = path.join(tempDir, extractedFile);
  171. await fs.rename(extractedFilePath, sidecarPath);
  172. console.log(`[INFO]: "${name}" file renamed to "${sidecarPath}"`);
  173. execSync(`chmod 755 ${sidecarPath}`);
  174. console.log(`[INFO]: "${name}" chmod binary finished`);
  175. } else {
  176. throw new Error(`Expected file not found in ${tempDir}`);
  177. }
  178. } else {
  179. // gz
  180. const readStream = fs.createReadStream(tempZip);
  181. const writeStream = fs.createWriteStream(sidecarPath);
  182. await new Promise((resolve, reject) => {
  183. const onError = (error) => {
  184. console.error(`[ERROR]: "${name}" gz failed:`, error.message);
  185. reject(error);
  186. };
  187. readStream
  188. .pipe(zlib.createGunzip().on("error", onError))
  189. .pipe(writeStream)
  190. .on("finish", () => {
  191. console.log(`[INFO]: "${name}" gunzip finished`);
  192. execSync(`chmod 755 ${sidecarPath}`);
  193. console.log(`[INFO]: "${name}" chmod binary finished`);
  194. resolve();
  195. })
  196. .on("error", onError);
  197. });
  198. }
  199. } catch (err) {
  200. // 需要删除文件
  201. await fs.remove(sidecarPath);
  202. throw err;
  203. } finally {
  204. // delete temp dir
  205. await fs.remove(tempDir);
  206. }
  207. }
  208. /**
  209. * download the file to the resources dir
  210. */
  211. async function resolveResource(binInfo) {
  212. const { file, downloadURL } = binInfo;
  213. const resDir = path.join(cwd, "src-tauri/resources");
  214. const targetPath = path.join(resDir, file);
  215. if (!FORCE && (await fs.pathExists(targetPath))) return;
  216. await fs.mkdirp(resDir);
  217. await downloadFile(downloadURL, targetPath);
  218. console.log(`[INFO]: ${file} finished`);
  219. }
  220. /**
  221. * download file and save to `path`
  222. */
  223. async function downloadFile(url, path) {
  224. const options = {};
  225. const httpProxy =
  226. process.env.HTTP_PROXY ||
  227. process.env.http_proxy ||
  228. process.env.HTTPS_PROXY ||
  229. process.env.https_proxy;
  230. if (httpProxy) {
  231. options.agent = proxyAgent(httpProxy);
  232. }
  233. const response = await fetch(url, {
  234. ...options,
  235. method: "GET",
  236. headers: { "Content-Type": "application/octet-stream" },
  237. });
  238. const buffer = await response.arrayBuffer();
  239. await fs.writeFile(path, new Uint8Array(buffer));
  240. console.log(`[INFO]: download finished "${url}"`);
  241. }
  242. /**
  243. * main
  244. */
  245. const SERVICE_URL =
  246. "https://github.com/zzzgydi/clash-verge-service/releases/download/latest";
  247. const resolveService = () =>
  248. resolveResource({
  249. file: "clash-verge-service.exe",
  250. downloadURL: `${SERVICE_URL}/clash-verge-service.exe`,
  251. });
  252. const resolveInstall = () =>
  253. resolveResource({
  254. file: "install-service.exe",
  255. downloadURL: `${SERVICE_URL}/install-service.exe`,
  256. });
  257. const resolveUninstall = () =>
  258. resolveResource({
  259. file: "uninstall-service.exe",
  260. downloadURL: `${SERVICE_URL}/uninstall-service.exe`,
  261. });
  262. const resolveMmdb = () =>
  263. resolveResource({
  264. file: "Country.mmdb",
  265. downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/country.mmdb`,
  266. });
  267. const resolveGeosite = () =>
  268. resolveResource({
  269. file: "geosite.dat",
  270. downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat`,
  271. });
  272. const resolveGeoIP = () =>
  273. resolveResource({
  274. file: "geoip.dat",
  275. downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip.dat`,
  276. });
  277. const resolveEnableLoopback = () =>
  278. resolveResource({
  279. file: "enableLoopback.exe",
  280. downloadURL: `https://github.com/Kuingsmile/uwp-tool/releases/download/latest/enableLoopback.exe`,
  281. });
  282. const tasks = [
  283. // { name: "clash", func: resolveClash, retry: 5 },
  284. {
  285. name: "clash-meta-alpha",
  286. func: () => getLatestVersion().then(() => resolveSidecar(clashMetaAlpha())),
  287. retry: 5,
  288. },
  289. {
  290. name: "clash-meta",
  291. func: () => resolveSidecar(clashMeta()),
  292. retry: 5,
  293. },
  294. // { name: "wintun", func: resolveWintun, retry: 5, winOnly: true },
  295. { name: "service", func: resolveService, retry: 5, winOnly: true },
  296. { name: "install", func: resolveInstall, retry: 5, winOnly: true },
  297. { name: "uninstall", func: resolveUninstall, retry: 5, winOnly: true },
  298. { name: "mmdb", func: resolveMmdb, retry: 5 },
  299. { name: "geosite", func: resolveGeosite, retry: 5 },
  300. { name: "geoip", func: resolveGeoIP, retry: 5 },
  301. {
  302. name: "enableLoopback",
  303. func: resolveEnableLoopback,
  304. retry: 5,
  305. winOnly: true,
  306. },
  307. ];
  308. async function runTask() {
  309. const task = tasks.shift();
  310. if (!task) return;
  311. if (task.winOnly && process.platform !== "win32") return runTask();
  312. for (let i = 0; i < task.retry; i++) {
  313. try {
  314. await task.func();
  315. break;
  316. } catch (err) {
  317. console.error(`[ERROR]: task::${task.name} try ${i} ==`, err.message);
  318. if (i === task.retry - 1) throw err;
  319. }
  320. }
  321. return runTask();
  322. }
  323. runTask();
  324. runTask();