check.mjs 7.9 KB

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