@askjo/camofox-browser 1.5.2 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Dockerfile +17 -2
- package/README.md +138 -8
- package/camofox.config.json +18 -0
- package/lib/auth.js +71 -0
- package/lib/config.js +27 -1
- package/lib/cookies.js +38 -1
- package/lib/downloads.js +10 -2
- package/lib/extract.js +74 -0
- package/lib/inflight.js +16 -0
- package/lib/metrics.js +29 -0
- package/lib/openapi.js +100 -0
- package/lib/persistence.js +89 -0
- package/lib/plugins.js +175 -0
- package/lib/reporter.js +751 -0
- package/lib/tmp-cleanup.js +40 -0
- package/lib/tracing.js +137 -0
- package/openclaw.plugin.json +1 -1
- package/package.json +8 -2
- package/plugins/persistence/AGENTS.md +37 -0
- package/plugins/persistence/README.md +48 -0
- package/plugins/persistence/index.js +124 -0
- package/plugins/persistence/persistence.test.js +117 -0
- package/plugins/persistence/plugin.test.js +98 -0
- package/plugins/vnc/AGENTS.md +42 -0
- package/plugins/vnc/README.md +165 -0
- package/plugins/vnc/apt.txt +7 -0
- package/plugins/vnc/index.js +142 -0
- package/plugins/vnc/spawn.js +8 -0
- package/plugins/vnc/vnc-launcher.js +64 -0
- package/plugins/vnc/vnc-watcher.sh +82 -0
- package/plugins/vnc/vnc.test.js +204 -0
- package/plugins/youtube/AGENTS.md +25 -0
- package/plugins/youtube/apt.txt +1 -0
- package/plugins/youtube/index.js +206 -0
- package/plugins/youtube/post-install.sh +5 -0
- package/plugins/youtube/youtube.test.js +41 -0
- package/scripts/exec.js +8 -0
- package/scripts/generate-openapi.js +24 -0
- package/scripts/install-plugin-deps.sh +63 -0
- package/scripts/plugin.js +342 -0
- package/scripts/plugin.test.js +117 -0
- package/server.js +2124 -355
- /package/{lib → plugins/youtube}/youtube.js +0 -0
package/lib/openapi.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAPI spec generation via swagger-jsdoc + docs UI (swagger-stripey).
|
|
3
|
+
*
|
|
4
|
+
* swagger-jsdoc scans JSDoc `@openapi` comments on route handlers in server.js
|
|
5
|
+
* (and any file passed in `apis`) to build the spec at startup.
|
|
6
|
+
* Docs UI lives in docs/api.html (swagger-stripey: Stripe-style 3-panel renderer).
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* import { mountDocs } from './lib/openapi.js';
|
|
10
|
+
* // After all routes are registered:
|
|
11
|
+
* mountDocs(app);
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import swaggerJsdoc from 'swagger-jsdoc';
|
|
15
|
+
import express from 'express';
|
|
16
|
+
import { readFileSync } from 'fs';
|
|
17
|
+
import { dirname, join } from 'path';
|
|
18
|
+
import { fileURLToPath } from 'url';
|
|
19
|
+
|
|
20
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
21
|
+
|
|
22
|
+
let version = 'unknown';
|
|
23
|
+
try {
|
|
24
|
+
const pkg = JSON.parse(readFileSync(join(__dirname, '..', 'package.json'), 'utf8'));
|
|
25
|
+
version = pkg.version;
|
|
26
|
+
} catch { /* ignore */ }
|
|
27
|
+
|
|
28
|
+
const swaggerDefinition = {
|
|
29
|
+
openapi: '3.0.3',
|
|
30
|
+
info: {
|
|
31
|
+
title: 'camofox-browser',
|
|
32
|
+
version,
|
|
33
|
+
description:
|
|
34
|
+
'Anti-detection browser automation server for AI agents. ' +
|
|
35
|
+
'Accessibility snapshots, element refs, session isolation, cookie import, proxy rotation, and structured logs.',
|
|
36
|
+
license: { name: 'MIT', url: 'https://opensource.org/licenses/MIT' },
|
|
37
|
+
contact: { name: 'Jo Inc', url: 'https://askjo.ai', email: 'oss@askjo.ai' },
|
|
38
|
+
},
|
|
39
|
+
servers: [{ url: 'http://localhost:9377', description: 'Local development' }],
|
|
40
|
+
tags: [
|
|
41
|
+
{ name: 'System', description: 'Server health, metrics, and status.' },
|
|
42
|
+
{ name: 'Tabs', description: 'Create, list, inspect, and destroy browser tabs.' },
|
|
43
|
+
{ name: 'Navigation', description: 'Navigate tabs to URLs or via search macros.' },
|
|
44
|
+
{ name: 'Interaction', description: 'Click, type, scroll, press keys, evaluate JS.' },
|
|
45
|
+
{ name: 'Content', description: 'Accessibility snapshots, screenshots, links, images, downloads.' },
|
|
46
|
+
{ name: 'Sessions', description: 'Per-user session state: cookies, teardown.' },
|
|
47
|
+
{ name: 'Browser', description: 'Global browser lifecycle (start/stop).' },
|
|
48
|
+
{ name: 'Legacy', description: 'OpenClaw-compatible endpoints (deprecated).' },
|
|
49
|
+
],
|
|
50
|
+
components: {
|
|
51
|
+
securitySchemes: {
|
|
52
|
+
BearerAuth: {
|
|
53
|
+
type: 'http',
|
|
54
|
+
scheme: 'bearer',
|
|
55
|
+
description: 'Bearer token matching CAMOFOX_API_KEY.',
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
schemas: {
|
|
59
|
+
Error: {
|
|
60
|
+
type: 'object',
|
|
61
|
+
required: ['error'],
|
|
62
|
+
properties: { error: { type: 'string' } },
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Mount GET /openapi.json and GET /docs on the Express app.
|
|
70
|
+
* Call AFTER all routes are registered so swagger-jsdoc can scan them.
|
|
71
|
+
*
|
|
72
|
+
* @param {import('express').Application} app
|
|
73
|
+
* @param {Object} [opts]
|
|
74
|
+
* @param {string[]} [opts.apis] - Glob patterns for files with @openapi JSDoc (default: ['./server.js'])
|
|
75
|
+
*/
|
|
76
|
+
export function mountDocs(app, opts = {}) {
|
|
77
|
+
const apis = opts.apis || ['./server.js'];
|
|
78
|
+
|
|
79
|
+
const spec = swaggerJsdoc({
|
|
80
|
+
definition: swaggerDefinition,
|
|
81
|
+
apis,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
app.get('/openapi.json', (_req, res) => {
|
|
85
|
+
res.json(spec);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Serve docs static assets (api.html, fox.png, openapi.json)
|
|
89
|
+
const docsDir = join(__dirname, '..', 'docs');
|
|
90
|
+
app.use('/docs', express.static(docsDir, { index: 'api.html' }));
|
|
91
|
+
|
|
92
|
+
// Also serve fox.png at root for backward compat with old Swagger UI HTML
|
|
93
|
+
app.get('/fox.png', (_req, res) => {
|
|
94
|
+
res.sendFile(join(docsDir, 'fox.png'));
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
return spec;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export { swaggerDefinition };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
function getUserPersistencePaths(profileDir, userId) {
|
|
6
|
+
const rootDir = path.resolve(profileDir);
|
|
7
|
+
const safeUserDir = crypto
|
|
8
|
+
.createHash('sha256')
|
|
9
|
+
.update(String(userId))
|
|
10
|
+
.digest('hex')
|
|
11
|
+
.slice(0, 32);
|
|
12
|
+
|
|
13
|
+
const userDir = path.join(rootDir, safeUserDir);
|
|
14
|
+
return {
|
|
15
|
+
rootDir,
|
|
16
|
+
userDir,
|
|
17
|
+
storageStatePath: path.join(userDir, 'storage-state.json'),
|
|
18
|
+
metaPath: path.join(userDir, 'meta.json'),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function loadPersistedStorageState(profileDir, userId, logger = console) {
|
|
23
|
+
if (!profileDir) return undefined;
|
|
24
|
+
|
|
25
|
+
const { storageStatePath } = getUserPersistencePaths(profileDir, userId);
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const raw = await fs.readFile(storageStatePath, 'utf8');
|
|
29
|
+
const parsed = JSON.parse(raw);
|
|
30
|
+
if (!parsed || typeof parsed !== 'object') return undefined;
|
|
31
|
+
if (!Array.isArray(parsed.cookies)) return undefined;
|
|
32
|
+
if (parsed.origins !== undefined && !Array.isArray(parsed.origins)) return undefined;
|
|
33
|
+
return storageStatePath;
|
|
34
|
+
} catch (err) {
|
|
35
|
+
if (err?.code === 'ENOENT') return undefined;
|
|
36
|
+
logger?.warn?.('failed to load persisted storage state', {
|
|
37
|
+
userId: String(userId),
|
|
38
|
+
storageStatePath,
|
|
39
|
+
error: err?.message || String(err),
|
|
40
|
+
});
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function persistStorageState({ profileDir, userId, context, logger = console }) {
|
|
46
|
+
if (!profileDir || !context) {
|
|
47
|
+
return { persisted: false, reason: 'disabled' };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const { userDir, storageStatePath, metaPath } = getUserPersistencePaths(profileDir, userId);
|
|
51
|
+
const suffix = `.tmp-${process.pid}-${Date.now()}`;
|
|
52
|
+
const tmpStoragePath = `${storageStatePath}${suffix}`;
|
|
53
|
+
const tmpMetaPath = `${metaPath}${suffix}`;
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
await fs.mkdir(userDir, { recursive: true });
|
|
57
|
+
await context.storageState({ path: tmpStoragePath });
|
|
58
|
+
await fs.rename(tmpStoragePath, storageStatePath);
|
|
59
|
+
await fs.writeFile(
|
|
60
|
+
tmpMetaPath,
|
|
61
|
+
JSON.stringify(
|
|
62
|
+
{
|
|
63
|
+
userId: String(userId),
|
|
64
|
+
updatedAt: new Date().toISOString(),
|
|
65
|
+
storageStatePath,
|
|
66
|
+
},
|
|
67
|
+
null,
|
|
68
|
+
2
|
|
69
|
+
)
|
|
70
|
+
);
|
|
71
|
+
await fs.rename(tmpMetaPath, metaPath);
|
|
72
|
+
return { persisted: true, userDir, storageStatePath, metaPath };
|
|
73
|
+
} catch (err) {
|
|
74
|
+
await fs.unlink(tmpStoragePath).catch(() => {});
|
|
75
|
+
await fs.unlink(tmpMetaPath).catch(() => {});
|
|
76
|
+
logger?.warn?.('failed to persist storage state', {
|
|
77
|
+
userId: String(userId),
|
|
78
|
+
storageStatePath,
|
|
79
|
+
error: err?.message || String(err),
|
|
80
|
+
});
|
|
81
|
+
return { persisted: false, reason: 'error', error: err };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export {
|
|
86
|
+
getUserPersistencePaths,
|
|
87
|
+
loadPersistedStorageState,
|
|
88
|
+
persistStorageState,
|
|
89
|
+
};
|
package/lib/plugins.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Camofox-browser plugin system.
|
|
3
|
+
*
|
|
4
|
+
* Plugins live in plugins/<name>/index.js and export a register(app, ctx) function.
|
|
5
|
+
* The ctx object provides access to sessions, config, logging, auth middleware,
|
|
6
|
+
* core functions, and an EventEmitter for lifecycle hooks.
|
|
7
|
+
*
|
|
8
|
+
* 29 events across 7 categories:
|
|
9
|
+
*
|
|
10
|
+
* BROWSER LIFECYCLE
|
|
11
|
+
* browser:launching { options } — mutate launch options
|
|
12
|
+
* browser:launched { browser, display } — after launch
|
|
13
|
+
* browser:restart { reason } — before restart cycle
|
|
14
|
+
* browser:closed { reason } — after browser closed
|
|
15
|
+
* browser:error { error } — uncaught browser error
|
|
16
|
+
*
|
|
17
|
+
* SESSION LIFECYCLE
|
|
18
|
+
* session:creating { userId, contextOptions } — mutate context options
|
|
19
|
+
* session:created { userId, context } — after context stored
|
|
20
|
+
* session:destroying { userId, reason } — before context close (context still alive)
|
|
21
|
+
* session:destroyed { userId, reason } — after cleanup
|
|
22
|
+
* session:expired { userId, idleMs } — reaper triggered
|
|
23
|
+
*
|
|
24
|
+
* TAB LIFECYCLE
|
|
25
|
+
* tab:created { userId, tabId, page, url }
|
|
26
|
+
* tab:navigated { userId, tabId, url, prevUrl }
|
|
27
|
+
* tab:destroyed { userId, tabId, reason }
|
|
28
|
+
* tab:recycled { userId, tabId }
|
|
29
|
+
* tab:error { userId, tabId, error }
|
|
30
|
+
*
|
|
31
|
+
* CONTENT
|
|
32
|
+
* tab:snapshot { userId, tabId, snapshot }
|
|
33
|
+
* tab:screenshot { userId, tabId, buffer }
|
|
34
|
+
* tab:evaluate { userId, tabId, expression }
|
|
35
|
+
* tab:evaluated { userId, tabId, result }
|
|
36
|
+
*
|
|
37
|
+
* INPUT
|
|
38
|
+
* tab:click { userId, tabId, ref, selector }
|
|
39
|
+
* tab:type { userId, tabId, text, ref, mode }
|
|
40
|
+
* tab:scroll { userId, tabId, direction, amount }
|
|
41
|
+
* tab:press { userId, tabId, key }
|
|
42
|
+
*
|
|
43
|
+
* DOWNLOADS
|
|
44
|
+
* tab:download:start { userId, tabId, filename, url }
|
|
45
|
+
* tab:download:complete { userId, tabId, filename, path, size }
|
|
46
|
+
*
|
|
47
|
+
* COOKIES / AUTH
|
|
48
|
+
* session:cookies:import { userId, count }
|
|
49
|
+
* session:storage:export { userId }
|
|
50
|
+
*
|
|
51
|
+
* SERVER
|
|
52
|
+
* server:starting { port }
|
|
53
|
+
* server:started { port, pid }
|
|
54
|
+
* server:shutdown { signal }
|
|
55
|
+
*
|
|
56
|
+
* Mutating hooks (browser:launching, session:creating) pass the options object
|
|
57
|
+
* by reference — plugins can modify it in place before core uses it.
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
import { EventEmitter } from 'events';
|
|
61
|
+
import fs from 'fs';
|
|
62
|
+
import path from 'path';
|
|
63
|
+
import { fileURLToPath } from 'url';
|
|
64
|
+
|
|
65
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
66
|
+
const ROOT_DIR = path.join(__dirname, '..');
|
|
67
|
+
const PLUGINS_DIR = path.join(ROOT_DIR, 'plugins');
|
|
68
|
+
const CONFIG_PATH = path.join(ROOT_DIR, 'camofox.config.json');
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Read plugin configuration from camofox.config.json.
|
|
72
|
+
* Supports two formats:
|
|
73
|
+
* - Array of strings: ["youtube", "persistence"] (no per-plugin config)
|
|
74
|
+
* - Object with per-plugin config: { "youtube": { "enabled": true }, "persistence": { "enabled": true, "profileDir": "/data" } }
|
|
75
|
+
* Returns { list: string[] | null, configs: Map<string, object> }
|
|
76
|
+
*/
|
|
77
|
+
function readPluginConfig() {
|
|
78
|
+
const configs = new Map();
|
|
79
|
+
try {
|
|
80
|
+
const raw = fs.readFileSync(CONFIG_PATH, 'utf-8');
|
|
81
|
+
const config = JSON.parse(raw);
|
|
82
|
+
if (!config.plugins) return { list: null, configs };
|
|
83
|
+
if (Array.isArray(config.plugins)) {
|
|
84
|
+
return { list: config.plugins, configs };
|
|
85
|
+
}
|
|
86
|
+
if (typeof config.plugins === 'object') {
|
|
87
|
+
const list = [];
|
|
88
|
+
for (const [name, pluginConf] of Object.entries(config.plugins)) {
|
|
89
|
+
if (pluginConf === false || (typeof pluginConf === 'object' && pluginConf.enabled === false)) continue;
|
|
90
|
+
list.push(name);
|
|
91
|
+
if (typeof pluginConf === 'object') configs.set(name, pluginConf);
|
|
92
|
+
}
|
|
93
|
+
return { list, configs };
|
|
94
|
+
}
|
|
95
|
+
} catch {}
|
|
96
|
+
return { list: null, configs };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Create the plugin event bus.
|
|
101
|
+
*/
|
|
102
|
+
export function createPluginEvents() {
|
|
103
|
+
const events = new EventEmitter();
|
|
104
|
+
events.setMaxListeners(50); // generous for many plugins
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Emit an event and await all listeners (including async ones).
|
|
108
|
+
* Use for mutating hooks where plugins must finish before core continues.
|
|
109
|
+
* Regular emit() is still used for fire-and-forget observational events.
|
|
110
|
+
*/
|
|
111
|
+
events.emitAsync = async function emitAsync(eventName, payload) {
|
|
112
|
+
const listeners = this.listeners(eventName);
|
|
113
|
+
await Promise.all(listeners.map(fn => fn(payload)));
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
return events;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Load and register all plugins from plugins/<name>/index.js.
|
|
121
|
+
*
|
|
122
|
+
* @param {object} app - Express app
|
|
123
|
+
* @param {object} ctx - Plugin context: { sessions, config, log, events, auth, ensureBrowser, getSession, destroySession }
|
|
124
|
+
* Mutable — plugins can replace ctx.createVirtualDisplay etc.
|
|
125
|
+
* @returns {string[]} - Names of loaded plugins
|
|
126
|
+
*/
|
|
127
|
+
export async function loadPlugins(app, ctx) {
|
|
128
|
+
const loaded = [];
|
|
129
|
+
|
|
130
|
+
if (!fs.existsSync(PLUGINS_DIR)) {
|
|
131
|
+
ctx.log('info', 'no plugins directory found, skipping plugin load');
|
|
132
|
+
return loaded;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const { list: allowList, configs: pluginConfigs } = readPluginConfig();
|
|
136
|
+
const entries = fs.readdirSync(PLUGINS_DIR, { withFileTypes: true });
|
|
137
|
+
|
|
138
|
+
for (const entry of entries) {
|
|
139
|
+
if (!entry.isDirectory()) continue;
|
|
140
|
+
const name = entry.name;
|
|
141
|
+
|
|
142
|
+
// Skip directories starting with _ or .
|
|
143
|
+
if (name.startsWith('_') || name.startsWith('.')) continue;
|
|
144
|
+
|
|
145
|
+
// If camofox.config.json specifies a plugins list, only load those
|
|
146
|
+
if (allowList && !allowList.includes(name)) {
|
|
147
|
+
ctx.log('debug', `plugin "${name}" not in camofox.config.json plugins list, skipping`);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const indexPath = path.join(PLUGINS_DIR, name, 'index.js');
|
|
152
|
+
if (!fs.existsSync(indexPath)) {
|
|
153
|
+
ctx.log('warn', `plugin "${name}" has no index.js, skipping`);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
try {
|
|
158
|
+
const mod = await import(indexPath);
|
|
159
|
+
const register = mod.default || mod.register;
|
|
160
|
+
if (typeof register !== 'function') {
|
|
161
|
+
ctx.log('warn', `plugin "${name}" does not export a register function, skipping`);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const pluginConfig = pluginConfigs.get(name) || {};
|
|
166
|
+
await register(app, ctx, pluginConfig);
|
|
167
|
+
loaded.push(name);
|
|
168
|
+
ctx.log('info', 'plugin loaded', { plugin: name });
|
|
169
|
+
} catch (err) {
|
|
170
|
+
ctx.log('error', 'plugin load failed', { plugin: name, error: err.message, stack: err.stack });
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return loaded;
|
|
175
|
+
}
|