check.mjs 8.2 KB

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