@askjo/camofox-browser 1.5.1 → 1.6.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 +20 -0
- package/camofox.config.json +10 -0
- package/lib/auth.js +71 -0
- package/lib/config.js +1 -0
- package/lib/cookies.js +38 -1
- package/lib/downloads.js +10 -2
- package/lib/inflight.js +16 -0
- package/lib/metrics.js +29 -0
- package/lib/persistence.js +89 -0
- package/lib/plugins.js +174 -0
- package/lib/tmp-cleanup.js +40 -0
- package/openclaw.plugin.json +1 -1
- package/package.json +5 -1
- package/plugin.ts +8 -1
- package/plugins/persistence/AGENTS.md +37 -0
- package/plugins/persistence/README.md +48 -0
- package/plugins/persistence/index.js +120 -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/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/install-plugin-deps.sh +63 -0
- package/scripts/plugin.js +342 -0
- package/scripts/plugin.test.js +117 -0
- package/server.js +286 -328
- /package/{lib → plugins/youtube}/youtube.js +0 -0
package/plugin.ts
CHANGED
|
@@ -399,6 +399,13 @@ export default function register(api: PluginApi) {
|
|
|
399
399
|
const text = await res.text();
|
|
400
400
|
throw new Error(`${res.status}: ${text}`);
|
|
401
401
|
}
|
|
402
|
+
// Guard: if server returns JSON/text instead of image (e.g. error with 200),
|
|
403
|
+
// return as text to avoid crashing the client with base64-encoded JSON.
|
|
404
|
+
const contentType = res.headers.get('content-type') || '';
|
|
405
|
+
if (!contentType.startsWith('image/')) {
|
|
406
|
+
const text = await res.text();
|
|
407
|
+
return { content: [{ type: "text", text: `Screenshot failed: ${text}` }] };
|
|
408
|
+
}
|
|
402
409
|
const arrayBuffer = await res.arrayBuffer();
|
|
403
410
|
const base64 = Buffer.from(arrayBuffer).toString("base64");
|
|
404
411
|
return {
|
|
@@ -406,7 +413,7 @@ export default function register(api: PluginApi) {
|
|
|
406
413
|
{
|
|
407
414
|
type: "image",
|
|
408
415
|
data: base64,
|
|
409
|
-
mimeType: "image/png",
|
|
416
|
+
mimeType: contentType || "image/png",
|
|
410
417
|
},
|
|
411
418
|
],
|
|
412
419
|
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# Persistence Plugin — Agent Guide
|
|
2
|
+
|
|
3
|
+
Saves and restores per-user browser storage state (cookies + localStorage) across session restarts using Playwright's `storageState` API. Enabled by default — profiles persist to `~/.camofox/profiles/`.
|
|
4
|
+
|
|
5
|
+
## How It Works
|
|
6
|
+
|
|
7
|
+
- `session:creating` hook → loads saved `storage_state.json` into `contextOptions.storageState`
|
|
8
|
+
- `session:created` hook → imports bootstrap cookies if no persisted state exists
|
|
9
|
+
- `session:cookies:import` / `session:destroyed` / `server:shutdown` → checkpoints state to disk
|
|
10
|
+
|
|
11
|
+
All hooks are async and awaited via `emitAsync()` — storage state is guaranteed loaded before the context is created.
|
|
12
|
+
|
|
13
|
+
## Key Files
|
|
14
|
+
|
|
15
|
+
- `index.js` — lifecycle hooks (no routes, no `child_process`)
|
|
16
|
+
- `persistence.test.js` — unit tests for `lib/persistence.js` helpers
|
|
17
|
+
- `plugin.test.js` — integration tests for plugin lifecycle hooks
|
|
18
|
+
|
|
19
|
+
## Storage Layout
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
~/.camofox/profiles/
|
|
23
|
+
└── <sha256(userId)>/
|
|
24
|
+
└── storage_state.json
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Configuration
|
|
28
|
+
|
|
29
|
+
Enabled by default. Override profile directory with `CAMOFOX_PROFILE_DIR` env var or `"profileDir"` in plugin config. To disable: `"persistence": { "enabled": false }` in `camofox.config.json`.
|
|
30
|
+
|
|
31
|
+
## Original Contributors
|
|
32
|
+
|
|
33
|
+
- [@company8](https://github.com/company8) — original persistence concept ([PR #62](https://github.com/jo-inc/camofox-browser/pull/62))
|
|
34
|
+
- [@eddieoz](https://github.com/eddieoz) — cookie auto-load on startup ([PR #55](https://github.com/jo-inc/camofox-browser/pull/55))
|
|
35
|
+
- [@pradeepe](https://github.com/pradeepe) — plugin system integration, atomic writes, inflight coalescing
|
|
36
|
+
|
|
37
|
+
For PRs touching this plugin, tag the contributors above for review.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# persistence
|
|
2
|
+
|
|
3
|
+
Optional per-user browser storage state persistence for camofox-browser.
|
|
4
|
+
|
|
5
|
+
Saves and restores cookies + localStorage across session restarts, container deploys, and idle timeouts using Playwright's `storageState` API.
|
|
6
|
+
|
|
7
|
+
## Configuration
|
|
8
|
+
|
|
9
|
+
In `camofox.config.json`:
|
|
10
|
+
|
|
11
|
+
```json
|
|
12
|
+
{
|
|
13
|
+
"plugins": {
|
|
14
|
+
"persistence": {
|
|
15
|
+
"enabled": true,
|
|
16
|
+
"profileDir": "/data/profiles"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Or override via environment variable:
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
CAMOFOX_PROFILE_DIR=/data/profiles
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## How it works
|
|
29
|
+
|
|
30
|
+
- **Session create**: If a persisted `storageState` exists for the `userId`, it's restored into the new Playwright context.
|
|
31
|
+
- **First run**: If no persisted state exists, bootstrap cookies from `CAMOFOX_COOKIES_DIR/cookies.txt` are imported (if present).
|
|
32
|
+
- **Cookie import / session close / shutdown**: Storage state is checkpointed to disk via atomic tmp-write + rename.
|
|
33
|
+
- **User isolation**: Each `userId` maps to a deterministic SHA256-hashed subdirectory under `profileDir`, so arbitrary userIds are path-safe.
|
|
34
|
+
|
|
35
|
+
## Docker
|
|
36
|
+
|
|
37
|
+
When running with Docker, mount the profile directory as a volume:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
docker run -d \
|
|
41
|
+
-p 9377:9377 \
|
|
42
|
+
-v /host/profiles:/data/profiles \
|
|
43
|
+
camofox-browser
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Credits
|
|
47
|
+
|
|
48
|
+
Based on PR #62 by [company8](https://github.com/company8).
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Persistence plugin for camofox-browser.
|
|
3
|
+
*
|
|
4
|
+
* Saves and restores per-user browser storage state (cookies + localStorage)
|
|
5
|
+
* across session restarts using Playwright's storageState API.
|
|
6
|
+
*
|
|
7
|
+
* Configuration (camofox.config.json):
|
|
8
|
+
* {
|
|
9
|
+
* "plugins": {
|
|
10
|
+
* "persistence": {
|
|
11
|
+
* "enabled": true,
|
|
12
|
+
* "profileDir": "/data/profiles"
|
|
13
|
+
* }
|
|
14
|
+
* }
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* Or via environment variables (overrides config file):
|
|
18
|
+
* CAMOFOX_PROFILE_DIR=/data/profiles
|
|
19
|
+
*
|
|
20
|
+
* Each userId gets a deterministic SHA256-hashed subdirectory under profileDir.
|
|
21
|
+
* Storage state is checkpointed on cookie import, session close, and shutdown.
|
|
22
|
+
* On session creation, saved state is restored into the new Playwright context
|
|
23
|
+
* via the session:creating hook (mutates contextOptions.storageState).
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import {
|
|
27
|
+
getUserPersistencePaths,
|
|
28
|
+
loadPersistedStorageState,
|
|
29
|
+
persistStorageState,
|
|
30
|
+
} from '../../lib/persistence.js';
|
|
31
|
+
import { importBootstrapCookies } from '../../lib/cookies.js';
|
|
32
|
+
|
|
33
|
+
export async function register(app, ctx, pluginConfig = {}) {
|
|
34
|
+
const { events, config, log } = ctx;
|
|
35
|
+
|
|
36
|
+
// Resolve profileDir: env var > plugin config > global config default (~/.camofox/profiles)
|
|
37
|
+
const profileDir = process.env.CAMOFOX_PROFILE_DIR || pluginConfig.profileDir || config.profileDir;
|
|
38
|
+
if (!profileDir) {
|
|
39
|
+
log('warn', 'persistence plugin: no profileDir configured, plugin disabled');
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const logger = {
|
|
44
|
+
warn: (msg, fields = {}) => log('warn', msg, fields),
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
log('info', 'persistence plugin enabled', { profileDir });
|
|
48
|
+
|
|
49
|
+
// Track active sessions for checkpoint on close
|
|
50
|
+
const activeSessions = new Map(); // userId -> context
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Checkpoint storage state to disk for a userId.
|
|
54
|
+
*/
|
|
55
|
+
async function checkpoint(userId, context, reason) {
|
|
56
|
+
if (!context) return;
|
|
57
|
+
const result = await persistStorageState({ profileDir, userId, context, logger });
|
|
58
|
+
if (result.persisted) {
|
|
59
|
+
log('info', 'storage state persisted', { userId, reason, path: result.storageStatePath });
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// --- Lifecycle hooks ---
|
|
65
|
+
|
|
66
|
+
// Before session context is created: inject storageState if we have one saved
|
|
67
|
+
events.on('session:creating', async ({ userId, contextOptions }) => {
|
|
68
|
+
const storageStatePath = await loadPersistedStorageState(profileDir, userId, logger);
|
|
69
|
+
if (storageStatePath) {
|
|
70
|
+
contextOptions.storageState = storageStatePath;
|
|
71
|
+
log('info', 'restoring persisted storage state', { userId, storageStatePath });
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// After session is created: import bootstrap cookies if no persisted state,
|
|
76
|
+
// and track the context for later checkpointing
|
|
77
|
+
events.on('session:created', async ({ userId, context }) => {
|
|
78
|
+
activeSessions.set(userId, context);
|
|
79
|
+
|
|
80
|
+
// If no persisted state was restored, try bootstrap cookies
|
|
81
|
+
const existingState = await loadPersistedStorageState(profileDir, userId, logger);
|
|
82
|
+
if (!existingState) {
|
|
83
|
+
const result = await importBootstrapCookies({
|
|
84
|
+
cookiesDir: config.cookiesDir,
|
|
85
|
+
context,
|
|
86
|
+
logger,
|
|
87
|
+
});
|
|
88
|
+
if (result.imported > 0) {
|
|
89
|
+
log('info', 'bootstrap cookies imported', { userId, count: result.imported, source: result.source });
|
|
90
|
+
await checkpoint(userId, context, 'bootstrap_cookies');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// On cookie import: checkpoint
|
|
96
|
+
events.on('session:cookies:import', async ({ userId }) => {
|
|
97
|
+
const context = activeSessions.get(userId);
|
|
98
|
+
if (context) {
|
|
99
|
+
await checkpoint(userId, context, 'cookie_import');
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// On session destroy: checkpoint then remove from tracking
|
|
104
|
+
events.on('session:destroyed', async ({ userId, reason }) => {
|
|
105
|
+
const context = activeSessions.get(userId);
|
|
106
|
+
if (context) {
|
|
107
|
+
// Context may already be closed — checkpoint will fail gracefully
|
|
108
|
+
await checkpoint(userId, context, reason).catch(() => {});
|
|
109
|
+
activeSessions.delete(userId);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// On shutdown: checkpoint all remaining sessions
|
|
114
|
+
events.on('server:shutdown', async () => {
|
|
115
|
+
for (const [userId, context] of activeSessions) {
|
|
116
|
+
await checkpoint(userId, context, 'shutdown').catch(() => {});
|
|
117
|
+
}
|
|
118
|
+
activeSessions.clear();
|
|
119
|
+
});
|
|
120
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { jest } from '@jest/globals';
|
|
5
|
+
import {
|
|
6
|
+
getUserPersistencePaths,
|
|
7
|
+
loadPersistedStorageState,
|
|
8
|
+
persistStorageState,
|
|
9
|
+
} from '../../lib/persistence.js';
|
|
10
|
+
|
|
11
|
+
describe('profile persistence helpers', () => {
|
|
12
|
+
let tmpDir;
|
|
13
|
+
|
|
14
|
+
beforeEach(async () => {
|
|
15
|
+
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'camofox-persistence-'));
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
afterEach(async () => {
|
|
19
|
+
if (tmpDir) {
|
|
20
|
+
await fs.rm(tmpDir, { recursive: true, force: true });
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('getUserPersistencePaths is deterministic and stays under root', () => {
|
|
25
|
+
const first = getUserPersistencePaths(tmpDir, 'agent/profile:default');
|
|
26
|
+
const second = getUserPersistencePaths(tmpDir, 'agent/profile:default');
|
|
27
|
+
|
|
28
|
+
expect(first).toEqual(second);
|
|
29
|
+
expect(first.userDir.startsWith(tmpDir)).toBe(true);
|
|
30
|
+
expect(first.storageStatePath.startsWith(first.userDir)).toBe(true);
|
|
31
|
+
expect(first.metaPath.startsWith(first.userDir)).toBe(true);
|
|
32
|
+
expect(path.basename(first.userDir)).not.toContain('/');
|
|
33
|
+
expect(path.basename(first.userDir)).not.toContain(':');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('loadPersistedStorageState returns undefined when no state exists', async () => {
|
|
37
|
+
await expect(loadPersistedStorageState(tmpDir, 'user-1')).resolves.toBeUndefined();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('persistStorageState writes storage state and metadata, then load returns the storage path', async () => {
|
|
41
|
+
const storageState = {
|
|
42
|
+
cookies: [{ name: 'session', value: 'abc', domain: '.example.com', path: '/' }],
|
|
43
|
+
origins: [{ origin: 'https://app.example.com', localStorage: [{ name: 'foo', value: 'bar' }] }],
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const context = {
|
|
47
|
+
storageState: jest.fn(async ({ path: targetPath }) => {
|
|
48
|
+
await fs.writeFile(targetPath, JSON.stringify(storageState, null, 2));
|
|
49
|
+
}),
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const result = await persistStorageState({
|
|
53
|
+
profileDir: tmpDir,
|
|
54
|
+
userId: 'user-1',
|
|
55
|
+
context,
|
|
56
|
+
logger: { warn: jest.fn() },
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
expect(result.persisted).toBe(true);
|
|
60
|
+
expect(context.storageState).toHaveBeenCalledTimes(1);
|
|
61
|
+
|
|
62
|
+
const loadedPath = await loadPersistedStorageState(tmpDir, 'user-1');
|
|
63
|
+
expect(loadedPath).toBe(result.storageStatePath);
|
|
64
|
+
|
|
65
|
+
const meta = JSON.parse(await fs.readFile(result.metaPath, 'utf8'));
|
|
66
|
+
expect(meta.userId).toBe('user-1');
|
|
67
|
+
expect(meta.storageStatePath).toBe(result.storageStatePath);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('loadPersistedStorageState ignores invalid JSON files', async () => {
|
|
71
|
+
const { storageStatePath } = getUserPersistencePaths(tmpDir, 'user-2');
|
|
72
|
+
await fs.mkdir(path.dirname(storageStatePath), { recursive: true });
|
|
73
|
+
await fs.writeFile(storageStatePath, '{not-json');
|
|
74
|
+
|
|
75
|
+
await expect(loadPersistedStorageState(tmpDir, 'user-2', { warn: jest.fn() })).resolves.toBeUndefined();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('a failed persist leaves the previous storage-state intact and cleans up tmp files', async () => {
|
|
79
|
+
const originalState = {
|
|
80
|
+
cookies: [{ name: 'orig', value: 'v1', domain: '.example.com', path: '/' }],
|
|
81
|
+
};
|
|
82
|
+
const goodContext = {
|
|
83
|
+
storageState: jest.fn(async ({ path: targetPath }) => {
|
|
84
|
+
await fs.writeFile(targetPath, JSON.stringify(originalState, null, 2));
|
|
85
|
+
}),
|
|
86
|
+
};
|
|
87
|
+
const first = await persistStorageState({
|
|
88
|
+
profileDir: tmpDir,
|
|
89
|
+
userId: 'user-3',
|
|
90
|
+
context: goodContext,
|
|
91
|
+
logger: { warn: jest.fn() },
|
|
92
|
+
});
|
|
93
|
+
expect(first.persisted).toBe(true);
|
|
94
|
+
|
|
95
|
+
const failingContext = {
|
|
96
|
+
storageState: jest.fn(async () => {
|
|
97
|
+
throw new Error('simulated crash mid-write');
|
|
98
|
+
}),
|
|
99
|
+
};
|
|
100
|
+
const second = await persistStorageState({
|
|
101
|
+
profileDir: tmpDir,
|
|
102
|
+
userId: 'user-3',
|
|
103
|
+
context: failingContext,
|
|
104
|
+
logger: { warn: jest.fn() },
|
|
105
|
+
});
|
|
106
|
+
expect(second.persisted).toBe(false);
|
|
107
|
+
|
|
108
|
+
const { userDir, storageStatePath } = getUserPersistencePaths(tmpDir, 'user-3');
|
|
109
|
+
const loaded = await loadPersistedStorageState(tmpDir, 'user-3');
|
|
110
|
+
expect(loaded).toBe(storageStatePath);
|
|
111
|
+
const parsed = JSON.parse(await fs.readFile(storageStatePath, 'utf8'));
|
|
112
|
+
expect(parsed).toEqual(originalState);
|
|
113
|
+
|
|
114
|
+
const leftovers = (await fs.readdir(userDir)).filter((name) => name.includes('.tmp-'));
|
|
115
|
+
expect(leftovers).toEqual([]);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { jest } from '@jest/globals';
|
|
5
|
+
import { createPluginEvents } from '../../lib/plugins.js';
|
|
6
|
+
import { register } from './index.js';
|
|
7
|
+
|
|
8
|
+
describe('persistence plugin', () => {
|
|
9
|
+
let tmpDir, events, ctx, mockApp;
|
|
10
|
+
|
|
11
|
+
beforeEach(async () => {
|
|
12
|
+
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'camofox-persist-plugin-'));
|
|
13
|
+
events = createPluginEvents();
|
|
14
|
+
mockApp = {};
|
|
15
|
+
ctx = {
|
|
16
|
+
events,
|
|
17
|
+
config: { cookiesDir: path.join(tmpDir, 'cookies') },
|
|
18
|
+
log: jest.fn(),
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterEach(async () => {
|
|
23
|
+
if (tmpDir) await fs.rm(tmpDir, { recursive: true, force: true });
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test('skips registration when no profileDir configured', async () => {
|
|
27
|
+
await register(mockApp, ctx, {});
|
|
28
|
+
expect(ctx.log).toHaveBeenCalledWith('warn', expect.stringContaining('no profileDir'));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('restores persisted state on session:creating', async () => {
|
|
32
|
+
await register(mockApp, ctx, { profileDir: tmpDir });
|
|
33
|
+
|
|
34
|
+
// Simulate a prior persisted state
|
|
35
|
+
const { getUserPersistencePaths } = await import('../../lib/persistence.js');
|
|
36
|
+
const { userDir, storageStatePath } = getUserPersistencePaths(tmpDir, 'user-1');
|
|
37
|
+
await fs.mkdir(userDir, { recursive: true });
|
|
38
|
+
await fs.writeFile(storageStatePath, JSON.stringify({
|
|
39
|
+
cookies: [{ name: 'sid', value: 'abc', domain: '.example.com', path: '/' }],
|
|
40
|
+
origins: [],
|
|
41
|
+
}));
|
|
42
|
+
|
|
43
|
+
const contextOptions = { viewport: { width: 1280, height: 720 } };
|
|
44
|
+
await events.emitAsync('session:creating', { userId: 'user-1', contextOptions });
|
|
45
|
+
|
|
46
|
+
expect(contextOptions.storageState).toBe(storageStatePath);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('checkpoints on session:cookies:import', async () => {
|
|
50
|
+
await register(mockApp, ctx, { profileDir: tmpDir });
|
|
51
|
+
|
|
52
|
+
const mockContext = {
|
|
53
|
+
storageState: jest.fn(async ({ path: p }) => {
|
|
54
|
+
await fs.writeFile(p, JSON.stringify({ cookies: [{ name: 'x', value: 'y', domain: '.test.com', path: '/' }] }));
|
|
55
|
+
}),
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// Simulate session created then cookie import
|
|
59
|
+
await events.emitAsync('session:created', { userId: 'user-2', context: mockContext });
|
|
60
|
+
await events.emitAsync('session:cookies:import', { userId: 'user-2' });
|
|
61
|
+
|
|
62
|
+
expect(mockContext.storageState).toHaveBeenCalled();
|
|
63
|
+
|
|
64
|
+
// Verify file was written
|
|
65
|
+
const { getUserPersistencePaths } = await import('../../lib/persistence.js');
|
|
66
|
+
const { storageStatePath } = getUserPersistencePaths(tmpDir, 'user-2');
|
|
67
|
+
const saved = JSON.parse(await fs.readFile(storageStatePath, 'utf8'));
|
|
68
|
+
expect(saved.cookies[0].name).toBe('x');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('checkpoints on session:destroyed', async () => {
|
|
72
|
+
await register(mockApp, ctx, { profileDir: tmpDir });
|
|
73
|
+
|
|
74
|
+
const mockContext = {
|
|
75
|
+
storageState: jest.fn(async ({ path: p }) => {
|
|
76
|
+
await fs.writeFile(p, JSON.stringify({ cookies: [], origins: [] }));
|
|
77
|
+
}),
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
await events.emitAsync('session:created', { userId: 'user-3', context: mockContext });
|
|
81
|
+
await events.emitAsync('session:destroyed', { userId: 'user-3', reason: 'test' });
|
|
82
|
+
|
|
83
|
+
expect(mockContext.storageState).toHaveBeenCalled();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('env var CAMOFOX_PROFILE_DIR overrides pluginConfig', async () => {
|
|
87
|
+
const envDir = path.join(tmpDir, 'env-override');
|
|
88
|
+
const orig = process.env.CAMOFOX_PROFILE_DIR;
|
|
89
|
+
process.env.CAMOFOX_PROFILE_DIR = envDir;
|
|
90
|
+
try {
|
|
91
|
+
await register(mockApp, ctx, { profileDir: '/should/not/use' });
|
|
92
|
+
expect(ctx.log).toHaveBeenCalledWith('info', 'persistence plugin enabled', { profileDir: envDir });
|
|
93
|
+
} finally {
|
|
94
|
+
if (orig === undefined) delete process.env.CAMOFOX_PROFILE_DIR;
|
|
95
|
+
else process.env.CAMOFOX_PROFILE_DIR = orig;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# VNC Plugin — Agent Guide
|
|
2
|
+
|
|
3
|
+
Interactive browser access via noVNC. Log into sites visually, solve CAPTCHAs, approve OAuth prompts — then export the authenticated storage state for agent reuse.
|
|
4
|
+
|
|
5
|
+
## Endpoints
|
|
6
|
+
|
|
7
|
+
- `GET /vnc/status` — check if VNC is running (no auth)
|
|
8
|
+
- `GET /sessions/:userId/storage_state` — export cookies + localStorage as JSON (requires auth)
|
|
9
|
+
|
|
10
|
+
## Activation
|
|
11
|
+
|
|
12
|
+
Disabled by default. Enable with `ENABLE_VNC=1` env var or `"vnc": { "enabled": true }` in `camofox.config.json`.
|
|
13
|
+
|
|
14
|
+
## Key Files
|
|
15
|
+
|
|
16
|
+
- `index.js` — route handlers only (no `child_process`, no `process.env` reads)
|
|
17
|
+
- `vnc-launcher.js` — process management, config resolution from env vars (`child_process` isolated here)
|
|
18
|
+
- `vnc-watcher.sh` — shell script that detects Xvfb, attaches x11vnc, starts noVNC
|
|
19
|
+
- `vnc.test.js` — unit tests
|
|
20
|
+
- `apt.txt` — system deps (x11vnc, novnc, websockify, etc.)
|
|
21
|
+
|
|
22
|
+
## Scanner Compliance
|
|
23
|
+
|
|
24
|
+
`child_process` is in `vnc-launcher.js`, route handlers are in `index.js`, env var reads are in `vnc-launcher.js` — separate files per OpenClaw scanner rules.
|
|
25
|
+
|
|
26
|
+
## Security
|
|
27
|
+
|
|
28
|
+
- noVNC binds to `127.0.0.1` by default — set `VNC_BIND=0.0.0.0` to expose externally
|
|
29
|
+
- Set `VNC_PASSWORD` for password-protected access
|
|
30
|
+
- `VIEW_ONLY=1` disables keyboard/mouse input (observation only)
|
|
31
|
+
- Storage state export endpoint requires auth (API key or loopback)
|
|
32
|
+
|
|
33
|
+
## Architecture
|
|
34
|
+
|
|
35
|
+
The plugin overrides `ctx.createVirtualDisplay` to use a higher-resolution display (default 1920x1080 instead of 1x1). `vnc-watcher.sh` polls for the Xvfb process, then attaches x11vnc + noVNC on top.
|
|
36
|
+
|
|
37
|
+
## Original Contributors
|
|
38
|
+
|
|
39
|
+
- [@leoneparise](https://github.com/leoneparise) — original VNC implementation + keyboard mode ([PR #65](https://github.com/jo-inc/camofox-browser/pull/65), [PR #66](https://github.com/jo-inc/camofox-browser/pull/66))
|
|
40
|
+
- [@pradeepe](https://github.com/pradeepe) — plugin system integration, scanner compliance refactor, security hardening
|
|
41
|
+
|
|
42
|
+
For PRs touching this plugin, tag the contributors above for review.
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# VNC Plugin
|
|
2
|
+
|
|
3
|
+
> Originally contributed by [@leoneparise](https://github.com/leoneparise) in [PR #65](https://github.com/jo-inc/camofox-browser/pull/65). Reworked as a plugin for the camofox extension system.
|
|
4
|
+
|
|
5
|
+
Interactive browser access via VNC. Log into sites visually, solve CAPTCHAs, approve OAuth prompts — then export the authenticated storage state for reuse by your agent.
|
|
6
|
+
|
|
7
|
+
## How it works
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
Camoufox (Xvfb :99, 1920x1080)
|
|
11
|
+
↑
|
|
12
|
+
x11vnc (attaches to :99, port 5900)
|
|
13
|
+
↑
|
|
14
|
+
noVNC / websockify (port 6080)
|
|
15
|
+
↑
|
|
16
|
+
Your browser → http://localhost:6080/vnc.html
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The plugin overrides Camoufox's default 1x1 virtual display with a human-usable resolution, then runs a watcher process that detects the Xvfb display and attaches x11vnc + noVNC. The watcher handles browser restarts automatically — when Camoufox relaunches on a new display, x11vnc reattaches.
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
### Docker
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
docker run -p 9377:9377 -p 6080:6080 \
|
|
27
|
+
-e ENABLE_VNC=1 \
|
|
28
|
+
camofox-browser
|
|
29
|
+
|
|
30
|
+
# Open http://localhost:6080/vnc.html in your browser
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Config file
|
|
34
|
+
|
|
35
|
+
```json
|
|
36
|
+
{
|
|
37
|
+
"plugins": {
|
|
38
|
+
"vnc": {
|
|
39
|
+
"enabled": true,
|
|
40
|
+
"resolution": "1920x1080",
|
|
41
|
+
"password": "optional-secret",
|
|
42
|
+
"viewOnly": false,
|
|
43
|
+
"novncPort": 6080
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Workflow: interactive login → agent reuse
|
|
50
|
+
|
|
51
|
+
1. **Start with VNC enabled:**
|
|
52
|
+
```bash
|
|
53
|
+
docker run -p 9377:9377 -p 6080:6080 -e ENABLE_VNC=1 camofox-browser
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
2. **Create a session and navigate to the login page:**
|
|
57
|
+
```bash
|
|
58
|
+
curl -X POST http://localhost:9377/tabs \
|
|
59
|
+
-H 'Content-Type: application/json' \
|
|
60
|
+
-d '{"userId": "my-agent", "sessionKey": "default", "url": "https://accounts.google.com"}'
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
3. **Log in visually** via http://localhost:6080/vnc.html — complete MFA, solve CAPTCHAs, etc.
|
|
64
|
+
|
|
65
|
+
4. **Export the authenticated state:**
|
|
66
|
+
```bash
|
|
67
|
+
curl http://localhost:9377/sessions/my-agent/storage_state \
|
|
68
|
+
-H 'Authorization: Bearer YOUR_CAMOFOX_API_KEY' \
|
|
69
|
+
-o storage_state.json
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
5. **Reuse on future runs** — pair with the [persistence plugin](../persistence/) to automatically restore state on session creation:
|
|
73
|
+
```json
|
|
74
|
+
{
|
|
75
|
+
"plugins": {
|
|
76
|
+
"vnc": { "enabled": true },
|
|
77
|
+
"persistence": { "enabled": true, "profileDir": "/data/profiles" }
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
With both plugins active, the persistence plugin automatically checkpoints storage state on session close and restores it on creation. The VNC plugin's export endpoint also triggers a persistence checkpoint via the `session:storage:export` event.
|
|
82
|
+
|
|
83
|
+
## API
|
|
84
|
+
|
|
85
|
+
### GET /sessions/:userId/storage_state
|
|
86
|
+
|
|
87
|
+
Export the full Playwright storage state (cookies + localStorage origins) for a user's active browser context.
|
|
88
|
+
|
|
89
|
+
**Auth:** Same as cookie import — requires `CAMOFOX_API_KEY` Bearer token, or loopback access in non-production.
|
|
90
|
+
|
|
91
|
+
**Response:**
|
|
92
|
+
```json
|
|
93
|
+
{
|
|
94
|
+
"cookies": [
|
|
95
|
+
{
|
|
96
|
+
"name": "session_id",
|
|
97
|
+
"value": "abc123",
|
|
98
|
+
"domain": ".example.com",
|
|
99
|
+
"path": "/",
|
|
100
|
+
"expires": 1700000000,
|
|
101
|
+
"httpOnly": true,
|
|
102
|
+
"secure": true,
|
|
103
|
+
"sameSite": "Lax"
|
|
104
|
+
}
|
|
105
|
+
],
|
|
106
|
+
"origins": [
|
|
107
|
+
{
|
|
108
|
+
"origin": "https://example.com",
|
|
109
|
+
"localStorage": [
|
|
110
|
+
{ "name": "theme", "value": "dark" }
|
|
111
|
+
]
|
|
112
|
+
}
|
|
113
|
+
]
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**Errors:**
|
|
118
|
+
- `404` — No active session for the given userId
|
|
119
|
+
- `403` — Missing or invalid API key
|
|
120
|
+
- `500` — Context is dead or storageState export failed
|
|
121
|
+
|
|
122
|
+
## Configuration
|
|
123
|
+
|
|
124
|
+
| Source | Variable | Description | Default |
|
|
125
|
+
|--------|----------|-------------|---------|
|
|
126
|
+
| env | `ENABLE_VNC` | Enable the plugin (`1`) | off |
|
|
127
|
+
| env | `VNC_PASSWORD` | x11vnc password | none (open) |
|
|
128
|
+
| env | `VNC_RESOLUTION` | Xvfb screen resolution | `1920x1080` |
|
|
129
|
+
| env | `VIEW_ONLY` | Disable mouse/keyboard input (`1`) | off |
|
|
130
|
+
| env | `VNC_PORT` | x11vnc listen port | `5900` |
|
|
131
|
+
| env | `NOVNC_PORT` | noVNC web UI port | `6080` |
|
|
132
|
+
| config | `plugins.vnc.enabled` | Enable the plugin | `false` |
|
|
133
|
+
| config | `plugins.vnc.password` | x11vnc password | none |
|
|
134
|
+
| config | `plugins.vnc.resolution` | Xvfb screen resolution | `1920x1080` |
|
|
135
|
+
| config | `plugins.vnc.viewOnly` | View-only mode | `false` |
|
|
136
|
+
| config | `plugins.vnc.vncPort` | x11vnc listen port | `5900` |
|
|
137
|
+
| config | `plugins.vnc.novncPort` | noVNC web UI port | `6080` |
|
|
138
|
+
|
|
139
|
+
Environment variables override config file values.
|
|
140
|
+
|
|
141
|
+
## Security
|
|
142
|
+
|
|
143
|
+
⚠️ **VNC is unencrypted by default.** When running in production:
|
|
144
|
+
|
|
145
|
+
- **Set `VNC_PASSWORD`** — without it, anyone who can reach port 6080 has full browser control
|
|
146
|
+
- **Bind 6080 to localhost** and access via SSH tunnel: `ssh -L 6080:localhost:6080 your-server`
|
|
147
|
+
- **Or use a firewall** to restrict access to port 6080
|
|
148
|
+
- In Docker: `-p 127.0.0.1:6080:6080` binds only to localhost
|
|
149
|
+
|
|
150
|
+
## System dependencies
|
|
151
|
+
|
|
152
|
+
The plugin declares its apt dependencies in `apt.txt` — these are installed automatically during `docker build` via `scripts/install-plugin-deps.sh`:
|
|
153
|
+
|
|
154
|
+
- `x11vnc` — attaches to Xvfb display
|
|
155
|
+
- `novnc` + `python3-websockify` — web-based VNC client
|
|
156
|
+
- `net-tools` + `procps` — display detection utilities
|
|
157
|
+
|
|
158
|
+
## Events
|
|
159
|
+
|
|
160
|
+
| Event | Payload | Description |
|
|
161
|
+
|-------|---------|-------------|
|
|
162
|
+
| `vnc:watcher:started` | `{ pid }` | Watcher process spawned |
|
|
163
|
+
| `vnc:watcher:stopped` | `{ code, signal }` | Watcher exited |
|
|
164
|
+
| `vnc:storage:exported` | `{ userId, cookies, origins }` | Storage state exported via API |
|
|
165
|
+
| `session:storage:export` | `{ userId }` | Emitted after export (persistence plugin listens) |
|