check.mjs 8.2 KB

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