check.mjs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. const CLASH_URL_PREFIX =
  10. "https://github.com/Dreamacro/clash/releases/download/premium/";
  11. const CLASH_LATEST_DATE = "2022.01.27";
  12. /**
  13. * get the correct clash release infomation
  14. */
  15. function resolveClash() {
  16. const { platform, arch } = process;
  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. }
  75. // delete temp dir
  76. await fs.remove(tempDir);
  77. }
  78. /**
  79. * only Windows
  80. * get the wintun.dll (not required)
  81. */
  82. async function resolveWintun() {
  83. const { platform } = process;
  84. if (platform !== "win32") return;
  85. const url = "https://www.wintun.net/builds/wintun-0.14.1.zip";
  86. const tempDir = path.join(cwd, "pre-dev-temp");
  87. const tempZip = path.join(tempDir, "wintun.zip");
  88. const wintunPath = path.join(tempDir, "wintun/bin/amd64/wintun.dll");
  89. const targetPath = path.join(cwd, "src-tauri/resources", "wintun.dll");
  90. if (!FORCE && (await fs.pathExists(targetPath))) return;
  91. await fs.mkdirp(tempDir);
  92. if (!(await fs.pathExists(tempZip))) {
  93. await downloadFile(url, tempZip);
  94. }
  95. // unzip
  96. const zip = new AdmZip(tempZip);
  97. zip.extractAllTo(tempDir, true);
  98. if (!(await fs.pathExists(wintunPath))) {
  99. throw new Error(`path not found "${wintunPath}"`);
  100. }
  101. // move wintun.dll to resources
  102. await fs.rename(wintunPath, targetPath);
  103. // delete temp dir
  104. await fs.remove(tempDir);
  105. console.log(`[INFO]: resolve wintun.dll finished`);
  106. }
  107. /**
  108. * get the Country.mmdb (not required)
  109. */
  110. async function resolveMmdb() {
  111. const url =
  112. "https://github.com/Dreamacro/maxmind-geoip/releases/latest/download/Country.mmdb";
  113. const resDir = path.join(cwd, "src-tauri", "resources");
  114. const resPath = path.join(resDir, "Country.mmdb");
  115. if (!FORCE && (await fs.pathExists(resPath))) return;
  116. await fs.mkdirp(resDir);
  117. await downloadFile(url, resPath);
  118. }
  119. /**
  120. * download file and save to `path`
  121. */
  122. async function downloadFile(url, path) {
  123. console.log(`[INFO]: downloading from "${url}"`);
  124. const response = await fetch(url, {
  125. method: "GET",
  126. headers: { "Content-Type": "application/octet-stream" },
  127. });
  128. const buffer = await response.arrayBuffer();
  129. await fs.writeFile(path, new Uint8Array(buffer));
  130. console.log(`[INFO]: download finished "${url}"`);
  131. }
  132. /// main
  133. resolveSidecar().catch(console.error);
  134. resolveWintun().catch(console.error);
  135. resolveMmdb().catch(console.error);