nomagick commited on
Commit
0a09aeb
·
unverified ·
1 Parent(s): 8951578
backend/functions/src/services/puppeteer.ts CHANGED
@@ -277,9 +277,14 @@ function giveSnapshot(stopActiveSnapshot) {
277
 
278
  const domainSet = new Set<string>();
279
  let reqCounter = 0;
 
 
280
 
281
  page.on('request', (req) => {
282
  reqCounter++;
 
 
 
283
  const requestUrl = req.url();
284
  if (!requestUrl.startsWith("http:") && !requestUrl.startsWith("https:") && requestUrl !== 'about:blank') {
285
  return req.abort('blockedbyclient', 1000);
@@ -291,7 +296,6 @@ function giveSnapshot(stopActiveSnapshot) {
291
 
292
  if (this.circuitBreakerHosts.has(parsedUrl.hostname.toLowerCase())) {
293
  page.emit('abuse', { url: requestUrl, page, sn, reason: `Abusive request: ${requestUrl}` });
294
-
295
  return req.abort('blockedbyclient', 1000);
296
  }
297
 
@@ -304,14 +308,22 @@ function giveSnapshot(stopActiveSnapshot) {
304
  return req.abort('blockedbyclient', 1000);
305
  }
306
 
307
- if (reqCounter > 2000) {
308
- page.emit('abuse', { url: requestUrl, page, sn, reason: `DDoS attack suspected: Too many requests: ${reqCounter}` });
 
309
 
310
- return req.abort('blockedbyclient', 1000);
 
 
 
 
 
 
311
  }
312
 
313
  if (domainSet.size > 200) {
314
- page.emit('abuse', { url: requestUrl, page, sn, reason: `DDoS attack suspected: Too many domains (${domainSet.size})` });
 
315
 
316
  return req.abort('blockedbyclient', 1000);
317
  }
@@ -329,7 +341,7 @@ const handlePageLoad = () => {
329
  if (window.haltSnapshot) {
330
  return;
331
  }
332
- if (document.readyState !== 'complete') {
333
  return;
334
  }
335
  const thisTextLength = (document.body.innerText || '').length;
@@ -503,7 +515,8 @@ document.addEventListener('load', handlePageLoad);
503
  if (options?.minIntervalMs) {
504
  ckpt.push(delay(options.minIntervalMs));
505
  }
506
- await Promise.race(ckpt);
 
507
  if (finalized) {
508
  yield { ...snapshot, screenshot } as PageSnapshot;
509
  break;
@@ -515,6 +528,9 @@ document.addEventListener('load', handlePageLoad);
515
  if (snapshot || screenshot) {
516
  yield { ...snapshot, screenshot } as PageSnapshot;
517
  }
 
 
 
518
  }
519
  } finally {
520
  gotoPromise.finally(() => {
 
277
 
278
  const domainSet = new Set<string>();
279
  let reqCounter = 0;
280
+ const t0 = Date.now();
281
+ let halt = false;
282
 
283
  page.on('request', (req) => {
284
  reqCounter++;
285
+ if (halt) {
286
+ return req.abort('blockedbyclient', 1000);
287
+ }
288
  const requestUrl = req.url();
289
  if (!requestUrl.startsWith("http:") && !requestUrl.startsWith("https:") && requestUrl !== 'about:blank') {
290
  return req.abort('blockedbyclient', 1000);
 
296
 
297
  if (this.circuitBreakerHosts.has(parsedUrl.hostname.toLowerCase())) {
298
  page.emit('abuse', { url: requestUrl, page, sn, reason: `Abusive request: ${requestUrl}` });
 
299
  return req.abort('blockedbyclient', 1000);
300
  }
301
 
 
308
  return req.abort('blockedbyclient', 1000);
309
  }
310
 
311
+ const dt = Math.ceil((Date.now() - t0) / 1000);
312
+ const rps = reqCounter / dt;
313
+ // console.log(`rps: ${rps}`);
314
 
315
+ if (reqCounter > 1000) {
316
+ if (rps > 60 || reqCounter > 2000) {
317
+ page.emit('abuse', { url: requestUrl, page, sn, reason: `DDoS attack suspected: Too many requests` });
318
+ halt = true;
319
+
320
+ return req.abort('blockedbyclient', 1000);
321
+ }
322
  }
323
 
324
  if (domainSet.size > 200) {
325
+ page.emit('abuse', { url: requestUrl, page, sn, reason: `DDoS attack suspected: Too many domains` });
326
+ halt = true;
327
 
328
  return req.abort('blockedbyclient', 1000);
329
  }
 
341
  if (window.haltSnapshot) {
342
  return;
343
  }
344
+ if (document.readyState === 'loading') {
345
  return;
346
  }
347
  const thisTextLength = (document.body.innerText || '').length;
 
515
  if (options?.minIntervalMs) {
516
  ckpt.push(delay(options.minIntervalMs));
517
  }
518
+ let error;
519
+ await Promise.race(ckpt).catch((err)=> error = err);
520
  if (finalized) {
521
  yield { ...snapshot, screenshot } as PageSnapshot;
522
  break;
 
528
  if (snapshot || screenshot) {
529
  yield { ...snapshot, screenshot } as PageSnapshot;
530
  }
531
+ if (error) {
532
+ throw error;
533
+ }
534
  }
535
  } finally {
536
  gotoPromise.finally(() => {
thinapps-shared CHANGED
@@ -1 +1 @@
1
- Subproject commit 7a221b7a8d5fd0244f33dd5dc9d62ce551c5bded
 
1
+ Subproject commit 24a942452fa12e622a82f05f817c6102c5e84891