check.mjs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import fs from "fs-extra";
  2. import zlib from "zlib";
  3. import path from "path";
  4. import AdmZip from "adm-zip";
  5. import fetch from "node-fetch";
  6. import proxyAgent from "https-proxy-agent";
  7. import { execSync } from "child_process";
  8. const cwd = process.cwd();
  9. const TEMP_DIR = path.join(cwd, "node_modules/.verge");
  10. const FORCE = process.argv.includes("--force");
  11. const SIDECAR_HOST = execSync("rustc -vV")
  12. .toString()
  13. .match(/(?<=host: ).+(?=\s*)/g)[0];
  14. /* ======= clash =======
  15. const CLASH_STORAGE_PREFIX = "https://release.dreamacro.workers.dev/";
  16. const CLASH_URL_PREFIX =
  17. "https://github.com/Dreamacro/clash/releases/download/premium/";
  18. const CLASH_LATEST_DATE = "latest";
  19. const CLASH_BACKUP_URL_PREFIX =
  20. "https://github.com/zhongfly/Clash-premium-backup/releases/download/";
  21. const CLASH_BACKUP_LATEST_DATE = "2023-09-05-gdcc8d87";
  22. //https://github.com/zhongfly/Clash-premium-backup/releases/download/2023-09-05-gdcc8d87/clash-windows-amd64-2023-09-05-gdcc8d87.zip
  23. //https://github.com/zhongfly/Clash-premium-backup/releases/download/2023-09-05-gdcc8d87/clash-windows-amd64-n2023-09-05-gdcc8d87.zip
  24. const CLASH_MAP = {
  25. "win32-x64": "clash-windows-amd64",
  26. "darwin-x64": "clash-darwin-amd64",
  27. "darwin-arm64": "clash-darwin-arm64",
  28. "linux-x64": "clash-linux-amd64",
  29. "linux-arm64": "clash-linux-arm64",
  30. };
  31. */
  32. /* ======= clash meta ======= */
  33. const META_URL_PREFIX = `https://github.com/wonfen/Clash.Meta/releases/download/`;
  34. const META_VERSION = "2023.11.21";
  35. const META_MAP = {
  36. "win32-x64": "clash.meta-win-amd64",
  37. "darwin-x64": "clash.meta-darwin-amd64",
  38. "darwin-arm64": "clash.meta-darwin-arm64",
  39. "linux-x64": "clash.meta-linux-amd64",
  40. "linux-arm64": "clash.meta-linux-arm64",
  41. };
  42. /*
  43. * check available
  44. */
  45. const { platform, arch } = process;
  46. /*
  47. if (!CLASH_MAP[`${platform}-${arch}`]) {
  48. throw new Error(`clash unsupported platform "${platform}-${arch}"`);
  49. }
  50. */
  51. if (!META_MAP[`${platform}-${arch}`]) {
  52. throw new Error(`clash meta unsupported platform "${platform}-${arch}"`);
  53. }
  54. /*
  55. function clash() {
  56. const name = CLASH_MAP[`${platform}-${arch}`];
  57. const isWin = platform === "win32";
  58. const urlExt = isWin ? "zip" : "gz";
  59. const downloadURL = `${CLASH_URL_PREFIX}${name}-${CLASH_LATEST_DATE}.${urlExt}`;
  60. const exeFile = `${name}${isWin ? ".exe" : ""}`;
  61. const zipFile = `${name}.${urlExt}`;
  62. return {
  63. name: "clash",
  64. targetFile: `clash-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
  65. exeFile,
  66. zipFile,
  67. downloadURL,
  68. };
  69. }
  70. function clashBackup() {
  71. const name = CLASH_MAP[`${platform}-${arch}`];
  72. const isWin = platform === "win32";
  73. const urlExt = isWin ? "zip" : "gz";
  74. const downloadURL = `${CLASH_BACKUP_URL_PREFIX}${CLASH_BACKUP_LATEST_DATE}/${name}-n${CLASH_BACKUP_LATEST_DATE}.${urlExt}`;
  75. const exeFile = `${name}${isWin ? ".exe" : ""}`;
  76. const zipFile = `${name}.${urlExt}`;
  77. return {
  78. name: "clash",
  79. targetFile: `clash-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
  80. exeFile,
  81. zipFile,
  82. downloadURL,
  83. };
  84. }
  85. function clashS3() {
  86. const name = CLASH_MAP[`${platform}-${arch}`];
  87. const isWin = platform === "win32";
  88. const urlExt = isWin ? "zip" : "gz";
  89. const downloadURL = `${CLASH_STORAGE_PREFIX}${CLASH_LATEST_DATE}/${name}-${CLASH_LATEST_DATE}.${urlExt}`;
  90. const exeFile = `${name}${isWin ? ".exe" : ""}`;
  91. const zipFile = `${name}.${urlExt}`;
  92. return {
  93. name: "clash",
  94. targetFile: `clash-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
  95. exeFile,
  96. zipFile,
  97. downloadURL,
  98. };
  99. }
  100. */
  101. function clashMeta() {
  102. const name = META_MAP[`${platform}-${arch}`];
  103. const isWin = platform === "win32";
  104. const urlExt = isWin ? "zip" : "gz";
  105. const downloadURL = `${META_URL_PREFIX}${META_VERSION}/${name}-${META_VERSION}.${urlExt}`;
  106. const exeFile = `${name}${isWin ? ".exe" : ""}`;
  107. const zipFile = `${name}-${META_VERSION}.${urlExt}`;
  108. return {
  109. name: "clash-meta",
  110. targetFile: `clash-meta-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
  111. exeFile,
  112. zipFile,
  113. downloadURL,
  114. };
  115. }
  116. /**
  117. * download sidecar and rename
  118. */
  119. async function resolveSidecar(binInfo) {
  120. const { name, targetFile, zipFile, exeFile, downloadURL } = binInfo;
  121. const sidecarDir = path.join(cwd, "src-tauri", "sidecar");
  122. const sidecarPath = path.join(sidecarDir, targetFile);
  123. await fs.mkdirp(sidecarDir);
  124. if (!FORCE && (await fs.pathExists(sidecarPath))) return;
  125. const tempDir = path.join(TEMP_DIR, name);
  126. const tempZip = path.join(tempDir, zipFile);
  127. const tempExe = path.join(tempDir, exeFile);
  128. await fs.mkdirp(tempDir);
  129. try {
  130. if (!(await fs.pathExists(tempZip))) {
  131. await downloadFile(downloadURL, tempZip);
  132. }
  133. if (zipFile.endsWith(".zip")) {
  134. const zip = new AdmZip(tempZip);
  135. zip.getEntries().forEach((entry) => {
  136. console.log(`[DEBUG]: "${name}" entry name`, entry.entryName);
  137. });
  138. zip.extractAllTo(tempDir, true);
  139. await fs.rename(tempExe, sidecarPath);
  140. console.log(`[INFO]: "${name}" unzip finished`);
  141. } else {
  142. // gz
  143. const readStream = fs.createReadStream(tempZip);
  144. const writeStream = fs.createWriteStream(sidecarPath);
  145. await new Promise((resolve, reject) => {
  146. const onError = (error) => {
  147. console.error(`[ERROR]: "${name}" gz failed:`, error.message);
  148. reject(error);
  149. };
  150. readStream
  151. .pipe(zlib.createGunzip().on("error", onError))
  152. .pipe(writeStream)
  153. .on("finish", () => {
  154. console.log(`[INFO]: "${name}" gunzip finished`);
  155. execSync(`chmod 755 ${sidecarPath}`);
  156. console.log(`[INFO]: "${name}" chmod binary finished`);
  157. resolve();
  158. })
  159. .on("error", onError);
  160. });
  161. }
  162. } catch (err) {
  163. // 需要删除文件
  164. await fs.remove(sidecarPath);
  165. throw err;
  166. } finally {
  167. // delete temp dir
  168. await fs.remove(tempDir);
  169. }
  170. }
  171. /**
  172. * prepare clash core
  173. * if the core version is not updated in time, use S3 storage as a backup.
  174. */
  175. async function resolveClash() {
  176. try {
  177. return await resolveSidecar(clash());
  178. } catch {
  179. console.log(`[WARN]: clash core needs to be updated`);
  180. return await resolveSidecar(clashS3());
  181. }
  182. }
  183. /**
  184. * only Windows
  185. * get the wintun.dll (not required)
  186. async function resolveWintun() {
  187. const { platform } = process;
  188. if (platform !== "win32") return;
  189. const url = "https://www.wintun.net/builds/wintun-0.14.1.zip";
  190. const tempDir = path.join(TEMP_DIR, "wintun");
  191. const tempZip = path.join(tempDir, "wintun.zip");
  192. const wintunPath = path.join(tempDir, "wintun/bin/amd64/wintun.dll");
  193. const targetPath = path.join(cwd, "src-tauri/resources", "wintun.dll");
  194. if (!FORCE && (await fs.pathExists(targetPath))) return;
  195. await fs.mkdirp(tempDir);
  196. if (!(await fs.pathExists(tempZip))) {
  197. await downloadFile(url, tempZip);
  198. }
  199. // unzip
  200. const zip = new AdmZip(tempZip);
  201. zip.extractAllTo(tempDir, true);
  202. if (!(await fs.pathExists(wintunPath))) {
  203. throw new Error(`path not found "${wintunPath}"`);
  204. }
  205. await fs.rename(wintunPath, targetPath);
  206. await fs.remove(tempDir);
  207. console.log(`[INFO]: resolve wintun.dll finished`);
  208. }
  209. */
  210. /**
  211. * download the file to the resources dir
  212. */
  213. async function resolveResource(binInfo) {
  214. const { file, downloadURL } = binInfo;
  215. const resDir = path.join(cwd, "src-tauri/resources");
  216. const targetPath = path.join(resDir, file);
  217. if (!FORCE && (await fs.pathExists(targetPath))) return;
  218. await fs.mkdirp(resDir);
  219. await downloadFile(downloadURL, targetPath);
  220. console.log(`[INFO]: ${file} finished`);
  221. }
  222. /**
  223. * download file and save to `path`
  224. */
  225. async function downloadFile(url, path) {
  226. const options = {};
  227. const httpProxy =
  228. process.env.HTTP_PROXY ||
  229. process.env.http_proxy ||
  230. process.env.HTTPS_PROXY ||
  231. process.env.https_proxy;
  232. if (httpProxy) {
  233. options.agent = proxyAgent(httpProxy);
  234. }
  235. const response = await fetch(url, {
  236. ...options,
  237. method: "GET",
  238. headers: { "Content-Type": "application/octet-stream" },
  239. });
  240. const buffer = await response.arrayBuffer();
  241. await fs.writeFile(path, new Uint8Array(buffer));
  242. console.log(`[INFO]: download finished "${url}"`);
  243. }
  244. /**
  245. * main
  246. */
  247. const SERVICE_URL =
  248. "https://github.com/zzzgydi/clash-verge-service/releases/download/latest";
  249. const resolveService = () =>
  250. resolveResource({
  251. file: "clash-verge-service.exe",
  252. downloadURL: `${SERVICE_URL}/clash-verge-service.exe`,
  253. });
  254. const resolveInstall = () =>
  255. resolveResource({
  256. file: "install-service.exe",
  257. downloadURL: `${SERVICE_URL}/install-service.exe`,
  258. });
  259. const resolveUninstall = () =>
  260. resolveResource({
  261. file: "uninstall-service.exe",
  262. downloadURL: `${SERVICE_URL}/uninstall-service.exe`,
  263. });
  264. const resolveMmdb = () =>
  265. resolveResource({
  266. file: "Country.mmdb",
  267. downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/country.mmdb`,
  268. });
  269. const resolveGeosite = () =>
  270. resolveResource({
  271. file: "geosite.dat",
  272. downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat`,
  273. });
  274. const resolveGeoIP = () =>
  275. resolveResource({
  276. file: "geoip.dat",
  277. downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip.dat`,
  278. });
  279. const resolveEnableLoopback = () =>
  280. resolveResource({
  281. file: "enableLoopback.exe",
  282. downloadURL: `https://github.com/Kuingsmile/uwp-tool/releases/download/latest/enableLoopback.exe`,
  283. });
  284. const tasks = [
  285. // { name: "clash", func: resolveClash, retry: 5 },
  286. { name: "clash-meta", func: () => resolveSidecar(clashMeta()), retry: 5 },
  287. // { name: "wintun", func: resolveWintun, retry: 5, winOnly: true },
  288. { name: "service", func: resolveService, retry: 5, winOnly: true },
  289. { name: "install", func: resolveInstall, retry: 5, winOnly: true },
  290. { name: "uninstall", func: resolveUninstall, retry: 5, winOnly: true },
  291. { name: "mmdb", func: resolveMmdb, retry: 5 },
  292. { name: "geosite", func: resolveGeosite, retry: 5 },
  293. { name: "geoip", func: resolveGeoIP, retry: 5 },
  294. {
  295. name: "enableLoopback",
  296. func: resolveEnableLoopback,
  297. retry: 5,
  298. winOnly: true,
  299. },
  300. ];
  301. async function runTask() {
  302. const task = tasks.shift();
  303. if (!task) return;
  304. if (task.winOnly && process.platform !== "win32") return runTask();
  305. for (let i = 0; i < task.retry; i++) {
  306. try {
  307. await task.func();
  308. break;
  309. } catch (err) {
  310. console.error(`[ERROR]: task::${task.name} try ${i} ==`, err.message);
  311. if (i === task.retry - 1) throw err;
  312. }
  313. }
  314. return runTask();
  315. }
  316. runTask();
  317. runTask();