check.mjs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 { execSync } from "child_process";
  7. const cwd = process.cwd();
  8. const FORCE = process.argv.includes("--force");
  9. /**
  10. * get the correct clash release infomation
  11. */
  12. function resolveClash() {
  13. const { platform, arch } = process;
  14. const CLASH_URL_PREFIX =
  15. "https://github.com/Dreamacro/clash/releases/download/premium/";
  16. const CLASH_LATEST_DATE = "2022.04.11";
  17. // todo
  18. const map = {
  19. "win32-x64": "clash-windows-amd64",
  20. "darwin-x64": "clash-darwin-amd64",
  21. "darwin-arm64": "clash-darwin-arm64",
  22. "linux-x64": "clash-linux-amd64",
  23. };
  24. const name = map[`${platform}-${arch}`];
  25. if (!name) {
  26. throw new Error(`unsupport platform "${platform}-${arch}"`);
  27. }
  28. const isWin = platform === "win32";
  29. const zip = isWin ? "zip" : "gz";
  30. const url = `${CLASH_URL_PREFIX}${name}-${CLASH_LATEST_DATE}.${zip}`;
  31. const exefile = `${name}${isWin ? ".exe" : ""}`;
  32. const zipfile = `${name}.${zip}`;
  33. return { url, zip, exefile, zipfile };
  34. }
  35. /**
  36. * get the sidecar bin
  37. */
  38. async function resolveSidecar() {
  39. const sidecarDir = path.join(cwd, "src-tauri", "sidecar");
  40. const host = execSync("rustc -vV | grep host").toString().slice(6).trim();
  41. const ext = process.platform === "win32" ? ".exe" : "";
  42. const sidecarFile = `clash-${host}${ext}`;
  43. const sidecarPath = path.join(sidecarDir, sidecarFile);
  44. await fs.mkdirp(sidecarDir);
  45. if (!FORCE && (await fs.pathExists(sidecarPath))) return;
  46. // download sidecar
  47. const binInfo = resolveClash();
  48. const tempDir = path.join(cwd, "pre-dev-temp");
  49. const tempZip = path.join(tempDir, binInfo.zipfile);
  50. const tempExe = path.join(tempDir, binInfo.exefile);
  51. await fs.mkdirp(tempDir);
  52. if (!(await fs.pathExists(tempZip))) await downloadFile(binInfo.url, tempZip);
  53. if (binInfo.zip === "zip") {
  54. const zip = new AdmZip(tempZip);
  55. zip.getEntries().forEach((entry) => {
  56. console.log("[INFO]: entry name", entry.entryName);
  57. });
  58. zip.extractAllTo(tempDir, true);
  59. // save as sidecar
  60. await fs.rename(tempExe, sidecarPath);
  61. console.log(`[INFO]: unzip finished`);
  62. } else {
  63. // gz
  64. const readStream = fs.createReadStream(tempZip);
  65. const writeStream = fs.createWriteStream(sidecarPath);
  66. readStream
  67. .pipe(zlib.createGunzip())
  68. .pipe(writeStream)
  69. .on("finish", () => {
  70. console.log(`[INFO]: gunzip finished`);
  71. execSync(`chmod 755 ${sidecarPath}`);
  72. console.log(`[INFO]: chmod binary finished`);
  73. })
  74. .on("error", (error) => console.error(error));
  75. }
  76. // delete temp dir
  77. await fs.remove(tempDir);
  78. }
  79. /**
  80. * only Windows
  81. * get the wintun.dll (not required)
  82. */
  83. async function resolveWintun() {
  84. const { platform } = process;
  85. if (platform !== "win32") return;
  86. const url = "https://www.wintun.net/builds/wintun-0.14.1.zip";
  87. const tempDir = path.join(cwd, "pre-dev-temp-1");
  88. const tempZip = path.join(tempDir, "wintun.zip");
  89. const wintunPath = path.join(tempDir, "wintun/bin/amd64/wintun.dll");
  90. const targetPath = path.join(cwd, "src-tauri/resources", "wintun.dll");
  91. if (!FORCE && (await fs.pathExists(targetPath))) return;
  92. await fs.mkdirp(tempDir);
  93. if (!(await fs.pathExists(tempZip))) {
  94. await downloadFile(url, tempZip);
  95. }
  96. // unzip
  97. const zip = new AdmZip(tempZip);
  98. zip.extractAllTo(tempDir, true);
  99. if (!(await fs.pathExists(wintunPath))) {
  100. throw new Error(`path not found "${wintunPath}"`);
  101. }
  102. // move wintun.dll to resources
  103. await fs.rename(wintunPath, targetPath);
  104. // delete temp dir
  105. await fs.remove(tempDir);
  106. console.log(`[INFO]: resolve wintun.dll finished`);
  107. }
  108. /**
  109. * get the Country.mmdb (not required)
  110. */
  111. async function resolveMmdb() {
  112. const url =
  113. "https://github.com/Dreamacro/maxmind-geoip/releases/latest/download/Country.mmdb";
  114. const resDir = path.join(cwd, "src-tauri", "resources");
  115. const resPath = path.join(resDir, "Country.mmdb");
  116. if (!FORCE && (await fs.pathExists(resPath))) return;
  117. await fs.mkdirp(resDir);
  118. await downloadFile(url, resPath);
  119. }
  120. /**
  121. * download file and save to `path`
  122. */
  123. async function downloadFile(url, path) {
  124. console.log(`[INFO]: downloading from "${url}"`);
  125. const response = await fetch(url, {
  126. method: "GET",
  127. headers: { "Content-Type": "application/octet-stream" },
  128. });
  129. const buffer = await response.arrayBuffer();
  130. await fs.writeFile(path, new Uint8Array(buffer));
  131. console.log(`[INFO]: download finished "${url}"`);
  132. }
  133. /// main
  134. resolveSidecar().catch(console.error);
  135. resolveWintun().catch(console.error);
  136. resolveMmdb().catch(console.error);