Spaces:
Paused
Paused
File size: 10,297 Bytes
d85b21d 3d01305 d85b21d 3d01305 d85b21d 6c928c9 d85b21d 6c928c9 d85b21d 178e38e 3d01305 178e38e 3d01305 d85b21d 178e38e d85b21d 178e38e d85b21d 178e38e d85b21d 3d01305 d85b21d 3d01305 d85b21d 3d01305 178e38e 3d01305 178e38e 3d01305 d85b21d 3d01305 d85b21d 3d01305 d85b21d 3d01305 d85b21d 3d01305 d85b21d 6c928c9 d85b21d 3d01305 d85b21d 3d01305 d85b21d 3d01305 d85b21d 3d01305 d85b21d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | #!/usr/bin/env tsx
/**
* Download curl-impersonate (lexiforest fork) prebuilt binary.
*
* Usage: npm run setup
* tsx scripts/setup-curl.ts
*
* Detects platform + arch, downloads the matching release from GitHub,
* extracts curl-impersonate into bin/.
*/
import { execSync } from "child_process";
import { existsSync, mkdirSync, chmodSync, readdirSync, copyFileSync, rmSync, writeFileSync } from "fs";
import { resolve, join } from "path";
const REPO = "lexiforest/curl-impersonate";
const FALLBACK_VERSION = "v1.4.4";
const BIN_DIR = resolve(process.cwd(), "bin");
const CACERT_URL = "https://curl.se/ca/cacert.pem";
interface PlatformInfo {
/** Pattern to match the asset name in GitHub Releases */
assetPattern: RegExp;
/** Name of the binary inside the archive */
binaryName: string;
/** Name to save the binary as in bin/ */
destName: string;
}
/** Parse --arch flag from CLI args to override process.arch (for cross-compilation). */
function getTargetArch(): string {
const idx = process.argv.indexOf("--arch");
if (idx !== -1 && process.argv[idx + 1]) {
return process.argv[idx + 1];
}
return process.arch;
}
function getPlatformInfo(version: string): PlatformInfo {
const platform = process.platform;
const arch = getTargetArch();
if (platform === "linux") {
const archStr = arch === "arm64" ? "aarch64-linux-gnu" : "x86_64-linux-gnu";
return {
assetPattern: new RegExp(`^curl-impersonate-${version.replaceAll(".", "\\.")}\\.${archStr}\\.tar\\.gz$`),
binaryName: "curl-impersonate",
destName: "curl-impersonate",
};
}
if (platform === "darwin") {
const archStr = arch === "arm64" ? "arm64-macos" : "x86_64-macos";
return {
assetPattern: new RegExp(`^curl-impersonate-${version.replaceAll(".", "\\.")}\\.${archStr}\\.tar\\.gz$`),
binaryName: "curl-impersonate",
destName: "curl-impersonate",
};
}
if (platform === "win32") {
// Windows: download libcurl-impersonate package
// v1.5+ renamed libcurl.dll → libcurl-impersonate.dll
return {
assetPattern: /libcurl-impersonate-.*\.x86_64-win32\.tar\.gz/,
binaryName: "libcurl-impersonate.dll",
destName: "libcurl.dll",
};
}
throw new Error(`Unsupported platform: ${platform}-${arch}`);
}
function githubHeaders(): Record<string, string> {
const h: Record<string, string> = { "Accept": "application/vnd.github+json" };
const token = process.env.GITHUB_TOKEN;
if (token) h["Authorization"] = `Bearer ${token}`;
return h;
}
/** Fetch the latest release tag from GitHub. */
async function getLatestVersion(): Promise<string> {
const apiUrl = `https://api.github.com/repos/${REPO}/releases/latest`;
console.log(`[setup] Checking latest release...`);
const resp = await fetch(apiUrl, { headers: githubHeaders() });
if (!resp.ok) {
console.warn(`[setup] Could not fetch latest release (${resp.status}), using fallback ${FALLBACK_VERSION}`);
return FALLBACK_VERSION;
}
const release = (await resp.json()) as { tag_name: string };
return release.tag_name;
}
async function getDownloadUrl(info: PlatformInfo, version: string): Promise<string> {
const apiUrl = `https://api.github.com/repos/${REPO}/releases/tags/${version}`;
console.log(`[setup] Fetching release info from ${apiUrl}`);
const resp = await fetch(apiUrl, { headers: githubHeaders() });
if (!resp.ok) {
throw new Error(`GitHub API returned ${resp.status}: ${await resp.text()}`);
}
const release = (await resp.json()) as { assets: { name: string; browser_download_url: string }[] };
const asset = release.assets.find((a) => info.assetPattern.test(a.name));
if (!asset) {
const relevantAssets = release.assets
.filter((a) =>
a.name.startsWith("curl-impersonate-") || a.name.startsWith("libcurl-impersonate-"),
)
.map((a) => a.name)
.join("\n ");
throw new Error(
`No matching asset for pattern ${info.assetPattern}.\nAvailable assets:\n ${relevantAssets}`,
);
}
console.log(`[setup] Found asset: ${asset.name}`);
return asset.browser_download_url;
}
function downloadAndExtract(url: string, info: PlatformInfo): void {
if (!existsSync(BIN_DIR)) {
mkdirSync(BIN_DIR, { recursive: true });
}
const tmpDir = resolve(BIN_DIR, ".tmp-extract");
if (existsSync(tmpDir)) {
rmSync(tmpDir, { recursive: true });
}
mkdirSync(tmpDir, { recursive: true });
const archivePath = resolve(tmpDir, "archive.tar.gz");
console.log(`[setup] Downloading ${url}...`);
execSync(`curl -L -o "${archivePath}" "${url}"`, { stdio: "inherit" });
console.log(`[setup] Extracting...`);
if (process.platform === "win32") {
// Windows bsdtar (default on CI) handles D: paths fine without --force-local
// GNU tar (e.g. Git Bash) needs --force-local; try without first, fallback with
const tarArchive = archivePath.replaceAll("\\", "/");
const tarDest = tmpDir.replaceAll("\\", "/");
try {
execSync(`tar xzf "${tarArchive}" -C "${tarDest}"`, { stdio: "inherit" });
} catch {
execSync(`tar xzf "${tarArchive}" --force-local -C "${tarDest}"`, { stdio: "inherit" });
}
} else {
execSync(`tar xzf "${archivePath}" -C "${tmpDir}"`, { stdio: "inherit" });
}
// Find the binary in extracted files (may be in a subdirectory)
const binary = findFile(tmpDir, info.binaryName);
if (!binary) {
const files = listFilesRecursive(tmpDir);
throw new Error(
`Could not find ${info.binaryName} in extracted archive.\nFiles found:\n ${files.join("\n ")}`,
);
}
const destPath = resolve(BIN_DIR, info.destName);
copyFileSync(binary, destPath);
// Also copy shared libraries (.so/.dylib/.dll) if present alongside the binary
const libDir = resolve(binary, "..");
if (existsSync(libDir)) {
const libs = readdirSync(libDir).filter(
(f) =>
f.endsWith(".so") || f.includes(".so.") ||
f.endsWith(".dylib") ||
(f.endsWith(".dll") && f !== info.destName),
);
for (const lib of libs) {
copyFileSync(resolve(libDir, lib), resolve(BIN_DIR, lib));
console.log(`[setup] Copied companion library: ${lib}`);
}
}
chmodSync(destPath, 0o755);
// Cleanup
rmSync(tmpDir, { recursive: true });
console.log(`[setup] Installed ${info.destName} to ${destPath}`);
}
function findFile(dir: string, name: string): string | null {
const entries = readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = join(dir, entry.name);
if (entry.isDirectory()) {
const found = findFile(fullPath, name);
if (found) return found;
} else if (entry.name === name) {
return fullPath;
}
}
return null;
}
function listFilesRecursive(dir: string): string[] {
const results: string[] = [];
const entries = readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = join(dir, entry.name);
if (entry.isDirectory()) {
results.push(...listFilesRecursive(fullPath));
} else {
results.push(fullPath);
}
}
return results;
}
/**
* Download Mozilla CA certificate bundle for BoringSSL (used on Windows).
* libcurl-impersonate uses BoringSSL which doesn't read the Windows cert store,
* so we need an explicit CA bundle.
*/
async function downloadCaCert(force: boolean): Promise<void> {
const caPath = resolve(BIN_DIR, "cacert.pem");
if (existsSync(caPath) && !force) {
console.log(`[setup] cacert.pem already exists`);
return;
}
console.log(`[setup] Downloading CA bundle from ${CACERT_URL}...`);
const resp = await fetch(CACERT_URL);
if (!resp.ok) {
console.warn(`[setup] Warning: could not download CA bundle (${resp.status}). HTTPS may fail.`);
return;
}
const content = await resp.text();
if (!existsSync(BIN_DIR)) {
mkdirSync(BIN_DIR, { recursive: true });
}
writeFileSync(caPath, content, "utf-8");
console.log(`[setup] Installed CA bundle to ${caPath}`);
}
async function main() {
const checkOnly = process.argv.includes("--check");
const force = process.argv.includes("--force");
// Resolve latest version from GitHub
const version = await getLatestVersion();
console.log(`[setup] curl-impersonate setup (${version})`);
const targetArch = getTargetArch();
console.log(`[setup] Platform: ${process.platform}-${targetArch}${targetArch !== process.arch ? ` (cross: host=${process.arch})` : ""}`);
const info = getPlatformInfo(version);
const isWindowsDll = process.platform === "win32";
const destBinary = resolve(BIN_DIR, info.destName);
if (checkOnly) {
if (existsSync(destBinary)) {
if (isWindowsDll) {
console.log(`[setup] ${info.destName} exists`);
} else {
try {
const ver = execSync(`"${destBinary}" --version`, { encoding: "utf-8" }).trim().split("\n")[0];
console.log(`[setup] Current: ${ver}`);
} catch {
console.log(`[setup] Binary exists but version check failed`);
}
}
console.log(`[setup] Latest: ${version}`);
} else {
console.log(`[setup] Not installed. Latest: ${version}`);
}
return;
}
if (existsSync(destBinary) && !force) {
console.log(`[setup] ${destBinary} already exists. Use --force to re-download.`);
return;
}
if (force && existsSync(destBinary)) {
rmSync(destBinary);
console.log(`[setup] Removed existing binary for forced re-download.`);
}
const url = await getDownloadUrl(info, version);
downloadAndExtract(url, info);
// Verify the binary runs (skip for Windows DLL — no CLI to test)
if (!isWindowsDll) {
try {
const ver = execSync(`"${destBinary}" --version`, { encoding: "utf-8" }).trim().split("\n")[0];
console.log(`[setup] Verified: ${ver}`);
} catch {
console.warn(`[setup] Warning: could not verify binary. It may need shared libraries.`);
}
} else {
console.log(`[setup] Installed libcurl-impersonate DLL for FFI transport`);
// BoringSSL needs a CA bundle — download it
await downloadCaCert(force);
}
console.log(`[setup] Done! curl-impersonate is ready.`);
}
main().catch((err) => {
console.error(`[setup] Error: ${err.message}`);
process.exit(1);
});
|