Repository navigation
Expand file tree
/
Copy pathdebug_wire_order.mjs
More file actions
124 lines (115 loc) · 4.22 KB
/
Copy pathdebug_wire_order.mjs
File metadata and controls
124 lines (115 loc) · 4.22 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
import { createGunzip } from 'node:zlib';
import { Readable } from 'node:stream';
const STREAM_BASE = 'https://frontend.cfx-services.net/api/servers/stream/';
const STREAM_ID_INTERVAL_SEC = 30;
const streamId = Math.floor(Math.floor(Date.now() / 1000) / STREAM_ID_INTERVAL_SEC) * STREAM_ID_INTERVAL_SEC;
const url = `${STREAM_BASE}${streamId}/`;
function readVarint(buf, offset) {
let value = 0, shift = 0, bytesRead = 0;
while (offset + bytesRead < buf.length && bytesRead < 5) {
const b = buf[offset + bytesRead]; bytesRead++;
value |= (b & 0x7f) << shift;
if ((b & 0x80) === 0) break;
shift += 7;
}
return { value, bytesRead };
}
function parseWireOrder(buf) {
const sequence = [];
let offset = 0, steps = 0;
while (offset < buf.length && steps++ < 50000) {
const tagRes = readVarint(buf, offset);
if (tagRes.bytesRead === 0) break;
offset += tagRes.bytesRead;
const fieldNum = tagRes.value >>> 3;
const wireType = tagRes.value & 7;
if (wireType === 2) {
const lenRes = readVarint(buf, offset);
if (lenRes.bytesRead === 0) break;
offset += lenRes.bytesRead;
const len = lenRes.value;
if (len <= 0 || len > 2*1024*1024 || offset + len > buf.length) break;
const raw = buf.subarray(offset, offset + len);
offset += len;
const s = raw.toString('utf8').replace(/\0/g, '').trim();
const isPrint = s.length >= 1 && /^[\x20-\x7e]*$/.test(s);
if (isPrint) {
sequence.push({ f: fieldNum, t: 'str', v: s });
} else {
const nested = parseWireOrder(raw);
if (nested.length > 0) sequence.push({ f: fieldNum, t: 'msg', v: nested });
}
} else if (wireType === 0) {
const vRes = readVarint(buf, offset);
if (vRes.bytesRead === 0) break;
offset += vRes.bytesRead;
sequence.push({ f: fieldNum, t: 'int', v: vRes.value });
} else if (wireType === 1) {
if (offset + 8 > buf.length) break;
offset += 8;
sequence.push({ f: fieldNum, t: '64b', v: '...' });
} else break;
}
return sequence;
}
console.log('Fetching stream...');
const response = await fetch(url, { headers: { 'User-Agent': 'FiveM-Server-List/1.0', Accept: '*/*', Referer: 'https://servers.fivem.net/' } });
const reader = response.body.getReader();
const chunks = [];
while (true) { const { done, value } = await reader.read(); if (done) break; chunks.push(value); }
let buffer = Buffer.concat(chunks);
if (buffer[0] === 0x1f && buffer[1] === 0x8b) {
const { gunzipSync } = await import('node:zlib');
buffer = gunzipSync(buffer);
}
// Parse frames
let off = 0;
const frames = [];
while (off + 4 <= buffer.length) {
const frameLen = buffer.readUInt32LE(off); off += 4;
if (frameLen <= 0 || frameLen > 10*1024*1024 || off + frameLen > buffer.length) break;
frames.push(buffer.subarray(off, off + frameLen));
off += frameLen;
}
console.log('Frames:', frames.length);
// Her frame icinde sub-messages var (varint len + payload)
function parseSubMessages(frame) {
const msgs = [];
let o = 0;
while (o < frame.length) {
const lenRes = readVarint(frame, o);
if (lenRes.bytesRead === 0) break;
o += lenRes.bytesRead;
const msgLen = lenRes.value;
if (msgLen <= 0 || o + msgLen > frame.length) break;
msgs.push(frame.subarray(o, o + msgLen));
o += msgLen;
}
return msgs;
}
// Rastgele 8 sunucu sec ve wire sirasini goster
const allMsgs = [];
for (const frame of frames) {
const subs = parseSubMessages(frame);
if (subs.length > 0) allMsgs.push(...subs);
else allMsgs.push(frame);
}
console.log('Toplam sunucu mesaji:', allMsgs.length);
// Farkli indekslerden sec
const indices = [0, 1, 5, 100, 500, 2000, 10000, 20000].filter(i => i < allMsgs.length);
for (const idx of indices) {
const seq = parseWireOrder(allMsgs[idx]);
console.log(`\n========== MESAJ #${idx} ==========`);
for (const item of seq) {
if (item.t === 'msg') {
console.log(` f${item.f} [nested]:`);
for (const sub of item.v) {
const val = typeof sub.v === 'string' ? sub.v.slice(0, 100) : sub.v;
console.log(` f${sub.f} ${sub.t}: ${val}`);
}
} else {
const val = typeof item.v === 'string' ? item.v.slice(0, 100) : item.v;
console.log(` f${item.f} ${item.t}: ${val}`);
}
}
}