netget-tool 0.3.4

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
Documentation
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
//! Minecraft Java Edition client: server-list pings (modern and legacy) and login probes,
//! each over a fresh TCP connection, as a launcher's server list does.
pub mod actions;
use crate::client::llm_budget::call_llm_for_client;
use crate::llm::actions::client_trait::{Client, ClientActionResult};
use crate::logging::emit::Log;
use crate::protocol::{ConnectContext, Event};
use crate::server::minecraft::wire;
use crate::state::client_handles::{ClientCommand, ClientSendOutcome};
use crate::state::{AccessLogOwner, ClientStatus};
pub use actions::MinecraftClientProtocol;
use anyhow::{Context, Result};
use serde_json::{json, Value};
use std::net::SocketAddr;
use tokio::{
    io::{AsyncReadExt, AsyncWriteExt},
    net::TcpStream,
    sync::mpsc,
};

/// Packets read during one login attempt before giving up on reaching a verdict.
pub const MAX_LOGIN_PACKETS: usize = 16;

pub async fn connect(ctx: ConnectContext) -> Result<SocketAddr> {
    let (host, port) = split_host_port(&ctx.remote_addr)?;
    tokio::net::lookup_host((host.as_str(), port))
        .await
        .context("resolve the server address")?
        .next()
        .context("the server address resolved to nothing")?;
    ctx.state
        .update_client_status(ctx.client_id, ClientStatus::Connected)
        .await;
    let external =
        crate::client::command_support::register_command_channel(&ctx.state, ctx.client_id).await;
    let (internal_tx, internal_rx) = mpsc::channel::<Value>(16);
    let (event_tx, mut event_rx) = mpsc::channel::<Event>(16);
    event_tx.try_send(Event::new(
        &actions::READY_EVENT,
        json!({"remote_addr": ctx.remote_addr}),
    ))?;
    let events_ctx = ctx.clone();
    let dispatcher = tokio::spawn(async move {
        let protocol = MinecraftClientProtocol;
        while let Some(event) = event_rx.recv().await {
            let instruction = events_ctx
                .state
                .get_instruction_for_client(events_ctx.client_id)
                .await
                .unwrap_or_default();
            let memory = events_ctx
                .state
                .get_memory_for_client(events_ctx.client_id)
                .await
                .unwrap_or_default();
            match call_llm_for_client(
                &events_ctx.llm_client,
                &events_ctx.state,
                events_ctx.client_id.to_string(),
                &instruction,
                &memory,
                Some(&event),
                &protocol,
                &events_ctx.status_tx,
            )
            .await
            {
                Ok(result) => {
                    if let Some(memory) = result.memory_updates {
                        events_ctx
                            .state
                            .set_memory_for_client(events_ctx.client_id, memory)
                            .await;
                    }
                    for action in result.actions {
                        if internal_tx.send(action).await.is_err() {
                            return;
                        }
                    }
                }
                Err(e) => Log::new(Some(&events_ctx.status_tx))
                    .warn(format!("Minecraft client handler: {e}")),
            }
        }
    });
    let dispatcher_abort = dispatcher.abort_handle();
    ctx.state
        .register_client_task(ctx.client_id, dispatcher)
        .await;
    let session_ctx = ctx.clone();
    let task = tokio::spawn(async move {
        let result = session(&session_ctx, &host, port, external, internal_rx, event_tx).await;
        if result.is_err() {
            dispatcher_abort.abort();
        }
        let status = match result {
            Ok(()) => ClientStatus::Disconnected,
            Err(e) => {
                Log::new(Some(&session_ctx.status_tx)).warn(format!("Minecraft client ended: {e}"));
                ClientStatus::Error(e.to_string())
            }
        };
        session_ctx
            .state
            .update_client_status(session_ctx.client_id, status)
            .await;
        session_ctx
            .state
            .remove_client_handle(session_ctx.client_id)
            .await;
        let _ = session_ctx.status_tx.send("__UPDATE_UI__".into());
    });
    ctx.state.register_client_task(ctx.client_id, task).await;
    Ok("0.0.0.0:0".parse()?)
}

/// `host:port`, `[v6]:port`, or a bare host (port 25565).
pub fn split_host_port(addr: &str) -> Result<(String, u16)> {
    if let Some(rest) = addr.strip_prefix('[') {
        let (host, tail) = rest.split_once(']').context("unclosed [ in address")?;
        let port = match tail.strip_prefix(':') {
            Some(p) => p.parse().context("port")?,
            None => 25565,
        };
        return Ok((host.to_string(), port));
    }
    match addr.rsplit_once(':') {
        Some((host, port)) if !host.contains(':') => {
            Ok((host.to_string(), port.parse().context("port")?))
        }
        _ => Ok((addr.to_string(), 25565)),
    }
}

/// Per-connection framing state: compression switches on during login.
struct Conn {
    stream: TcpStream,
    threshold: Option<i32>,
}

impl Conn {
    async fn open(host: &str, port: u16) -> Result<Self> {
        let stream = tokio::time::timeout(wire::IO_TIMEOUT, TcpStream::connect((host, port)))
            .await
            .context("Minecraft connect deadline")??;
        Ok(Self {
            stream,
            threshold: None,
        })
    }

    async fn write(&mut self, bytes: &[u8]) -> Result<()> {
        tokio::time::timeout(wire::IO_TIMEOUT, self.stream.write_all(bytes))
            .await
            .context("Minecraft write deadline")??;
        Ok(())
    }

    /// Send one packet, in the compressed layout once compression is on (always below any
    /// threshold here, so uncompressed with a zero data length).
    async fn send(&mut self, id: i32, body: &[u8]) -> Result<()> {
        let bytes = if self.threshold.is_some() {
            let mut inner = vec![0u8];
            wire::put_varint(&mut inner, id);
            inner.extend_from_slice(body);
            let mut out = Vec::new();
            wire::put_varint(&mut out, inner.len() as i32);
            out.extend_from_slice(&inner);
            out
        } else {
            wire::frame(id, body)
        };
        self.write(&bytes).await
    }

    async fn recv(&mut self) -> Result<(i32, Vec<u8>)> {
        let raw = wire::read_raw(
            &mut self.stream,
            wire::MAX_CLIENTBOUND_PACKET,
            wire::IO_TIMEOUT,
        )
        .await?
        .context("server closed the connection")?;
        if self.threshold.is_none() {
            return wire::split_id(&raw);
        }
        let (data_len, n) = wire::get_varint(&raw)?;
        if data_len == 0 {
            return wire::split_id(&raw[n..]);
        }
        anyhow::ensure!(
            data_len > 0 && data_len as usize <= wire::MAX_CLIENTBOUND_PACKET,
            "compressed packet declares {data_len} bytes"
        );
        let mut inflated = Vec::new();
        flate2::read::ZlibDecoder::new(&raw[n..])
            .take(data_len as u64 + 1)
            .read_to_end(&mut inflated)
            .context("inflate a compressed packet")?;
        anyhow::ensure!(
            inflated.len() == data_len as usize,
            "compressed packet inflated to {} bytes, not {data_len}",
            inflated.len()
        );
        wire::split_id(&inflated)
    }
}

use std::io::Read as _;

async fn status(host: &str, port: u16, protocol: i32) -> Result<Value> {
    let mut conn = Conn::open(host, port).await?;
    let mut out = wire::encode_handshake(&wire::Handshake {
        protocol_version: protocol,
        server_address: host.to_string(),
        server_port: port,
        next_state: wire::STATE_STATUS,
    });
    out.extend_from_slice(&wire::frame(0x00, &[]));
    conn.write(&out).await?;
    let (id, body) = conn.recv().await?;
    anyhow::ensure!(id == 0x00, "status answered with packet 0x{id:02x}");
    let text = wire::Reader::new(&body).string(wire::MAX_JSON_CHARS)?;
    let status: Value = serde_json::from_str(&text).context("status is not JSON")?;
    let token = crate::utils::clock::SystemTime::now()
        .duration_since(crate::utils::clock::UNIX_EPOCH)
        .map(|d| d.as_millis() as i64)
        .unwrap_or(1);
    let started = crate::utils::clock::Instant::now();
    conn.send(0x01, &token.to_be_bytes()).await?;
    // A server that answers status but not ping still gave its entry; latency stays null.
    let latency = match conn.recv().await {
        Ok((0x01, body)) if body == token.to_be_bytes() => {
            Some(started.elapsed().as_secs_f64() * 1000.0)
        }
        _ => None,
    };
    let sample: Vec<Value> = status["players"]["sample"]
        .as_array()
        .map(|s| {
            s.iter()
                .take(wire::MAX_SAMPLE)
                .map(|p| json!({"name": p["name"], "id": p["id"]}))
                .collect()
        })
        .unwrap_or_default();
    Ok(json!({
        "legacy": false,
        "version_name": status["version"]["name"],
        "protocol": status["version"]["protocol"],
        "online_players": status["players"]["online"],
        "max_players": status["players"]["max"],
        "motd": wire::plain_text(&status["description"]),
        "sample": sample,
        "has_favicon": status.get("favicon").is_some(),
        "enforces_secure_chat": status.get("enforcesSecureChat"),
        "latency_ms": latency,
    }))
}

async fn legacy_status(host: &str, port: u16) -> Result<Value> {
    let mut conn = Conn::open(host, port).await?;
    conn.write(&wire::encode_legacy_ping(host, port)).await?;
    let reply = tokio::time::timeout(wire::IO_TIMEOUT, async {
        let kind = conn.stream.read_u8().await?;
        anyhow::ensure!(kind == 0xFF, "legacy ping answered with 0x{kind:02x}");
        let units = conn.stream.read_u16().await? as usize;
        let mut text = vec![0u8; units * 2];
        conn.stream.read_exact(&mut text).await?;
        Ok(text)
    })
    .await
    .context("legacy ping deadline")??;
    let mut event = wire::decode_legacy_reply(&reply)?;
    event["legacy"] = Value::Bool(true);
    event["motd"] = json!(event["motd"].as_str().unwrap_or_default());
    Ok(event)
}

async fn login(
    host: &str,
    port: u16,
    protocol: i32,
    username: &str,
    uuid: [u8; 16],
) -> Result<Value> {
    let mut conn = Conn::open(host, port).await?;
    let mut out = wire::encode_handshake(&wire::Handshake {
        protocol_version: protocol,
        server_address: host.to_string(),
        server_port: port,
        next_state: wire::STATE_LOGIN,
    });
    out.extend_from_slice(&wire::encode_login_start(protocol, username, &uuid));
    conn.write(&out).await?;
    let mut event = json!({});
    for _ in 0..MAX_LOGIN_PACKETS {
        let (id, body) = conn.recv().await?;
        let mut r = wire::Reader::new(&body);
        match id {
            0x00 => {
                let text = r.string(wire::MAX_JSON_CHARS)?;
                let reason = serde_json::from_str::<Value>(&text)
                    .map(|v| wire::plain_text(&v))
                    .unwrap_or(text);
                event["outcome"] = json!("disconnected");
                event["reason"] = json!(reason);
                return Ok(event);
            }
            0x01 => {
                event["outcome"] = json!("encryption_required");
                return Ok(event);
            }
            0x02 => {
                let uuid = if protocol >= 735 {
                    wire::format_uuid(r.bytes(16)?)
                } else {
                    r.string(36)?
                };
                event["outcome"] = json!("accepted");
                event["uuid"] = json!(uuid);
                event["username"] = json!(r.string(wire::MAX_USERNAME_CHARS)?);
                return Ok(event);
            }
            0x03 => {
                let threshold = r.varint()?;
                event["compression_threshold"] = json!(threshold);
                conn.threshold = Some(threshold);
            }
            // Login plugin request: answer "not understood", as a vanilla client does.
            0x04 => {
                let message_id = r.varint()?;
                let mut reply = Vec::new();
                wire::put_varint(&mut reply, message_id);
                reply.push(0);
                conn.send(0x02, &reply).await?;
            }
            // Cookie request (1.20.5+): answer with no cookie.
            0x05 => {
                let key = r.string(32767)?;
                let mut reply = Vec::new();
                wire::put_string(&mut reply, &key);
                reply.push(0);
                conn.send(0x04, &reply).await?;
            }
            other => anyhow::bail!("unexpected login packet 0x{other:02x}"),
        }
    }
    anyhow::bail!("no login verdict within {MAX_LOGIN_PACKETS} packets")
}

async fn run(host: &str, port: u16, action: &Value) -> Result<(Event, Value)> {
    match action["type"].as_str() {
        Some("minecraft_status") => {
            let data = status(host, port, actions::protocol_version(action)?).await?;
            Ok((Event::new(&actions::STATUS_EVENT, data.clone()), data))
        }
        Some("minecraft_legacy_status") => {
            let data = legacy_status(host, port).await?;
            Ok((Event::new(&actions::STATUS_EVENT, data.clone()), data))
        }
        Some("minecraft_login") => {
            let uuid = match action["uuid"].as_str() {
                Some(u) => wire::parse_uuid(u)?,
                None => [0u8; 16],
            };
            let data = login(
                host,
                port,
                actions::protocol_version(action)?,
                action["username"].as_str().unwrap_or_default(),
                uuid,
            )
            .await?;
            Ok((Event::new(&actions::LOGIN_EVENT, data.clone()), data))
        }
        _ => anyhow::bail!("Unknown Minecraft client action"),
    }
}

async fn session(
    ctx: &ConnectContext,
    host: &str,
    port: u16,
    mut external: mpsc::Receiver<ClientCommand>,
    mut internal: mpsc::Receiver<Value>,
    events: mpsc::Sender<Event>,
) -> Result<()> {
    loop {
        let (action, mut injected) = tokio::select! {
            command = external.recv() => match command {
                Some(c) => (c.action.clone(), Some(c)),
                None => return Ok(()),
            },
            action = internal.recv() => match action {
                Some(a) => (a, None),
                None => return Ok(()),
            },
        };
        match MinecraftClientProtocol.execute_action(action.clone()) {
            Ok(ClientActionResult::Disconnect) => {
                if let Some(command) = injected.take() {
                    crate::client::command_support::reply(
                        command,
                        Ok(ClientSendOutcome::Disconnected),
                    );
                }
                return Ok(());
            }
            Ok(_) => {}
            Err(e) => {
                if let Some(command) = injected.take() {
                    crate::client::command_support::reply(
                        command,
                        Ok(ClientSendOutcome::Rejected {
                            error: e.to_string(),
                        }),
                    );
                }
                continue;
            }
        }
        let name = action["type"].as_str().unwrap_or_default().to_string();
        if injected.is_some() {
            ctx.state
                .record_access_log(
                    AccessLogOwner::Client(ctx.client_id.as_u32()),
                    "Minecraft",
                    None,
                    "injected_action",
                    json!({"action": name}),
                    vec![],
                )
                .await;
        }
        // An unreachable or misbehaving server is logged; the client stays up for the next try.
        match run(host, port, &action).await {
            Ok((event, data)) => {
                if let Some(command) = injected.take() {
                    crate::client::command_support::reply(
                        command,
                        Ok(ClientSendOutcome::Executed {
                            detail: data.to_string(),
                        }),
                    );
                }
                Log::new(Some(&ctx.status_tx)).info(format!("Minecraft {name}: {data}"));
                events
                    .try_send(event)
                    .context("Minecraft event queue full; consumer stalled")?;
            }
            Err(e) => {
                if let Some(command) = injected.take() {
                    crate::client::command_support::reply(
                        command,
                        Err(anyhow::anyhow!(e.to_string())),
                    );
                }
                Log::new(Some(&ctx.status_tx)).warn(format!("Minecraft {name} failed: {e:#}"));
            }
        }
    }
}