aboutsummaryrefslogblamecommitdiff
path: root/src/diplonat.rs
blob: 7049530b9cc3e1cedeffffc23ab0f23dcfc76149 (plain) (tree)
1
2
3
4
5
6
7
8
9
                   
                    

                              
                                     
                                   
                               
 
                     
                      
                          
                

 

                                      



                                                                            
 
                                
                                     

                         






                                  
 
                    
                 

                  
      
 

                   
 
                                                

                           

                            
       
 
                  
   
 
use anyhow::Result;
use tokio::try_join;

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

pub struct Diplonat {
  consul: ConsulActor,
  firewall: FirewallActor,
  igd: IgdActor,
}

impl Diplonat {
  pub async fn new() -> Result<Self> {
    let rt_cfg = ConfigOpts::from_env()?;
    println!("{:#?}", rt_cfg);
    
    let ca = ConsulActor::new(&rt_cfg.consul.url, &rt_cfg.consul.node_name);

    let fw = FirewallActor::new(
        rt_cfg.firewall.refresh_time,
        &ca.rx_open_ports
    ).await?;
    
    let ia = IgdActor::new(
      &rt_cfg.igd.private_ip, 
      rt_cfg.igd.refresh_time, 
      rt_cfg.igd.expiration_time, 
      &ca.rx_open_ports
    ).await?;

    let ctx = Self {
      consul: ca,
      igd: ia,
      firewall: fw
    };

    return Ok(ctx);
  }

  pub async fn listen(&mut self) -> Result<()> {
    try_join!(
      self.consul.listen(),
      self.igd.listen(),
      self.firewall.listen()
    )?;

    return Ok(());
  }
}