check.mjs 13 KB

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