check.mjs 9.1 KB

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