check.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. import fs from "fs-extra";
  2. import zlib from "zlib";
  3. import tar from "tar";
  4. import path from "path";
  5. import AdmZip from "adm-zip";
  6. import fetch from "node-fetch";
  7. import proxyAgent from "https-proxy-agent";
  8. import { execSync } from "child_process";
  9. const cwd = process.cwd();
  10. const TEMP_DIR = path.join(cwd, "node_modules/.verge");
  11. const FORCE = process.argv.includes("--force");
  12. const SIDECAR_HOST = execSync("rustc -vV")
  13. .toString()
  14. .match(/(?<=host: ).+(?=\s*)/g)[0];
  15. /* ======= clash =======
  16. const CLASH_STORAGE_PREFIX = "https://release.dreamacro.workers.dev/";
  17. const CLASH_URL_PREFIX =
  18. "https://github.com/Dreamacro/clash/releases/download/premium/";
  19. const CLASH_LATEST_DATE = "latest";
  20. const CLASH_BACKUP_URL_PREFIX =
  21. "https://github.com/zhongfly/Clash-premium-backup/releases/download/";
  22. const CLASH_BACKUP_LATEST_DATE = "2023-09-05-gdcc8d87";
  23. //https://github.com/zhongfly/Clash-premium-backup/releases/download/2023-09-05-gdcc8d87/clash-windows-amd64-2023-09-05-gdcc8d87.zip
  24. //https://github.com/zhongfly/Clash-premium-backup/releases/download/2023-09-05-gdcc8d87/clash-windows-amd64-n2023-09-05-gdcc8d87.zip
  25. const CLASH_MAP = {
  26. "win32-x64": "clash-windows-amd64",
  27. "darwin-x64": "clash-darwin-amd64",
  28. "darwin-arm64": "clash-darwin-arm64",
  29. "linux-x64": "clash-linux-amd64",
  30. "linux-arm64": "clash-linux-arm64",
  31. };
  32. */
  33. /* ======= clash meta ======= */
  34. const META_URL_PREFIX = `https://github.com/wonfen/Clash.Meta/releases/download/latest`;
  35. // const META_VERSION = "2023.11.23";
  36. const META_MAP = {
  37. "win32-x64": "clash.meta-win-amd64",
  38. "darwin-x64": "clash.meta-darwin-amd64",
  39. "darwin-arm64": "clash.meta-darwin-arm64",
  40. "linux-x64": "clash.meta-linux-amd64",
  41. "linux-arm64": "clash.meta-linux-arm64",
  42. };
  43. /*
  44. * check available
  45. */
  46. const { platform, arch } = process;
  47. /*
  48. if (!CLASH_MAP[`${platform}-${arch}`]) {
  49. throw new Error(`clash unsupported platform "${platform}-${arch}"`);
  50. }
  51. */
  52. if (!META_MAP[`${platform}-${arch}`]) {
  53. throw new Error(`clash meta unsupported platform "${platform}-${arch}"`);
  54. }
  55. /*
  56. function clash() {
  57. const name = CLASH_MAP[`${platform}-${arch}`];
  58. const isWin = platform === "win32";
  59. const urlExt = isWin ? "zip" : "gz";
  60. const downloadURL = `${CLASH_URL_PREFIX}${name}-${CLASH_LATEST_DATE}.${urlExt}`;
  61. const exeFile = `${name}${isWin ? ".exe" : ""}`;
  62. const zipFile = `${name}.${urlExt}`;
  63. return {
  64. name: "clash",
  65. targetFile: `clash-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
  66. exeFile,
  67. zipFile,
  68. downloadURL,
  69. };
  70. }
  71. function clashBackup() {
  72. const name = CLASH_MAP[`${platform}-${arch}`];
  73. const isWin = platform === "win32";
  74. const urlExt = isWin ? "zip" : "gz";
  75. const downloadURL = `${CLASH_BACKUP_URL_PREFIX}${CLASH_BACKUP_LATEST_DATE}/${name}-n${CLASH_BACKUP_LATEST_DATE}.${urlExt}`;
  76. const exeFile = `${name}${isWin ? ".exe" : ""}`;
  77. const zipFile = `${name}.${urlExt}`;
  78. return {
  79. name: "clash",
  80. targetFile: `clash-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
  81. exeFile,
  82. zipFile,
  83. downloadURL,
  84. };
  85. }
  86. function clashS3() {
  87. const name = CLASH_MAP[`${platform}-${arch}`];
  88. const isWin = platform === "win32";
  89. const urlExt = isWin ? "zip" : "gz";
  90. const downloadURL = `${CLASH_STORAGE_PREFIX}${CLASH_LATEST_DATE}/${name}-${CLASH_LATEST_DATE}.${urlExt}`;
  91. const exeFile = `${name}${isWin ? ".exe" : ""}`;
  92. const zipFile = `${name}.${urlExt}`;
  93. return {
  94. name: "clash",
  95. targetFile: `clash-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
  96. exeFile,
  97. zipFile,
  98. downloadURL,
  99. };
  100. }
  101. */
  102. function clashMeta() {
  103. const name = META_MAP[`${platform}-${arch}`];
  104. const isWin = platform === "win32";
  105. /* const urlExt = isWin ? "zip" : "gz";
  106. const downloadURL = `${META_URL_PREFIX}${META_VERSION}/${name}-${META_VERSION}.${urlExt}`;
  107. const exeFile = `${name}${isWin ? ".exe" : ""}`;
  108. const zipFile = `${name}-${META_VERSION}.${urlExt}`; */
  109. const urlExt = isWin ? "zip" : "tgz";
  110. const downloadURL = `${META_URL_PREFIX}/${name}.${urlExt}`;
  111. const exeFile = isWin ? "虚空终端-win-amd64.exe" : name;
  112. const zipFile = `${name}.${urlExt}`;
  113. return {
  114. name: "clash-meta",
  115. targetFile: `clash-meta-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
  116. exeFile,
  117. zipFile,
  118. downloadURL,
  119. };
  120. }
  121. /**
  122. * download sidecar and rename
  123. */
  124. async function resolveSidecar(binInfo) {
  125. const { name, targetFile, zipFile, exeFile, downloadURL } = binInfo;
  126. const sidecarDir = path.join(cwd, "src-tauri", "sidecar");
  127. const sidecarPath = path.join(sidecarDir, targetFile);
  128. await fs.mkdirp(sidecarDir);
  129. if (!FORCE && (await fs.pathExists(sidecarPath))) return;
  130. const tempDir = path.join(TEMP_DIR, name);
  131. const tempZip = path.join(tempDir, zipFile);
  132. const tempExe = path.join(tempDir, exeFile);
  133. await fs.mkdirp(tempDir);
  134. try {
  135. if (!(await fs.pathExists(tempZip))) {
  136. await downloadFile(downloadURL, tempZip);
  137. }
  138. if (zipFile.endsWith(".zip")) {
  139. const zip = new AdmZip(tempZip);
  140. zip.getEntries().forEach((entry) => {
  141. console.log(`[DEBUG]: "${name}" entry name`, entry.entryName);
  142. });
  143. zip.extractAllTo(tempDir, true);
  144. await fs.rename(tempExe, sidecarPath);
  145. console.log(`[INFO]: "${name}" unzip finished`);
  146. } else if (zipFile.endsWith(".tgz")) {
  147. // tgz
  148. await fs.mkdirp(tempDir);
  149. await tar.extract({
  150. cwd: tempDir,
  151. file: tempZip,
  152. //strip: 1, // 可能需要根据实际的 .tgz 文件结构调整
  153. });
  154. const files = await fs.readdir(tempDir);
  155. console.log(`[DEBUG]: "${name}" files in tempDir:`, files);
  156. const extractedFile = files.find((file) => file.startsWith("虚空终端-"));
  157. if (extractedFile) {
  158. const extractedFilePath = path.join(tempDir, extractedFile);
  159. await fs.rename(extractedFilePath, sidecarPath);
  160. console.log(`[INFO]: "${name}" file renamed to "${sidecarPath}"`);
  161. execSync(`chmod 755 ${sidecarPath}`);
  162. console.log(`[INFO]: "${name}" chmod binary finished`);
  163. } else {
  164. throw new Error(`Expected file not found in ${tempDir}`);
  165. }
  166. } else {
  167. // gz
  168. const readStream = fs.createReadStream(tempZip);
  169. const writeStream = fs.createWriteStream(sidecarPath);
  170. await new Promise((resolve, reject) => {
  171. const onError = (error) => {
  172. console.error(`[ERROR]: "${name}" gz failed:`, error.message);
  173. reject(error);
  174. };
  175. readStream
  176. .pipe(zlib.createGunzip().on("error", onError))
  177. .pipe(writeStream)
  178. .on("finish", () => {
  179. console.log(`[INFO]: "${name}" gunzip finished`);
  180. execSync(`chmod 755 ${sidecarPath}`);
  181. console.log(`[INFO]: "${name}" chmod binary finished`);
  182. resolve();
  183. })
  184. .on("error", onError);
  185. });
  186. }
  187. } catch (err) {
  188. // 需要删除文件
  189. await fs.remove(sidecarPath);
  190. throw err;
  191. } finally {
  192. // delete temp dir
  193. await fs.remove(tempDir);
  194. }
  195. }
  196. /**
  197. * prepare clash core
  198. * if the core version is not updated in time, use S3 storage as a backup.
  199. */
  200. async function resolveClash() {
  201. try {
  202. return await resolveSidecar(clash());
  203. } catch {
  204. console.log(`[WARN]: clash core needs to be updated`);
  205. return await resolveSidecar(clashS3());
  206. }
  207. }
  208. /**
  209. * only Windows
  210. * get the wintun.dll (not required)
  211. async function resolveWintun() {
  212. const { platform } = process;
  213. if (platform !== "win32") return;
  214. const url = "https://www.wintun.net/builds/wintun-0.14.1.zip";
  215. const tempDir = path.join(TEMP_DIR, "wintun");
  216. const tempZip = path.join(tempDir, "wintun.zip");
  217. const wintunPath = path.join(tempDir, "wintun/bin/amd64/wintun.dll");
  218. const targetPath = path.join(cwd, "src-tauri/resources", "wintun.dll");
  219. if (!FORCE && (await fs.pathExists(targetPath))) return;
  220. await fs.mkdirp(tempDir);
  221. if (!(await fs.pathExists(tempZip))) {
  222. await downloadFile(url, tempZip);
  223. }
  224. // unzip
  225. const zip = new AdmZip(tempZip);
  226. zip.extractAllTo(tempDir, true);
  227. if (!(await fs.pathExists(wintunPath))) {
  228. throw new Error(`path not found "${wintunPath}"`);
  229. }
  230. await fs.rename(wintunPath, targetPath);
  231. await fs.remove(tempDir);
  232. console.log(`[INFO]: resolve wintun.dll finished`);
  233. }
  234. */
  235. /**
  236. * download the file to the resources dir
  237. */
  238. async function resolveResource(binInfo) {
  239. const { file, downloadURL } = binInfo;
  240. const resDir = path.join(cwd, "src-tauri/resources");
  241. const targetPath = path.join(resDir, file);
  242. if (!FORCE && (await fs.pathExists(targetPath))) return;
  243. await fs.mkdirp(resDir);
  244. await downloadFile(downloadURL, targetPath);
  245. console.log(`[INFO]: ${file} finished`);
  246. }
  247. /**
  248. * download file and save to `path`
  249. */
  250. async function downloadFile(url, path) {
  251. const options = {};
  252. const httpProxy =
  253. process.env.HTTP_PROXY ||
  254. process.env.http_proxy ||
  255. process.env.HTTPS_PROXY ||
  256. process.env.https_proxy;
  257. if (httpProxy) {
  258. options.agent = proxyAgent(httpProxy);
  259. }
  260. const response = await fetch(url, {
  261. ...options,
  262. method: "GET",
  263. headers: { "Content-Type": "application/octet-stream" },
  264. });
  265. const buffer = await response.arrayBuffer();
  266. await fs.writeFile(path, new Uint8Array(buffer));
  267. console.log(`[INFO]: download finished "${url}"`);
  268. }
  269. /**
  270. * main
  271. */
  272. const SERVICE_URL =
  273. "https://github.com/zzzgydi/clash-verge-service/releases/download/latest";
  274. const resolveService = () =>
  275. resolveResource({
  276. file: "clash-verge-service.exe",
  277. downloadURL: `${SERVICE_URL}/clash-verge-service.exe`,
  278. });
  279. const resolveInstall = () =>
  280. resolveResource({
  281. file: "install-service.exe",
  282. downloadURL: `${SERVICE_URL}/install-service.exe`,
  283. });
  284. const resolveUninstall = () =>
  285. resolveResource({
  286. file: "uninstall-service.exe",
  287. downloadURL: `${SERVICE_URL}/uninstall-service.exe`,
  288. });
  289. const resolveMmdb = () =>
  290. resolveResource({
  291. file: "Country.mmdb",
  292. downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/country.mmdb`,
  293. });
  294. const resolveGeosite = () =>
  295. resolveResource({
  296. file: "geosite.dat",
  297. downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat`,
  298. });
  299. const resolveGeoIP = () =>
  300. resolveResource({
  301. file: "geoip.dat",
  302. downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip.dat`,
  303. });
  304. const resolveEnableLoopback = () =>
  305. resolveResource({
  306. file: "enableLoopback.exe",
  307. downloadURL: `https://github.com/Kuingsmile/uwp-tool/releases/download/latest/enableLoopback.exe`,
  308. });
  309. const tasks = [
  310. // { name: "clash", func: resolveClash, retry: 5 },
  311. { name: "clash-meta", func: () => resolveSidecar(clashMeta()), retry: 5 },
  312. // { name: "wintun", func: resolveWintun, retry: 5, winOnly: true },
  313. { name: "service", func: resolveService, retry: 5, winOnly: true },
  314. { name: "install", func: resolveInstall, retry: 5, winOnly: true },
  315. { name: "uninstall", func: resolveUninstall, retry: 5, winOnly: true },
  316. { name: "mmdb", func: resolveMmdb, retry: 5 },
  317. { name: "geosite", func: resolveGeosite, retry: 5 },
  318. { name: "geoip", func: resolveGeoIP, retry: 5 },
  319. {
  320. name: "enableLoopback",
  321. func: resolveEnableLoopback,
  322. retry: 5,
  323. winOnly: true,
  324. },
  325. ];
  326. async function runTask() {
  327. const task = tasks.shift();
  328. if (!task) return;
  329. if (task.winOnly && process.platform !== "win32") return runTask();
  330. for (let i = 0; i < task.retry; i++) {
  331. try {
  332. await task.func();
  333. break;
  334. } catch (err) {
  335. console.error(`[ERROR]: task::${task.name} try ${i} ==`, err.message);
  336. if (i === task.retry - 1) throw err;
  337. }
  338. }
  339. return runTask();
  340. }
  341. runTask();
  342. runTask();