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
//! Milter client: the MTA side of a mail filter conversation.
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::milter::wire;
use crate::state::client_handles::{ClientCommand, ClientSendOutcome};
use crate::state::{AccessLogOwner, ClientStatus};
pub use actions::MilterClientProtocol;
use anyhow::{bail, ensure, Context, Result};
use serde_json::{json, Value};
use std::net::SocketAddr;
use std::time::Duration;
use tokio::io::{ReadHalf, WriteHalf};
use tokio::net::TcpStream;
use tokio::sync::mpsc;

pub const MAX_FOLLOWUP_DEPTH: usize = 8;
/// How long the filter may take to answer one command.
pub const REPLY_TIMEOUT: Duration = Duration::from_secs(60);
const BODY_CHUNK: usize = 64 * 1024;

fn action_names(mask: u32) -> Vec<&'static str> {
    [
        (wire::F_ADDHDRS, "add_header"),
        (wire::F_CHGBODY, "change_body"),
        (wire::F_ADDRCPT, "add_rcpt"),
        (wire::F_DELRCPT, "del_rcpt"),
        (wire::F_CHGHDRS, "change_header"),
        (wire::F_QUARANTINE, "quarantine"),
    ]
    .into_iter()
    .filter(|(b, _)| mask & b != 0)
    .map(|(_, n)| n)
    .collect()
}

pub async fn connect(ctx: ConnectContext) -> Result<SocketAddr> {
    let stream = tokio::time::timeout(wire::IO_TIMEOUT, TcpStream::connect(&ctx.remote_addr))
        .await
        .context("milter connect deadline")??;
    let local = stream.local_addr()?;
    let (mut r, mut w) = tokio::io::split(stream);
    wire::write(
        &mut w,
        wire::C_OPTNEG,
        &wire::optneg(wire::VERSION, wire::ALL_ACTIONS, wire::OFFERED_PROTOCOL),
    )
    .await?;
    let (cmd, data) = wire::read_packet(&mut r, wire::IO_TIMEOUT)
        .await?
        .context("the filter closed the connection during negotiation")?;
    ensure!(
        cmd == wire::C_OPTNEG,
        "the filter answered negotiation with {:?}",
        cmd as char
    );
    let (version, agreed, protocol) = wire::parse_optneg(&data)?;
    ensure!(
        protocol & !wire::OFFERED_PROTOCOL == 0,
        "the filter asked for protocol steps ({:#x}) this MTA did not offer",
        protocol & !wire::OFFERED_PROTOCOL
    );
    let steps: Vec<&str> = wire::PROTOCOL_STEPS
        .iter()
        .filter(|(b, _)| protocol & b != 0)
        .map(|(_, n)| *n)
        .collect();
    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, usize)>(16);
    let (event_tx, mut event_rx) = mpsc::channel::<(Event, usize)>(64);
    event_tx.try_send((
        Event::new(
            &actions::CONNECTED_EVENT,
            json!({"version": version, "actions": action_names(agreed), "protocol": steps}),
        ),
        0,
    ))?;
    let events_ctx = ctx.clone();
    let dispatcher = tokio::spawn(async move {
        while let Some((event, depth)) = 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),
                &MilterClientProtocol,
                &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, depth + 1)).await.is_err() {
                            return;
                        }
                    }
                }
                Err(e) => Log::new(Some(&events_ctx.status_tx))
                    .warn(format!("Milter 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 = run(
            &session_ctx,
            &mut r,
            &mut w,
            protocol,
            external,
            internal_rx,
            event_tx,
        )
        .await;
        let _ = wire::write(&mut w, wire::C_QUIT, &[]).await;
        dispatcher_abort.abort();
        let status = match result {
            Ok(()) => ClientStatus::Disconnected,
            Err(e) => {
                Log::new(Some(&session_ctx.status_tx)).warn(format!("Milter 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(local)
}

/// The filter's answer to one command. `implicit` when it gave none: it asked for the stage to be
/// left out or not answered, which the protocol reads as continue.
struct Verdict {
    code: u8,
    data: Vec<u8>,
    mods: Vec<Value>,
    implicit: bool,
}

impl Verdict {
    fn implicit() -> Self {
        Self {
            code: wire::R_CONTINUE,
            data: vec![],
            mods: vec![],
            implicit: true,
        }
    }
}

/// Read until the filter's verdict, collecting the modifications before it.
async fn verdict(r: &mut ReadHalf<TcpStream>) -> Result<Verdict> {
    let mut mods = Vec::new();
    loop {
        let (cmd, data) = wire::read_packet(r, REPLY_TIMEOUT)
            .await?
            .context("the filter closed the connection")?;
        let s = wire::strings(&data);
        let first = || s.first().cloned().unwrap_or_default();
        match cmd {
            wire::R_CONTINUE
            | wire::R_ACCEPT
            | wire::R_REJECT
            | wire::R_TEMPFAIL
            | wire::R_DISCARD
            | wire::R_REPLYCODE
            | wire::R_SKIP => {
                return Ok(Verdict {
                    code: cmd,
                    data,
                    mods,
                    implicit: false,
                })
            }
            wire::R_PROGRESS => {}
            wire::R_ADDHEADER => {
                mods.push(json!({"kind": "add_header", "name": first(), "value": s.get(1)}))
            }
            wire::R_CHGHEADER => {
                ensure!(data.len() >= 4, "short change_header");
                let index = u32::from_be_bytes(data[..4].try_into().unwrap());
                let f = wire::strings(&data[4..]);
                mods.push(json!({"kind": "change_header", "index": index, "name": f.first(), "value": f.get(1)}));
            }
            wire::R_ADDRCPT => mods.push(json!({"kind": "add_rcpt", "recipient": first()})),
            wire::R_DELRCPT => mods.push(json!({"kind": "del_rcpt", "recipient": first()})),
            wire::R_REPLBODY => {
                mods.push(json!({"kind": "replace_body", "body": String::from_utf8_lossy(&data)}))
            }
            wire::R_QUARANTINE => mods.push(json!({"kind": "quarantine", "reason": first()})),
            other => bail!("unexpected reply {:?} from the filter", other as char),
        }
    }
}

/// The negotiated protocol steps, and the I/O they govern.
struct Conn<'a> {
    r: &'a mut ReadHalf<TcpStream>,
    w: &'a mut WriteHalf<TcpStream>,
    protocol: u32,
}

impl Conn<'_> {
    /// Send one command and read its verdict — unless the filter asked for the stage to be left
    /// out (`skip`) or not answered (`no_reply`).
    async fn step(&mut self, cmd: u8, data: &[u8], skip: u32, no_reply: u32) -> Result<Verdict> {
        if self.protocol & skip != 0 {
            return Ok(Verdict::implicit());
        }
        wire::write(self.w, cmd, data).await?;
        if self.protocol & no_reply != 0 {
            return Ok(Verdict::implicit());
        }
        verdict(self.r).await
    }
}

fn reply_event(stage: &str, v: Verdict) -> Event {
    let text = (v.code == wire::R_REPLYCODE).then(|| {
        wire::strings(&v.data)
            .into_iter()
            .next()
            .unwrap_or_default()
    });
    Event::new(
        &actions::REPLY_EVENT,
        json!({"stage": stage, "decision": wire::reply_name(v.code), "text": text, "modifications": v.mods, "implicit": v.implicit}),
    )
}

/// Carry out one action; the event it raises, if any.
async fn perform(c: &mut Conn<'_>, a: &Value) -> Result<Option<Event>> {
    let strs = |k: &str| -> Vec<String> {
        a[k].as_array()
            .map(|v| {
                v.iter()
                    .filter_map(Value::as_str)
                    .map(str::to_string)
                    .collect()
            })
            .unwrap_or_default()
    };
    Ok(Some(match a["type"].as_str().unwrap_or_default() {
        "milter_connect" => {
            let addr = a["address"].as_str().unwrap_or_default();
            let family = if addr.contains(':') { '6' } else { '4' };
            let body = wire::connect_body(
                a["hostname"].as_str().unwrap_or_default(),
                family,
                a["port"].as_u64().unwrap_or(0) as u16,
                addr,
            );
            reply_event(
                "connect",
                c.step(wire::C_CONNECT, &body, wire::P_NOCONNECT, wire::P_NR_CONN)
                    .await?,
            )
        }
        "milter_helo" => {
            let body = wire::cstrings(&[a["name"].as_str().unwrap_or_default()]);
            reply_event(
                "helo",
                c.step(wire::C_HELO, &body, wire::P_NOHELO, wire::P_NR_HELO)
                    .await?,
            )
        }
        "milter_mail" | "milter_rcpt" => {
            let (field, cmd, stage, skip, no_reply) = if a["type"] == "milter_mail" {
                (
                    "sender",
                    wire::C_MAIL,
                    "mail",
                    wire::P_NOMAIL,
                    wire::P_NR_MAIL,
                )
            } else {
                (
                    "recipient",
                    wire::C_RCPT,
                    "rcpt",
                    wire::P_NORCPT,
                    wire::P_NR_RCPT,
                )
            };
            let mut fields = vec![a[field].as_str().unwrap_or_default().to_string()];
            fields.extend(strs("esmtp_args"));
            let refs: Vec<&str> = fields.iter().map(String::as_str).collect();
            reply_event(
                stage,
                c.step(cmd, &wire::cstrings(&refs), skip, no_reply).await?,
            )
        }
        "milter_message" => {
            let mut v = c
                .step(wire::C_DATA, &[], wire::P_NODATA, wire::P_NR_DATA)
                .await?;
            if v.code == wire::R_CONTINUE {
                for h in a["headers"].as_array().cloned().unwrap_or_default() {
                    let body = wire::cstrings(&[
                        h["name"].as_str().unwrap_or_default(),
                        h["value"].as_str().unwrap_or_default(),
                    ]);
                    v = c
                        .step(wire::C_HEADER, &body, wire::P_NOHDRS, wire::P_NR_HDR)
                        .await?;
                    if v.code != wire::R_CONTINUE {
                        break;
                    }
                }
            }
            if v.code == wire::R_CONTINUE {
                v = c
                    .step(wire::C_EOH, &[], wire::P_NOEOH, wire::P_NR_EOH)
                    .await?;
            }
            if v.code == wire::R_CONTINUE {
                for chunk in a["body"]
                    .as_str()
                    .unwrap_or_default()
                    .as_bytes()
                    .chunks(BODY_CHUNK)
                {
                    v = c
                        .step(wire::C_BODY, chunk, wire::P_NOBODY, wire::P_NR_BODY)
                        .await?;
                    if v.code != wire::R_CONTINUE {
                        break;
                    }
                }
            }
            // SMFIR_SKIP: the filter has seen enough of the body; end of message follows.
            if v.code == wire::R_CONTINUE || v.code == wire::R_SKIP {
                v = c.step(wire::C_BODYEOB, &[], 0, 0).await?;
            }
            reply_event("message", v)
        }
        "milter_abort" => {
            wire::write(c.w, wire::C_ABORT, &[]).await?;
            return Ok(None);
        }
        t => bail!("unsupported action {t}"),
    }))
}

async fn run(
    ctx: &ConnectContext,
    r: &mut ReadHalf<TcpStream>,
    w: &mut WriteHalf<TcpStream>,
    protocol: u32,
    mut external: mpsc::Receiver<ClientCommand>,
    mut internal: mpsc::Receiver<(Value, usize)>,
    events: mpsc::Sender<(Event, usize)>,
) -> Result<()> {
    let log = Log::new(Some(&ctx.status_tx));
    loop {
        let (action, depth, caller) = tokio::select! {
            c = external.recv() => match c { Some(c) => (c.action.clone(), 0, Some(c)), None => return Ok(()) },
            a = internal.recv() => match a { Some((a, d)) => (a, d, None), None => return Ok(()) },
        };
        let reply = |c: Option<ClientCommand>, o: ClientSendOutcome| {
            if let Some(c) = c {
                crate::client::command_support::reply(c, Ok(o));
            }
        };
        match MilterClientProtocol.execute_action(action.clone()) {
            Ok(ClientActionResult::Disconnect) => {
                reply(caller, ClientSendOutcome::Disconnected);
                return Ok(());
            }
            Ok(_) => {}
            Err(e) => {
                reply(
                    caller,
                    ClientSendOutcome::Rejected {
                        error: e.to_string(),
                    },
                );
                continue;
            }
        }
        if depth > MAX_FOLLOWUP_DEPTH {
            log.warn(format!(
                "Milter client: handler chain stopped after {MAX_FOLLOWUP_DEPTH} follow-ups"
            ));
            continue;
        }
        if caller.is_some() {
            ctx.state
                .record_access_log(
                    AccessLogOwner::Client(ctx.client_id.as_u32()),
                    "Milter",
                    None,
                    "injected_action",
                    action.clone(),
                    vec![],
                )
                .await;
        }
        match perform(
            &mut Conn {
                r: &mut *r,
                w: &mut *w,
                protocol,
            },
            &action,
        )
        .await?
        {
            Some(event) => {
                reply(
                    caller,
                    ClientSendOutcome::Executed {
                        detail: event.data.to_string(),
                    },
                );
                events
                    .try_send((event, depth))
                    .context("milter event queue full; consumer stalled")?;
            }
            None => reply(caller, ClientSendOutcome::Sent { bytes_sent: 5 }),
        }
    }
}