@askjo/camofox-browser 1.4.0 → 1.5.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/lib/proxy.js ADDED
@@ -0,0 +1,277 @@
1
+ import crypto from 'crypto';
2
+
3
+ // ---------------------------------------------------------------------------
4
+ // Credential helpers
5
+ // ---------------------------------------------------------------------------
6
+
7
+ function decodeProxyCredential(value) {
8
+ if (!value) return value;
9
+ try {
10
+ return decodeURIComponent(value);
11
+ } catch {
12
+ return value;
13
+ }
14
+ }
15
+
16
+ export function normalizePlaywrightProxy(proxy) {
17
+ if (!proxy) return proxy;
18
+ return {
19
+ ...proxy,
20
+ username: decodeProxyCredential(proxy.username),
21
+ password: decodeProxyCredential(proxy.password),
22
+ };
23
+ }
24
+
25
+ // ---------------------------------------------------------------------------
26
+ // Session helpers
27
+ // ---------------------------------------------------------------------------
28
+
29
+ function makeSessionId(prefix = 'sess') {
30
+ return `${prefix}-${crypto.randomUUID().replace(/-/g, '').slice(0, 12)}`;
31
+ }
32
+
33
+ // ---------------------------------------------------------------------------
34
+ // Provider interface
35
+ // ---------------------------------------------------------------------------
36
+ //
37
+ // A proxy provider shapes credentials and declares capabilities.
38
+ //
39
+ // {
40
+ // name: string — e.g. 'decodo', 'brightdata', 'generic'
41
+ // canRotateSessions: bool — per-context session rotation supported
42
+ // launchRetries: number — how many browser launch attempts
43
+ // launchTimeoutMs: number — per-attempt timeout
44
+ // buildSessionUsername(baseUsername, options) → string
45
+ // buildProxyUrl(proxy, config) → string | null
46
+ // }
47
+ //
48
+ // options: { country, state, city, zip, sessionId, sessionDurationMinutes }
49
+ // ---------------------------------------------------------------------------
50
+
51
+ function sanitizeBackconnectValue(value) {
52
+ if (!value) return '';
53
+ return String(value)
54
+ .trim()
55
+ .toLowerCase()
56
+ .replace(/\s+/g, '_')
57
+ .replace(/[^a-z0-9_]/g, '_')
58
+ .replace(/_+/g, '_')
59
+ .replace(/^_+|_+$/g, '');
60
+ }
61
+
62
+ /**
63
+ * Decodo residential proxy provider.
64
+ * Username DSL: user-{base}-country-{cc}-state-{st}-session-{id}-sessionduration-{min}
65
+ */
66
+ export const decodoProvider = {
67
+ name: 'decodo',
68
+ canRotateSessions: true,
69
+ launchRetries: 10,
70
+ launchTimeoutMs: 180000,
71
+
72
+ buildSessionUsername(baseUsername, options = {}) {
73
+ const username = sanitizeBackconnectValue(baseUsername);
74
+ if (!username) return '';
75
+
76
+ const parts = [`user-${username}`];
77
+ const country = sanitizeBackconnectValue(options.country);
78
+ const state = sanitizeBackconnectValue(options.state);
79
+ const city = sanitizeBackconnectValue(options.city);
80
+ const zip = sanitizeBackconnectValue(options.zip);
81
+ const sessionId = sanitizeBackconnectValue(options.sessionId);
82
+ const sessionDurationMinutes = Number.isFinite(options.sessionDurationMinutes)
83
+ ? Math.max(1, Math.min(1440, Math.trunc(options.sessionDurationMinutes)))
84
+ : null;
85
+
86
+ if (country) parts.push(`country-${country}`);
87
+ if (state) parts.push(`state-${state}`);
88
+ if (city) parts.push(`city-${city}`);
89
+ if (zip) parts.push(`zip-${zip}`);
90
+ if (sessionId) parts.push(`session-${sessionId}`);
91
+ if (sessionDurationMinutes) parts.push(`sessionduration-${sessionDurationMinutes}`);
92
+
93
+ return parts.join('-');
94
+ },
95
+
96
+ buildProxyUrl(proxy, config) {
97
+ if (!proxy?.username || !config?.password) return null;
98
+ const user = encodeURIComponent(proxy.username);
99
+ const pass = encodeURIComponent(config.password);
100
+ const host = config.backconnectHost;
101
+ const port = config.backconnectPort;
102
+ return `http://${user}:${pass}@${host}:${port}`;
103
+ },
104
+ };
105
+
106
+ /**
107
+ * Generic backconnect provider — no username rewriting, just pass-through.
108
+ * Works with any SOCKS/HTTP proxy that supports sticky sessions via
109
+ * separate session IDs in the username field (e.g. BrightData, Oxylabs).
110
+ */
111
+ export const genericBackconnectProvider = {
112
+ name: 'generic',
113
+ canRotateSessions: true,
114
+ launchRetries: 5,
115
+ launchTimeoutMs: 120000,
116
+
117
+ buildSessionUsername(baseUsername, options = {}) {
118
+ // Simple pass-through: base username + session suffix
119
+ const base = String(baseUsername || '').trim();
120
+ const sessionId = options.sessionId ? `-${String(options.sessionId).trim()}` : '';
121
+ return `${base}${sessionId}`;
122
+ },
123
+
124
+ buildProxyUrl(proxy, config) {
125
+ if (!proxy?.username || !config?.password) return null;
126
+ const user = encodeURIComponent(proxy.username);
127
+ const pass = encodeURIComponent(config.password);
128
+ const host = config.backconnectHost;
129
+ const port = config.backconnectPort;
130
+ return `http://${user}:${pass}@${host}:${port}`;
131
+ },
132
+ };
133
+
134
+ // Provider registry
135
+ const providers = {
136
+ decodo: decodoProvider,
137
+ generic: genericBackconnectProvider,
138
+ };
139
+
140
+ export function getProvider(name) {
141
+ return providers[name] || null;
142
+ }
143
+
144
+ export function registerProvider(name, provider) {
145
+ providers[name] = provider;
146
+ }
147
+
148
+ // ---------------------------------------------------------------------------
149
+ // Proxy pool factory
150
+ // ---------------------------------------------------------------------------
151
+
152
+ function buildBackconnectProxy(config, provider, sessionId) {
153
+ const username = provider.buildSessionUsername(config.username, {
154
+ country: config.country,
155
+ state: config.state,
156
+ city: config.city,
157
+ zip: config.zip,
158
+ sessionId,
159
+ sessionDurationMinutes: config.sessionDurationMinutes,
160
+ });
161
+
162
+ return {
163
+ server: `http://${config.backconnectHost}:${config.backconnectPort}`,
164
+ username,
165
+ password: config.password,
166
+ sessionId,
167
+ };
168
+ }
169
+
170
+ /**
171
+ * Create proxy strategy helpers.
172
+ * - round_robin: per-context port rotation across a fixed pool
173
+ * - backconnect: residential backconnect endpoint with sticky sessions (provider-shaped)
174
+ */
175
+ export function createProxyPool(config) {
176
+ const {
177
+ strategy = 'round_robin',
178
+ host,
179
+ ports,
180
+ username,
181
+ password,
182
+ backconnectHost,
183
+ backconnectPort,
184
+ providerName,
185
+ } = config;
186
+
187
+ if (strategy === 'backconnect') {
188
+ if (!backconnectHost || !backconnectPort || !username || !password) return null;
189
+
190
+ const provider = getProvider(providerName || 'decodo') || decodoProvider;
191
+
192
+ return {
193
+ mode: 'backconnect',
194
+ provider,
195
+ canRotateSessions: provider.canRotateSessions,
196
+ launchRetries: provider.launchRetries,
197
+ launchTimeoutMs: provider.launchTimeoutMs,
198
+ size: 1,
199
+
200
+ getLaunchProxy(sessionId = makeSessionId('browser')) {
201
+ return buildBackconnectProxy(config, provider, sessionId);
202
+ },
203
+
204
+ getNext(sessionId = makeSessionId('ctx')) {
205
+ return buildBackconnectProxy(config, provider, sessionId);
206
+ },
207
+ };
208
+ }
209
+
210
+ // round_robin — no session rotation, single attempt
211
+ if (!host || !ports || ports.length === 0) return null;
212
+
213
+ let index = 0;
214
+
215
+ function makeProxy(port) {
216
+ return {
217
+ server: `http://${host}:${port}`,
218
+ username,
219
+ password,
220
+ };
221
+ }
222
+
223
+ return {
224
+ mode: 'round_robin',
225
+ provider: null,
226
+ canRotateSessions: false,
227
+ launchRetries: 1,
228
+ launchTimeoutMs: 60000,
229
+ size: ports.length,
230
+
231
+ getLaunchProxy() {
232
+ return makeProxy(ports[0]);
233
+ },
234
+
235
+ getNext() {
236
+ const port = ports[index % ports.length];
237
+ index++;
238
+ return makeProxy(port);
239
+ },
240
+ };
241
+ }
242
+
243
+ // ---------------------------------------------------------------------------
244
+ // URL builder (for CLI tools like yt-dlp)
245
+ // ---------------------------------------------------------------------------
246
+
247
+ /**
248
+ * Build a proxy URL string (http://user:pass@host:port) suitable for
249
+ * CLI tools like yt-dlp --proxy.
250
+ */
251
+ export function buildProxyUrl(pool, config) {
252
+ if (!pool) return null;
253
+
254
+ if (pool.mode === 'backconnect') {
255
+ const proxy = pool.getLaunchProxy(makeSessionId('ytdlp'));
256
+ if (pool.provider?.buildProxyUrl) {
257
+ return pool.provider.buildProxyUrl(proxy, config);
258
+ }
259
+ // Fallback for pools without provider
260
+ if (!proxy?.username || !config?.password) return null;
261
+ const user = encodeURIComponent(proxy.username);
262
+ const pass = encodeURIComponent(config.password);
263
+ return `http://${user}:${pass}@${config.backconnectHost}:${config.backconnectPort}`;
264
+ }
265
+
266
+ // round_robin — pick the first port
267
+ if (!config?.host || !config?.ports?.length) return null;
268
+ const user = config.username ? encodeURIComponent(config.username) : '';
269
+ const pass = config.password ? encodeURIComponent(config.password) : '';
270
+ const auth = user ? `${user}:${pass}@` : '';
271
+ return `http://${auth}${config.host}:${config.ports[0]}`;
272
+ }
273
+
274
+ // Legacy alias for backward compatibility
275
+ export function buildDecodoBackconnectUsername(baseUsername, options) {
276
+ return decodoProvider.buildSessionUsername(baseUsername, options);
277
+ }
package/lib/youtube.js CHANGED
@@ -91,17 +91,28 @@ async function detectYtDlp(log) {
91
91
  await runYtDlp(candidate, ['--version'], 5000);
92
92
  ytDlpPath = candidate;
93
93
  log('info', 'yt-dlp found', { path: candidate });
94
- return;
94
+ return true;
95
95
  } catch {}
96
96
  }
97
97
  log('warn', 'yt-dlp not found — YouTube transcript endpoint will use browser fallback');
98
+ return false;
98
99
  }
99
100
 
100
101
  function hasYtDlp() {
101
102
  return ytDlpPath !== null;
102
103
  }
103
104
 
104
- async function ytDlpTranscript(reqId, url, videoId, lang) {
105
+ /**
106
+ * Re-detect yt-dlp if initial startup detection failed.
107
+ * Called lazily before each transcript request so a transient
108
+ * startup failure doesn't permanently disable yt-dlp.
109
+ */
110
+ async function ensureYtDlp(log) {
111
+ if (ytDlpPath) return true;
112
+ return await detectYtDlp(log);
113
+ }
114
+
115
+ async function ytDlpTranscript(reqId, url, videoId, lang, proxyUrl = null) {
105
116
  if (!ytDlpPath) {
106
117
  throw new Error('yt-dlp is not available');
107
118
  }
@@ -110,10 +121,13 @@ async function ytDlpTranscript(reqId, url, videoId, lang) {
110
121
  const normalizedLang = normalizeLanguage(lang);
111
122
  const tmpDir = await mkdtemp(join(tmpdir(), 'yt-'));
112
123
 
124
+ // Build proxy args if a proxy URL is provided
125
+ const proxyArgs = proxyUrl ? ['--proxy', proxyUrl] : [];
126
+
113
127
  try {
114
128
  const titleResult = await runYtDlp(
115
129
  ytDlpPath,
116
- ['--skip-download', '--no-warnings', '--print', '%(title)s', normalizedUrl],
130
+ [...proxyArgs, '--skip-download', '--no-warnings', '--print', '%(title)s', normalizedUrl],
117
131
  15000,
118
132
  );
119
133
  const title = titleResult.stdout.trim().split('\n')[0] || '';
@@ -121,6 +135,7 @@ async function ytDlpTranscript(reqId, url, videoId, lang) {
121
135
  await runYtDlp(
122
136
  ytDlpPath,
123
137
  [
138
+ ...proxyArgs,
124
139
  '--skip-download',
125
140
  '--write-sub',
126
141
  '--write-auto-sub',
@@ -283,4 +298,4 @@ function formatVttTs(ts) {
283
298
  return ts;
284
299
  }
285
300
 
286
- export { detectYtDlp, hasYtDlp, ytDlpTranscript, parseJson3, parseVtt, parseXml };
301
+ export { detectYtDlp, hasYtDlp, ensureYtDlp, ytDlpTranscript, parseJson3, parseVtt, parseXml };
@@ -2,7 +2,7 @@
2
2
  "id": "camofox-browser",
3
3
  "name": "Camofox Browser",
4
4
  "description": "Anti-detection browser automation for AI agents using Camoufox (Firefox-based)",
5
- "version": "1.4.0",
5
+ "version": "1.5.0",
6
6
  "configSchema": {
7
7
  "type": "object",
8
8
  "properties": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@askjo/camofox-browser",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Headless browser automation server and OpenClaw plugin for AI agents - anti-detection, element refs, and session isolation",
5
5
  "type": "module",
6
6
  "main": "server.js",
@@ -66,6 +66,7 @@
66
66
  "playwright": "^1.50.0",
67
67
  "playwright-core": "^1.58.0",
68
68
  "playwright-extra": "^4.3.6",
69
+ "prom-client": "^15.1.3",
69
70
  "puppeteer-extra-plugin-stealth": "^2.11.2"
70
71
  },
71
72
  "devDependencies": {
package/plugin.ts CHANGED
@@ -151,6 +151,7 @@ async function startServer(
151
151
  // Server not ready yet
152
152
  }
153
153
  }
154
+ proc.kill();
154
155
  throw new Error("Server failed to start within 15 seconds");
155
156
  }
156
157
 
@@ -449,7 +450,7 @@ export default function register(api: PluginApi) {
449
450
  const userId = ctx.agentId || fallbackUserId;
450
451
  const result = await fetchApi(baseUrl, `/tabs/${tabId}/evaluate`, {
451
452
  method: "POST",
452
- body: { userId, expression },
453
+ body: JSON.stringify({ userId, expression }),
453
454
  });
454
455
  return toToolResult(result);
455
456
  },