tocat-plugins 0.2.0

The plugins compiled into tocat, a socat-inspired relay
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
//! `compress` / `decompress`: zstd stages for tocat.
//!
//! # Direction
//!
//! `direction = "both"` would compress *both* paths, which is almost never what
//! anyone wants. Compression is usually asymmetric: declare the pair
//! explicitly:
//!
//! ```toml
//! # near end of a compressed tunnel
//! [[plugin]]
//! name = "compress"
//! direction = "source-to-sink"
//!
//! [[plugin]]
//! name = "decompress"
//! direction = "sink-to-source"
//! ```
//!
//! The far end runs the mirror image (`decompress` forward, `compress`
//! reverse) and the two relays form a compressed link over an otherwise
//! plaintext hop.
//!
//! # Latency
//!
//! A relay must not sit on bytes waiting for a better compression window, so
//! every chunk ends with a zstd flush: whatever arrived is on the wire before
//! the call returns. That costs ratio (a flush closes the current block) and
//! it is the right trade for an interactive stream. Set `flush = false` for
//! bulk transfers where throughput matters and nobody is waiting on a prompt;
//! output then appears only as zstd fills its internal buffer, and on EOF.

use std::io::Write;

use serde::{Deserialize, Serialize};
use tocat_api::{
    BuildCtx, Ctx, Execution, LogLevel, Plugin, PluginError, PluginFactory, Result, Stage,
};

pub const COMPRESS: &str = "compress";
pub const DECOMPRESS: &str = "decompress";

const DEFAULT_LEVEL: i32 = 3;

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct CompressConfig {
    /// zstd level, 1–22. Higher is smaller and slower; 3 is zstd's default and
    /// is usually the right answer for a relay.
    #[serde(default = "default_level")]
    pub level: i32,

    /// Flush after every chunk so bytes reach the peer immediately.
    #[serde(default = "default_true")]
    pub flush: bool,

    /// Log the compression ratio when the stream ends.
    #[serde(default)]
    pub report: bool,
}

impl Default for CompressConfig {
    fn default() -> Self {
        Self {
            level: DEFAULT_LEVEL,
            flush: true,
            report: false,
        }
    }
}

#[derive(Debug, Clone, Default, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct DecompressConfig {
    /// Log the expansion ratio when the stream ends.
    #[serde(default)]
    pub report: bool,
}

fn default_level() -> i32 {
    DEFAULT_LEVEL
}

fn default_true() -> bool {
    true
}

/// Maps [std::io::Error] to [PluginError]
fn io(err: std::io::Error, name: &'static str) -> PluginError {
    PluginError::runtime(name, err)
}

pub struct Compress {
    encoder: zstd::stream::write::Encoder<'static, Vec<u8>>,
    flush_each_chunk: bool,
    report: bool,
    read: u64,
    written: u64,
    finished: bool,
}

impl Compress {
    /// Move whatever zstd has produced so far into the pipeline.
    fn drain(&mut self, ctx: &mut Ctx<'_>) {
        let out = self.encoder.get_mut();

        if !out.is_empty() {
            self.written += out.len() as u64;
            ctx.forward(out);
            out.clear();
        }
    }
}

impl Plugin for Compress {
    fn name(&self) -> &str {
        COMPRESS
    }

    fn on_bytes(&mut self, ctx: &mut Ctx<'_>, input: &[u8]) -> Result<()> {
        self.read += input.len() as u64;

        self.encoder.write_all(input).map_err(|e| io(e, COMPRESS))?;

        if self.flush_each_chunk {
            self.encoder.flush().map_err(|e| io(e, COMPRESS))?;
        }

        self.drain(ctx);

        Ok(())
    }

    fn on_eof(&mut self, ctx: &mut Ctx<'_>) -> Result<()> {
        if self.finished {
            return Ok(());
        }

        // Closes the frame. Without the epilogue the peer's decoder will
        // reject the stream as truncated.
        self.encoder.do_finish().map_err(|e| io(e, COMPRESS))?;
        self.finished = true;
        self.drain(ctx);

        if self.report && self.read > 0 {
            let ratio = self.written as f64 / self.read as f64;
            let (read, written) = (self.read, self.written);
            ctx.log(
                LogLevel::Info,
                &format!("compressed {read} to {written} bytes ({ratio:.3})"),
            );
        }

        Ok(())
    }
}

pub struct Decompress {
    decoder: zstd::stream::write::Decoder<'static, Vec<u8>>,
    report: bool,
    read: u64,
    written: u64,
}

impl Decompress {
    fn drain(&mut self, ctx: &mut Ctx<'_>) {
        let out = self.decoder.get_mut();

        if !out.is_empty() {
            self.written += out.len() as u64;
            ctx.forward(out);
            out.clear();
        }
    }
}

impl Plugin for Decompress {
    fn name(&self) -> &str {
        DECOMPRESS
    }

    fn on_bytes(&mut self, ctx: &mut Ctx<'_>, input: &[u8]) -> Result<()> {
        self.read += input.len() as u64;

        // zstd frames do not line up with read boundaries; the decoder holds
        // any partial frame until the rest arrives.
        self.decoder
            .write_all(input)
            .map_err(|e| io(e, DECOMPRESS))?;
        self.decoder.flush().map_err(|e| io(e, DECOMPRESS))?;

        self.drain(ctx);

        Ok(())
    }

    fn on_eof(&mut self, ctx: &mut Ctx<'_>) -> Result<()> {
        self.decoder.flush().map_err(|e| io(e, DECOMPRESS))?;
        self.drain(ctx);

        if self.report && self.read > 0 {
            let (read, written) = (self.read, self.written);
            ctx.log(
                LogLevel::Info,
                &format!("decompressed {read} to {written} bytes"),
            );
        }

        Ok(())
    }
}

pub struct CompressFactory;

impl PluginFactory for CompressFactory {
    fn name(&self) -> &str {
        COMPRESS
    }

    fn description(&self) -> &str {
        "zstd-compress this direction"
    }

    fn execution(&self) -> Execution {
        // Expensive per byte: worth a task of its own.
        Execution::Detached
    }

    fn build(&self, ctx: &mut BuildCtx<'_>) -> Result<Stage> {
        let config: CompressConfig = ctx.config()?;

        if !(1..=22).contains(&config.level) {
            return Err(PluginError::config(
                COMPRESS,
                format!("level {} is outside 1..=22", config.level),
            ));
        }

        let encoder = zstd::stream::write::Encoder::new(Vec::new(), config.level)
            .map_err(|e| io(e, COMPRESS))?;

        Ok(Stage::filter(Compress {
            encoder,
            flush_each_chunk: config.flush,
            report: config.report,
            read: 0,
            written: 0,
            finished: false,
        }))
    }
}

pub struct DecompressFactory;

impl PluginFactory for DecompressFactory {
    fn name(&self) -> &str {
        DECOMPRESS
    }

    fn description(&self) -> &str {
        "zstd-decompress this direction"
    }

    fn execution(&self) -> Execution {
        // Expensive per byte: worth a task of its own.
        Execution::Detached
    }

    fn build(&self, ctx: &mut BuildCtx<'_>) -> Result<Stage> {
        let config: DecompressConfig = ctx.config()?;

        let decoder =
            zstd::stream::write::Decoder::new(Vec::new()).map_err(|e| io(e, DECOMPRESS))?;

        Ok(Stage::filter(Decompress {
            decoder,
            report: config.report,
            read: 0,
            written: 0,
        }))
    }
}

#[cfg(test)]
mod tests {
    use serde_json::{Value, json};
    use tocat_api::{
        ChannelId, ChannelTarget, Direction, EffectSink, Emission, HostBuilder, PipelineMeta,
        StageInfo,
    };

    use super::*;

    struct NullHost;

    impl HostBuilder for NullHost {
        fn open_channel(&mut self, _target: ChannelTarget) -> Result<ChannelId> {
            Ok(ChannelId(0))
        }
    }

    #[derive(Default)]
    struct Silent;

    impl EffectSink for Silent {
        fn write(&mut self, _channel: ChannelId, _bytes: &[u8]) {}
        fn log(&mut self, _level: LogLevel, _stage: &str, _message: &str) {}
    }

    fn meta() -> PipelineMeta {
        PipelineMeta::new(Direction::SourceToSink, "src", "sink")
    }

    fn build(factory: &dyn PluginFactory, config: Value) -> Box<dyn Plugin> {
        let map = config.as_object().expect("object").clone();
        let meta = meta();
        let mut host = NullHost;
        let stage = StageInfo {
            index: 0,
            total: 1,
            name: factory.name(),
            upstream: "src",
            downstream: "sink",
        };
        let mut ctx = BuildCtx::new(factory.name(), &map, &meta, stage, &mut host);
        match factory.build(&mut ctx).expect("build") {
            Stage::Filter(plugin) => plugin,
            Stage::External(_) => unreachable!("compress stages are filters"),
        }
    }

    fn feed(plugin: &mut dyn Plugin, input: &[u8]) -> Vec<u8> {
        let meta = meta();
        let mut emission = Emission::new();
        let mut sink = Silent;
        {
            let mut ctx = Ctx::new(&meta, "compress", input, &mut emission, &mut sink);
            plugin.on_bytes(&mut ctx, input).expect("on_bytes");
        }

        emission.bytes().to_vec()
    }

    fn finish(plugin: &mut dyn Plugin) -> Vec<u8> {
        let meta = meta();
        let mut emission = Emission::new();
        let mut sink = Silent;
        {
            let mut ctx = Ctx::new(&meta, "compress", &[], &mut emission, &mut sink);
            plugin.on_eof(&mut ctx).expect("on_eof");
        }

        emission.bytes().to_vec()
    }

    #[test]
    fn round_trips_across_chunk_boundaries() {
        let mut encoder = build(&CompressFactory, json!({}));
        let mut decoder = build(&DecompressFactory, json!({}));

        let mut wire = Vec::new();
        for chunk in [
            b"hello ".as_slice(),
            b"compressed ".as_slice(),
            b"world".as_slice(),
        ] {
            wire.extend_from_slice(&feed(encoder.as_mut(), chunk));
        }
        wire.extend_from_slice(&finish(encoder.as_mut()));

        // Hand the decoder a boundary that does not match the encoder's.
        let (head, tail) = wire.split_at(wire.len() / 2);
        let mut plain = feed(decoder.as_mut(), head);
        plain.extend_from_slice(&feed(decoder.as_mut(), tail));
        plain.extend_from_slice(&finish(decoder.as_mut()));

        assert_eq!(plain, b"hello compressed world");
    }

    #[test]
    fn flushing_makes_each_chunk_visible_immediately() {
        let mut encoder = build(&CompressFactory, json!({}));
        assert!(
            !feed(encoder.as_mut(), b"first").is_empty(),
            "a flushing encoder must emit before EOF",
        );
    }

    #[test]
    fn rejects_out_of_range_levels() {
        let map = json!({ "level": 30 }).as_object().unwrap().clone();
        let meta = meta();
        let mut host = NullHost;
        let stage = StageInfo {
            index: 0,
            total: 1,
            name: COMPRESS,
            upstream: "src",
            downstream: "sink",
        };
        let mut ctx = BuildCtx::new(COMPRESS, &map, &meta, stage, &mut host);
        assert!(CompressFactory.build(&mut ctx).is_err());
    }
}