check.mjs 12 KB

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