rtrtr 0.3.3

A versatile tool for managing route filters
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
//! Controlling the entire operation.

use std::{fs, io};
use std::cell::RefCell;
use std::collections::HashMap;
use std::net::IpAddr;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use clap::crate_version;
use daemonbase::error::Failed;
use log::error;
use serde::Deserialize;
use tokio::runtime;
use crate::{http, metrics};
use crate::comms::{Gate, GateAgent, Link};
use crate::config::{Config, ConfigFile, Marked};
use crate::targets::Target;
use crate::units::Unit;


//------------ HttpClientConfig ----------------------------------------------

#[derive(Clone, Debug, Default, Deserialize)]
pub struct HttpClientConfig {
    /// The proxy servers to use for outgoing HTTP requests.
    #[cfg(feature = "socks")]
    #[serde(default, rename = "http-proxies")]
    proxies: Vec<String>,

    /// Additional root certificates for outgoing HTTP requests.
    #[serde(default, rename = "http-root-certs")]
    root_certs: Vec<PathBuf>,

    /// The user agent string to use for outgoing HTTP requests.
    #[serde(rename = "http-user-agent")]
    user_agent: Option<String>,

    /// Local address to bind to for outgoing HTTP requests.
    #[serde(rename = "http-client-addr")]
    local_addr: Option<IpAddr>,
}


//------------ Component -----------------------------------------------------

/// Facilities available to all components.
///
/// Upon being started, every component receives one of these. It provides
/// access to information and services available to all components.
#[derive(Debug)]
pub struct Component {
    /// The component’s name.
    name: Arc<str>,

    /// The HTTP client config.
    http_config: Arc<HttpClientConfig>,

    /// A reference to the metrics collection.
    metrics: metrics::Collection,

    /// A reference to the HTTP resources collection.
    http_resources: http::Resources,
}

impl Component {
    /// Creates a new component from its, well, components.
    fn new(
        name: String,
        http_config: Arc<HttpClientConfig>,
        metrics: metrics::Collection,
        http_resources: http::Resources,
    ) -> Self {
        Component {
            name: name.into(), http_config, metrics, http_resources,
        }
    }

    /// Returns the name of the component.
    pub fn name(&self) -> &Arc<str> {
        &self.name
    }

    /// Register a metrics source.
    pub fn register_metrics(&mut self, source: Arc<dyn metrics::Source>) {
        self.metrics.register(self.name.clone(), Arc::downgrade(&source));
    }

    /// Register an HTTP resources.
    pub fn register_http_resource(
        &mut self, process: Arc<dyn http::ProcessRequest>
    ) {
        self.http_resources.register(Arc::downgrade(&process))
    }

    /// Creates a new HTTP client for the component.
    pub fn http_client(&self) -> Result<reqwest::ClientBuilder, String> {
        let mut builder = reqwest::Client::builder();
        
        #[cfg(feature = "socks")]
        for proxy in &self.http_config.proxies {
            let proxy = match reqwest::Proxy::all(proxy) {
                Ok(proxy) => proxy,
                Err(err) => {
                    return Err(format!(
                        "Invalid rrdp-proxy '{proxy}': {err}"
                    ));
                }
            };
            builder = builder.proxy(proxy);
        }

        for path in &self.http_config.root_certs {
            builder = builder.add_root_certificate(
                Self::load_cert(path)?
            );
        }

        builder = builder.user_agent(
            match self.http_config.user_agent.as_ref() {
                Some(agent) => agent.as_str(),
                None => concat!("RTRTR ", crate_version!()),
            }
        );

        #[cfg(feature = "native-tls")]
        {
            builder = builder.use_native_tls();
        }

        if let Some(addr) = self.http_config.local_addr {
            builder = builder.local_address(addr)
        }

        Ok(builder)
    }

    /// Loads a WebPKI trusted certificate.
    fn load_cert(path: &Path) -> Result<reqwest::Certificate, String> {
        let mut file = match fs::File::open(path) {
            Ok(file) => file,
            Err(err) => {
                return Err(format!(
                    "Cannot open rrdp-root-cert file '{}': {}'",
                    path.display(), err
                ));
            }
        };
        let mut data = Vec::new();
        if let Err(err) = io::Read::read_to_end(&mut file, &mut data) {
            return Err(format!(
                "Cannot read rrdp-root-cert file '{}': {}'",
                path.display(), err
            ));
        }
        reqwest::Certificate::from_pem(&data).map_err(|err| {
            format!(
                "Cannot decode rrdp-root-cert file '{}': {}'",
                path.display(), err
            )
        })
    }
}


//------------ Manager -------------------------------------------------------

/// A manager for components and auxiliary services.
#[derive(Default)]
pub struct Manager {
    /// The currently active units represented by agents to their gates.
    units: HashMap<String, GateAgent>,

    /// Gates for newly loaded, not yet spawned units.
    pending: HashMap<String, Gate>,

    /// The HTTP client config.
    http_config: Arc<HttpClientConfig>,

    /// The metrics collection maintained by this managers.
    metrics: metrics::Collection,

    /// The HTTP resources collection maintained by this manager.
    http_resources: http::Resources,
}


impl Manager {
    /// Creates a new manager.
    pub fn new(http_config: &HttpClientConfig) -> Self {
        Self {
            http_config: http_config.clone().into(),
            .. Default::default()
        }
    }

    /// Loads the given config file.
    ///
    /// Parses the given file as a TOML config file. All links to units
    /// referenced in the configuration are pre-connected.
    ///
    /// If there are any errors in the config file, they are logged as errors
    /// and a generic error is returned.
    ///
    /// If the method succeeds, you need to spawn all units and targets via
    /// the [`spawn`](Self::spawn) method.
    ///
    /// Returns both a new manager and the parsed config.
    pub fn load(
        file: ConfigFile
    ) -> Result<(Self, Config), Failed> {
        // Prepare the thread-local used to allow serde load the links in the
        // units and targets.
        GATES.with(|gates| {
            gates.replace(Some(Default::default()))
        });

        // Now load the config file.
        let config = match Config::from_toml(file.bytes(), file.dir()) {
            Ok(config) => config,
            Err(err) => {
                match file.path() {
                    Some(path) => error!("{}: {}", path.display(), err),
                    None => error!("{}", err)
                }
                return Err(Failed)
            }
        };

        let mut manager = Self::new(&config.http_client);

        // All entries in the thread-local that have a gate are new. They must
        // appear in config’s units or we have unresolved links.
        let gates = GATES.with(|gates| gates.replace(None) ).unwrap();
        let mut errs = Vec::new();
        for (name, load) in gates {
            if let Some(gate) = load.gate {
                if !config.units.units.contains_key(&name) {
                    for mut link in load.links {
                        link.resolve_config(&file);
                        errs.push(link.mark(
                            format!("unresolved link to unit '{name}'")
                        ))
                    }
                }
                else {
                    manager.units.insert(name.clone(), load.agent);
                    manager.pending.insert(name, gate);
                }
            }
        }
        if !errs.is_empty() {
            for err in errs {
                error!("{}", err);
            }
            return Err(Failed)
        }

        Ok((manager, config))
    }

    /// Allows creating components and adding them to the manager.
    ///
    /// Because creating components that contain links requires some setup
    /// work, this has to happen inside a closure. Inside the closure, you
    /// can create units and targets and add them to the correct set. Once
    /// the closure returns, all of the units and targets are spawned onto
    /// the runtime represented by the `runtime` handle. If all of this
    /// succeeds, the method will return whatever the closure returned.
    pub fn add_components<F, T>(
        &mut self, runtime: &runtime::Handle, op: F
    ) -> Result<T, Failed>
    where
        F: FnOnce(&mut UnitSet, &mut TargetSet) -> T
    {
        GATES.with(|gates| {
            gates.replace(
                Some(self.units.iter().map(|(key, value)| {
                    (key.clone(), value.clone().into())
                }).collect())
            )
        });
        
        let mut units = UnitSet::new();
        let mut targets = TargetSet::new();
        let res = op(&mut units, &mut targets);

        // All entries in the thread-local that have a gate are new. They must
        // appear in unit set or we have unresolved links.
        let gates = GATES.with(|gates| gates.replace(None)).unwrap();
        let mut errs = Vec::new();
        for (name, load) in gates {
            if let Some(gate) = load.gate {
                if !units.units.contains_key(&name) {
                    errs.push(
                        format!("unresolved link to unit '{name}'")
                    )
                }
                else {
                    self.units.insert(name.clone(), load.agent);
                    self.pending.insert(name, gate);
                }
            }
        }
        if !errs.is_empty() {
            for err in errs {
                error!("{}", err);
            }
            return Err(Failed)
        }

        self.spawn(&mut units, &mut targets, runtime);
        Ok(res)
    }

    /// Spawns all units and targets unto the given runtime.
    ///
    /// # Panics
    ///
    /// The method panics if the config hasn’t been successfully loaded via
    /// the same manager earlier.
    pub fn spawn(
        &mut self,
        units: &mut UnitSet,
        targets: &mut TargetSet,
        runtime: &runtime::Handle,
    ) {
        for (name, unit) in units.units.drain() {
            let gate = match self.pending.remove(&name) {
                Some(gate) => gate,
                None => {
                    error!("Unit {} is unused and will not be started.", name);
                    continue
                }
            };
            let controller = Component::new(
                name, self.http_config.clone(), self.metrics.clone(),
                self.http_resources.clone()
            );
            runtime.spawn(unit.run(controller, gate));
        }

        for (name, target) in targets.targets.drain() {
            let controller = Component::new(
                name, self.http_config.clone(), self.metrics.clone(),
                self.http_resources.clone()
            );
            runtime.spawn(target.run(controller));
        }

    }

    /// Returns a new reference to the manager’s metrics collection.
    pub fn metrics(&self) -> metrics::Collection {
        self.metrics.clone()
    }

    /// Returns a new reference the the HTTP resources collection.
    pub fn http_resources(&self) -> http::Resources {
        self.http_resources.clone()
    }
}


//------------ UnitSet -------------------------------------------------------

/// A set of units to be started.
#[derive(Default, Deserialize)]
#[serde(transparent)]
pub struct UnitSet {
    units: HashMap<String, Unit>,
}

impl UnitSet {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn insert(&mut self, name: impl Into<String>, unit: Unit) {
        self.units.insert(name.into(), unit);
    }
}


//------------ TargetSet -----------------------------------------------------

/// A set of targets to be started.
#[derive(Default, Deserialize)]
#[serde(transparent)]
pub struct TargetSet {
    targets: HashMap<String, Target>,
}

impl TargetSet {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn insert(&mut self, name: impl Into<String>, target: Target) {
        self.targets.insert(name.into(), target);
    }
}

//------------ LoadUnit ------------------------------------------------------

/// A unit referenced during loading.
struct LoadUnit {
    /// The gate of the unit.
    ///
    /// This is some only if the unit is newly created and has not yet been
    /// spawned onto a runtime.
    gate: Option<Gate>,

    /// A gate agent for the unit.
    agent: GateAgent,

    /// A list of location of links in the config.
    ///
    /// This is only used for generating errors if non-existing units are
    /// referenced in the config file.
    links: Vec<Marked<()>>,
}

impl Default for LoadUnit {
    fn default() -> Self {
        let (gate, agent) = Gate::new();
        LoadUnit {
            gate: Some(gate),
            agent,
            links: Vec::new()
        }
    }
}

impl From<GateAgent> for LoadUnit {
    fn from(agent: GateAgent) -> Self {
        LoadUnit {
            gate: None,
            agent,
            links: Vec::new()
        }
    }
}


//------------ Loading Links -------------------------------------------------

thread_local!(
    static GATES: RefCell<Option<HashMap<String, LoadUnit>>> = const {
        RefCell::new(None)
    }
);


/// Loads a link with the given name.
///
/// # Panics
///
/// This funtion panics if it is called outside of a run of
/// [`Manager::load`].
pub fn load_link(name: Marked<String>) -> Link {
    GATES.with(|gates| {
        let mut gates = gates.borrow_mut();
        let gates = gates.as_mut().unwrap();

        let mark = name.mark(());
        let name = name.into_inner();
        let unit = gates.entry(name).or_default();
        unit.links.push(mark);
        unit.agent.create_link()
    })
}