-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
502 lines (440 loc) · 17.5 KB
/
server.js
File metadata and controls
502 lines (440 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
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
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
206
207
208
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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
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
const express = require('express');
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const protobuf = require('protobufjs');
const app = express();
const PORT = process.env.PORT || 3000;
const LOG_FILE = path.join(__dirname, 'requests.log');
// In-memory request store (capped at 1000)
const MAX_REQUESTS = 1000;
const requests = [];
let nextId = 1;
// ── Proto schema loading ──
const protoDir = path.join(__dirname, 'proto');
const protoTypes = {}; // { messageName: protobuf Type }
// Endpoint → proto message type mapping
const endpointProtoMap = {
'/reporting-ingress': 'Report',
'/otlp-reporting-ingress': 'opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest',
};
const DECODE_OPTIONS = { longs: String, enums: String, bytes: String, defaults: false };
// Load all proto files (resolve imports from proto/ directory)
function loadProto(file) {
const root = new protobuf.Root();
root.resolvePath = (origin, target) => {
// If target is already absolute, use it as-is
if (path.isAbsolute(target)) return target;
// Otherwise resolve relative to proto dir
return path.join(protoDir, target);
};
return root.load(file);
}
Promise.all([
loadProto(path.join(protoDir, 'report.proto')),
loadProto(path.join(protoDir, 'opentelemetry', 'proto', 'collector', 'trace', 'v1', 'trace_service.proto')),
]).then(([reportRoot, otlpRoot]) => {
protoTypes['Report'] = reportRoot.lookupType('Report');
protoTypes['opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest'] =
otlpRoot.lookupType('opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest');
console.log('Loaded proto schemas: Report, ExportTraceServiceRequest');
}).catch(err => {
console.warn('Failed to load proto schemas:', err.message);
});
// ── Protobuf decoder (schema-based with schema-less fallback) ──
function decodeProtobuf(buffer, reqPath) {
// Find the right message type for this endpoint
const typeName = endpointProtoMap[reqPath];
const MessageType = typeName ? protoTypes[typeName] : null;
// Try schema-based decode
if (MessageType) {
try {
const message = MessageType.decode(buffer);
return MessageType.toObject(message, DECODE_OPTIONS);
} catch (e) {
// Fall through to schema-less
}
}
// If no endpoint match, try all known types
if (!MessageType) {
for (const Type of Object.values(protoTypes)) {
try {
const message = Type.decode(buffer);
const obj = Type.toObject(message, DECODE_OPTIONS);
if (Object.keys(obj).length > 0) return obj;
} catch (e) { /* try next */ }
}
}
// Schema-less fallback
try {
const reader = protobuf.Reader.create(buffer);
return decodeMessage(reader, reader.len);
} catch (e) {
return null;
}
}
function decodeMessage(reader, end) {
const result = {};
while (reader.pos < end) {
const tag = reader.uint32();
const fieldNumber = tag >>> 3;
const wireType = tag & 7;
const key = `field_${fieldNumber}`;
let value;
switch (wireType) {
case 0: // varint
value = reader.uint64().toString();
// Show as number if it fits safely
if (Number(value) <= Number.MAX_SAFE_INTEGER) value = Number(value);
break;
case 1: // 64-bit
value = reader.double();
break;
case 2: { // length-delimited (string, bytes, or embedded message)
const bytes = reader.bytes();
// Try to decode as nested message first
const nested = tryDecodeNested(bytes);
if (nested !== null) {
value = nested;
} else {
// Try as UTF-8 string
const str = Buffer.from(bytes).toString('utf8');
if (isPrintable(str)) {
value = str;
} else {
value = Buffer.from(bytes).toString('hex');
}
}
break;
}
case 5: // 32-bit
value = reader.float();
break;
default:
reader.skipType(wireType);
continue;
}
// Handle repeated fields
if (key in result) {
if (!Array.isArray(result[key])) result[key] = [result[key]];
result[key].push(value);
} else {
result[key] = value;
}
}
return result;
}
function tryDecodeNested(bytes) {
try {
if (bytes.length === 0) return null;
const reader = protobuf.Reader.create(bytes);
const msg = decodeMessage(reader, reader.len);
// Only accept if we consumed all bytes and got at least one field
if (reader.pos === bytes.length && Object.keys(msg).length > 0) return msg;
return null;
} catch (e) {
return null;
}
}
function isPrintable(str) {
// Check if string is mostly printable ASCII/UTF-8
return str.length > 0 && /^[\x20-\x7E\t\n\r\u00A0-\uFFFF]+$/.test(str);
}
// Parse all common body types
app.use(express.json({ limit: '10mb' }));
app.use(express.text({ limit: '10mb', type: 'text/*' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
app.use(express.raw({ limit: '10mb', type: '*/*' }));
// ── Reserved routes (served before the catch-all) ──
// API: return stored requests, with optional ?url= filter
app.get('/__api/requests', (req, res) => {
let result = requests;
const urlFilter = req.query.url;
if (urlFilter) {
const lower = urlFilter.toLowerCase();
result = result.filter(r => r.url.toLowerCase().includes(lower));
}
res.json(result);
});
// UI: self-contained HTML dashboard
app.get('/ui', (_req, res) => {
res.type('html').send(UI_HTML);
});
// ── Catch-all logger ──
app.all('*', (req, res) => {
const timestamp = new Date().toISOString();
const divider = '='.repeat(60);
// Format body for display
let bodyStr = '';
if (req.body !== undefined && req.body !== null) {
if (Buffer.isBuffer(req.body)) {
bodyStr = req.body.length > 0 ? req.body.toString('utf8') : '(empty)';
} else if (typeof req.body === 'object') {
bodyStr = JSON.stringify(req.body, null, 2);
} else {
bodyStr = String(req.body);
}
} else {
bodyStr = '(none)';
}
// Format headers
const headersStr = Object.entries(req.headers)
.map(([key, value]) => ` ${key}: ${value}`)
.join('\n');
// Build the log entry
const entry = [
divider,
`Timestamp: ${timestamp}`,
`Method: ${req.method}`,
`URL: ${req.originalUrl}`,
`Path: ${req.path}`,
`Query: ${JSON.stringify(req.query)}`,
`IP: ${req.ip}`,
`Headers:`,
headersStr,
`Body:`,
` ${bodyStr}`,
divider,
'', // trailing newline
].join('\n');
// Print to console
console.log(entry);
// Append to log file
fs.appendFile(LOG_FILE, entry + '\n', (err) => {
if (err) console.error('Failed to write to log file:', err);
});
// Detect protobuf content and decode
const contentType = (req.headers['content-type'] || '').toLowerCase();
const isProtobuf = contentType.includes('protobuf');
let decodedBody = null;
let rawBodyHex = null;
if (isProtobuf && Buffer.isBuffer(req.body) && req.body.length > 0) {
// Decompress if gzip-encoded
let protoBytes = req.body;
const encoding = (req.headers['content-encoding'] || '').toLowerCase();
if (encoding === 'gzip') {
try { protoBytes = zlib.gunzipSync(req.body); } catch (e) { /* use raw */ }
} else if (encoding === 'deflate') {
try { protoBytes = zlib.inflateSync(req.body); } catch (e) { /* use raw */ }
}
rawBodyHex = protoBytes.toString('hex');
bodyStr = rawBodyHex;
decodedBody = decodeProtobuf(protoBytes, req.path);
}
// Store in memory
const reqEntry = {
id: nextId++,
timestamp,
method: req.method,
url: req.originalUrl,
path: req.path,
query: req.query,
ip: req.ip,
headers: req.headers,
body: bodyStr,
};
if (decodedBody) {
reqEntry.decodedBody = decodedBody;
reqEntry.rawBodyHex = rawBodyHex;
}
requests.push(reqEntry);
if (requests.length > MAX_REQUESTS) requests.shift();
res.status(200).json({ status: 'logged', method: req.method, path: req.originalUrl });
});
app.listen(PORT, () => {
console.log(`Reporting Inspector running on http://localhost:${PORT}`);
console.log(`Dashboard at http://localhost:${PORT}/ui`);
console.log(`Logging requests to ${LOG_FILE}`);
});
// ── Inline UI HTML ──
const UI_HTML = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Reporting Inspector</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #0f1117; color: #e1e4e8; height: 100vh; display: flex; flex-direction: column; }
/* Top bar */
.topbar { display: flex; align-items: center; gap: 12px; padding: 12px 16px; background: #161b22; border-bottom: 1px solid #30363d; flex-shrink: 0; }
.topbar h1 { font-size: 16px; font-weight: 600; white-space: nowrap; }
.topbar input { flex: 1; padding: 8px 12px; border-radius: 6px; border: 1px solid #30363d; background: #0d1117; color: #e1e4e8; font-size: 14px; outline: none; }
.topbar input:focus { border-color: #58a6ff; }
.topbar .count { font-size: 13px; color: #8b949e; white-space: nowrap; }
/* Main layout */
.main { display: flex; flex: 1; overflow: hidden; }
/* Request list */
.list { width: 380px; min-width: 280px; border-right: 1px solid #30363d; overflow-y: auto; flex-shrink: 0; }
.list-item { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid #21262d; cursor: pointer; transition: background 0.15s; }
.list-item:hover { background: #161b22; }
.list-item.active { background: #1c2333; border-left: 3px solid #58a6ff; }
.method-badge { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 4px; text-transform: uppercase; flex-shrink: 0; min-width: 58px; text-align: center; }
.method-GET { background: #1b4332; color: #52c41a; }
.method-POST { background: #0d2847; color: #58a6ff; }
.method-PUT { background: #3b2607; color: #f0a020; }
.method-PATCH { background: #2a1a3e; color: #bc8cff; }
.method-DELETE { background: #3d1114; color: #f85149; }
.method-HEAD { background: #1c2333; color: #8b949e; }
.method-OPTIONS { background: #1c2333; color: #8b949e; }
.item-info { overflow: hidden; flex: 1; }
.item-url { font-size: 13px; font-family: 'SF Mono', Monaco, Consolas, monospace; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-time { font-size: 11px; color: #8b949e; margin-top: 2px; }
/* Detail panel */
.detail { flex: 1; overflow-y: auto; padding: 20px; }
.detail.empty { display: flex; align-items: center; justify-content: center; color: #484f58; font-size: 15px; }
.section { margin-bottom: 20px; }
.section-title { font-size: 12px; font-weight: 600; text-transform: uppercase; color: #8b949e; margin-bottom: 8px; letter-spacing: 0.5px; }
.meta-grid { display: grid; grid-template-columns: auto 1fr; gap: 4px 16px; font-size: 13px; }
.meta-grid .label { color: #8b949e; font-weight: 500; }
.meta-grid .value { font-family: 'SF Mono', Monaco, Consolas, monospace; word-break: break-all; }
/* Headers table */
.headers-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.headers-table td { padding: 4px 8px; border-bottom: 1px solid #21262d; vertical-align: top; }
.headers-table td:first-child { color: #58a6ff; font-weight: 500; white-space: nowrap; width: 1%; font-family: 'SF Mono', Monaco, Consolas, monospace; }
.headers-table td:last-child { font-family: 'SF Mono', Monaco, Consolas, monospace; word-break: break-all; }
/* Body block */
.body-block { background: #161b22; border: 1px solid #30363d; border-radius: 6px; padding: 12px; font-family: 'SF Mono', Monaco, Consolas, monospace; font-size: 13px; white-space: pre-wrap; word-break: break-all; max-height: 400px; overflow-y: auto; }
/* Empty state */
.no-requests { text-align: center; padding: 40px; color: #484f58; }
</style>
</head>
<body>
<div class="topbar">
<h1>Reporting Inspector</h1>
<input type="text" id="filter" placeholder="Filter by URL..." autocomplete="off">
<span class="count" id="count">0 requests</span>
</div>
<div class="main">
<div class="list" id="list">
<div class="no-requests">No requests yet</div>
</div>
<div class="detail empty" id="detail">
Select a request to view details
</div>
</div>
<script>
const filterInput = document.getElementById('filter');
const listEl = document.getElementById('list');
const detailEl = document.getElementById('detail');
const countEl = document.getElementById('count');
let allRequests = [];
let selectedId = null;
let renderedDetailId = null;
function methodClass(m) { return 'method-' + (m || 'GET').toUpperCase(); }
function fmtTime(ts) {
const d = new Date(ts);
return d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit' })
+ '.' + String(d.getMilliseconds()).padStart(3, '0');
}
function renderList(reqs) {
if (reqs.length === 0) {
listEl.innerHTML = '<div class="no-requests">No requests match</div>';
return;
}
listEl.innerHTML = reqs.map(r => {
const active = r.id === selectedId ? ' active' : '';
return '<div class="list-item' + active + '" data-id="' + r.id + '">'
+ '<span class="method-badge ' + methodClass(r.method) + '">' + r.method + '</span>'
+ '<div class="item-info">'
+ '<div class="item-url">' + escHtml(r.url) + '</div>'
+ '<div class="item-time">' + fmtTime(r.timestamp) + '</div>'
+ '</div></div>';
}).reverse().join('');
}
function renderDetail(r, force) {
if (!r) {
detailEl.className = 'detail empty';
detailEl.innerHTML = 'Select a request to view details';
renderedDetailId = null;
return;
}
if (!force && renderedDetailId === r.id) return;
renderedDetailId = r.id;
detailEl.className = 'detail';
const headersHtml = Object.entries(r.headers || {}).map(([k, v]) =>
'<tr><td>' + escHtml(k) + '</td><td>' + escHtml(String(v)) + '</td></tr>'
).join('');
const queryHtml = Object.keys(r.query || {}).length > 0
? Object.entries(r.query).map(([k,v]) =>
'<tr><td>' + escHtml(k) + '</td><td>' + escHtml(String(v)) + '</td></tr>'
).join('')
: '<tr><td colspan="2" style="color:#484f58">(none)</td></tr>';
let bodyContent = r.body || '(none)';
try { bodyContent = JSON.stringify(JSON.parse(bodyContent), null, 2); } catch(e) {}
// Build body sections
let bodySections = '';
if (r.decodedBody) {
const decoded = JSON.stringify(r.decodedBody, null, 2);
bodySections =
'<div class="section">' +
'<div class="section-title">Body (Decoded Protobuf)</div>' +
'<div class="body-block">' + escHtml(decoded) + '</div>' +
'</div>' +
'<div class="section">' +
'<div class="section-title">Body (Raw)</div>' +
'<div class="body-block">' + escHtml(r.rawBodyHex || r.body || '(none)') + '</div>' +
'</div>';
} else {
bodySections =
'<div class="section">' +
'<div class="section-title">Body</div>' +
'<div class="body-block">' + escHtml(bodyContent) + '</div>' +
'</div>';
}
detailEl.innerHTML =
'<div class="section">' +
'<div class="section-title">Request</div>' +
'<div class="meta-grid">' +
'<span class="label">Method</span><span class="value"><span class="method-badge ' + methodClass(r.method) + '">' + r.method + '</span></span>' +
'<span class="label">URL</span><span class="value">' + escHtml(r.url) + '</span>' +
'<span class="label">Path</span><span class="value">' + escHtml(r.path) + '</span>' +
'<span class="label">IP</span><span class="value">' + escHtml(r.ip || '') + '</span>' +
'<span class="label">Time</span><span class="value">' + escHtml(r.timestamp) + '</span>' +
'</div>' +
'</div>' +
'<div class="section">' +
'<div class="section-title">Query Parameters</div>' +
'<table class="headers-table">' + queryHtml + '</table>' +
'</div>' +
'<div class="section">' +
'<div class="section-title">Headers</div>' +
'<table class="headers-table">' + headersHtml + '</table>' +
'</div>' +
bodySections;
}
function escHtml(s) {
return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
}
function getFiltered() {
const f = filterInput.value.toLowerCase();
return f ? allRequests.filter(r => r.url.toLowerCase().includes(f)) : allRequests;
}
async function poll() {
try {
const url = '/__api/requests';
const resp = await fetch(url);
allRequests = await resp.json();
const filtered = getFiltered();
countEl.textContent = filtered.length + ' request' + (filtered.length !== 1 ? 's' : '');
renderList(filtered);
if (selectedId) renderDetail(allRequests.find(r => r.id === selectedId));
} catch(e) {}
}
listEl.addEventListener('click', (e) => {
const item = e.target.closest('.list-item');
if (!item) return;
selectedId = Number(item.dataset.id);
renderList(getFiltered());
renderDetail(allRequests.find(r => r.id === selectedId), true);
});
filterInput.addEventListener('input', () => {
const filtered = getFiltered();
countEl.textContent = filtered.length + ' request' + (filtered.length !== 1 ? 's' : '');
renderList(filtered);
});
poll();
setInterval(poll, 2000);
</script>
</body>
</html>`;