// Lastgenerator des Minecraft-Härtetests: schlanke, echte Protokoll-Clients (ohne Abhängigkeiten). // Jeder Bot meldet sich regulär an, durchläuft Konfiguration und Spielphase, bestätigt Teleports, Keep-Alives // und Chunk-Pakete und sendet 20-mal je Sekunde seine Position. Paketinhalte der Welt werden nicht ausgewertet. // Aufruf: node bots.mjs // Steuerung über stdin (JSON je Zeile): {"ziel": }, {"ende": true}. Ausgabe auf stdout: JSON je Zeile. import net from 'node:net'; import zlib from 'node:zlib'; import crypto from 'node:crypto'; import fs from 'node:fs'; import readline from 'node:readline'; const [paketDatei, protokollS, portS, seedS, workerS, workerzahlS] = process.argv.slice(2); const PROTOKOLL = Number(protokollS), PORT = Number(portS), SEED = Number(seedS) >>> 0; const WORKER = Number(workerS), WORKERZAHL = Number(workerzahlS); const P = JSON.parse(fs.readFileSync(paketDatei, 'utf8')); const id = (phase, richtung, name) => { const e = P[phase]?.[richtung]?.['minecraft:' + name]; if (!e) throw new Error(`Paket fehlt: ${phase}/${richtung}/${name}`); return e.protocol_id; }; const optId = (phase, richtung, name) => P[phase]?.[richtung]?.['minecraft:' + name]?.protocol_id ?? -1; const C = { intention: id('handshake', 'serverbound', 'intention'), hello: id('login', 'serverbound', 'hello'), loginAck: id('login', 'serverbound', 'login_acknowledged'), lcCompress: id('login', 'clientbound', 'login_compression'), lcFinished: id('login', 'clientbound', 'login_finished'), lcDisconnect: id('login', 'clientbound', 'login_disconnect'), cfgInfo: id('configuration', 'serverbound', 'client_information'), cfgKeepS: id('configuration', 'serverbound', 'keep_alive'), cfgPongS: id('configuration', 'serverbound', 'pong'), cfgPacksS: id('configuration', 'serverbound', 'select_known_packs'), cfgFinishS: id('configuration', 'serverbound', 'finish_configuration'), cfgKeepC: id('configuration', 'clientbound', 'keep_alive'), cfgPingC: id('configuration', 'clientbound', 'ping'), cfgPacksC: id('configuration', 'clientbound', 'select_known_packs'), cfgFinishC: id('configuration', 'clientbound', 'finish_configuration'), cfgDisconnect: id('configuration', 'clientbound', 'disconnect'), plKeepC: id('play', 'clientbound', 'keep_alive'), plPingC: id('play', 'clientbound', 'ping'), plPosC: id('play', 'clientbound', 'player_position'), plBatchC: id('play', 'clientbound', 'chunk_batch_finished'), plDisconnect: id('play', 'clientbound', 'disconnect'), plStartCfg: id('play', 'clientbound', 'start_configuration'), plKeepS: id('play', 'serverbound', 'keep_alive'), plPongS: id('play', 'serverbound', 'pong'), plTpS: id('play', 'serverbound', 'accept_teleportation'), plMoveS: id('play', 'serverbound', 'move_player_pos'), plBatchS: id('play', 'serverbound', 'chunk_batch_received'), plCfgAck: id('play', 'serverbound', 'configuration_acknowledged'), plLoaded: optId('play', 'serverbound', 'player_loaded'), plTickEnd: optId('play', 'serverbound', 'client_tick_end'), }; const GRUPPE = 6, RASTER = 256, RADIUS = 96, TEMPO = 0.2158, HOEHE = 120; function varintLen(v) { let n = 1; v >>>= 0; while (v > 127) { v >>>= 7; n++; } return n; } function putVarint(b, o, v) { v >>>= 0; while (v > 127) { b[o++] = (v & 127) | 128; v >>>= 7; } b[o++] = v; return o; } function liesVarint(b, o, ende) { let v = 0, s = 0; for (let i = 0; i < 5; i++) { if (o + i >= ende) return null; const x = b[o + i]; v |= (x & 127) << s; if (!(x & 128)) return [v >>> 0, o + i + 1]; s += 7; } throw new Error('varint zu lang'); } const str = (s) => { const t = Buffer.from(s, 'utf8'); const b = Buffer.alloc(varintLen(t.length) + t.length); t.copy(b, putVarint(b, 0, t.length)); return b; }; const vi = (v) => { const b = Buffer.alloc(varintLen(v)); putVarint(b, 0, v); return b; }; function zufall(a) { return () => { a = (a + 0x6D2B79F5) | 0; let t = Math.imul(a ^ (a >>> 15), 1 | a); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; } // Gruppenzentren: Spirale über ein Raster, von der Mitte nach außen, damit jede Stufe die vorige Stufe enthält. function zentrum(g) { let x = 0, z = 0, dx = 1, dz = 0, schritt = 1, n = 0; while (n < g) { for (let w = 0; w < 2 && n < g; w++) { for (let k = 0; k < schritt && n < g; k++) { x += dx; z += dz; n++; } [dx, dz] = [-dz, dx]; } schritt++; } return [x * RASTER, z * RASTER]; } const zaehler = { verbunden: 0, spielend: 0, getrennt: 0, fehler: 0, bytes: 0, chunksBatches: 0, teleports: 0 }; const gruende = {}; const bots = new Map(); const spielend = []; class Bot { constructor(i) { this.i = i; this.name = 'Bot' + String(i).padStart(4, '0'); this.phase = 'login'; this.kompression = -1; this.puffer = Buffer.alloc(0); this.imSpiel = false; this.x = 0; this.y = HOEHE; this.z = 0; this.platziert = false; this.wegX = null; this.wegZ = null; this.pause = 0; this.rnd = zufall((SEED ^ Math.imul(i + 1, 0x9E3779B1)) | 0); const [hx, hz] = zentrum(Math.floor(i / GRUPPE)); this.heimX = hx + 0.5; this.heimZ = hz + 0.5; this.s = net.createConnection({ host: '127.0.0.1', port: PORT, noDelay: true }); this.s.on('connect', () => this.start()); this.s.on('data', (d) => this.empfang(d)); this.s.on('error', (e) => { zaehler.fehler++; gruende['socket:' + e.code] = (gruende['socket:' + e.code] || 0) + 1; }); this.s.on('close', () => this.zu()); } zu() { if (this.geschlossen) return; this.geschlossen = true; zaehler.getrennt++; if (this.imSpiel) { zaehler.spielend--; const k = spielend.indexOf(this); if (k >= 0) spielend.splice(k, 1); } bots.delete(this.i); if (!beenden) ausgabe({ ev: 'getrennt', i: this.i, phase: this.phase, grund: this.grund || null }); } sende(pid, teile = []) { if (this.s.destroyed || !this.s.writable) return; const koerper = Buffer.concat([vi(pid), ...teile]); let rahmen; if (this.kompression >= 0) { if (koerper.length >= this.kompression) { const z = zlib.deflateSync(koerper); const kopf = vi(koerper.length); rahmen = Buffer.concat([vi(kopf.length + z.length), kopf, z]); } else { rahmen = Buffer.concat([vi(koerper.length + 1), Buffer.from([0]), koerper]); } } else { rahmen = Buffer.concat([vi(koerper.length), koerper]); } this.s.write(rahmen); } start() { zaehler.verbunden++; const port = Buffer.alloc(2); port.writeUInt16BE(PORT); this.sende(C.intention, [vi(PROTOKOLL), str('127.0.0.1'), port, vi(2)]); const uuid = crypto.createHash('md5').update('OfflinePlayer:' + this.name).digest(); uuid[6] = (uuid[6] & 0x0f) | 0x30; uuid[8] = (uuid[8] & 0x3f) | 0x80; this.sende(C.hello, [str(this.name), uuid]); } empfang(d) { zaehler.bytes += d.length; this.puffer = this.puffer.length ? Buffer.concat([this.puffer, d]) : d; let o = 0; try { while (o < this.puffer.length) { const l = liesVarint(this.puffer, o, this.puffer.length); if (!l) break; const [laenge, start] = l; if (start + laenge > this.puffer.length) break; this.rahmen(start, start + laenge); o = start + laenge; } } catch (e) { this.grund = 'parser:' + e.message; gruende[this.grund] = (gruende[this.grund] || 0) + 1; this.s.destroy(); return; } this.puffer = o >= this.puffer.length ? Buffer.alloc(0) : this.puffer.subarray(o); } rahmen(a, e) { const b = this.puffer; let daten = b, o = a, ende = e; if (this.kompression >= 0) { const [dl, n] = liesVarint(b, o, e); o = n; if (dl !== 0) { // Große Pakete (Chunks, Entitäten) sind im Spiel für den Bot belanglos und werden nicht entpackt. if (this.phase === 'play') return; daten = zlib.inflateSync(b.subarray(o, e)); o = 0; ende = daten.length; } } const [pid, k] = liesVarint(daten, o, ende); this.paket(pid, daten, k, ende); } paket(pid, b, o, e) { if (this.phase === 'login') { if (pid === C.lcCompress) { this.kompression = liesVarint(b, o, e)[0]; } else if (pid === C.lcFinished) { this.sende(C.loginAck); this.phase = 'configuration'; this.info(); } else if (pid === C.lcDisconnect) { this.trenne('login', b, o, e); } } else if (this.phase === 'configuration') { if (pid === C.cfgKeepC) this.sende(C.cfgKeepS, [Buffer.from(b.subarray(o, o + 8))]); else if (pid === C.cfgPingC) this.sende(C.cfgPongS, [Buffer.from(b.subarray(o, o + 4))]); else if (pid === C.cfgPacksC) this.sende(C.cfgPacksS, [Buffer.from(b.subarray(o, e))]); else if (pid === C.cfgFinishC) { this.sende(C.cfgFinishS); this.phase = 'play'; } else if (pid === C.cfgDisconnect) { this.trenne('configuration', b, o, e); } } else { if (pid === C.plKeepC) this.sende(C.plKeepS, [Buffer.from(b.subarray(o, o + 8))]); else if (pid === C.plPingC) this.sende(C.plPongS, [Buffer.from(b.subarray(o, o + 4))]); else if (pid === C.plBatchC) { zaehler.chunksBatches++; const f = Buffer.alloc(4); f.writeFloatBE(25); this.sende(C.plBatchS, [f]); } else if (pid === C.plPosC) this.teleport(b, o); else if (pid === C.plStartCfg) { this.sende(C.plCfgAck); this.phase = 'configuration'; this.info(); } else if (pid === C.plDisconnect) { this.trenne('play', b, o, e); } } } info() { const vd = Buffer.from([10]); this.sende(C.cfgInfo, [str('de_de'), vd, vi(0), Buffer.from([1]), Buffer.from([0x7f]), vi(1), Buffer.from([0]), Buffer.from([0]), vi(0)]); } trenne(wo, b, o, e) { const roh = Buffer.from(b.subarray(o, Math.min(e, o + 160))).toString('latin1').replace(/[^\x20-\x7e]/g, ''); this.grund = wo + ':' + roh.slice(0, 120); gruende[this.grund] = (gruende[this.grund] || 0) + 1; } teleport(b, o) { const [tid, n] = liesVarint(b, o, b.length); const x = b.readDoubleBE(n), y = b.readDoubleBE(n + 8), z = b.readDoubleBE(n + 16); const flags = b.readInt32BE(n + 48 + 8); this.x = (flags & 1) ? this.x + x : x; this.y = (flags & 2) ? this.y + y : y; this.z = (flags & 4) ? this.z + z : z; const yaw = b.readFloatBE(n + 48), pitch = b.readFloatBE(n + 52); zaehler.teleports++; this.letzterTp = { tid, yaw: (flags & 8) ? 0 : yaw, pitch: (flags & 16) ? 0 : pitch }; if (!this.imSpiel) { // Ein echter Client lädt nach dem Beitritt erst das Gelände. Sofortiges Bestätigen vor dem ersten Server-Tick // löst die Bewegungsprüfung aus, darum wartet der Bot kurz und bestätigt dann den neuesten Teleport. if (!this.ersterTp) this.ersterTp = setTimeout(() => this.beitreten(), 500); return; } this.bestaetige(); // Bewegung nur im Takt: Der Server erlaubt seit 26.x höchstens ein Positionspaket je Client-Tick (bis client_tick_end). if (Math.abs(this.x - this.heimX) < RADIUS * 2 && Math.abs(this.z - this.heimZ) < RADIUS * 2) this.platziert = true; } bestaetige() { // Seit 26.x bestätigt der Client den Teleport mit seiner Position und Blickrichtung nach dem Teleport. const best = Buffer.alloc(32), t = this.letzterTp; best.writeDoubleBE(this.x, 0); best.writeDoubleBE(this.y, 8); best.writeDoubleBE(this.z, 16); best.writeFloatBE(t.yaw, 24); best.writeFloatBE(t.pitch, 28); this.sende(C.plTpS, [vi(t.tid), best]); } beitreten() { if (this.geschlossen || this.imSpiel) return; this.bestaetige(); if (C.plLoaded >= 0) this.sende(C.plLoaded); this.imSpiel = true; zaehler.spielend++; spielend.push(this); ausgabe({ ev: 'spielt', i: this.i, name: this.name, x: Math.floor(this.heimX), z: Math.floor(this.heimZ), y: HOEHE }); } bewegung() { const p = Buffer.alloc(25); p.writeDoubleBE(this.x, 0); p.writeDoubleBE(this.y, 8); p.writeDoubleBE(this.z, 16); p[24] = 0; this.sende(C.plMoveS, [p]); } takt() { if (this.platziert) { if (this.pause > 0) this.pause--; else { if (this.wegX === null) { const w = this.rnd() * Math.PI * 2, r = Math.sqrt(this.rnd()) * RADIUS; this.wegX = this.heimX + Math.cos(w) * r; this.wegZ = this.heimZ + Math.sin(w) * r; } const dx = this.wegX - this.x, dz = this.wegZ - this.z, d = Math.hypot(dx, dz); if (d <= TEMPO) { this.x = this.wegX; this.z = this.wegZ; this.wegX = null; this.pause = 20 + Math.floor(this.rnd() * 41); } else { this.x += dx / d * TEMPO; this.z += dz / d * TEMPO; } } } this.bewegung(); if (C.plTickEnd >= 0) this.sende(C.plTickEnd); } } let beenden = false; function ausgabe(o) { process.stdout.write(JSON.stringify({ w: WORKER, t: Date.now() / 1000, ...o }) + '\n'); } // Takt in fünf Scheiben je 50 ms, damit nicht alle Bots im selben Moment senden. Verzug der Zeitgeber = Maß für die Auslastung. let scheibe = 0, zuletzt = performance.now(); const verzug = []; setInterval(() => { const jetzt = performance.now(); verzug.push(Math.max(0, jetzt - zuletzt - 10)); zuletzt = jetzt; for (let k = scheibe; k < spielend.length; k += 5) spielend[k].takt(); scheibe = (scheibe + 1) % 5; }, 10); let ziel = 0, naechster = 0; // Anmelderate je Worker: höchstens 4 neue Verbindungen je Sekunde (wird im Protokoll genannt). setInterval(() => { for (let n = 0; n < 1 && naechster < ziel; n++) { const i = naechster++; if (i % WORKERZAHL === WORKER) bots.set(i, new Bot(i)); else n--; } }, 250); setInterval(() => { const v = verzug.splice(0).sort((a, b) => a - b); const q = (p) => (v.length ? v[Math.min(v.length - 1, Math.floor(p * v.length))] : 0); ausgabe({ ev: 'stat', spielend: zaehler.spielend, verbunden: zaehler.verbunden, getrennt: zaehler.getrennt, fehler: zaehler.fehler, bytes: zaehler.bytes, teleports: zaehler.teleports, batches: zaehler.chunksBatches, verzug_p50: +q(.5).toFixed(2), verzug_p99: +q(.99).toFixed(2), verzug_max: +(v.at(-1) ?? 0).toFixed(2), gruende }); }, 5000); readline.createInterface({ input: process.stdin }).on('line', (z) => { let m; try { m = JSON.parse(z); } catch { return; } if (m.ziel !== undefined) ziel = m.ziel; if (m.platziert !== undefined) { const b = bots.get(m.platziert); if (b) b.platziert = true; } if (m.ende) { beenden = true; for (const b of bots.values()) b.s.destroy(); setTimeout(() => process.exit(0), 300); } }); process.stdin.on('end', () => { beenden = true; process.exit(0); });