brave-real-browser-mcp-server 2.27.32 → 2.29.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/dist/browser-manager.js +344 -0
- package/dist/handlers/advanced-tools.js +899 -179
- package/dist/handlers/navigation-handlers.js +220 -16
- package/dist/handlers/tool-executor.js +201 -0
- package/dist/tool-definitions.js +104 -6
- package/package.json +2 -2
package/dist/browser-manager.js
CHANGED
|
@@ -36,6 +36,350 @@ function loadEnvFile() {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
loadEnvFile();
|
|
39
|
+
// Global proxy rotation state
|
|
40
|
+
let proxyRotation = {
|
|
41
|
+
enabled: false,
|
|
42
|
+
proxies: [],
|
|
43
|
+
rotationStrategy: 'round-robin',
|
|
44
|
+
rotateAfterRequests: 50,
|
|
45
|
+
rotateOnBlock: true,
|
|
46
|
+
currentIndex: 0,
|
|
47
|
+
requestCount: 0,
|
|
48
|
+
proxyStats: new Map()
|
|
49
|
+
};
|
|
50
|
+
// Initialize proxy rotation
|
|
51
|
+
export function initProxyRotation(proxies, strategy = 'round-robin') {
|
|
52
|
+
const parsedProxies = proxies.map(p => {
|
|
53
|
+
if (typeof p === 'string') {
|
|
54
|
+
// Parse string format: protocol://user:pass@host:port or host:port
|
|
55
|
+
const match = p.match(/^(?:(https?|socks5):\/\/)?(?:([^:]+):([^@]+)@)?([^:]+):(\d+)$/);
|
|
56
|
+
if (match) {
|
|
57
|
+
return {
|
|
58
|
+
protocol: match[1] || 'http',
|
|
59
|
+
username: match[2],
|
|
60
|
+
password: match[3],
|
|
61
|
+
host: match[4],
|
|
62
|
+
port: parseInt(match[5], 10)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
// Simple host:port
|
|
66
|
+
const [host, port] = p.split(':');
|
|
67
|
+
return { host, port: parseInt(port, 10), protocol: 'http' };
|
|
68
|
+
}
|
|
69
|
+
return p;
|
|
70
|
+
});
|
|
71
|
+
proxyRotation = {
|
|
72
|
+
enabled: true,
|
|
73
|
+
proxies: parsedProxies,
|
|
74
|
+
rotationStrategy: strategy,
|
|
75
|
+
rotateAfterRequests: 50,
|
|
76
|
+
rotateOnBlock: true,
|
|
77
|
+
currentIndex: 0,
|
|
78
|
+
requestCount: 0,
|
|
79
|
+
proxyStats: new Map()
|
|
80
|
+
};
|
|
81
|
+
// Initialize stats for each proxy
|
|
82
|
+
parsedProxies.forEach(p => {
|
|
83
|
+
const key = `${p.host}:${p.port}`;
|
|
84
|
+
proxyRotation.proxyStats.set(key, { uses: 0, failures: 0, lastUsed: 0 });
|
|
85
|
+
});
|
|
86
|
+
console.log(`[ProxyRotation] Initialized with ${parsedProxies.length} proxies, strategy: ${strategy}`);
|
|
87
|
+
}
|
|
88
|
+
// Get current proxy
|
|
89
|
+
export function getCurrentProxy() {
|
|
90
|
+
if (!proxyRotation.enabled || proxyRotation.proxies.length === 0) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return proxyRotation.proxies[proxyRotation.currentIndex];
|
|
94
|
+
}
|
|
95
|
+
// Get proxy as URL string
|
|
96
|
+
export function getCurrentProxyUrl() {
|
|
97
|
+
const proxy = getCurrentProxy();
|
|
98
|
+
if (!proxy)
|
|
99
|
+
return null;
|
|
100
|
+
let url = `${proxy.protocol || 'http'}://`;
|
|
101
|
+
if (proxy.username && proxy.password) {
|
|
102
|
+
url += `${proxy.username}:${proxy.password}@`;
|
|
103
|
+
}
|
|
104
|
+
url += `${proxy.host}:${proxy.port}`;
|
|
105
|
+
return url;
|
|
106
|
+
}
|
|
107
|
+
// Rotate to next proxy
|
|
108
|
+
export function rotateProxy(reason = 'scheduled') {
|
|
109
|
+
if (!proxyRotation.enabled || proxyRotation.proxies.length === 0) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
const prevProxy = getCurrentProxy();
|
|
113
|
+
const prevKey = prevProxy ? `${prevProxy.host}:${prevProxy.port}` : '';
|
|
114
|
+
switch (proxyRotation.rotationStrategy) {
|
|
115
|
+
case 'round-robin':
|
|
116
|
+
proxyRotation.currentIndex = (proxyRotation.currentIndex + 1) % proxyRotation.proxies.length;
|
|
117
|
+
break;
|
|
118
|
+
case 'random':
|
|
119
|
+
proxyRotation.currentIndex = Math.floor(Math.random() * proxyRotation.proxies.length);
|
|
120
|
+
break;
|
|
121
|
+
case 'least-used':
|
|
122
|
+
let minUses = Infinity;
|
|
123
|
+
let minIndex = 0;
|
|
124
|
+
proxyRotation.proxies.forEach((p, i) => {
|
|
125
|
+
const key = `${p.host}:${p.port}`;
|
|
126
|
+
const stats = proxyRotation.proxyStats.get(key);
|
|
127
|
+
if (stats && stats.uses < minUses) {
|
|
128
|
+
minUses = stats.uses;
|
|
129
|
+
minIndex = i;
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
proxyRotation.currentIndex = minIndex;
|
|
133
|
+
break;
|
|
134
|
+
case 'on-error':
|
|
135
|
+
// Only rotate on error/blocked, otherwise keep current
|
|
136
|
+
if (reason === 'error' || reason === 'blocked') {
|
|
137
|
+
proxyRotation.currentIndex = (proxyRotation.currentIndex + 1) % proxyRotation.proxies.length;
|
|
138
|
+
}
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
proxyRotation.requestCount = 0;
|
|
142
|
+
const newProxy = getCurrentProxy();
|
|
143
|
+
const newKey = newProxy ? `${newProxy.host}:${newProxy.port}` : '';
|
|
144
|
+
// Update stats
|
|
145
|
+
if (newKey && proxyRotation.proxyStats.has(newKey)) {
|
|
146
|
+
const stats = proxyRotation.proxyStats.get(newKey);
|
|
147
|
+
stats.uses++;
|
|
148
|
+
stats.lastUsed = Date.now();
|
|
149
|
+
if (reason === 'error' || reason === 'blocked') {
|
|
150
|
+
const prevStats = proxyRotation.proxyStats.get(prevKey);
|
|
151
|
+
if (prevStats)
|
|
152
|
+
prevStats.failures++;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
console.log(`[ProxyRotation] Rotated proxy (${reason}): ${prevKey} -> ${newKey}`);
|
|
156
|
+
return newProxy;
|
|
157
|
+
}
|
|
158
|
+
// Check if proxy should be rotated
|
|
159
|
+
export function shouldRotateProxy() {
|
|
160
|
+
if (!proxyRotation.enabled)
|
|
161
|
+
return false;
|
|
162
|
+
proxyRotation.requestCount++;
|
|
163
|
+
if (proxyRotation.rotateAfterRequests &&
|
|
164
|
+
proxyRotation.requestCount >= proxyRotation.rotateAfterRequests) {
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
// Mark current proxy as blocked/failed
|
|
170
|
+
export function markProxyFailed(blocked = false) {
|
|
171
|
+
if (!proxyRotation.enabled)
|
|
172
|
+
return;
|
|
173
|
+
const proxy = getCurrentProxy();
|
|
174
|
+
if (proxy) {
|
|
175
|
+
const key = `${proxy.host}:${proxy.port}`;
|
|
176
|
+
const stats = proxyRotation.proxyStats.get(key);
|
|
177
|
+
if (stats) {
|
|
178
|
+
stats.failures++;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (proxyRotation.rotateOnBlock && blocked) {
|
|
182
|
+
rotateProxy('blocked');
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
// Get proxy stats
|
|
186
|
+
export function getProxyStats() {
|
|
187
|
+
return {
|
|
188
|
+
enabled: proxyRotation.enabled,
|
|
189
|
+
currentProxy: getCurrentProxyUrl(),
|
|
190
|
+
totalProxies: proxyRotation.proxies.length,
|
|
191
|
+
currentIndex: proxyRotation.currentIndex,
|
|
192
|
+
requestCount: proxyRotation.requestCount,
|
|
193
|
+
strategy: proxyRotation.rotationStrategy,
|
|
194
|
+
stats: Object.fromEntries(proxyRotation.proxyStats)
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
// Global rate limiter configuration
|
|
198
|
+
let rateLimiterConfig = {
|
|
199
|
+
enabled: false,
|
|
200
|
+
requestsPerSecond: 5,
|
|
201
|
+
requestsPerMinute: 100,
|
|
202
|
+
burstLimit: 10,
|
|
203
|
+
domainLimits: new Map(),
|
|
204
|
+
globalCooldown: 100,
|
|
205
|
+
retryAfter: 5000
|
|
206
|
+
};
|
|
207
|
+
// Global rate limiter state
|
|
208
|
+
let rateLimiterState = {
|
|
209
|
+
lastRequestTime: 0,
|
|
210
|
+
requestsInSecond: 0,
|
|
211
|
+
requestsInMinute: 0,
|
|
212
|
+
secondWindowStart: Date.now(),
|
|
213
|
+
minuteWindowStart: Date.now(),
|
|
214
|
+
domainRequestTimes: new Map(),
|
|
215
|
+
isThrottled: false,
|
|
216
|
+
throttleUntil: 0
|
|
217
|
+
};
|
|
218
|
+
/**
|
|
219
|
+
* Initialize rate limiter with custom configuration
|
|
220
|
+
*/
|
|
221
|
+
export function initRateLimiter(config = {}) {
|
|
222
|
+
rateLimiterConfig = {
|
|
223
|
+
...rateLimiterConfig,
|
|
224
|
+
enabled: true,
|
|
225
|
+
...config
|
|
226
|
+
};
|
|
227
|
+
// Reset state
|
|
228
|
+
rateLimiterState = {
|
|
229
|
+
lastRequestTime: 0,
|
|
230
|
+
requestsInSecond: 0,
|
|
231
|
+
requestsInMinute: 0,
|
|
232
|
+
secondWindowStart: Date.now(),
|
|
233
|
+
minuteWindowStart: Date.now(),
|
|
234
|
+
domainRequestTimes: new Map(),
|
|
235
|
+
isThrottled: false,
|
|
236
|
+
throttleUntil: 0
|
|
237
|
+
};
|
|
238
|
+
console.log(`[RateLimiter] Initialized: ${rateLimiterConfig.requestsPerSecond}/sec, ${rateLimiterConfig.requestsPerMinute}/min`);
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Set rate limit for a specific domain
|
|
242
|
+
*/
|
|
243
|
+
export function setDomainRateLimit(domain, requestsPerSecond, requestsPerMinute) {
|
|
244
|
+
rateLimiterConfig.domainLimits.set(domain, {
|
|
245
|
+
requestsPerSecond,
|
|
246
|
+
requestsPerMinute: requestsPerMinute || requestsPerSecond * 60
|
|
247
|
+
});
|
|
248
|
+
console.log(`[RateLimiter] Domain limit set for ${domain}: ${requestsPerSecond}/sec`);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Get the delay needed before making a request
|
|
252
|
+
* Returns 0 if request can be made immediately
|
|
253
|
+
*/
|
|
254
|
+
export function getRateLimitDelay(url) {
|
|
255
|
+
if (!rateLimiterConfig.enabled)
|
|
256
|
+
return 0;
|
|
257
|
+
const now = Date.now();
|
|
258
|
+
// Check if we're in a throttle period
|
|
259
|
+
if (rateLimiterState.isThrottled && now < rateLimiterState.throttleUntil) {
|
|
260
|
+
return rateLimiterState.throttleUntil - now;
|
|
261
|
+
}
|
|
262
|
+
else if (rateLimiterState.isThrottled) {
|
|
263
|
+
rateLimiterState.isThrottled = false;
|
|
264
|
+
}
|
|
265
|
+
// Update sliding windows
|
|
266
|
+
if (now - rateLimiterState.secondWindowStart >= 1000) {
|
|
267
|
+
rateLimiterState.requestsInSecond = 0;
|
|
268
|
+
rateLimiterState.secondWindowStart = now;
|
|
269
|
+
}
|
|
270
|
+
if (now - rateLimiterState.minuteWindowStart >= 60000) {
|
|
271
|
+
rateLimiterState.requestsInMinute = 0;
|
|
272
|
+
rateLimiterState.minuteWindowStart = now;
|
|
273
|
+
}
|
|
274
|
+
// Check global limits
|
|
275
|
+
if (rateLimiterState.requestsInSecond >= rateLimiterConfig.requestsPerSecond) {
|
|
276
|
+
return 1000 - (now - rateLimiterState.secondWindowStart);
|
|
277
|
+
}
|
|
278
|
+
if (rateLimiterState.requestsInMinute >= rateLimiterConfig.requestsPerMinute) {
|
|
279
|
+
return 60000 - (now - rateLimiterState.minuteWindowStart);
|
|
280
|
+
}
|
|
281
|
+
// Check global cooldown
|
|
282
|
+
const timeSinceLastRequest = now - rateLimiterState.lastRequestTime;
|
|
283
|
+
if (timeSinceLastRequest < rateLimiterConfig.globalCooldown) {
|
|
284
|
+
return rateLimiterConfig.globalCooldown - timeSinceLastRequest;
|
|
285
|
+
}
|
|
286
|
+
// Check domain-specific limits
|
|
287
|
+
if (url) {
|
|
288
|
+
try {
|
|
289
|
+
const domain = new URL(url).hostname;
|
|
290
|
+
const domainLimit = rateLimiterConfig.domainLimits.get(domain);
|
|
291
|
+
if (domainLimit) {
|
|
292
|
+
const domainTimes = rateLimiterState.domainRequestTimes.get(domain) || [];
|
|
293
|
+
// Clean up old timestamps
|
|
294
|
+
const recentTimes = domainTimes.filter(t => now - t < 60000);
|
|
295
|
+
rateLimiterState.domainRequestTimes.set(domain, recentTimes);
|
|
296
|
+
// Check domain limits
|
|
297
|
+
const lastSecondRequests = recentTimes.filter(t => now - t < 1000).length;
|
|
298
|
+
if (lastSecondRequests >= domainLimit.requestsPerSecond) {
|
|
299
|
+
const oldestInSecond = recentTimes.filter(t => now - t < 1000)[0];
|
|
300
|
+
return oldestInSecond ? 1000 - (now - oldestInSecond) : 1000;
|
|
301
|
+
}
|
|
302
|
+
const lastMinuteRequests = recentTimes.length;
|
|
303
|
+
if (lastMinuteRequests >= domainLimit.requestsPerMinute) {
|
|
304
|
+
const oldestInMinute = recentTimes[0];
|
|
305
|
+
return oldestInMinute ? 60000 - (now - oldestInMinute) : 60000;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
// Invalid URL, ignore domain limits
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return 0;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Wait for rate limit if needed, then record the request
|
|
317
|
+
*/
|
|
318
|
+
export async function waitForRateLimit(url) {
|
|
319
|
+
const delay = getRateLimitDelay(url);
|
|
320
|
+
if (delay > 0) {
|
|
321
|
+
console.log(`[RateLimiter] Waiting ${delay}ms before request${url ? ` to ${new URL(url).hostname}` : ''}`);
|
|
322
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
323
|
+
}
|
|
324
|
+
// Record the request
|
|
325
|
+
recordRequest(url);
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Record a request for rate limiting tracking
|
|
329
|
+
*/
|
|
330
|
+
export function recordRequest(url) {
|
|
331
|
+
if (!rateLimiterConfig.enabled)
|
|
332
|
+
return;
|
|
333
|
+
const now = Date.now();
|
|
334
|
+
rateLimiterState.lastRequestTime = now;
|
|
335
|
+
rateLimiterState.requestsInSecond++;
|
|
336
|
+
rateLimiterState.requestsInMinute++;
|
|
337
|
+
// Record domain-specific request
|
|
338
|
+
if (url) {
|
|
339
|
+
try {
|
|
340
|
+
const domain = new URL(url).hostname;
|
|
341
|
+
const domainTimes = rateLimiterState.domainRequestTimes.get(domain) || [];
|
|
342
|
+
domainTimes.push(now);
|
|
343
|
+
rateLimiterState.domainRequestTimes.set(domain, domainTimes);
|
|
344
|
+
}
|
|
345
|
+
catch {
|
|
346
|
+
// Invalid URL
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Signal that a rate limit response (429) was received
|
|
352
|
+
*/
|
|
353
|
+
export function onRateLimitHit(retryAfterMs) {
|
|
354
|
+
const delay = retryAfterMs || rateLimiterConfig.retryAfter;
|
|
355
|
+
rateLimiterState.isThrottled = true;
|
|
356
|
+
rateLimiterState.throttleUntil = Date.now() + delay;
|
|
357
|
+
console.log(`[RateLimiter] Rate limit hit! Throttling for ${delay}ms`);
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Get current rate limiter stats
|
|
361
|
+
*/
|
|
362
|
+
export function getRateLimiterStats() {
|
|
363
|
+
const now = Date.now();
|
|
364
|
+
return {
|
|
365
|
+
enabled: rateLimiterConfig.enabled,
|
|
366
|
+
requestsInSecond: rateLimiterState.requestsInSecond,
|
|
367
|
+
requestsInMinute: rateLimiterState.requestsInMinute,
|
|
368
|
+
limitsPerSecond: rateLimiterConfig.requestsPerSecond,
|
|
369
|
+
limitsPerMinute: rateLimiterConfig.requestsPerMinute,
|
|
370
|
+
isThrottled: rateLimiterState.isThrottled,
|
|
371
|
+
throttleRemaining: rateLimiterState.isThrottled ? Math.max(0, rateLimiterState.throttleUntil - now) : 0,
|
|
372
|
+
domainLimits: Object.fromEntries(rateLimiterConfig.domainLimits),
|
|
373
|
+
cooldown: rateLimiterConfig.globalCooldown
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Disable rate limiter
|
|
378
|
+
*/
|
|
379
|
+
export function disableRateLimiter() {
|
|
380
|
+
rateLimiterConfig.enabled = false;
|
|
381
|
+
console.log('[RateLimiter] Disabled');
|
|
382
|
+
}
|
|
39
383
|
// Browser error categorization
|
|
40
384
|
export var BrowserErrorType;
|
|
41
385
|
(function (BrowserErrorType) {
|