lazydns 0.3.20

A light and fast DNS server/forwarder implementation in Rust
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
//! Sequence executor plugin (executable wrapper)
//!
//! This module provides the `SequencePlugin` used by the runtime to execute
//! a sequential list of plugins. It is extracted here from the larger
//! `plugins::advanced` module so it can be reused by executable/plugin
//! composition code and documented independently.

use crate::Result;
use crate::plugin::{Context, Plugin, RETURN_FLAG};
use async_trait::async_trait;
use std::sync::Arc;
use tracing::trace;

// Plugin builder registration for SequencePlugin
// Full sequence parsing with conditions is complex
// and should be handled by the builder system.
// crate::register_plugin_builder!(SequencePlugin);

/// A sequential execution step for `SequencePlugin`.
pub enum SequenceStep {
    /// Execute a plugin unconditionally
    Exec(Arc<dyn Plugin>),
    /// Execute a plugin conditionally
    If {
        /// Condition invoked with current `Context`
        condition: Arc<dyn Fn(&Context) -> bool + Send + Sync>,
        /// Plugin to execute when condition is true
        action: Arc<dyn Plugin>,
        /// Human-readable condition description (for tracing)
        desc: String,
    },
}

impl std::fmt::Debug for SequenceStep {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SequenceStep::Exec(plugin) => f.debug_tuple("Exec").field(plugin).finish(),
            SequenceStep::If { action, desc, .. } => f
                .debug_struct("If")
                .field("action", action)
                .field("cond", desc)
                .finish(),
        }
    }
}

/// Executes a sequence of plugins in order.
#[derive(Debug)]
pub struct SequencePlugin {
    steps: Vec<SequenceStep>,
    tag: Option<String>,
}

impl SequencePlugin {
    /// Create a new sequence plugin from a simple list of plugins.
    pub fn new(plugins: Vec<Arc<dyn Plugin>>) -> Self {
        let steps = plugins.into_iter().map(SequenceStep::Exec).collect();
        Self { steps, tag: None }
    }

    /// Create a sequence plugin with explicit steps (including conditional steps).
    pub fn with_steps(steps: Vec<SequenceStep>) -> Self {
        Self { steps, tag: None }
    }

    /// Create a sequence plugin with explicit steps and an optional tag.
    /// This preserves the configured tag so `display_name()` can include it.
    pub fn with_steps_and_tag(steps: Vec<SequenceStep>, tag: Option<String>) -> Self {
        Self { steps, tag }
    }
}

#[async_trait]
impl Plugin for SequencePlugin {
    async fn execute(&self, ctx: &mut Context) -> Result<()> {
        for step in &self.steps {
            match step {
                SequenceStep::Exec(plugin) => {
                    trace!(
                        plugin = plugin.display_name(),
                        "Sequence: executing plugin (exec)"
                    );
                    match plugin.execute(ctx).await {
                        Ok(_) => trace!(plugin = plugin.display_name(), "Sequence: exec succeeded"),
                        Err(e) => {
                            trace!(plugin = plugin.display_name(), error = %e, "Sequence: exec failed");
                            return Err(e);
                        }
                    }
                }
                SequenceStep::If {
                    condition,
                    action,
                    desc,
                } => {
                    let cond = condition(ctx);
                    trace!(condition = %desc, result = cond, plugin = action.display_name(), "Sequence: conditional step evaluated");
                    if cond {
                        trace!(plugin = action.display_name(), condition = %desc, "Sequence: executing conditional action");
                        match action.execute(ctx).await {
                            Ok(_) => {
                                trace!(plugin = action.display_name(), condition = %desc, "Sequence: conditional action succeeded")
                            }
                            Err(e) => {
                                trace!(plugin = action.display_name(), condition = %desc, error = %e, "Sequence: conditional action failed");
                                return Err(e);
                            }
                        }
                    }
                }
            }

            // Handle jump_target (push/return semantics): execute target and continue with next step
            while ctx.has_metadata("jump_target") {
                if let Some(target) = ctx.get_metadata::<String>("jump_target").cloned() {
                    // Remove jump target and return flag before executing target
                    ctx.remove_metadata("jump_target");
                    ctx.remove_metadata(RETURN_FLAG);

                    trace!(jump_target = %target, "Sequence: handling jump target (push/return)");

                    // Get registry from context metadata
                    if let Some(registry) = ctx
                        .get_metadata::<std::sync::Arc<crate::plugin::Registry>>(
                            "__plugin_registry",
                        )
                    {
                        if let Some(target_plugin) = registry.get(&target) {
                            // Save the current RETURN_FLAG state before executing jump target
                            // This prevents jump targets from stopping the calling sequence
                            let saved_return_flag = ctx.get_metadata::<bool>(RETURN_FLAG).copied();

                            match target_plugin.execute(ctx).await {
                                Ok(_) => {
                                    trace!(jump_target = %target, "Sequence: jump target succeeded")
                                }
                                Err(e) => {
                                    trace!(jump_target = %target, error = %e, "Sequence: jump target failed");
                                    return Err(e);
                                }
                            }

                            // Restore the RETURN_FLAG state after jump target execution
                            // This ensures jump targets don't affect the calling sequence's flow
                            if let Some(flag) = saved_return_flag {
                                ctx.set_metadata(RETURN_FLAG, flag);
                            } else {
                                ctx.remove_metadata(RETURN_FLAG);
                            }
                        } else {
                            trace!(jump_target = %target, "Sequence: jump target plugin not found");
                        }
                    }
                } else {
                    break;
                }
            }

            // Handle goto_label (replace sequence semantics): stop and return to PluginHandler
            if ctx.has_metadata("goto_label") {
                // Set RETURN_FLAG to signal PluginHandler to handle the goto
                ctx.set_metadata(RETURN_FLAG, true);
                trace!("Sequence: goto_label detected, stopping sequence execution");
                break;
            }

            // If a plugin set the return flag (and it's not a goto), stop executing further steps.
            if matches!(ctx.get_metadata::<bool>(RETURN_FLAG), Some(true)) {
                break;
            }
        }
        Ok(())
    }

    fn name(&self) -> &str {
        "sequence"
    }

    fn tag(&self) -> Option<&str> {
        self.tag.as_deref()
    }

    fn init(config: &crate::config::PluginConfig) -> Result<std::sync::Arc<dyn Plugin>> {
        // For now, implement a simple sequence that expects a "plugins" array
        // with plugin names. Full sequence parsing with conditions is complex
        // and should be handled by the builder system.
        let args = config.effective_args();

        if let Some(serde_yaml::Value::Sequence(_plugin_names)) = args.get("plugins") {
            // This is a simplified implementation - in practice, sequences with
            // plugin references need to be resolved later in the build process
            // For now, return an empty sequence that will be resolved later
            Ok(std::sync::Arc::new(Self {
                steps: vec![],
                tag: config.tag.clone(),
            }))
        } else {
            // Default to empty sequence
            Ok(std::sync::Arc::new(Self {
                steps: vec![],
                tag: config.tag.clone(),
            }))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dns::Message;
    use crate::plugin::Context;
    use std::sync::Arc;

    #[derive(Debug)]
    struct Recorder {
        order: Arc<std::sync::Mutex<Vec<&'static str>>>,
        label: &'static str,
    }

    #[async_trait]
    impl Plugin for Recorder {
        async fn execute(&self, ctx: &mut Context) -> Result<()> {
            ctx.set_metadata("seen", true);
            let mut order = self.order.lock().unwrap_or_else(|poisoned| {
                // In tests, if mutex is poisoned, still allow access
                poisoned.into_inner()
            });
            order.push(self.label);
            Ok(())
        }

        fn name(&self) -> &str {
            self.label
        }
    }

    #[test]
    fn test_sequence_new() {
        let seq = SequencePlugin::new(vec![]);
        assert_eq!(seq.name(), "sequence");
    }

    #[test]
    fn test_sequence_with_steps() {
        let seq = SequencePlugin::with_steps(vec![]);
        assert!(seq.tag.is_none());
    }

    #[test]
    fn test_sequence_with_steps_and_tag() {
        let seq = SequencePlugin::with_steps_and_tag(vec![], Some("my_tag".to_string()));
        assert_eq!(seq.tag, Some("my_tag".to_string()));
    }

    #[test]
    fn test_sequence_debug() {
        let seq = SequencePlugin::new(vec![]);
        let debug_str = format!("{:?}", seq);
        assert!(debug_str.contains("SequencePlugin"));
    }

    #[test]
    fn test_sequence_step_exec_debug() {
        #[derive(Debug)]
        struct DummyPlugin;

        #[async_trait]
        impl Plugin for DummyPlugin {
            async fn execute(&self, _ctx: &mut Context) -> Result<()> {
                Ok(())
            }
            fn name(&self) -> &str {
                "dummy"
            }
        }

        let step = SequenceStep::Exec(Arc::new(DummyPlugin));
        let debug_str = format!("{:?}", step);
        assert!(debug_str.contains("Exec"));
    }

    #[test]
    fn test_sequence_step_if_debug() {
        #[derive(Debug)]
        struct DummyPlugin;

        #[async_trait]
        impl Plugin for DummyPlugin {
            async fn execute(&self, _ctx: &mut Context) -> Result<()> {
                Ok(())
            }
            fn name(&self) -> &str {
                "dummy"
            }
        }

        let step = SequenceStep::If {
            condition: Arc::new(|_| true),
            action: Arc::new(DummyPlugin),
            desc: "always true".to_string(),
        };
        let debug_str = format!("{:?}", step);
        assert!(debug_str.contains("If"));
        assert!(debug_str.contains("always true"));
    }

    #[tokio::test]
    async fn sequence_executes_in_order() {
        let order = Arc::new(std::sync::Mutex::new(Vec::new()));
        let seq = SequencePlugin::new(vec![
            Arc::new(Recorder {
                order: order.clone(),
                label: "one",
            }),
            Arc::new(Recorder {
                order: order.clone(),
                label: "two",
            }),
        ]);

        let mut ctx = Context::new(Message::new());
        seq.execute(&mut ctx).await.unwrap();

        let logged = order.lock().unwrap_or_else(|p| p.into_inner()).clone();
        assert_eq!(logged, vec!["one", "two"]);
    }

    #[tokio::test]
    async fn test_sequence_conditional_step_true() {
        #[derive(Debug)]
        struct SetFlag;

        #[async_trait]
        impl Plugin for SetFlag {
            async fn execute(&self, ctx: &mut Context) -> Result<()> {
                ctx.set_metadata("flag_set", true);
                Ok(())
            }
            fn name(&self) -> &str {
                "set_flag"
            }
        }

        let seq = SequencePlugin::with_steps(vec![SequenceStep::If {
            condition: Arc::new(|_| true),
            action: Arc::new(SetFlag),
            desc: "always true".to_string(),
        }]);

        let mut ctx = Context::new(Message::new());
        seq.execute(&mut ctx).await.unwrap();

        assert_eq!(ctx.get_metadata::<bool>("flag_set"), Some(&true));
    }

    #[tokio::test]
    async fn test_sequence_conditional_step_false() {
        #[derive(Debug)]
        struct SetFlag;

        #[async_trait]
        impl Plugin for SetFlag {
            async fn execute(&self, ctx: &mut Context) -> Result<()> {
                ctx.set_metadata("flag_set", true);
                Ok(())
            }
            fn name(&self) -> &str {
                "set_flag"
            }
        }

        let seq = SequencePlugin::with_steps(vec![SequenceStep::If {
            condition: Arc::new(|_| false),
            action: Arc::new(SetFlag),
            desc: "always false".to_string(),
        }]);

        let mut ctx = Context::new(Message::new());
        seq.execute(&mut ctx).await.unwrap();

        assert!(ctx.get_metadata::<bool>("flag_set").is_none());
    }

    #[tokio::test]
    async fn test_sequence_stops_on_return_flag() {
        let order = Arc::new(std::sync::Mutex::new(Vec::new()));

        #[derive(Debug)]
        struct SetReturnFlag {
            order: Arc<std::sync::Mutex<Vec<&'static str>>>,
        }

        #[async_trait]
        impl Plugin for SetReturnFlag {
            async fn execute(&self, ctx: &mut Context) -> Result<()> {
                let mut order = self.order.lock().unwrap_or_else(|p| p.into_inner());
                order.push("first");
                ctx.set_metadata(RETURN_FLAG, true);
                Ok(())
            }
            fn name(&self) -> &str {
                "set_return"
            }
        }

        let seq = SequencePlugin::new(vec![
            Arc::new(SetReturnFlag {
                order: order.clone(),
            }),
            Arc::new(Recorder {
                order: order.clone(),
                label: "should_not_run",
            }),
        ]);

        let mut ctx = Context::new(Message::new());
        seq.execute(&mut ctx).await.unwrap();

        let logged = order.lock().unwrap_or_else(|p| p.into_inner()).clone();
        // Only "first" should be logged, "should_not_run" should be skipped
        assert_eq!(logged, vec!["first"]);
    }
}