check.mjs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 NO_META = process.argv.includes("--no-meta") || false;
  12. /**
  13. * get the correct clash release infomation
  14. */
  15. function resolveClash() {
  16. const { platform, arch } = process;
  17. const CLASH_URL_PREFIX =
  18. "https://github.com/Dreamacro/clash/releases/download/premium/";
  19. const CLASH_LATEST_DATE = "2022.08.26";
  20. // todo
  21. const map = {
  22. "win32-x64": "clash-windows-amd64",
  23. "darwin-x64": "clash-darwin-amd64",
  24. "darwin-arm64": "clash-darwin-arm64",
  25. "linux-x64": "clash-linux-amd64",
  26. "linux-arm64": "clash-linux-armv8",
  27. };
  28. const name = map[`${platform}-${arch}`];
  29. if (!name) {
  30. throw new Error(`unsupport platform "${platform}-${arch}"`);
  31. }
  32. const isWin = platform === "win32";
  33. const zip = isWin ? "zip" : "gz";
  34. const url = `${CLASH_URL_PREFIX}${name}-${CLASH_LATEST_DATE}.${zip}`;
  35. const exefile = `${name}${isWin ? ".exe" : ""}`;
  36. const zipfile = `${name}.${zip}`;
  37. return { url, zip, exefile, zipfile };
  38. }
  39. /**
  40. * get the correct Clash.Meta release infomation
  41. */
  42. async function resolveClashMeta() {
  43. const { platform, arch } = process;
  44. const urlPrefix = `https://github.com/MetaCubeX/Clash.Meta/releases/download/`;
  45. const latestVersion = "v1.13.2";
  46. const map = {
  47. "win32-x64": "Clash.Meta-windows-amd64",
  48. "darwin-x64": "Clash.Meta-darwin-amd64",
  49. "darwin-arm64": "Clash.Meta-darwin-arm64",
  50. "linux-x64": "Clash.Meta-linux-amd64-compatible",
  51. "linux-arm64": "Clash.Meta-linux-arm64",
  52. };
  53. const name = map[`${platform}-${arch}`];
  54. if (!name) {
  55. throw new Error(`unsupport platform "${platform}-${arch}"`);
  56. }
  57. const isWin = platform === "win32";
  58. const ext = isWin ? "zip" : "gz";
  59. const url = `${urlPrefix}${latestVersion}/${name}-${latestVersion}.${ext}`;
  60. const exefile = `${name}${isWin ? ".exe" : ""}`;
  61. const zipfile = `${name}-${latestVersion}.${ext}`;
  62. return { url, zip: ext, exefile, zipfile };
  63. }
  64. /**
  65. * get the sidecar bin
  66. * clash and Clash Meta
  67. */
  68. async function resolveSidecar() {
  69. const sidecarDir = path.join(cwd, "src-tauri", "sidecar");
  70. const host = execSync("rustc -vV")
  71. .toString()
  72. .match(/(?<=host: ).+(?=\s*)/g)[0];
  73. const ext = process.platform === "win32" ? ".exe" : "";
  74. await clash();
  75. if (!NO_META) await clashMeta();
  76. async function clash() {
  77. const sidecarFile = `clash-${host}${ext}`;
  78. const sidecarPath = path.join(sidecarDir, sidecarFile);
  79. await fs.mkdirp(sidecarDir);
  80. if (!FORCE && (await fs.pathExists(sidecarPath))) return;
  81. // download sidecar
  82. const binInfo = resolveClash();
  83. const tempDir = path.join(TEMP_DIR, "clash");
  84. const tempZip = path.join(tempDir, binInfo.zipfile);
  85. const tempExe = path.join(tempDir, binInfo.exefile);
  86. await fs.mkdirp(tempDir);
  87. if (!(await fs.pathExists(tempZip)))
  88. await downloadFile(binInfo.url, tempZip);
  89. if (binInfo.zip === "zip") {
  90. const zip = new AdmZip(tempZip);
  91. zip.getEntries().forEach((entry) => {
  92. console.log("[INFO]: entry name", entry.entryName);
  93. });
  94. zip.extractAllTo(tempDir, true);
  95. // save as sidecar
  96. await fs.rename(tempExe, sidecarPath);
  97. console.log(`[INFO]: unzip finished`);
  98. } else {
  99. // gz
  100. const readStream = fs.createReadStream(tempZip);
  101. const writeStream = fs.createWriteStream(sidecarPath);
  102. readStream
  103. .pipe(zlib.createGunzip())
  104. .pipe(writeStream)
  105. .on("finish", () => {
  106. console.log(`[INFO]: gunzip finished`);
  107. execSync(`chmod 755 ${sidecarPath}`);
  108. console.log(`[INFO]: chmod binary finished`);
  109. })
  110. .on("error", (error) => console.error(error));
  111. }
  112. // delete temp dir
  113. await fs.remove(tempDir);
  114. }
  115. async function clashMeta() {
  116. const sidecarFile = `clash-meta-${host}${ext}`;
  117. const sidecarPath = path.join(sidecarDir, sidecarFile);
  118. await fs.mkdirp(sidecarDir);
  119. if (!FORCE && (await fs.pathExists(sidecarPath))) return;
  120. // download sidecar
  121. const binInfo = await resolveClashMeta();
  122. const tempDir = path.join(TEMP_DIR, "clash-meta");
  123. const tempZip = path.join(tempDir, binInfo.zipfile);
  124. const tempExe = path.join(tempDir, binInfo.exefile);
  125. await fs.mkdirp(tempDir);
  126. if (!(await fs.pathExists(tempZip)))
  127. await downloadFile(binInfo.url, tempZip);
  128. if (binInfo.zip === "zip") {
  129. const zip = new AdmZip(tempZip);
  130. zip.getEntries().forEach((entry) => {
  131. console.log("[INFO]: entry name", entry.entryName);
  132. });
  133. zip.extractAllTo(tempDir, true);
  134. // save as sidecar
  135. await fs.rename(tempExe, sidecarPath);
  136. console.log(`[INFO]: unzip finished`);
  137. } else {
  138. // gz
  139. const readStream = fs.createReadStream(tempZip);
  140. const writeStream = fs.createWriteStream(sidecarPath);
  141. readStream
  142. .pipe(zlib.createGunzip())
  143. .pipe(writeStream)
  144. .on("finish", () => {
  145. console.log(`[INFO]: gunzip finished`);
  146. execSync(`chmod 755 ${sidecarPath}`);
  147. console.log(`[INFO]: chmod binary finished`);
  148. })
  149. .on("error", (error) => console.error(error));
  150. }
  151. // delete temp dir
  152. await fs.remove(tempDir);
  153. }
  154. }
  155. /**
  156. * only Windows
  157. * get the wintun.dll (not required)
  158. */
  159. async function resolveWintun() {
  160. const { platform } = process;
  161. if (platform !== "win32") return;
  162. const url = "https://www.wintun.net/builds/wintun-0.14.1.zip";
  163. const tempDir = path.join(TEMP_DIR, "wintun");
  164. const tempZip = path.join(tempDir, "wintun.zip");
  165. const wintunPath = path.join(tempDir, "wintun/bin/amd64/wintun.dll");
  166. const targetPath = path.join(cwd, "src-tauri/resources", "wintun.dll");
  167. if (!FORCE && (await fs.pathExists(targetPath))) return;
  168. await fs.mkdirp(tempDir);
  169. if (!(await fs.pathExists(tempZip))) {
  170. await downloadFile(url, tempZip);
  171. }
  172. // unzip
  173. const zip = new AdmZip(tempZip);
  174. zip.extractAllTo(tempDir, true);
  175. if (!(await fs.pathExists(wintunPath))) {
  176. throw new Error(`path not found "${wintunPath}"`);
  177. }
  178. await fs.rename(wintunPath, targetPath);
  179. await fs.remove(tempDir);
  180. console.log(`[INFO]: resolve wintun.dll finished`);
  181. }
  182. /**
  183. * only Windows
  184. * get the clash-verge-service.exe
  185. */
  186. async function resolveService() {
  187. const { platform } = process;
  188. if (platform !== "win32") return;
  189. const resDir = path.join(cwd, "src-tauri/resources");
  190. const repo =
  191. "https://github.com/zzzgydi/clash-verge-service/releases/download/latest";
  192. async function help(bin) {
  193. const targetPath = path.join(resDir, bin);
  194. if (!FORCE && (await fs.pathExists(targetPath))) return;
  195. const url = `${repo}/${bin}`;
  196. await downloadFile(url, targetPath);
  197. }
  198. await fs.mkdirp(resDir);
  199. await help("clash-verge-service.exe");
  200. await help("install-service.exe");
  201. await help("uninstall-service.exe");
  202. console.log(`[INFO]: resolve Service finished`);
  203. }
  204. /**
  205. * get the Country.mmdb (not required)
  206. */
  207. async function resolveMmdb() {
  208. const url =
  209. "https://github.com/Dreamacro/maxmind-geoip/releases/download/20221012/Country.mmdb";
  210. const resDir = path.join(cwd, "src-tauri", "resources");
  211. const resPath = path.join(resDir, "Country.mmdb");
  212. if (!FORCE && (await fs.pathExists(resPath))) return;
  213. await fs.mkdirp(resDir);
  214. await downloadFile(url, resPath);
  215. }
  216. /**
  217. * download file and save to `path`
  218. */
  219. async function downloadFile(url, path) {
  220. console.log(`[INFO]: downloading from "${url}"`);
  221. const options = {};
  222. const httpProxy =
  223. process.env.HTTP_PROXY ||
  224. process.env.http_proxy ||
  225. process.env.HTTPS_PROXY ||
  226. process.env.https_proxy;
  227. if (httpProxy) {
  228. options.agent = proxyAgent(httpProxy);
  229. }
  230. const response = await fetch(url, {
  231. ...options,
  232. method: "GET",
  233. headers: { "Content-Type": "application/octet-stream" },
  234. });
  235. const buffer = await response.arrayBuffer();
  236. await fs.writeFile(path, new Uint8Array(buffer));
  237. console.log(`[INFO]: download finished "${url}"`);
  238. }
  239. /// main
  240. resolveSidecar().catch(console.error);
  241. resolveWintun().catch(console.error);
  242. resolveMmdb().catch(console.error);
  243. resolveService().catch(console.error);