refactor: slop cleanup

This commit is contained in:
zhom
2025-07-31 05:28:45 +04:00
parent 63000c72bd
commit fc6ddb7cbf
9 changed files with 185 additions and 242 deletions
+2 -83
View File
@@ -1,5 +1,6 @@
import { spawn } from "node:child_process";
import path from "node:path";
import type { LaunchOptions } from "camoufox-js/dist/utils.js";
import {
type CamoufoxConfig,
deleteCamoufoxConfig,
@@ -9,88 +10,6 @@ import {
saveCamoufoxConfig,
} from "./camoufox-storage.js";
export interface CamoufoxLaunchOptions {
// Operating system to use for fingerprint generation
os?: "windows" | "macos" | "linux" | ("windows" | "macos" | "linux")[];
// Blocking options
block_images?: boolean;
block_webrtc?: boolean;
block_webgl?: boolean;
// Security options
disable_coop?: boolean;
// Geolocation options
geoip?: string | boolean;
// UI behavior
humanize?: boolean | number;
// Localization
locale?: string | string[];
// Extensions and fonts
addons?: string[];
fonts?: string[];
custom_fonts_only?: boolean;
exclude_addons?: "UBO"[];
// Screen and window
screen?: {
minWidth?: number;
maxWidth?: number;
minHeight?: number;
maxHeight?: number;
};
window?: [number, number];
fingerprint?: any;
disableTheming?: boolean;
showcursor?: boolean;
// Version and mode
ff_version?: number;
headless?: boolean;
main_world_eval?: boolean;
// Custom executable path
executable_path?: string;
// Firefox preferences
firefox_user_prefs?: Record<string, unknown>;
user_data_dir?: string;
// Proxy settings
proxy?:
| string
| {
server: string;
username?: string;
password?: string;
bypass?: string;
};
// Cache and performance
enable_cache?: boolean;
// Additional options
args?: string[];
env?: Record<string, string | number | boolean>;
debug?: boolean;
virtual_display?: string;
webgl_config?: [string, string];
// Custom options
timezone?: string;
country?: string;
geolocation?: {
latitude: number;
longitude: number;
accuracy?: number;
};
}
/**
* Start a Camoufox instance in a separate process
* @param options Camoufox launch options
@@ -99,7 +18,7 @@ export interface CamoufoxLaunchOptions {
* @returns Promise resolving to the Camoufox configuration
*/
export async function startCamoufoxProcess(
options: CamoufoxLaunchOptions = {},
options: LaunchOptions = {},
profilePath?: string,
url?: string,
): Promise<CamoufoxConfig> {
+2 -2
View File
@@ -1,11 +1,11 @@
import fs from "node:fs";
import path from "node:path";
import type { LaunchOptions } from "camoufox-js/dist/utils.js";
import tmp from "tmp";
import type { CamoufoxLaunchOptions } from "./camoufox-launcher.js";
export interface CamoufoxConfig {
id: string;
options: CamoufoxLaunchOptions;
options: LaunchOptions;
profilePath?: string;
url?: string;
port?: number;
+7 -41
View File
@@ -1,4 +1,5 @@
import type { Browser, BrowserContext, Page } from "playwright-core";
import { Camoufox } from "camoufox-js";
import type { Page } from "playwright-core";
import { getCamoufoxConfig, saveCamoufoxConfig } from "./camoufox-storage.js";
/**
@@ -46,15 +47,8 @@ export async function runCamoufoxWorker(id: string): Promise<void> {
process.on("SIGTERM", () => void gracefulShutdown());
process.on("SIGINT", () => void gracefulShutdown());
// Keep the process alive
setInterval(() => {
// Keep alive
}, 1000);
// Launch browser in background - this can take time and may fail
setImmediate(async () => {
let browser: Browser | null = null;
let context: BrowserContext | null = null;
let page: Page | null = null;
try {
@@ -66,7 +60,7 @@ export async function runCamoufoxWorker(id: string): Promise<void> {
camoufoxOptions.user_data_dir = config.profilePath;
}
// Set anti-detect options
// Theming
camoufoxOptions.disableTheming = true;
camoufoxOptions.showcursor = false;
@@ -75,36 +69,8 @@ export async function runCamoufoxWorker(id: string): Promise<void> {
camoufoxOptions.headless = false;
}
// Import Camoufox dynamically
let Camoufox: any;
try {
const camoufoxModule = await import("camoufox-js");
Camoufox = camoufoxModule.Camoufox;
} catch (importError) {
// If Camoufox is not available, just keep the process alive
return;
}
// Launch Camoufox with timeout
const result = await Promise.race([
Camoufox(camoufoxOptions),
new Promise((_, reject) =>
setTimeout(() => reject(new Error("Camoufox launch timeout")), 30000),
),
]);
// Handle the result
if ("browser" in result && typeof result.browser === "function") {
context = result;
browser = context?.browser() ?? null;
} else {
browser = result as Browser;
context = await browser.newContext();
}
if (!browser) {
throw new Error("Failed to get browser instance");
}
const browser = await Camoufox(camoufoxOptions);
const context = await browser.newContext();
// Update config with actual browser details
let wsEndpoint: string | undefined;
@@ -129,7 +95,7 @@ export async function runCamoufoxWorker(id: string): Promise<void> {
waitUntil: "domcontentloaded",
timeout: 30000,
});
} catch (error) {
} catch {
// URL opening failure doesn't affect startup success
}
}
@@ -146,7 +112,7 @@ export async function runCamoufoxWorker(id: string): Promise<void> {
process.exit(0);
}
}, 2000);
} catch (error) {
} catch {
// Browser launch failed, but worker is still "successful"
// Process will stay alive due to the main setInterval above
}
+8 -11
View File
@@ -1,6 +1,6 @@
import type { LaunchOptions } from "camoufox-js/dist/utils.js";
import { program } from "commander";
import {
type CamoufoxLaunchOptions,
startCamoufoxProcess,
stopAllCamoufoxProcesses,
stopCamoufoxProcess,
@@ -241,15 +241,8 @@ program
// Firefox preferences
.option("--firefox-prefs <prefs>", "Firefox user preferences (JSON string)")
// Anti-detect options
.option(
"--disable-theming",
"disable Firefox theming (required for anti-detect)",
)
.option(
"--no-showcursor",
"disable cursor display (required for anti-detect)",
)
.option("--disable-theming", "disable Firefox theming")
.option("--no-showcursor", "disable cursor display")
.description("manage Camoufox browser instances")
.action(
@@ -260,7 +253,7 @@ program
if (action === "start") {
try {
// Build Camoufox options in the format expected by camoufox-js
const camoufoxOptions: CamoufoxLaunchOptions = {};
const camoufoxOptions: LaunchOptions = {};
// OS fingerprinting
if (options.os && typeof options.os === "string") {
@@ -403,6 +396,10 @@ program
}
}
// Theming
if (options.disableTheming) camoufoxOptions.disableTheming = true;
if (options.noShowcursor) camoufoxOptions.showcursor = false;
// Use the launcher to start Camoufox properly
const config = await startCamoufoxProcess(
camoufoxOptions,