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
464
465
466
467
468
469
470
//! Gemini server (gemini://, TLS, port 1965) — the model writes the capsule.
//!
//! One connection carries one exchange: TLS handshake, one request line (an absolute URL and
//! CRLF), one response header, a body only after a 2x, then `close_notify` and close. That
//! shape decides the whole loop:
//!
//! 1. **NetGet refuses what is not a request before the model sees it.** A URL over 1024 bytes,
//!    a relative or malformed one, a BOM, userinfo or a fragment get `59`; another scheme gets
//!    `53` (this server is not a proxy). No model call is spent on any of them.
//! 2. **Every response closes the connection**, so there is no idle phase: the two deadlines
//!    are the handshake and the request line, and both wrap reads only — a request parked for a
//!    human under a `manual` rule is not closed by either.
//! 3. **The model cannot write a header.** Its actions are rendered by [`wire`]; the loop sends
//!    the first rendered response and nothing after it.
//! 4. **Failure is a status, never silence.** Backend overload → `41` (the spec's "server
//!    unavailable due to overload"), any other backend failure → `40` (the generic temporary
//!    failure), a model that produced no response → `40`. Meta texts are fixed literals.
pub mod actions;
pub mod wire;

use crate::llm::action_helper::call_llm;
use crate::llm::actions::protocol_trait::ActionResult;
use crate::llm::ollama_client::OllamaClient;
use crate::logging::emit::Log;
use crate::protocol::Event;
use crate::server::connection::ConnectionId;
use crate::state::app_state::AppState;
use anyhow::Result;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
use tokio::sync::{mpsc, Mutex};
use tokio_rustls::TlsAcceptor;

pub use wire::{MAX_REQUEST_BYTES, MAX_URL_BYTES};

/// How long a connected peer has to complete the TLS handshake. Real clients send ClientHello
/// at once and finish in a round trip; nothing in this phase involves the model.
const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(60);

/// How long a peer that completed the handshake has to send its request line.
///
/// A real Gemini client sends it in the same flight as its Finished. 300 seconds is the window a
/// `manual` rule gives a human — the peer may be NetGet's own TLS client, which completes the
/// handshake and then waits for its operator to type the request.
const FIRST_BYTE_TIMEOUT: Duration = Duration::from_secs(300);

/// Concurrent connections admitted before new ones are refused — the house default.
const MAX_CONNECTIONS: usize = crate::server::accept_bounded::DEFAULT_MAX_CONNECTIONS;

/// What a peer over [`MAX_CONNECTIONS`] gets: nothing, then a close. Every Gemini response is
/// sent inside TLS, and a refused peer is refused precisely so that it does not get a handshake
/// — plaintext `41` bytes here would be a malformed TLS record, not a refusal. `dot` and `doh`
/// refuse the same way for the same reason.
const CONNECTION_CAP_REFUSAL: &[u8] = b"";

/// Responses NetGet writes itself when the model cannot answer. Fixed literals: nothing
/// derived from an error can reach the peer.
const UNAVAILABLE_RESPONSE: &[u8] = b"40 request could not be processed\r\n";
const OVERLOADED_RESPONSE: &[u8] = b"41 backend at capacity, retry later\r\n";
const TOO_LONG_RESPONSE: &[u8] = b"59 Request too long\r\n";

/// After the last response, how long and how much the server keeps reading before dropping
/// the socket. Closing over unread input sends RST, which can overtake the response the client
/// has not read yet — a `59` for an over-long request, typically.
const LINGER_TIME: Duration = Duration::from_secs(2);
const LINGER_BYTES: usize = 64 * 1024;

/// The two read deadlines, resolved from startup parameters.
#[derive(Debug, Clone, Copy)]
pub struct Deadlines {
    pub handshake: Duration,
    pub first_byte: Duration,
}

impl Deadlines {
    pub fn new(handshake_secs: Option<u64>, first_byte_secs: Option<u64>) -> Self {
        Self {
            handshake: handshake_secs
                .map(Duration::from_secs)
                .unwrap_or(HANDSHAKE_TIMEOUT),
            first_byte: first_byte_secs
                .map(Duration::from_secs)
                .unwrap_or(FIRST_BYTE_TIMEOUT),
        }
    }
}

pub struct GeminiServer;

impl GeminiServer {
    pub async fn spawn_with_llm_actions(
        listen_addr: SocketAddr,
        llm_client: OllamaClient,
        app_state: Arc<AppState>,
        status_tx: mpsc::UnboundedSender<String>,
        server_id: crate::state::ServerId,
        tls_config: Arc<rustls::ServerConfig>,
        deadlines: Deadlines,
    ) -> Result<SocketAddr> {
        let listener = TcpListener::bind(listen_addr).await?;
        let local_addr = listener.local_addr()?;
        Log::new(Some(&status_tx)).info(format!("Gemini server listening on {}", local_addr));

        let protocol = Arc::new(actions::GeminiProtocol::new());
        let acceptor = TlsAcceptor::from(tls_config);
        let task_registrar = app_state.clone();
        let limiter = crate::server::accept_bounded::ConnectionLimiter::new(MAX_CONNECTIONS);
        let accept_handle = tokio::spawn(async move {
            loop {
                match crate::server::accept_bounded::accept_bounded(
                    &listener,
                    &limiter,
                    CONNECTION_CAP_REFUSAL,
                    "Gemini",
                    Some(&status_tx),
                )
                .await
                {
                    Ok((socket, peer_addr, permit)) => {
                        let connection_id =
                            ConnectionId::new(app_state.get_next_unified_id().await);
                        use crate::state::server::{
                            ConnectionState as ServerConnectionState, ConnectionStatus,
                            ProtocolConnectionInfo,
                        };
                        let now = crate::utils::clock::Instant::now();
                        app_state
                            .add_connection_to_server(
                                server_id,
                                ServerConnectionState {
                                    id: connection_id,
                                    remote_addr: peer_addr,
                                    local_addr,
                                    bytes_sent: 0,
                                    bytes_received: 0,
                                    packets_sent: 0,
                                    packets_received: 0,
                                    last_activity: now,
                                    status: ConnectionStatus::Active,
                                    status_changed_at: now,
                                    protocol_info: ProtocolConnectionInfo::empty(),
                                },
                            )
                            .await;
                        let _ = status_tx.send("__UPDATE_UI__".to_string());

                        let session = Session {
                            peer_addr,
                            llm_client: llm_client.clone(),
                            app_state: app_state.clone(),
                            status_tx: status_tx.clone(),
                            server_id,
                            protocol: protocol.clone(),
                            connection_id,
                            deadlines,
                        };
                        let acceptor = acceptor.clone();
                        // Tracked, not detached: stop_server must abort this task too.
                        let task_owner = app_state.clone();
                        task_owner
                            .spawn_server_task(server_id, async move {
                                // Released when the connection ends; this task is the whole
                                // connection, so MAX_CONNECTIONS caps live connections.
                                let _permit = permit;
                                session.run(acceptor, socket).await
                            })
                            .await;
                    }
                    Err(e) => {
                        Log::new(Some(&status_tx)).error(format!("Gemini accept error: {}", e));
                        break;
                    }
                }
            }
        });

        task_registrar
            .register_server_task(server_id, accept_handle)
            .await;
        Ok(local_addr)
    }
}

struct Session {
    peer_addr: SocketAddr,
    llm_client: OllamaClient,
    app_state: Arc<AppState>,
    status_tx: mpsc::UnboundedSender<String>,
    server_id: crate::state::ServerId,
    protocol: Arc<actions::GeminiProtocol>,
    connection_id: ConnectionId,
    deadlines: Deadlines,
}

/// What reading the request line produced.
enum RequestRead {
    Line(String, usize),
    /// The line exceeded [`MAX_REQUEST_BYTES`] including its CRLF.
    TooLong,
    TimedOut,
    Closed,
}

impl Session {
    async fn run(self, acceptor: TlsAcceptor, socket: tokio::net::TcpStream) {
        let log = Log::new(Some(&self.status_tx));
        let tls =
            match tokio::time::timeout(self.deadlines.handshake, acceptor.accept(socket)).await {
                Err(_) => {
                    log.info(format!(
                        "Gemini peer {} did not complete the TLS handshake within {}s; closing",
                        self.peer_addr,
                        self.deadlines.handshake.as_secs()
                    ));
                    self.finish().await;
                    return;
                }
                Ok(Err(e)) => {
                    log.warn(format!(
                        "Gemini TLS handshake with {} failed: {}",
                        self.peer_addr, e
                    ));
                    self.finish().await;
                    return;
                }
                Ok(Ok(tls)) => tls,
            };
        log.info(format!("Gemini client connected from {}", self.peer_addr));

        let (mut reader, write_half) = tokio::io::split(tls);
        let write_half = Arc::new(Mutex::new(write_half));

        // Registered once the handshake is done — before it there is no channel a reply could
        // travel on — and before the request is read, so the operator can answer a peer whose
        // request is parked for them.
        let peer_rx = crate::server::peer_support::register_peer_channel(
            &self.app_state,
            self.server_id,
            self.connection_id.as_u32(),
        )
        .await;
        crate::server::peer_support::spawn_peer_command_task(
            peer_rx,
            self.protocol.clone(),
            self.app_state.clone(),
            self.server_id,
            self.connection_id.as_u32(),
            write_half.clone(),
            self.status_tx.clone(),
        );

        self.exchange(&mut reader, &write_half).await;

        self.app_state
            .remove_peer_handle(self.server_id, self.connection_id.as_u32())
            .await;
        // close_notify, then drain what the peer already sent so the TCP close is a FIN.
        let _ = write_half.lock().await.shutdown().await;
        let deadline = tokio::time::Instant::now() + LINGER_TIME;
        let mut sink = [0u8; 4096];
        let mut drained = 0usize;
        while drained < LINGER_BYTES {
            match tokio::time::timeout_at(deadline, reader.read(&mut sink)).await {
                Ok(Ok(n)) if n > 0 => drained += n,
                _ => break,
            }
        }
        self.finish().await;
    }

    async fn finish(&self) {
        self.app_state
            .update_connection_status(
                self.server_id,
                self.connection_id,
                crate::state::server::ConnectionStatus::Closed,
            )
            .await;
        let _ = self.status_tx.send("__UPDATE_UI__".to_string());
    }

    async fn write<W>(&self, write_half: &Arc<Mutex<W>>, data: &[u8]) -> std::io::Result<()>
    where
        W: tokio::io::AsyncWrite + Unpin,
    {
        {
            let mut writer = write_half.lock().await;
            writer.write_all(data).await?;
            writer.flush().await?;
        }
        self.app_state
            .update_connection_stats(
                self.server_id,
                self.connection_id,
                None,
                Some(data.len() as u64),
                None,
                Some(1),
            )
            .await;
        Ok(())
    }

    /// Read one request line: up to CRLF (or a bare LF), bounded in size and in time.
    async fn read_request<R: tokio::io::AsyncRead + Unpin>(&self, reader: &mut R) -> RequestRead {
        let mut pending: Vec<u8> = Vec::new();
        let mut chunk = [0u8; 1024];
        loop {
            if let Some(idx) = pending.iter().position(|b| *b == b'\n') {
                if idx + 1 > MAX_REQUEST_BYTES {
                    return RequestRead::TooLong;
                }
                let line = String::from_utf8_lossy(&pending[..idx]);
                return RequestRead::Line(line.trim_end_matches('\r').to_string(), idx + 1);
            }
            if pending.len() >= MAX_REQUEST_BYTES {
                return RequestRead::TooLong;
            }
            match tokio::time::timeout(self.deadlines.first_byte, reader.read(&mut chunk)).await {
                Err(_) => return RequestRead::TimedOut,
                Ok(Ok(0)) | Ok(Err(_)) => return RequestRead::Closed,
                Ok(Ok(n)) => pending.extend_from_slice(&chunk[..n]),
            }
        }
    }

    async fn exchange<R, W>(&self, reader: &mut R, write_half: &Arc<Mutex<W>>)
    where
        R: tokio::io::AsyncRead + Unpin,
        W: tokio::io::AsyncWrite + Unpin,
    {
        let log = Log::new(Some(&self.status_tx));
        let (line, n) = match self.read_request(reader).await {
            RequestRead::Line(line, n) => (line, n),
            RequestRead::TooLong => {
                log.warn(format!(
                    "Gemini request from {} exceeded {} bytes decision=fail_closed_request_too_long",
                    self.peer_addr, MAX_REQUEST_BYTES
                ));
                let _ = self.write(write_half, TOO_LONG_RESPONSE).await;
                return;
            }
            RequestRead::TimedOut => {
                log.info(format!(
                    "Gemini peer {} sent no request within {}s; closing",
                    self.peer_addr,
                    self.deadlines.first_byte.as_secs()
                ));
                return;
            }
            RequestRead::Closed => {
                log.info(format!(
                    "Gemini peer {} closed without a request",
                    self.peer_addr
                ));
                return;
            }
        };
        self.app_state
            .update_connection_stats(
                self.server_id,
                self.connection_id,
                Some(n as u64),
                None,
                Some(1),
                None,
            )
            .await;

        let request = match wire::parse_request(&line) {
            Ok(request) => request,
            Err(refusal) => {
                log.info(format!(
                    "Gemini request from {} refused decision={}",
                    self.peer_addr,
                    refusal.decision()
                ));
                let _ = self.write(write_half, refusal.response().as_bytes()).await;
                return;
            }
        };

        let event = Event::new(
            &actions::GEMINI_REQUEST_EVENT,
            serde_json::json!({
                "url": request.url,
                "host": request.host,
                "path": request.path,
                "query": request.query,
                "answer_with": actions::answer_with_for_request(
                    &request.path,
                    request.query.as_deref(),
                ),
            }),
        );
        let result = match call_llm(
            &self.llm_client,
            &self.app_state,
            self.server_id,
            Some(self.connection_id),
            &event,
            self.protocol.as_ref(),
        )
        .await
        {
            Ok(result) => result,
            Err(e) => {
                let (category, response) = match crate::utils::WireFailure::classify(&e) {
                    crate::utils::WireFailure::Overloaded => ("overloaded", OVERLOADED_RESPONSE),
                    crate::utils::WireFailure::Unavailable => ("unavailable", UNAVAILABLE_RESPONSE),
                };
                log.warn(format!(
                    "Gemini {} from {} decision=fail_closed_llm_error category={}",
                    request.url, self.peer_addr, category
                ));
                log.debug(format!("Gemini LLM call failed: {}", e));
                let _ = self.write(write_half, response).await;
                return;
            }
        };
        for message in &result.messages {
            log.info(message);
        }

        let mut responses: Vec<Vec<u8>> = Vec::new();
        let mut stack = result.protocol_results;
        stack.reverse();
        while let Some(item) = stack.pop() {
            match item {
                ActionResult::Output(bytes) => responses.push(bytes),
                ActionResult::Multiple(items) => stack.extend(items.into_iter().rev()),
                _ => {}
            }
        }

        let Some(response) = responses.first() else {
            log.warn(format!(
                "Gemini {} from {} decision=model_silent ({} failed action(s)); answering 40",
                request.url,
                self.peer_addr,
                result.failures.len()
            ));
            let _ = self.write(write_half, UNAVAILABLE_RESPONSE).await;
            return;
        };
        if responses.len() > 1 {
            log.warn(format!(
                "Gemini {}: the model produced {} responses to one request; sending the first",
                request.url,
                responses.len()
            ));
        }
        let status = wire::leading_status(response).unwrap_or(0);
        let decision = if (10..40).contains(&status) {
            "model_answer"
        } else {
            "model_reject"
        };
        log.info(format!(
            "Gemini {} from {} decision={} status={}",
            request.url, self.peer_addr, decision, status
        ));
        if let Err(e) = self.write(write_half, response).await {
            log.debug(format!("Gemini write to {} failed: {}", self.peer_addr, e));
        }
    }
}