check.mjs 12 KB

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