@skrillex1224/playwright-toolkit 2.1.197 → 2.1.199
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/README.md +255 -255
- package/browser.d.ts +8 -8
- package/dist/browser.js.map +1 -1
- package/dist/index.cjs +182 -288
- package/dist/index.cjs.map +3 -3
- package/dist/index.js +182 -288
- package/dist/index.js.map +3 -3
- package/dist/internals/proxy-meter.js +627 -549
- package/index.d.ts +628 -657
- package/package.json +66 -66
|
@@ -1,549 +1,627 @@
|
|
|
1
|
-
import http from 'http';
|
|
2
|
-
import net from 'net';
|
|
3
|
-
import fs from 'fs';
|
|
4
|
-
import { URL } from 'url';
|
|
5
|
-
|
|
6
|
-
const HOST = '127.0.0.1';
|
|
7
|
-
const PORT = Number(process.env.PROXY_METER_PORT || 8899);
|
|
8
|
-
const LOG_PATH = String(process.env.PROXY_METER_LOG || '/tmp/proxy-meter.json');
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
return
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
if (!
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
return
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
if (
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
const
|
|
206
|
-
const
|
|
207
|
-
const
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
.
|
|
217
|
-
|
|
218
|
-
.
|
|
219
|
-
.
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
return {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
let
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
tracker.
|
|
448
|
-
});
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
const
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
});
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
1
|
+
import http from 'http';
|
|
2
|
+
import net from 'net';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import { URL } from 'url';
|
|
5
|
+
|
|
6
|
+
const HOST = '127.0.0.1';
|
|
7
|
+
const PORT = Number(process.env.PROXY_METER_PORT || 8899);
|
|
8
|
+
const LOG_PATH = String(process.env.PROXY_METER_LOG || '/tmp/proxy-meter.json');
|
|
9
|
+
const STATE_PATH = String(process.env.PROXY_METER_STATE || '').trim();
|
|
10
|
+
const FLUSH_INTERVAL_MS = Number(process.env.PROXY_METER_FLUSH_MS || 5000);
|
|
11
|
+
const UPSTREAM_URL = String(process.env.PROXY_METER_UPSTREAM || '').trim();
|
|
12
|
+
const RUN_ID = String(process.env.PROXY_METER_RUN_ID || process.env.APIFY_ACTOR_RUN_ID || '').trim();
|
|
13
|
+
|
|
14
|
+
const isTruthy = (value) => /^(1|true|yes|on)$/i.test(String(value || '').trim());
|
|
15
|
+
const DEBUG_ENABLED = isTruthy(process.env.PROXY_METER_DEBUG);
|
|
16
|
+
const DEBUG_MAX_EVENTS = Math.max(10, Number(process.env.PROXY_METER_DEBUG_MAX_EVENTS || 400));
|
|
17
|
+
const LARGE_RESPONSE_THRESHOLD_BYTES = Math.max(1024, Number(process.env.PROXY_METER_LARGE_BYTES || 1024 * 1024));
|
|
18
|
+
|
|
19
|
+
const state = {
|
|
20
|
+
startedAt: new Date().toISOString(),
|
|
21
|
+
totalInBytes: 0,
|
|
22
|
+
totalOutBytes: 0,
|
|
23
|
+
hosts: {},
|
|
24
|
+
debug: DEBUG_ENABLED
|
|
25
|
+
? {
|
|
26
|
+
enabled: true,
|
|
27
|
+
totalEvents: 0,
|
|
28
|
+
droppedEvents: 0,
|
|
29
|
+
events: [],
|
|
30
|
+
domains: {},
|
|
31
|
+
domainStatus: {},
|
|
32
|
+
}
|
|
33
|
+
: null,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
let lifecycleState = {
|
|
37
|
+
pid: process.pid,
|
|
38
|
+
status: 'starting',
|
|
39
|
+
host: HOST,
|
|
40
|
+
port: PORT,
|
|
41
|
+
startedAt: state.startedAt,
|
|
42
|
+
readyAt: '',
|
|
43
|
+
exitedAt: '',
|
|
44
|
+
exitCode: null,
|
|
45
|
+
error: '',
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const writeLifecycleState = (patch = {}) => {
|
|
49
|
+
if (!STATE_PATH) return;
|
|
50
|
+
lifecycleState = {
|
|
51
|
+
...lifecycleState,
|
|
52
|
+
...patch,
|
|
53
|
+
};
|
|
54
|
+
try {
|
|
55
|
+
fs.writeFileSync(STATE_PATH, JSON.stringify(lifecycleState, null, 2));
|
|
56
|
+
} catch {}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
writeLifecycleState();
|
|
60
|
+
|
|
61
|
+
const getHostBucket = (host) => {
|
|
62
|
+
if (!host) return null;
|
|
63
|
+
if (!state.hosts[host]) {
|
|
64
|
+
state.hosts[host] = { inBytes: 0, outBytes: 0, connections: 0, requests: 0 };
|
|
65
|
+
}
|
|
66
|
+
return state.hosts[host];
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const addTraffic = (host, dir, bytes) => {
|
|
70
|
+
const bucket = getHostBucket(host);
|
|
71
|
+
if (!bucket) return;
|
|
72
|
+
const size = Number(bytes) || 0;
|
|
73
|
+
if (size <= 0) return;
|
|
74
|
+
if (dir === 'in') {
|
|
75
|
+
bucket.inBytes += size;
|
|
76
|
+
state.totalInBytes += size;
|
|
77
|
+
} else {
|
|
78
|
+
bucket.outBytes += size;
|
|
79
|
+
state.totalOutBytes += size;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const safeError = (error) => {
|
|
84
|
+
const message = String(error?.message || error || '').trim();
|
|
85
|
+
if (!message) return '';
|
|
86
|
+
return message.length > 240 ? message.slice(0, 240) : message;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const statusLabel = (statusCode, error) => {
|
|
90
|
+
const code = Number(statusCode) || 0;
|
|
91
|
+
if (error || code <= 0) return 'ERR';
|
|
92
|
+
return `${Math.floor(code / 100)}xx`;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const ensureDebugDomain = (host) => {
|
|
96
|
+
if (!state.debug || !host) return null;
|
|
97
|
+
if (!state.debug.domains[host]) {
|
|
98
|
+
state.debug.domains[host] = {
|
|
99
|
+
domain: host,
|
|
100
|
+
count: 0,
|
|
101
|
+
failedCount: 0,
|
|
102
|
+
inBytes: 0,
|
|
103
|
+
outBytes: 0,
|
|
104
|
+
totalBytes: 0,
|
|
105
|
+
durationTotalMs: 0,
|
|
106
|
+
largeResponseCount: 0,
|
|
107
|
+
reconnectCount: 0,
|
|
108
|
+
connectSuccessCount: 0,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
return state.debug.domains[host];
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const ensureDebugDomainStatus = (host, status) => {
|
|
115
|
+
if (!state.debug || !host || !status) return null;
|
|
116
|
+
const key = `${host}::${status}`;
|
|
117
|
+
if (!state.debug.domainStatus[key]) {
|
|
118
|
+
state.debug.domainStatus[key] = {
|
|
119
|
+
domain: host,
|
|
120
|
+
status,
|
|
121
|
+
count: 0,
|
|
122
|
+
failedCount: 0,
|
|
123
|
+
inBytes: 0,
|
|
124
|
+
outBytes: 0,
|
|
125
|
+
totalBytes: 0,
|
|
126
|
+
durationTotalMs: 0,
|
|
127
|
+
largeResponseCount: 0,
|
|
128
|
+
reconnectCount: 0,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
return state.debug.domainStatus[key];
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const recordDebugEvent = (event) => {
|
|
135
|
+
if (!state.debug || !event || typeof event !== 'object') return;
|
|
136
|
+
const host = String(event.host || '').trim();
|
|
137
|
+
if (!host) return;
|
|
138
|
+
|
|
139
|
+
const code = Number(event.statusCode) || 0;
|
|
140
|
+
const inBytes = Math.max(0, Number(event.inBytes) || 0);
|
|
141
|
+
const outBytes = Math.max(0, Number(event.outBytes) || 0);
|
|
142
|
+
const totalBytes = inBytes + outBytes;
|
|
143
|
+
const durationMs = Math.max(0, Number(event.durationMs) || 0);
|
|
144
|
+
const error = String(event.error || '').trim();
|
|
145
|
+
const failed = Boolean(error) || (code > 0 && code >= 400);
|
|
146
|
+
const status = statusLabel(code, error);
|
|
147
|
+
const large = inBytes >= LARGE_RESPONSE_THRESHOLD_BYTES;
|
|
148
|
+
const channel = String(event.channel || '').trim() || 'http';
|
|
149
|
+
|
|
150
|
+
const normalized = {
|
|
151
|
+
ts: event.ts || new Date().toISOString(),
|
|
152
|
+
runId: RUN_ID,
|
|
153
|
+
channel,
|
|
154
|
+
host,
|
|
155
|
+
method: String(event.method || ''),
|
|
156
|
+
path: String(event.path || ''),
|
|
157
|
+
statusCode: code,
|
|
158
|
+
durationMs,
|
|
159
|
+
inBytes,
|
|
160
|
+
outBytes,
|
|
161
|
+
totalBytes,
|
|
162
|
+
error,
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
state.debug.totalEvents += 1;
|
|
166
|
+
if (state.debug.events.length < DEBUG_MAX_EVENTS) {
|
|
167
|
+
state.debug.events.push(normalized);
|
|
168
|
+
} else {
|
|
169
|
+
state.debug.droppedEvents += 1;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const domain = ensureDebugDomain(host);
|
|
173
|
+
if (domain) {
|
|
174
|
+
domain.count += 1;
|
|
175
|
+
if (failed) domain.failedCount += 1;
|
|
176
|
+
domain.inBytes += inBytes;
|
|
177
|
+
domain.outBytes += outBytes;
|
|
178
|
+
domain.totalBytes += totalBytes;
|
|
179
|
+
domain.durationTotalMs += durationMs;
|
|
180
|
+
if (large) domain.largeResponseCount += 1;
|
|
181
|
+
if (channel === 'connect' && code === 200) {
|
|
182
|
+
domain.connectSuccessCount += 1;
|
|
183
|
+
if (domain.connectSuccessCount > 1) {
|
|
184
|
+
domain.reconnectCount += 1;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const byStatus = ensureDebugDomainStatus(host, status);
|
|
190
|
+
if (byStatus) {
|
|
191
|
+
byStatus.count += 1;
|
|
192
|
+
if (failed) byStatus.failedCount += 1;
|
|
193
|
+
byStatus.inBytes += inBytes;
|
|
194
|
+
byStatus.outBytes += outBytes;
|
|
195
|
+
byStatus.totalBytes += totalBytes;
|
|
196
|
+
byStatus.durationTotalMs += durationMs;
|
|
197
|
+
if (large) byStatus.largeResponseCount += 1;
|
|
198
|
+
if (channel === 'connect' && code === 200 && domain?.reconnectCount > 0) {
|
|
199
|
+
byStatus.reconnectCount = domain.reconnectCount;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const toDebugRow = (row) => {
|
|
205
|
+
const count = Number(row?.count) || 0;
|
|
206
|
+
const durationTotalMs = Number(row?.durationTotalMs) || 0;
|
|
207
|
+
const avgDurationMs = count > 0 ? durationTotalMs / count : 0;
|
|
208
|
+
const failedCount = Number(row?.failedCount) || 0;
|
|
209
|
+
const failureRatePct = count > 0 ? (failedCount / count) * 100 : 0;
|
|
210
|
+
return {
|
|
211
|
+
domain: String(row?.domain || ''),
|
|
212
|
+
status: String(row?.status || ''),
|
|
213
|
+
count,
|
|
214
|
+
failedCount,
|
|
215
|
+
inBytes: Math.max(0, Number(row?.inBytes) || 0),
|
|
216
|
+
outBytes: Math.max(0, Number(row?.outBytes) || 0),
|
|
217
|
+
totalBytes: Math.max(0, Number(row?.totalBytes) || 0),
|
|
218
|
+
avgDurationMs: Math.max(0, avgDurationMs),
|
|
219
|
+
reconnectCount: Math.max(0, Number(row?.reconnectCount) || 0),
|
|
220
|
+
largeResponseCount: Math.max(0, Number(row?.largeResponseCount) || 0),
|
|
221
|
+
failureRatePct,
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const buildDebugSummary = () => {
|
|
226
|
+
if (!state.debug) return null;
|
|
227
|
+
const domainRows = Object.values(state.debug.domains || {}).map(toDebugRow);
|
|
228
|
+
const domainStatus = Object.values(state.debug.domainStatus || {}).map(toDebugRow);
|
|
229
|
+
|
|
230
|
+
const requestCount = domainRows.reduce((sum, row) => sum + row.count, 0);
|
|
231
|
+
const failedCount = domainRows.reduce((sum, row) => sum + row.failedCount, 0);
|
|
232
|
+
const largeResponseCount = domainRows.reduce((sum, row) => sum + row.largeResponseCount, 0);
|
|
233
|
+
const reconnectCount = domainRows.reduce((sum, row) => sum + row.reconnectCount, 0);
|
|
234
|
+
const failureRatePct = requestCount > 0 ? (failedCount / requestCount) * 100 : 0;
|
|
235
|
+
|
|
236
|
+
const topFailureDomains = [...domainRows]
|
|
237
|
+
.filter((row) => row.failedCount > 0)
|
|
238
|
+
.sort((a, b) => {
|
|
239
|
+
if (b.failedCount !== a.failedCount) return b.failedCount - a.failedCount;
|
|
240
|
+
return b.totalBytes - a.totalBytes;
|
|
241
|
+
})
|
|
242
|
+
.slice(0, 20);
|
|
243
|
+
const topLargeDomains = [...domainRows]
|
|
244
|
+
.filter((row) => row.largeResponseCount > 0)
|
|
245
|
+
.sort((a, b) => {
|
|
246
|
+
if (b.largeResponseCount !== a.largeResponseCount) return b.largeResponseCount - a.largeResponseCount;
|
|
247
|
+
return b.totalBytes - a.totalBytes;
|
|
248
|
+
})
|
|
249
|
+
.slice(0, 20);
|
|
250
|
+
const topReconnectDomains = [...domainRows]
|
|
251
|
+
.filter((row) => row.reconnectCount > 0)
|
|
252
|
+
.sort((a, b) => {
|
|
253
|
+
if (b.reconnectCount !== a.reconnectCount) return b.reconnectCount - a.reconnectCount;
|
|
254
|
+
return b.totalBytes - a.totalBytes;
|
|
255
|
+
})
|
|
256
|
+
.slice(0, 20);
|
|
257
|
+
|
|
258
|
+
domainStatus.sort((a, b) => {
|
|
259
|
+
if (b.totalBytes !== a.totalBytes) return b.totalBytes - a.totalBytes;
|
|
260
|
+
return b.count - a.count;
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
return {
|
|
264
|
+
largeResponseThresholdBytes: LARGE_RESPONSE_THRESHOLD_BYTES,
|
|
265
|
+
requestCount,
|
|
266
|
+
failedCount,
|
|
267
|
+
failureRatePct,
|
|
268
|
+
largeResponseCount,
|
|
269
|
+
reconnectCount,
|
|
270
|
+
domainStatus: domainStatus.slice(0, 200),
|
|
271
|
+
topFailureDomains,
|
|
272
|
+
topLargeDomains,
|
|
273
|
+
topReconnectDomains,
|
|
274
|
+
};
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
const flushSnapshot = () => {
|
|
278
|
+
try {
|
|
279
|
+
const snapshot = {
|
|
280
|
+
startedAt: state.startedAt,
|
|
281
|
+
totalInBytes: state.totalInBytes,
|
|
282
|
+
totalOutBytes: state.totalOutBytes,
|
|
283
|
+
hosts: state.hosts,
|
|
284
|
+
updatedAt: new Date().toISOString(),
|
|
285
|
+
};
|
|
286
|
+
if (state.debug) {
|
|
287
|
+
snapshot.debug = {
|
|
288
|
+
enabled: true,
|
|
289
|
+
totalEvents: state.debug.totalEvents,
|
|
290
|
+
sampledEvents: state.debug.events.length,
|
|
291
|
+
droppedEvents: state.debug.droppedEvents,
|
|
292
|
+
events: state.debug.events,
|
|
293
|
+
summary: buildDebugSummary(),
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
fs.writeFileSync(LOG_PATH, JSON.stringify(snapshot, null, 2));
|
|
297
|
+
} catch {}
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const parseUpstream = () => {
|
|
301
|
+
if (!UPSTREAM_URL) return null;
|
|
302
|
+
let parsed;
|
|
303
|
+
try {
|
|
304
|
+
parsed = new URL(UPSTREAM_URL);
|
|
305
|
+
} catch {
|
|
306
|
+
throw new Error(`[proxy-meter] invalid upstream url: ${UPSTREAM_URL}`);
|
|
307
|
+
}
|
|
308
|
+
if (parsed.protocol !== 'http:') {
|
|
309
|
+
throw new Error(`[proxy-meter] upstream protocol not supported: ${parsed.protocol}`);
|
|
310
|
+
}
|
|
311
|
+
const authHeader = parsed.username
|
|
312
|
+
? `Basic ${Buffer.from(`${decodeURIComponent(parsed.username)}:${decodeURIComponent(parsed.password || '')}`).toString('base64')}`
|
|
313
|
+
: '';
|
|
314
|
+
return {
|
|
315
|
+
host: parsed.hostname,
|
|
316
|
+
port: Number(parsed.port) || 80,
|
|
317
|
+
authHeader,
|
|
318
|
+
};
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
let fatalSignal = '';
|
|
322
|
+
const markFatal = (error) => {
|
|
323
|
+
fatalSignal = safeError(error);
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
process.on('uncaughtException', (error) => {
|
|
327
|
+
markFatal(error);
|
|
328
|
+
writeLifecycleState({
|
|
329
|
+
status: 'exited',
|
|
330
|
+
exitedAt: new Date().toISOString(),
|
|
331
|
+
exitCode: 1,
|
|
332
|
+
error: fatalSignal || 'uncaught_exception',
|
|
333
|
+
});
|
|
334
|
+
process.exit(1);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
process.on('unhandledRejection', (error) => {
|
|
338
|
+
markFatal(error);
|
|
339
|
+
writeLifecycleState({
|
|
340
|
+
status: 'exited',
|
|
341
|
+
exitedAt: new Date().toISOString(),
|
|
342
|
+
exitCode: 1,
|
|
343
|
+
error: fatalSignal || 'unhandled_rejection',
|
|
344
|
+
});
|
|
345
|
+
process.exit(1);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
const upstream = parseUpstream();
|
|
349
|
+
|
|
350
|
+
const resolveTarget = (req) => {
|
|
351
|
+
try {
|
|
352
|
+
if (req.url && req.url.startsWith('http')) {
|
|
353
|
+
return new URL(req.url);
|
|
354
|
+
}
|
|
355
|
+
const host = req.headers.host || '';
|
|
356
|
+
return new URL(`http://${host}${req.url || '/'}`);
|
|
357
|
+
} catch {
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
const createRequestTracker = (base) => {
|
|
363
|
+
const startedAt = Date.now();
|
|
364
|
+
let inBytes = 0;
|
|
365
|
+
let outBytes = 0;
|
|
366
|
+
let closed = false;
|
|
367
|
+
return {
|
|
368
|
+
addIn: (bytes) => {
|
|
369
|
+
inBytes += Math.max(0, Number(bytes) || 0);
|
|
370
|
+
},
|
|
371
|
+
addOut: (bytes) => {
|
|
372
|
+
outBytes += Math.max(0, Number(bytes) || 0);
|
|
373
|
+
},
|
|
374
|
+
close: (extra = {}) => {
|
|
375
|
+
if (closed) return;
|
|
376
|
+
closed = true;
|
|
377
|
+
recordDebugEvent({
|
|
378
|
+
ts: new Date().toISOString(),
|
|
379
|
+
runId: RUN_ID,
|
|
380
|
+
durationMs: Math.max(0, Date.now() - startedAt),
|
|
381
|
+
inBytes,
|
|
382
|
+
outBytes,
|
|
383
|
+
totalBytes: inBytes + outBytes,
|
|
384
|
+
...base,
|
|
385
|
+
...extra,
|
|
386
|
+
});
|
|
387
|
+
},
|
|
388
|
+
};
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
const forwardHttp = (req, res) => {
|
|
392
|
+
const target = resolveTarget(req);
|
|
393
|
+
const fallbackHost = String(req.headers?.host || '').split(':')[0].trim();
|
|
394
|
+
const hostname = target?.hostname || fallbackHost || '(unknown)';
|
|
395
|
+
const path = target ? `${target.pathname}${target.search || ''}` : String(req.url || '/');
|
|
396
|
+
const tracker = createRequestTracker({
|
|
397
|
+
channel: 'http',
|
|
398
|
+
host: hostname,
|
|
399
|
+
method: String(req.method || 'GET'),
|
|
400
|
+
path,
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
if (!target) {
|
|
404
|
+
tracker.close({ statusCode: 0, error: 'invalid_target_url' });
|
|
405
|
+
res.writeHead(502);
|
|
406
|
+
res.end('Bad Gateway');
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const bucket = getHostBucket(hostname);
|
|
411
|
+
if (bucket) bucket.requests += 1;
|
|
412
|
+
|
|
413
|
+
const headers = { ...req.headers };
|
|
414
|
+
if (upstream && upstream.authHeader && !headers['proxy-authorization']) {
|
|
415
|
+
headers['proxy-authorization'] = upstream.authHeader;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const requestOptions = upstream
|
|
419
|
+
? {
|
|
420
|
+
hostname: upstream.host,
|
|
421
|
+
port: upstream.port,
|
|
422
|
+
method: req.method,
|
|
423
|
+
path: target.href,
|
|
424
|
+
headers,
|
|
425
|
+
}
|
|
426
|
+
: {
|
|
427
|
+
hostname: target.hostname,
|
|
428
|
+
port: Number(target.port) || 80,
|
|
429
|
+
method: req.method,
|
|
430
|
+
path: `${target.pathname}${target.search || ''}`,
|
|
431
|
+
headers,
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
let responseStatus = 0;
|
|
435
|
+
const proxyReq = http.request(requestOptions, (proxyRes) => {
|
|
436
|
+
responseStatus = Number(proxyRes.statusCode) || 0;
|
|
437
|
+
res.writeHead(responseStatus || 502, proxyRes.headers);
|
|
438
|
+
proxyRes.on('data', (chunk) => {
|
|
439
|
+
const size = chunk?.length || 0;
|
|
440
|
+
addTraffic(hostname, 'in', size);
|
|
441
|
+
tracker.addIn(size);
|
|
442
|
+
});
|
|
443
|
+
proxyRes.on('end', () => {
|
|
444
|
+
tracker.close({ statusCode: responseStatus });
|
|
445
|
+
});
|
|
446
|
+
proxyRes.on('error', (error) => {
|
|
447
|
+
tracker.close({ statusCode: responseStatus, error: safeError(error) });
|
|
448
|
+
});
|
|
449
|
+
proxyRes.pipe(res);
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
req.on('data', (chunk) => {
|
|
453
|
+
const size = chunk?.length || 0;
|
|
454
|
+
addTraffic(hostname, 'out', size);
|
|
455
|
+
tracker.addOut(size);
|
|
456
|
+
});
|
|
457
|
+
req.on('aborted', () => {
|
|
458
|
+
tracker.close({ statusCode: responseStatus || 499, error: 'request_aborted' });
|
|
459
|
+
});
|
|
460
|
+
req.pipe(proxyReq);
|
|
461
|
+
|
|
462
|
+
res.on('close', () => {
|
|
463
|
+
tracker.close({ statusCode: responseStatus || 499 });
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
proxyReq.on('error', (error) => {
|
|
467
|
+
tracker.close({ statusCode: responseStatus, error: safeError(error) });
|
|
468
|
+
res.writeHead(502);
|
|
469
|
+
res.end('Bad Gateway');
|
|
470
|
+
});
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
const forwardConnect = (req, clientSocket, head) => {
|
|
474
|
+
const [host, portText] = String(req.url || '').split(':');
|
|
475
|
+
const hostname = host || '(unknown)';
|
|
476
|
+
const port = Number(portText) || 443;
|
|
477
|
+
const tracker = createRequestTracker({
|
|
478
|
+
channel: 'connect',
|
|
479
|
+
host: hostname,
|
|
480
|
+
method: 'CONNECT',
|
|
481
|
+
path: `${hostname}:${port}`,
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
const bucket = getHostBucket(hostname);
|
|
485
|
+
if (bucket) bucket.connections += 1;
|
|
486
|
+
|
|
487
|
+
let established = false;
|
|
488
|
+
let responseStatus = 0;
|
|
489
|
+
|
|
490
|
+
const onError = (socketA, socketB) => (error) => {
|
|
491
|
+
tracker.close({ statusCode: responseStatus, error: safeError(error) });
|
|
492
|
+
try { socketA.destroy(); } catch {}
|
|
493
|
+
try { socketB.destroy(); } catch {}
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
const attachTrafficHooks = (upstreamSocket) => {
|
|
497
|
+
upstreamSocket.on('data', (chunk) => {
|
|
498
|
+
const size = chunk?.length || 0;
|
|
499
|
+
addTraffic(hostname, 'in', size);
|
|
500
|
+
tracker.addIn(size);
|
|
501
|
+
});
|
|
502
|
+
clientSocket.on('data', (chunk) => {
|
|
503
|
+
const size = chunk?.length || 0;
|
|
504
|
+
addTraffic(hostname, 'out', size);
|
|
505
|
+
tracker.addOut(size);
|
|
506
|
+
});
|
|
507
|
+
clientSocket.on('close', () => {
|
|
508
|
+
tracker.close({
|
|
509
|
+
statusCode: responseStatus || (established ? 200 : 0),
|
|
510
|
+
error: established ? '' : 'connect_closed',
|
|
511
|
+
});
|
|
512
|
+
});
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
if (upstream) {
|
|
516
|
+
const upstreamSocket = net.connect(upstream.port, upstream.host, () => {
|
|
517
|
+
const headers = [
|
|
518
|
+
`CONNECT ${hostname}:${port} HTTP/1.1`,
|
|
519
|
+
`Host: ${hostname}:${port}`,
|
|
520
|
+
];
|
|
521
|
+
if (upstream.authHeader) {
|
|
522
|
+
headers.push(`Proxy-Authorization: ${upstream.authHeader}`);
|
|
523
|
+
}
|
|
524
|
+
headers.push('\r\n');
|
|
525
|
+
upstreamSocket.write(headers.join('\r\n'));
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
let responseBuffer = Buffer.alloc(0);
|
|
529
|
+
const onHandshake = (chunk) => {
|
|
530
|
+
responseBuffer = Buffer.concat([responseBuffer, chunk]);
|
|
531
|
+
const headerEnd = responseBuffer.indexOf('\r\n\r\n');
|
|
532
|
+
if (headerEnd === -1) return;
|
|
533
|
+
upstreamSocket.off('data', onHandshake);
|
|
534
|
+
const headerText = responseBuffer.slice(0, headerEnd).toString('utf8');
|
|
535
|
+
const statusLine = headerText.split('\r\n')[0] || '';
|
|
536
|
+
responseStatus = Number((statusLine.split(' ')[1] || '').trim());
|
|
537
|
+
if (responseStatus !== 200) {
|
|
538
|
+
tracker.close({ statusCode: responseStatus, error: `upstream_connect_${responseStatus || 0}` });
|
|
539
|
+
clientSocket.write('HTTP/1.1 502 Bad Gateway\r\n\r\n');
|
|
540
|
+
clientSocket.destroy();
|
|
541
|
+
upstreamSocket.destroy();
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
established = true;
|
|
546
|
+
clientSocket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
|
|
547
|
+
const rest = responseBuffer.slice(headerEnd + 4);
|
|
548
|
+
if (rest.length) {
|
|
549
|
+
addTraffic(hostname, 'in', rest.length);
|
|
550
|
+
tracker.addIn(rest.length);
|
|
551
|
+
clientSocket.write(rest);
|
|
552
|
+
}
|
|
553
|
+
if (head && head.length) {
|
|
554
|
+
addTraffic(hostname, 'out', head.length);
|
|
555
|
+
tracker.addOut(head.length);
|
|
556
|
+
upstreamSocket.write(head);
|
|
557
|
+
}
|
|
558
|
+
clientSocket.pipe(upstreamSocket);
|
|
559
|
+
upstreamSocket.pipe(clientSocket);
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
attachTrafficHooks(upstreamSocket);
|
|
563
|
+
upstreamSocket.on('data', onHandshake);
|
|
564
|
+
clientSocket.on('error', onError(clientSocket, upstreamSocket));
|
|
565
|
+
upstreamSocket.on('error', onError(clientSocket, upstreamSocket));
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
const serverSocket = net.connect(port, hostname, () => {
|
|
570
|
+
established = true;
|
|
571
|
+
responseStatus = 200;
|
|
572
|
+
clientSocket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
|
|
573
|
+
if (head && head.length) {
|
|
574
|
+
addTraffic(hostname, 'out', head.length);
|
|
575
|
+
tracker.addOut(head.length);
|
|
576
|
+
serverSocket.write(head);
|
|
577
|
+
}
|
|
578
|
+
clientSocket.pipe(serverSocket);
|
|
579
|
+
serverSocket.pipe(clientSocket);
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
attachTrafficHooks(serverSocket);
|
|
583
|
+
clientSocket.on('error', onError(clientSocket, serverSocket));
|
|
584
|
+
serverSocket.on('error', onError(clientSocket, serverSocket));
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
const server = http.createServer(forwardHttp);
|
|
588
|
+
server.on('connect', forwardConnect);
|
|
589
|
+
server.on('error', (error) => {
|
|
590
|
+
markFatal(error);
|
|
591
|
+
writeLifecycleState({
|
|
592
|
+
status: 'exited',
|
|
593
|
+
exitedAt: new Date().toISOString(),
|
|
594
|
+
exitCode: 1,
|
|
595
|
+
error: fatalSignal || 'server_error',
|
|
596
|
+
});
|
|
597
|
+
flushSnapshot();
|
|
598
|
+
process.exit(1);
|
|
599
|
+
});
|
|
600
|
+
server.listen(PORT, HOST, () => {
|
|
601
|
+
writeLifecycleState({
|
|
602
|
+
status: 'ready',
|
|
603
|
+
readyAt: new Date().toISOString(),
|
|
604
|
+
error: '',
|
|
605
|
+
});
|
|
606
|
+
console.log(`[proxy-meter] listening ${HOST}:${PORT} log=${LOG_PATH}`);
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
setInterval(flushSnapshot, FLUSH_INTERVAL_MS).unref();
|
|
610
|
+
flushSnapshot();
|
|
611
|
+
|
|
612
|
+
const shutdown = () => {
|
|
613
|
+
flushSnapshot();
|
|
614
|
+
process.exit(0);
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
process.on('SIGINT', shutdown);
|
|
618
|
+
process.on('SIGTERM', shutdown);
|
|
619
|
+
process.on('exit', (code) => {
|
|
620
|
+
flushSnapshot();
|
|
621
|
+
writeLifecycleState({
|
|
622
|
+
status: 'exited',
|
|
623
|
+
exitedAt: new Date().toISOString(),
|
|
624
|
+
exitCode: Number.isFinite(code) ? code : null,
|
|
625
|
+
error: fatalSignal,
|
|
626
|
+
});
|
|
627
|
+
});
|