aboutsummaryrefslogtreecommitdiff
path: root/src/diplonat.rs
blob: b2310f9f7f52cc7f7fc6f4819e362a7879cce8c6 (plain) (blame)
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
use anyhow::{Result, anyhow};
use log::debug;
use tokio::try_join;

use crate::config::ConfigOpts;
use crate::consul_actor::ConsulActor;
use crate::acme_actor::AcmeActor;
use crate::fw_actor::FirewallActor;
use crate::igd_actor::IgdActor;

pub struct Diplonat {
  consul: ConsulActor,

  acme: Option<AcmeActor>,
  firewall: Option<FirewallActor>,
  igd: Option<IgdActor>,
}

impl Diplonat {
  pub async fn new() -> Result<Self> {
    let config = ConfigOpts::from_env()?;
    debug!("{:#?}", config);
    
    let consul_actor = ConsulActor::new(config.consul);

    let acme_actor = AcmeActor::new(
      config.acme,
      &consul_actor.rx_open_ports
    ).await?;

    let firewall_actor = FirewallActor::new(
        config.firewall,
        &consul_actor.rx_open_ports
    ).await?;
    
    let igd_actor = IgdActor::new(
      config.igd,
      &consul_actor.rx_open_ports
    ).await?;

    if acme_actor.is_none() && 
       firewall_actor.is_none() && 
       igd_actor.is_none() {
      return Err(anyhow!(
        "At least enable *one* module, otherwise it's boring!"));
    }

    let ctx = Self {
      consul: consul_actor,
      acme: acme_actor,
      firewall: firewall_actor,
      igd: igd_actor,
    };

    return Ok(ctx);
  }

  pub async fn listen(&mut self) -> Result<()> {
    let acme = &mut self.acme;
    let firewall = &mut self.firewall;
    let igd = &mut self.igd;

    try_join!(
      self.consul.listen(),
      async {
        match acme {
          Some(x) => x.listen().await,
          None => Ok(())
        }
      },
      async {
        match firewall {
          Some(x) => x.listen().await,
          None => Ok(())
        }
      },
      async {
        match igd {
          Some(x) => x.listen().await,
          None => Ok(())
        }
      },
    )?;

    return Ok(());
  }
}