nntp-proxy 0.5.1

NNTP proxy server with per-command backend multiplexing, caching, metrics, and TUI dashboard
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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! Backend communication module
//!
//! This module handles NNTP client operations when proxying to backend servers.
//! The proxy acts as an NNTP client to upstream servers.
//!
//! # Structure
//!
//! - Response status parsing functions (pure, easily testable)
//! - Command execution helpers - Send command, read response
//!
//! Backend requests and response parsing are driven by `RequestContext`; callers
//! should not rebuild command strings after request validation.

use anyhow::Result;
use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt};

use crate::pool::PooledBuffer;
use crate::protocol::RequestContext;

pub(crate) use crate::session::multiline_framing::BackendResponseOrder;

/// Opaque result of reading enough backend bytes to classify one response.
///
/// Callers can inspect status and single-line bytes through methods, but they do
/// not receive framing internals or boundary offsets. Any caller that needs to
/// transfer or capture a body must pass the same buffer back through this module.
pub(crate) struct BackendReadResult {
    inner: BackendReadResultInner,
}

/// Failure while reading a complete single-line backend reply into caller-owned
/// scratch storage.
#[derive(Debug)]
pub(crate) enum SingleLineReplyReadError {
    /// The scratch buffer filled before the framer accepted a complete reply.
    Full { bytes_read: usize },
    /// The underlying connection returned an I/O error.
    Io(std::io::Error),
    /// The backend closed before a complete reply was accepted.
    Closed,
    /// The bytes read so far cannot be a valid reply for the request.
    Invalid { bytes_read: usize },
}

enum BackendReadResultInner {
    Response(crate::session::multiline_framing::BackendResponseRead),
    Invalid(crate::session::multiline_framing::ResponseReadError),
}

pub(crate) async fn read_single_line_reply<C>(
    conn: &mut C,
    request: &RequestContext,
    scratch: &mut [u8],
) -> Result<String, SingleLineReplyReadError>
where
    C: tokio::io::AsyncRead + Unpin,
{
    let mut total = 0usize;

    loop {
        if total == scratch.len() {
            return Err(SingleLineReplyReadError::Full { bytes_read: total });
        }

        let n = conn
            .read(&mut scratch[total..])
            .await
            .map_err(SingleLineReplyReadError::Io)?;

        if n == 0 {
            return Err(SingleLineReplyReadError::Closed);
        }

        total += n;

        match crate::session::multiline_framing::unpacked_single_line_response(
            request,
            &scratch[..total],
        ) {
            Ok(bytes) => {
                return Ok(String::from_utf8_lossy(bytes).into_owned());
            }
            Err(crate::session::multiline_framing::ResponseReadError::Incomplete) => {}
            Err(crate::session::multiline_framing::ResponseReadError::Invalid(_)) => {
                return Err(SingleLineReplyReadError::Invalid { bytes_read: total });
            }
        }
    }
}

/// Return the cache-wire terminator used when rendering stored multiline
/// payloads back to a client.
#[must_use]
pub(crate) fn cached_response_completion() -> std::io::IoSlice<'static> {
    crate::session::multiline_framing::cached_response_completion()
}

/// Return the payload body from a response that was already captured as a
/// complete multiline payload.
///
/// This is a facade over the framer-owned payload split logic; cache code should
/// not inspect multiline terminators directly.
#[must_use]
pub(crate) fn captured_multiline_payload_body(payload: &[u8]) -> Option<&[u8]> {
    crate::session::multiline_framing::captured_multiline_payload_body(payload)
}

/// Capabilities response for the local proxy capability command.
#[must_use]
pub(crate) const fn capabilities_response(auth_enabled: bool) -> &'static [u8] {
    if auth_enabled {
        crate::session::multiline_framing::CAPABILITIES_WITH_AUTHINFO_RESPONSE
    } else {
        crate::session::multiline_framing::CAPABILITIES_WITHOUT_AUTHINFO_RESPONSE
    }
}

/// Capabilities response used after authentication has already been satisfied.
#[must_use]
pub(crate) const fn capabilities_without_authinfo_response() -> &'static [u8] {
    crate::session::multiline_framing::CAPABILITIES_WITHOUT_AUTHINFO_RESPONSE
}

impl BackendReadResult {
    #[must_use]
    pub(crate) fn status_code(&self) -> Option<crate::protocol::StatusCode> {
        match &self.inner {
            BackendReadResultInner::Response(response) => Some(response.status_code()),
            BackendReadResultInner::Invalid(_) => None,
        }
    }

    #[must_use]
    pub(crate) fn single_line_bytes<'a>(&self, buffer: &'a PooledBuffer) -> Option<&'a [u8]> {
        match &self.inner {
            BackendReadResultInner::Response(response) => response.single_line_bytes(buffer),
            BackendReadResultInner::Invalid(_) => None,
        }
    }

    pub(crate) fn log_warnings(
        &self,
        buffer: &[u8],
        client_addr: impl std::fmt::Display,
        backend_id: crate::types::BackendId,
    ) {
        if let BackendReadResultInner::Invalid(err) = &self.inner {
            err.log_warnings(buffer, client_addr, backend_id);
        }
    }
}

fn duration_micros_u64(duration: std::time::Duration) -> u64 {
    u64::try_from(duration.as_micros()).unwrap_or(u64::MAX)
}

async fn read_until_backend_reply<C>(
    conn: &mut C,
    request: &RequestContext,
    buffer: &mut PooledBuffer,
) -> Result<BackendReadResult>
where
    C: AsyncReadExt + Unpin,
{
    loop {
        match crate::session::multiline_framing::backend_response_read(request, buffer) {
            Ok(response) => {
                return Ok(BackendReadResult {
                    inner: BackendReadResultInner::Response(response),
                });
            }
            Err(err @ crate::session::multiline_framing::ResponseReadError::Invalid(_)) => {
                return Ok(BackendReadResult {
                    inner: BackendReadResultInner::Invalid(err),
                });
            }
            Err(crate::session::multiline_framing::ResponseReadError::Incomplete) => {
                let more = buffer.read_more(conn).await?;
                if more == 0 {
                    anyhow::bail!(
                        "Backend EOF before complete backend response ({} bytes)",
                        buffer.initialized()
                    );
                }
            }
        }
    }
}

/// Format a hex preview of response bytes for debugging Invalid responses
///
/// # Arguments
/// * `data` - Raw response bytes
/// * `max_bytes` - Maximum number of bytes to include in preview
///
/// # Returns
/// Hex string with space-separated bytes (e.g., "41 42 43" for "ABC")
///
/// # Examples
/// ```
/// # use nntp_proxy::session::format_hex_preview;
/// let data = b"430 No such article\r\n";
/// let hex = format_hex_preview(data, 256);
/// assert!(hex.starts_with("34 33 30 20")); // "430 "
/// ```
#[must_use]
pub fn format_hex_preview(data: &[u8], max_bytes: usize) -> String {
    let preview = &data[..data.len().min(max_bytes)];
    preview
        .iter()
        .map(|b| format!("{b:02x}"))
        .collect::<Vec<_>>()
        .join(" ")
}

// ─── Command execution ──────────────────────────────────────────────────────

/// Send a typed request and read enough bytes for backend response classification.
pub(crate) async fn send_request<C>(
    conn: &mut C,
    request: &RequestContext,
    buffer: &mut PooledBuffer,
) -> Result<BackendReadResult>
where
    C: AsyncReadExt + AsyncWriteExt + Unpin,
{
    request.write_wire_to(conn).await?;

    let n = buffer.read_from(conn).await?;
    if n == 0 {
        anyhow::bail!("Backend connection closed unexpectedly");
    }

    read_until_backend_reply(conn, request, buffer).await
}

/// Send a typed request with timing measurements.
pub(crate) async fn send_request_timed<C>(
    conn: &mut C,
    request: &RequestContext,
    buffer: &mut PooledBuffer,
) -> Result<(BackendReadResult, u64, u64, u64)>
where
    C: AsyncReadExt + AsyncWriteExt + Unpin,
{
    use std::time::Instant;

    let start = Instant::now();
    request.write_wire_to(conn).await?;
    let after_send = Instant::now();

    let n = buffer.read_from(conn).await?;
    if n == 0 {
        anyhow::bail!("Backend connection closed unexpectedly");
    }

    let response = read_until_backend_reply(conn, request, buffer).await?;
    let after_recv = Instant::now();
    let send_elapsed = after_send.duration_since(start);
    let recv_elapsed = after_recv.duration_since(after_send);
    let elapsed = after_recv.duration_since(start);

    Ok((
        response,
        duration_micros_u64(elapsed),
        duration_micros_u64(send_elapsed),
        duration_micros_u64(recv_elapsed),
    ))
}

/// Capture one backend response into owned pooled chunks.
///
/// This is used for payload cache ingestion and other paths that explicitly need
/// ownership of the complete response. It still delegates all response boundary
/// detection to the framer.
pub(crate) async fn write_response_with_optional_capture<W>(
    request: &RequestContext,
    buffer: &mut PooledBuffer,
    conn: &mut crate::stream::ConnectionStream,
    writer: &mut W,
    captured: &mut crate::pool::ChunkedResponse,
    pool: &crate::pool::BufferPool,
    backend_id: crate::types::BackendId,
) -> Result<(u64, bool), crate::session::response_transfer::ResponseTransferError>
where
    W: AsyncWrite + Unpin,
{
    crate::session::multiline_framing::write_response_with_optional_capture(
        request, buffer, conn, writer, captured, pool, backend_id,
    )
    .await
}

/// Observe and drain one backend response without retaining its bytes.
///
/// This is for retry/control paths that must consume the backend response to
/// keep the connection reusable but do not need ownership of the response body.
pub(crate) async fn observe_response(
    request: &RequestContext,
    buffer: &mut PooledBuffer,
    conn: &mut crate::stream::ConnectionStream,
    pool: &crate::pool::BufferPool,
    backend_id: crate::types::BackendId,
) -> Result<(), crate::session::response_transfer::ResponseTransferError> {
    crate::session::multiline_framing::observe_response(request, buffer, conn, pool, backend_id)
        .await
}

/// Capture an isolated multiline response into a single pooled capture buffer.
///
/// Used by client and precheck paths that issue commands outside the normal
/// per-command transfer loop.
pub(crate) async fn capture_complete_multiline_response(
    conn: &mut crate::stream::ConnectionStream,
    buffer: &mut PooledBuffer,
    capture: &mut PooledBuffer,
) -> anyhow::Result<()> {
    crate::session::multiline_framing::capture_isolated_multiline_response(conn, buffer, capture)
        .await
}

/// Capture an isolated multiline response into chunked pooled storage while it
/// remains within the framer-owned retention limit.
pub(crate) async fn capture_complete_multiline_response_chunked_optional(
    conn: &mut crate::stream::ConnectionStream,
    buffer: &mut PooledBuffer,
    pool: &crate::pool::BufferPool,
    response: &mut crate::pool::ChunkedResponse,
) -> anyhow::Result<bool> {
    crate::session::multiline_framing::capture_isolated_multiline_response_chunked_optional(
        conn, buffer, pool, response,
    )
    .await
    .map_err(|err| anyhow::anyhow!("backend multiline response capture failed: {err:?}"))
}

/// Drain an isolated multiline response without retaining the bytes.
pub(crate) async fn observe_complete_multiline_response(
    conn: &mut crate::stream::ConnectionStream,
    buffer: &mut PooledBuffer,
) -> anyhow::Result<()> {
    crate::session::multiline_framing::observe_isolated_multiline_response(conn, buffer)
        .await
        .map_err(|err| anyhow::anyhow!("backend multiline response drain failed: {err:?}"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::protocol::StatusCode;
    use std::collections::VecDeque;
    use std::pin::Pin;
    use std::task::{Context, Poll};

    /// Mock stream that returns data in configurable chunks
    struct ChunkedStream {
        chunks: VecDeque<Vec<u8>>,
        written: Vec<u8>,
    }

    impl ChunkedStream {
        fn new(chunks: Vec<Vec<u8>>) -> Self {
            Self {
                chunks: chunks.into(),
                written: Vec::new(),
            }
        }
    }

    impl tokio::io::AsyncRead for ChunkedStream {
        fn poll_read(
            mut self: Pin<&mut Self>,
            _cx: &mut Context<'_>,
            buf: &mut tokio::io::ReadBuf<'_>,
        ) -> Poll<std::io::Result<()>> {
            if let Some(chunk) = self.chunks.pop_front() {
                let len = chunk.len().min(buf.remaining());
                buf.put_slice(&chunk[..len]);
                if len < chunk.len() {
                    // Put remainder back
                    self.chunks.push_front(chunk[len..].to_vec());
                }
            }
            Poll::Ready(Ok(()))
        }
    }

    impl tokio::io::AsyncWrite for ChunkedStream {
        fn poll_write(
            mut self: Pin<&mut Self>,
            _cx: &mut Context<'_>,
            buf: &[u8],
        ) -> Poll<std::io::Result<usize>> {
            self.written.extend_from_slice(buf);
            Poll::Ready(Ok(buf.len()))
        }

        fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
            Poll::Ready(Ok(()))
        }

        fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
            Poll::Ready(Ok(()))
        }
    }

    #[tokio::test]
    async fn test_send_request_partial_read_accumulates() {
        // Simulate a backend that sends "200 OK\r\n" in two chunks:
        // first read returns "20", second returns "0 OK\r\n"
        let mut stream = ChunkedStream::new(vec![b"20".to_vec(), b"0 OK\r\n".to_vec()]);

        let pool = crate::pool::BufferPool::for_tests();
        let mut buffer = pool.acquire();

        let request = RequestContext::from_verb_args(b"DATE", b"");
        let response = send_request(&mut stream, &request, &mut buffer)
            .await
            .expect("send_request should handle partial reads");
        let status_code = response
            .status_code()
            .expect("DATE response should be valid");
        assert_eq!(status_code, StatusCode::new(200));
        assert!(!request.has_response_body(status_code));
    }

    #[tokio::test]
    async fn test_send_request_reads_complete_initial_reply_when_code_arrives_first() {
        // RFC-compliant servers can split a single-line response across TCP reads.
        // Reading only the 3-byte status code would leave the rest of the line
        // in the socket for the next command and desynchronize the connection.
        let mut stream = ChunkedStream::new(vec![b"111".to_vec(), b" 20260501173336\r\n".to_vec()]);

        let pool = crate::pool::BufferPool::for_tests();
        let mut buffer = pool.acquire();

        let request = RequestContext::from_verb_args(b"DATE", b"");
        let response = send_request(&mut stream, &request, &mut buffer)
            .await
            .expect("send_request should read through complete backend response");
        let status_code = response
            .status_code()
            .expect("DATE response should be valid");
        assert_eq!(status_code, StatusCode::new(111));
        assert!(!request.has_response_body(status_code));
        assert_eq!(&buffer[..buffer.initialized()], b"111 20260501173336\r\n");
    }

    #[tokio::test]
    async fn test_send_request_single_byte_reads() {
        // Extreme case: each byte comes separately
        let data = b"211 Group\r\n";
        let chunks: Vec<Vec<u8>> = data.iter().map(|&b| vec![b]).collect();
        let mut stream = ChunkedStream::new(chunks);

        let pool = crate::pool::BufferPool::for_tests();
        let mut buffer = pool.acquire();

        let request = RequestContext::from_verb_args(b"GROUP", b"alt.test");
        let response = send_request(&mut stream, &request, &mut buffer)
            .await
            .expect("send_request should handle single-byte reads");
        let status_code = response
            .status_code()
            .expect("GROUP response should be valid");
        assert_eq!(status_code, StatusCode::new(211));
        assert!(!request.has_response_body(status_code));
    }

    #[tokio::test]
    async fn read_single_line_reply_uses_caller_scratch_for_split_reply() {
        let mut stream = ChunkedStream::new(vec![b"111 20260501".to_vec(), b"173336\r\n".to_vec()]);
        let request = RequestContext::from_verb_args(b"DATE", b"");
        let mut scratch = [0u8; 32];

        let response = read_single_line_reply(&mut stream, &request, &mut scratch)
            .await
            .expect("split single-line reply should complete");

        assert_eq!(response, "111 20260501173336\r\n");
        assert_eq!(&scratch[..response.len()], response.as_bytes());
    }

    #[tokio::test]
    async fn read_single_line_reply_reports_full_scratch_with_bytes_read() {
        let mut stream = ChunkedStream::new(vec![b"111 ".to_vec()]);
        let request = RequestContext::from_verb_args(b"DATE", b"");
        let mut scratch = [0u8; 4];

        let err = read_single_line_reply(&mut stream, &request, &mut scratch)
            .await
            .expect_err("unterminated reply should fill scratch");

        assert!(matches!(
            err,
            SingleLineReplyReadError::Full { bytes_read: 4 }
        ));
        assert_eq!(&scratch, b"111 ");
    }

    #[tokio::test]
    async fn read_single_line_reply_reports_invalid_bytes_read() {
        let mut stream = ChunkedStream::new(vec![b"abc\r\n".to_vec()]);
        let request = RequestContext::from_verb_args(b"DATE", b"");
        let mut scratch = [0u8; 16];

        let err = read_single_line_reply(&mut stream, &request, &mut scratch)
            .await
            .expect_err("nonnumeric reply should be invalid");

        assert!(matches!(
            err,
            SingleLineReplyReadError::Invalid { bytes_read: 5 }
        ));
        assert_eq!(&scratch[..5], b"abc\r\n");
    }

    #[tokio::test]
    async fn read_single_line_reply_rejects_packed_trailing_bytes() {
        let packed_reply = b"111 20260501173336\r\n222 next\r\n";
        let mut stream = ChunkedStream::new(vec![packed_reply.to_vec()]);
        let request = RequestContext::from_verb_args(b"DATE", b"");
        let mut scratch = [0u8; 64];

        let err = read_single_line_reply(&mut stream, &request, &mut scratch)
            .await
            .expect_err("packed setup reply must not wait for more bytes");

        assert!(matches!(
            err,
            SingleLineReplyReadError::Invalid { bytes_read } if bytes_read == packed_reply.len()
        ));
    }

    // ─── Hex preview tests ──────────────────────────────────────────────────

    #[test]
    fn test_format_hex_preview_empty() {
        let data = b"";
        let result = format_hex_preview(data, 256);
        assert_eq!(result, "");
    }

    #[test]
    fn test_format_hex_preview_small() {
        let data = b"ABC";
        let result = format_hex_preview(data, 256);
        // A=41, B=42, C=43
        assert_eq!(result, "41 42 43");
    }

    #[test]
    fn test_format_hex_preview_respects_max_bytes() {
        let data = b"ABCDEFGH";
        let result = format_hex_preview(data, 4);
        // Only first 4 bytes: A=41, B=42, C=43, D=44
        assert_eq!(result, "41 42 43 44");
    }

    #[test]
    fn test_format_hex_preview_response_bytes() {
        let data = b"430 No such article\r\n";
        let result = format_hex_preview(data, 256);
        // Should show every byte in the preview
        assert!(result.starts_with("34 33 30 20")); // "430 "
        assert!(result.ends_with("0d 0a")); // \r\n
        assert_eq!(result.split_whitespace().count(), data.len());
    }

    #[test]
    fn test_format_hex_preview_non_ascii() {
        let data = &[0xFF, 0xFE, 0x00, 0x01];
        let result = format_hex_preview(data, 256);
        assert_eq!(result, "ff fe 00 01");
    }

    #[test]
    fn test_format_hex_preview_256_bytes() {
        // Create 300 byte array
        let data: Vec<u8> = (0..=255).chain(0..44).collect();
        assert_eq!(data.len(), 300);

        let result = format_hex_preview(&data, 256);
        // Should only include first 256 bytes
        assert_eq!(result.split_whitespace().count(), 256);

        // Verify last hex is "ff" (byte 255)
        assert!(result.ends_with("ff"));
    }
}