clash-verge-rev/scripts/check.mjs

418 lines
12 KiB
JavaScript
Raw Normal View History

2021-12-24 01:58:15 +08:00
import fs from "fs-extra";
2021-12-29 01:01:09 +08:00
import zlib from "zlib";
import tar from "tar";
2021-12-24 01:58:15 +08:00
import path from "path";
import AdmZip from "adm-zip";
import fetch from "node-fetch";
import proxyAgent from "https-proxy-agent";
2021-12-24 01:58:15 +08:00
import { execSync } from "child_process";
const cwd = process.cwd();
2022-04-24 15:35:30 +08:00
const TEMP_DIR = path.join(cwd, "node_modules/.verge");
2022-03-09 20:14:15 +08:00
const FORCE = process.argv.includes("--force");
2022-11-22 20:44:44 +08:00
/* ======= clash =======
2023-07-11 13:25:55 +08:00
const CLASH_STORAGE_PREFIX = "https://release.dreamacro.workers.dev/";
2022-11-22 20:44:44 +08:00
const CLASH_URL_PREFIX =
"https://github.com/Dreamacro/clash/releases/download/premium/";
2023-11-03 15:52:36 +08:00
const CLASH_LATEST_DATE = "latest";
2022-11-22 20:44:44 +08:00
const CLASH_BACKUP_URL_PREFIX =
"https://github.com/zhongfly/Clash-premium-backup/releases/download/";
const CLASH_BACKUP_LATEST_DATE = "2023-09-05-gdcc8d87";
//https://github.com/zhongfly/Clash-premium-backup/releases/download/2023-09-05-gdcc8d87/clash-windows-amd64-2023-09-05-gdcc8d87.zip
//https://github.com/zhongfly/Clash-premium-backup/releases/download/2023-09-05-gdcc8d87/clash-windows-amd64-n2023-09-05-gdcc8d87.zip
2022-11-22 20:44:44 +08:00
const CLASH_MAP = {
"win32-x64": "clash-windows-amd64",
"darwin-x64": "clash-darwin-amd64",
"darwin-arm64": "clash-darwin-arm64",
"linux-x64": "clash-linux-amd64",
2023-09-10 19:06:02 +08:00
"linux-arm64": "clash-linux-arm64",
2022-11-22 20:44:44 +08:00
};
*/
2022-11-22 20:44:44 +08:00
/* ======= clash meta ======= */
const META_URL_PREFIX = `https://github.com/wonfen/Clash.Meta/releases/download/latest`;
// const META_VERSION = "2023.11.23";
2022-11-22 20:44:44 +08:00
const META_MAP = {
"win32-x64": "clash.meta-win-amd64",
"win32-arm64": "clash.meta-win-arm64",
2023-07-11 13:25:55 +08:00
"darwin-x64": "clash.meta-darwin-amd64",
"darwin-arm64": "clash.meta-darwin-arm64",
"linux-x64": "clash.meta-linux-amd64",
2023-07-11 13:25:55 +08:00
"linux-arm64": "clash.meta-linux-arm64",
2022-11-22 20:44:44 +08:00
};
2021-12-24 01:58:15 +08:00
/*
2022-11-22 20:44:44 +08:00
* check available
2021-12-24 01:58:15 +08:00
*/
const PLATFORM_MAP = {
"x86_64-pc-windows-msvc": "win32",
"aarch64-pc-windows-msvc": "win32",
"x86_64-apple-darwin": "darwin",
"aarch64-apple-darwin": "darwin",
"x86_64-unknown-linux-gnu": "linux",
"aarch64-unknown-linux-gnu": "linux",
};
const ARCH_MAP = {
"x86_64-pc-windows-msvc": "x64",
"aarch64-pc-windows-msvc": "arm64",
"x86_64-apple-darwin": "x64",
"aarch64-apple-darwin": "arm64",
"x86_64-unknown-linux-gnu": "x64",
"aarch64-unknown-linux-gnu": "arm64",
};
const target = process.argv.slice(2)[0];
const { platform, arch } = target
? { platform: PLATFORM_MAP[target], arch: ARCH_MAP[target] }
: process;
2021-12-29 01:01:09 +08:00
const SIDECAR_HOST = target
? target
: execSync("rustc -vV")
.toString()
.match(/(?<=host: ).+(?=\s*)/g)[0];
/*
2022-11-22 20:44:44 +08:00
if (!CLASH_MAP[`${platform}-${arch}`]) {
2023-01-30 20:50:08 +08:00
throw new Error(`clash unsupported platform "${platform}-${arch}"`);
2022-11-22 20:44:44 +08:00
}
*/
2022-11-22 20:44:44 +08:00
if (!META_MAP[`${platform}-${arch}`]) {
2023-01-30 20:50:08 +08:00
throw new Error(`clash meta unsupported platform "${platform}-${arch}"`);
2022-11-22 20:44:44 +08:00
}
/*
2022-11-22 20:44:44 +08:00
function clash() {
const name = CLASH_MAP[`${platform}-${arch}`];
2021-12-24 01:58:15 +08:00
const isWin = platform === "win32";
2022-11-22 20:44:44 +08:00
const urlExt = isWin ? "zip" : "gz";
const downloadURL = `${CLASH_URL_PREFIX}${name}-${CLASH_LATEST_DATE}.${urlExt}`;
const exeFile = `${name}${isWin ? ".exe" : ""}`;
const zipFile = `${name}.${urlExt}`;
return {
name: "clash",
targetFile: `clash-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
exeFile,
zipFile,
downloadURL,
2022-05-16 01:52:50 +08:00
};
2022-11-22 20:44:44 +08:00
}
2022-05-16 01:52:50 +08:00
function clashBackup() {
const name = CLASH_MAP[`${platform}-${arch}`];
const isWin = platform === "win32";
const urlExt = isWin ? "zip" : "gz";
const downloadURL = `${CLASH_BACKUP_URL_PREFIX}${CLASH_BACKUP_LATEST_DATE}/${name}-n${CLASH_BACKUP_LATEST_DATE}.${urlExt}`;
const exeFile = `${name}${isWin ? ".exe" : ""}`;
const zipFile = `${name}.${urlExt}`;
return {
name: "clash",
targetFile: `clash-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
exeFile,
zipFile,
downloadURL,
};
}
2023-07-11 13:25:55 +08:00
function clashS3() {
const name = CLASH_MAP[`${platform}-${arch}`];
const isWin = platform === "win32";
const urlExt = isWin ? "zip" : "gz";
const downloadURL = `${CLASH_STORAGE_PREFIX}${CLASH_LATEST_DATE}/${name}-${CLASH_LATEST_DATE}.${urlExt}`;
const exeFile = `${name}${isWin ? ".exe" : ""}`;
const zipFile = `${name}.${urlExt}`;
return {
name: "clash",
targetFile: `clash-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
exeFile,
zipFile,
downloadURL,
};
}
*/
2022-11-22 20:44:44 +08:00
function clashMeta() {
const name = META_MAP[`${platform}-${arch}`];
2022-05-16 01:52:50 +08:00
const isWin = platform === "win32";
/* const urlExt = isWin ? "zip" : "gz";
2022-11-22 20:44:44 +08:00
const downloadURL = `${META_URL_PREFIX}${META_VERSION}/${name}-${META_VERSION}.${urlExt}`;
const exeFile = `${name}${isWin ? ".exe" : ""}`;
const zipFile = `${name}-${META_VERSION}.${urlExt}`; */
const urlExt = isWin ? "zip" : "tgz";
const downloadURL = `${META_URL_PREFIX}/${name}.${urlExt}`;
const exeFile = isWin ? "虚空终端-win-amd64.exe" : name;
const zipFile = `${name}.${urlExt}`;
2022-11-22 20:44:44 +08:00
return {
name: "clash-meta",
targetFile: `clash-meta-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
exeFile,
zipFile,
downloadURL,
};
2022-05-16 01:52:50 +08:00
}
2021-12-24 01:58:15 +08:00
/**
2022-11-22 20:44:44 +08:00
* download sidecar and rename
2021-12-24 01:58:15 +08:00
*/
2022-11-22 20:44:44 +08:00
async function resolveSidecar(binInfo) {
const { name, targetFile, zipFile, exeFile, downloadURL } = binInfo;
2022-05-16 01:52:50 +08:00
2022-11-22 20:44:44 +08:00
const sidecarDir = path.join(cwd, "src-tauri", "sidecar");
const sidecarPath = path.join(sidecarDir, targetFile);
2022-05-16 01:52:50 +08:00
2022-11-22 20:44:44 +08:00
await fs.mkdirp(sidecarDir);
if (!FORCE && (await fs.pathExists(sidecarPath))) return;
2022-05-16 01:52:50 +08:00
2022-11-22 20:44:44 +08:00
const tempDir = path.join(TEMP_DIR, name);
const tempZip = path.join(tempDir, zipFile);
const tempExe = path.join(tempDir, exeFile);
2022-05-16 01:52:50 +08:00
2022-11-22 20:44:44 +08:00
await fs.mkdirp(tempDir);
2023-07-11 13:25:55 +08:00
try {
2023-07-23 13:11:17 +08:00
if (!(await fs.pathExists(tempZip))) {
2023-07-11 13:25:55 +08:00
await downloadFile(downloadURL, tempZip);
2023-07-23 13:11:17 +08:00
}
2023-07-11 13:25:55 +08:00
if (zipFile.endsWith(".zip")) {
const zip = new AdmZip(tempZip);
zip.getEntries().forEach((entry) => {
console.log(`[DEBUG]: "${name}" entry name`, entry.entryName);
});
zip.extractAllTo(tempDir, true);
await fs.rename(tempExe, sidecarPath);
console.log(`[INFO]: "${name}" unzip finished`);
} else if (zipFile.endsWith(".tgz")) {
// tgz
await fs.mkdirp(tempDir);
await tar.extract({
cwd: tempDir,
file: tempZip,
//strip: 1, // 可能需要根据实际的 .tgz 文件结构调整
});
const files = await fs.readdir(tempDir);
console.log(`[DEBUG]: "${name}" files in tempDir:`, files);
const extractedFile = files.find((file) => file.startsWith("虚空终端-"));
if (extractedFile) {
const extractedFilePath = path.join(tempDir, extractedFile);
await fs.rename(extractedFilePath, sidecarPath);
console.log(`[INFO]: "${name}" file renamed to "${sidecarPath}"`);
execSync(`chmod 755 ${sidecarPath}`);
console.log(`[INFO]: "${name}" chmod binary finished`);
} else {
throw new Error(`Expected file not found in ${tempDir}`);
}
2023-07-11 13:25:55 +08:00
} else {
// gz
const readStream = fs.createReadStream(tempZip);
const writeStream = fs.createWriteStream(sidecarPath);
await new Promise((resolve, reject) => {
const onError = (error) => {
2023-07-23 13:11:17 +08:00
console.error(`[ERROR]: "${name}" gz failed:`, error.message);
2023-07-11 13:25:55 +08:00
reject(error);
};
readStream
.pipe(zlib.createGunzip().on("error", onError))
.pipe(writeStream)
.on("finish", () => {
console.log(`[INFO]: "${name}" gunzip finished`);
execSync(`chmod 755 ${sidecarPath}`);
console.log(`[INFO]: "${name}" chmod binary finished`);
resolve();
})
.on("error", onError);
2022-05-16 01:52:50 +08:00
});
2023-07-11 13:25:55 +08:00
}
} catch (err) {
2023-07-23 13:11:17 +08:00
// 需要删除文件
await fs.remove(sidecarPath);
2023-07-11 13:25:55 +08:00
throw err;
} finally {
// delete temp dir
await fs.remove(tempDir);
2021-12-29 01:01:09 +08:00
}
2023-07-11 13:25:55 +08:00
}
2021-12-24 01:58:15 +08:00
2023-07-11 13:25:55 +08:00
/**
* prepare clash core
* if the core version is not updated in time, use S3 storage as a backup.
*/
async function resolveClash() {
try {
return await resolveSidecar(clash());
} catch {
console.log(`[WARN]: clash core needs to be updated`);
return await resolveSidecar(clashS3());
}
2021-12-24 01:58:15 +08:00
}
2022-03-17 19:29:30 +08:00
/**
* only Windows
* get the wintun.dll (not required)
2022-03-17 19:29:30 +08:00
async function resolveWintun() {
const { platform } = process;
if (platform !== "win32") return;
const url = "https://www.wintun.net/builds/wintun-0.14.1.zip";
2022-04-24 15:35:30 +08:00
const tempDir = path.join(TEMP_DIR, "wintun");
2022-03-17 19:29:30 +08:00
const tempZip = path.join(tempDir, "wintun.zip");
const wintunPath = path.join(tempDir, "wintun/bin/amd64/wintun.dll");
const targetPath = path.join(cwd, "src-tauri/resources", "wintun.dll");
if (!FORCE && (await fs.pathExists(targetPath))) return;
await fs.mkdirp(tempDir);
if (!(await fs.pathExists(tempZip))) {
await downloadFile(url, tempZip);
}
// unzip
const zip = new AdmZip(tempZip);
zip.extractAllTo(tempDir, true);
if (!(await fs.pathExists(wintunPath))) {
throw new Error(`path not found "${wintunPath}"`);
}
await fs.rename(wintunPath, targetPath);
await fs.remove(tempDir);
console.log(`[INFO]: resolve wintun.dll finished`);
}
*/
2022-04-24 15:35:30 +08:00
/**
2022-11-22 20:44:44 +08:00
* download the file to the resources dir
2022-04-24 15:35:30 +08:00
*/
2022-11-22 20:44:44 +08:00
async function resolveResource(binInfo) {
const { file, downloadURL } = binInfo;
2022-04-24 15:35:30 +08:00
const resDir = path.join(cwd, "src-tauri/resources");
2022-11-22 20:44:44 +08:00
const targetPath = path.join(resDir, file);
2022-04-24 15:35:30 +08:00
2022-11-22 20:44:44 +08:00
if (!FORCE && (await fs.pathExists(targetPath))) return;
await fs.mkdirp(resDir);
2022-11-22 20:44:44 +08:00
await downloadFile(downloadURL, targetPath);
2022-11-22 20:44:44 +08:00
console.log(`[INFO]: ${file} finished`);
}
2021-12-24 01:58:15 +08:00
/**
* download file and save to `path`
*/
async function downloadFile(url, path) {
const options = {};
const httpProxy =
process.env.HTTP_PROXY ||
process.env.http_proxy ||
process.env.HTTPS_PROXY ||
process.env.https_proxy;
if (httpProxy) {
options.agent = proxyAgent(httpProxy);
}
2021-12-24 01:58:15 +08:00
const response = await fetch(url, {
...options,
2021-12-24 01:58:15 +08:00
method: "GET",
headers: { "Content-Type": "application/octet-stream" },
});
const buffer = await response.arrayBuffer();
await fs.writeFile(path, new Uint8Array(buffer));
2021-12-29 01:01:09 +08:00
console.log(`[INFO]: download finished "${url}"`);
2021-12-24 01:58:15 +08:00
}
2022-11-22 20:44:44 +08:00
/**
* main
*/
const SERVICE_URL =
"https://github.com/zzzgydi/clash-verge-service/releases/download/latest";
const resolveService = () =>
resolveResource({
file: "clash-verge-service.exe",
downloadURL: `${SERVICE_URL}/clash-verge-service.exe`,
});
const resolveInstall = () =>
resolveResource({
file: "install-service.exe",
downloadURL: `${SERVICE_URL}/install-service.exe`,
});
const resolveUninstall = () =>
resolveResource({
file: "uninstall-service.exe",
downloadURL: `${SERVICE_URL}/uninstall-service.exe`,
});
const resolveMmdb = () =>
resolveResource({
file: "Country.mmdb",
downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/country.mmdb`,
2022-11-22 20:44:44 +08:00
});
const resolveGeosite = () =>
resolveResource({
file: "geosite.dat",
downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat`,
});
2022-11-22 20:44:44 +08:00
const resolveGeoIP = () =>
resolveResource({
file: "geoip.dat",
downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip.dat`,
});
2023-11-22 16:15:41 +08:00
const resolveEnableLoopback = () =>
resolveResource({
file: "enableLoopback.exe",
downloadURL: `https://github.com/Kuingsmile/uwp-tool/releases/download/latest/enableLoopback.exe`,
});
2022-11-22 20:44:44 +08:00
const tasks = [
// { name: "clash", func: resolveClash, retry: 5 },
2022-11-22 20:44:44 +08:00
{ name: "clash-meta", func: () => resolveSidecar(clashMeta()), retry: 5 },
// { name: "wintun", func: resolveWintun, retry: 5, winOnly: true },
2022-11-22 20:44:44 +08:00
{ name: "service", func: resolveService, retry: 5, winOnly: true },
{ name: "install", func: resolveInstall, retry: 5, winOnly: true },
{ name: "uninstall", func: resolveUninstall, retry: 5, winOnly: true },
{ name: "mmdb", func: resolveMmdb, retry: 5 },
{ name: "geosite", func: resolveGeosite, retry: 5 },
{ name: "geoip", func: resolveGeoIP, retry: 5 },
2023-11-22 16:15:41 +08:00
{
name: "enableLoopback",
func: resolveEnableLoopback,
retry: 5,
winOnly: true,
},
2022-11-22 20:44:44 +08:00
];
async function runTask() {
const task = tasks.shift();
if (!task) return;
2022-11-22 23:01:04 +08:00
if (task.winOnly && process.platform !== "win32") return runTask();
2022-11-22 20:44:44 +08:00
for (let i = 0; i < task.retry; i++) {
try {
await task.func();
break;
} catch (err) {
2023-07-23 13:11:17 +08:00
console.error(`[ERROR]: task::${task.name} try ${i} ==`, err.message);
if (i === task.retry - 1) throw err;
2022-11-22 20:44:44 +08:00
}
}
return runTask();
}
runTask();
runTask();