check.mjs 12 KB

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