Skip to content
Snippets Groups Projects
check.mjs 9.15 KiB
Newer Older
GyDi's avatar
GyDi committed
import fs from "fs-extra";
GyDi's avatar
GyDi committed
import zlib from "zlib";
GyDi's avatar
GyDi committed
import path from "path";
import AdmZip from "adm-zip";
import fetch from "node-fetch";
import proxyAgent from "https-proxy-agent";
GyDi's avatar
GyDi committed
import { execSync } from "child_process";

const cwd = process.cwd();
GyDi's avatar
GyDi committed
const TEMP_DIR = path.join(cwd, "node_modules/.verge");
GyDi's avatar
GyDi committed
const FORCE = process.argv.includes("--force");
GyDi's avatar
GyDi committed

const SIDECAR_HOST = execSync("rustc -vV")
  .toString()
  .match(/(?<=host: ).+(?=\s*)/g)[0];

/* ======= clash ======= */
GyDi's avatar
GyDi committed
const CLASH_STORAGE_PREFIX = "https://release.dreamacro.workers.dev/";
GyDi's avatar
GyDi committed
const CLASH_URL_PREFIX =
  "https://github.com/Dreamacro/clash/releases/download/premium/";
GyDi's avatar
GyDi committed
const CLASH_LATEST_DATE = "2023.07.22";
GyDi's avatar
GyDi committed

const CLASH_MAP = {
  "win32-x64": "clash-windows-amd64",
  "darwin-x64": "clash-darwin-amd64",
  "darwin-arm64": "clash-darwin-arm64",
  "linux-x64": "clash-linux-amd64",
  "linux-arm64": "clash-linux-armv8",
};

/* ======= clash meta ======= */
const META_URL_PREFIX = `https://github.com/MetaCubeX/Clash.Meta/releases/download/`;
GyDi's avatar
GyDi committed
const META_VERSION = "v1.15.0";
GyDi's avatar
GyDi committed

const META_MAP = {
GyDi's avatar
GyDi committed
  "win32-x64": "clash.meta-windows-amd64-compatible",
  "darwin-x64": "clash.meta-darwin-amd64",
  "darwin-arm64": "clash.meta-darwin-arm64",
  "linux-x64": "clash.meta-linux-amd64-compatible",
  "linux-arm64": "clash.meta-linux-arm64",
GyDi's avatar
GyDi committed
};
GyDi's avatar
GyDi committed

/**
GyDi's avatar
GyDi committed
 * check available
GyDi's avatar
GyDi committed
 */
GyDi's avatar
GyDi committed

GyDi's avatar
GyDi committed
const { platform, arch } = process;
if (!CLASH_MAP[`${platform}-${arch}`]) {
GyDi's avatar
GyDi committed
  throw new Error(`clash unsupported platform "${platform}-${arch}"`);
GyDi's avatar
GyDi committed
}
if (!META_MAP[`${platform}-${arch}`]) {
GyDi's avatar
GyDi committed
  throw new Error(`clash meta unsupported platform "${platform}-${arch}"`);
GyDi's avatar
GyDi committed
}
GyDi's avatar
GyDi committed

GyDi's avatar
GyDi committed
function clash() {
  const name = CLASH_MAP[`${platform}-${arch}`];
GyDi's avatar
GyDi committed

  const isWin = platform === "win32";
GyDi's avatar
GyDi committed
  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,
GyDi's avatar
GyDi committed
}
GyDi's avatar
GyDi committed
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,
  };
}

GyDi's avatar
GyDi committed
function clashMeta() {
  const name = META_MAP[`${platform}-${arch}`];
  const isWin = platform === "win32";
GyDi's avatar
GyDi committed
  const urlExt = isWin ? "zip" : "gz";
  const downloadURL = `${META_URL_PREFIX}${META_VERSION}/${name}-${META_VERSION}.${urlExt}`;
  const exeFile = `${name}${isWin ? ".exe" : ""}`;
  const zipFile = `${name}-${META_VERSION}.${urlExt}`;

  return {
    name: "clash-meta",
    targetFile: `clash-meta-${SIDECAR_HOST}${isWin ? ".exe" : ""}`,
    exeFile,
    zipFile,
    downloadURL,
  };
GyDi's avatar
GyDi committed
/**
GyDi's avatar
GyDi committed
 * download sidecar and rename
GyDi's avatar
GyDi committed
 */
GyDi's avatar
GyDi committed
async function resolveSidecar(binInfo) {
  const { name, targetFile, zipFile, exeFile, downloadURL } = binInfo;
GyDi's avatar
GyDi committed
  const sidecarDir = path.join(cwd, "src-tauri", "sidecar");
  const sidecarPath = path.join(sidecarDir, targetFile);
GyDi's avatar
GyDi committed
  await fs.mkdirp(sidecarDir);
  if (!FORCE && (await fs.pathExists(sidecarPath))) return;
GyDi's avatar
GyDi committed
  const tempDir = path.join(TEMP_DIR, name);
  const tempZip = path.join(tempDir, zipFile);
  const tempExe = path.join(tempDir, exeFile);
GyDi's avatar
GyDi committed
  await fs.mkdirp(tempDir);
GyDi's avatar
GyDi committed
  try {
GyDi's avatar
GyDi committed
    if (!(await fs.pathExists(tempZip))) {
GyDi's avatar
GyDi committed
      await downloadFile(downloadURL, tempZip);
GyDi's avatar
GyDi committed
    }
GyDi's avatar
GyDi committed

    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 {
      // gz
      const readStream = fs.createReadStream(tempZip);
      const writeStream = fs.createWriteStream(sidecarPath);
      await new Promise((resolve, reject) => {
        const onError = (error) => {
GyDi's avatar
GyDi committed
          console.error(`[ERROR]: "${name}" gz failed:`, error.message);
GyDi's avatar
GyDi committed
          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);
GyDi's avatar
GyDi committed
    }
  } catch (err) {
GyDi's avatar
GyDi committed
    // 需要删除文件
    await fs.remove(sidecarPath);
GyDi's avatar
GyDi committed
    throw err;
  } finally {
    // delete temp dir
    await fs.remove(tempDir);
GyDi's avatar
GyDi committed
  }
GyDi's avatar
GyDi committed
}
GyDi's avatar
GyDi committed

GyDi's avatar
GyDi committed
/**
 * 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());
  }
GyDi's avatar
GyDi committed
/**
 * only Windows
 * get the wintun.dll (not required)
 */
async function resolveWintun() {
  const { platform } = process;

  if (platform !== "win32") return;

  const url = "https://www.wintun.net/builds/wintun-0.14.1.zip";

GyDi's avatar
GyDi committed
  const tempDir = path.join(TEMP_DIR, "wintun");
GyDi's avatar
GyDi committed
  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`);
}

GyDi's avatar
GyDi committed
/**
GyDi's avatar
GyDi committed
 * download the file to the resources dir
GyDi's avatar
GyDi committed
 */
GyDi's avatar
GyDi committed
async function resolveResource(binInfo) {
  const { file, downloadURL } = binInfo;
GyDi's avatar
GyDi committed

  const resDir = path.join(cwd, "src-tauri/resources");
GyDi's avatar
GyDi committed
  const targetPath = path.join(resDir, file);
GyDi's avatar
GyDi committed

GyDi's avatar
GyDi committed
  if (!FORCE && (await fs.pathExists(targetPath))) return;

  await fs.mkdirp(resDir);
GyDi's avatar
GyDi committed
  await downloadFile(downloadURL, targetPath);
GyDi's avatar
GyDi committed
  console.log(`[INFO]: ${file} finished`);
GyDi's avatar
GyDi committed
/**
 * 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);
  }

GyDi's avatar
GyDi committed
  const response = await fetch(url, {
    ...options,
GyDi's avatar
GyDi committed
    method: "GET",
    headers: { "Content-Type": "application/octet-stream" },
  });
  const buffer = await response.arrayBuffer();
  await fs.writeFile(path, new Uint8Array(buffer));
GyDi's avatar
GyDi committed

  console.log(`[INFO]: download finished "${url}"`);
GyDi's avatar
GyDi committed
/**
 * 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/Dreamacro/maxmind-geoip/releases/download/20230712/Country.mmdb`,
GyDi's avatar
GyDi committed
  });
const resolveGeosite = () =>
  resolveResource({
    file: "geosite.dat",
    downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat`,
GyDi's avatar
GyDi committed
const resolveGeoIP = () =>
  resolveResource({
    file: "geoip.dat",
    downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip.dat`,
GyDi's avatar
GyDi committed

const tasks = [
GyDi's avatar
GyDi committed
  { name: "clash", func: resolveClash, retry: 5 },
GyDi's avatar
GyDi committed
  { name: "clash-meta", func: () => resolveSidecar(clashMeta()), retry: 5 },
  { name: "wintun", func: resolveWintun, retry: 5, winOnly: true },
  { 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 },
];

async function runTask() {
  const task = tasks.shift();
  if (!task) return;
GyDi's avatar
GyDi committed
  if (task.winOnly && process.platform !== "win32") return runTask();
GyDi's avatar
GyDi committed

  for (let i = 0; i < task.retry; i++) {
    try {
      await task.func();
      break;
    } catch (err) {
GyDi's avatar
GyDi committed
      console.error(`[ERROR]: task::${task.name} try ${i} ==`, err.message);
      if (i === task.retry - 1) throw err;
GyDi's avatar
GyDi committed
    }
  }
  return runTask();
}

runTask();
runTask();