forked from Deuxfleurs/diplonat
environment.rs successfully replaced with new config/ configuration loader. No API changes, more tests, cleaner code: life is swell.
This commit is contained in:
parent
ae9550ce23
commit
644e707956
7 changed files with 34 additions and 78 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
target/
|
target/
|
||||||
*.swp
|
*.swp
|
||||||
|
.env
|
||||||
|
|
|
@ -9,7 +9,7 @@ use crate::config::RuntimeConfig;
|
||||||
// runtime.rs applies business logic and builds RuntimeConfig structs.
|
// runtime.rs applies business logic and builds RuntimeConfig structs.
|
||||||
|
|
||||||
/// Base configuration options
|
/// Base configuration options
|
||||||
#[derive(Clone, Debug, Default, Deserialize)]
|
#[derive(Clone, Default, Deserialize)]
|
||||||
pub struct ConfigOptsBase {
|
pub struct ConfigOptsBase {
|
||||||
/// This node's private IP address [default: None]
|
/// This node's private IP address [default: None]
|
||||||
pub private_ip: Option<String>,
|
pub private_ip: Option<String>,
|
||||||
|
@ -20,7 +20,7 @@ pub struct ConfigOptsBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ACME configuration options
|
/// ACME configuration options
|
||||||
#[derive(Clone, Debug, Default, Deserialize)]
|
#[derive(Clone, Default, Deserialize)]
|
||||||
pub struct ConfigOptsAcme {
|
pub struct ConfigOptsAcme {
|
||||||
/// Whether ACME is enabled [default: false]
|
/// Whether ACME is enabled [default: false]
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
|
@ -31,7 +31,7 @@ pub struct ConfigOptsAcme {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Consul configuration options
|
/// Consul configuration options
|
||||||
#[derive(Clone, Debug, Default, Deserialize)]
|
#[derive(Clone, Default, Deserialize)]
|
||||||
pub struct ConfigOptsConsul {
|
pub struct ConfigOptsConsul {
|
||||||
/// Consul's node name [default: None]
|
/// Consul's node name [default: None]
|
||||||
pub node_name: Option<String>,
|
pub node_name: Option<String>,
|
||||||
|
@ -40,7 +40,6 @@ pub struct ConfigOptsConsul {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Model of all potential configuration options
|
/// Model of all potential configuration options
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct ConfigOpts {
|
pub struct ConfigOpts {
|
||||||
pub base: ConfigOptsBase,
|
pub base: ConfigOptsBase,
|
||||||
pub acme: ConfigOptsAcme,
|
pub acme: ConfigOptsAcme,
|
||||||
|
|
|
@ -9,25 +9,30 @@ use crate::config::{ConfigOpts, ConfigOptsAcme, ConfigOptsBase, ConfigOptsConsul
|
||||||
// In this file, we take ConfigOpts and transform them into ready-to-use RuntimeConfig.
|
// In this file, we take ConfigOpts and transform them into ready-to-use RuntimeConfig.
|
||||||
// We apply default values and business logic.
|
// We apply default values and business logic.
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct RuntimeConfigAcme {
|
pub struct RuntimeConfigAcme {
|
||||||
pub email: String,
|
pub email: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct RuntimeConfigConsul {
|
pub struct RuntimeConfigConsul {
|
||||||
pub node_name: String,
|
pub node_name: String,
|
||||||
pub url: String,
|
pub url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct RuntimeConfigFirewall {
|
pub struct RuntimeConfigFirewall {
|
||||||
pub refresh_time: Duration,
|
pub refresh_time: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct RuntimeConfigIgd {
|
pub struct RuntimeConfigIgd {
|
||||||
pub private_ip: String,
|
pub private_ip: String,
|
||||||
pub expiration_time: Duration,
|
pub expiration_time: Duration,
|
||||||
pub refresh_time: Duration,
|
pub refresh_time: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct RuntimeConfig {
|
pub struct RuntimeConfig {
|
||||||
pub acme: Option<RuntimeConfigAcme>,
|
pub acme: Option<RuntimeConfigAcme>,
|
||||||
pub consul: RuntimeConfigConsul,
|
pub consul: RuntimeConfigConsul,
|
||||||
|
@ -57,7 +62,9 @@ impl RuntimeConfigAcme {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
let email = opts.email.unwrap();
|
let email = opts.email.expect(
|
||||||
|
"'DIPLONAT_ACME_EMAIL' environment variable is required \
|
||||||
|
if 'DIPLONAT_ACME_ENABLE' == 'true'");
|
||||||
|
|
||||||
Ok(Some(Self {
|
Ok(Some(Self {
|
||||||
email,
|
email,
|
||||||
|
@ -67,7 +74,8 @@ impl RuntimeConfigAcme {
|
||||||
|
|
||||||
impl RuntimeConfigConsul {
|
impl RuntimeConfigConsul {
|
||||||
pub(super) fn new(opts: ConfigOptsConsul) -> Result<Self> {
|
pub(super) fn new(opts: ConfigOptsConsul) -> Result<Self> {
|
||||||
let node_name = opts.node_name.unwrap();
|
let node_name = opts.node_name.expect(
|
||||||
|
"'DIPLONAT_CONSUL_NODE_NAME' environment variable is required");
|
||||||
let url = opts.url.unwrap_or(super::CONSUL_URL.to_string());
|
let url = opts.url.unwrap_or(super::CONSUL_URL.to_string());
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
|
@ -90,7 +98,8 @@ impl RuntimeConfigFirewall {
|
||||||
|
|
||||||
impl RuntimeConfigIgd {
|
impl RuntimeConfigIgd {
|
||||||
pub(super) fn new(opts: ConfigOptsBase) -> Result<Self> {
|
pub(super) fn new(opts: ConfigOptsBase) -> Result<Self> {
|
||||||
let private_ip = opts.private_ip.unwrap();
|
let private_ip = opts.private_ip.expect(
|
||||||
|
"'DIPLONAT_PRIVATE_IP' environment variable is required");
|
||||||
let expiration_time = Duration::from_secs(
|
let expiration_time = Duration::from_secs(
|
||||||
opts.expiration_time.unwrap_or(super::EXPIRATION_TIME).into());
|
opts.expiration_time.unwrap_or(super::EXPIRATION_TIME).into());
|
||||||
let refresh_time = Duration::from_secs(
|
let refresh_time = Duration::from_secs(
|
||||||
|
|
|
@ -1,29 +1,33 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use tokio::try_join;
|
use tokio::try_join;
|
||||||
|
|
||||||
|
use crate::config::ConfigOpts;
|
||||||
use crate::consul_actor::ConsulActor;
|
use crate::consul_actor::ConsulActor;
|
||||||
use crate::igd_actor::IgdActor;
|
|
||||||
use crate::environment::Environment;
|
|
||||||
use crate::fw_actor::FirewallActor;
|
use crate::fw_actor::FirewallActor;
|
||||||
|
use crate::igd_actor::IgdActor;
|
||||||
|
|
||||||
pub struct Diplonat {
|
pub struct Diplonat {
|
||||||
consul: ConsulActor,
|
consul: ConsulActor,
|
||||||
|
firewall: FirewallActor,
|
||||||
igd: IgdActor,
|
igd: IgdActor,
|
||||||
firewall: FirewallActor
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Diplonat {
|
impl Diplonat {
|
||||||
pub async fn new() -> Result<Self> {
|
pub async fn new() -> Result<Self> {
|
||||||
let env = Environment::new()?;
|
let rt_cfg = ConfigOpts::from_env()?;
|
||||||
let ca = ConsulActor::new(&env.consul_url, &env.consul_node_name);
|
println!("{:#?}", rt_cfg);
|
||||||
let ia = IgdActor::new(
|
|
||||||
&env.private_ip,
|
let ca = ConsulActor::new(&rt_cfg.consul.url, &rt_cfg.consul.node_name);
|
||||||
env.refresh_time,
|
|
||||||
env.expiration_time,
|
let fw = FirewallActor::new(
|
||||||
|
rt_cfg.firewall.refresh_time,
|
||||||
&ca.rx_open_ports
|
&ca.rx_open_ports
|
||||||
).await?;
|
).await?;
|
||||||
|
|
||||||
let fw = FirewallActor::new(
|
let ia = IgdActor::new(
|
||||||
env.refresh_time,
|
&rt_cfg.igd.private_ip,
|
||||||
|
rt_cfg.igd.refresh_time,
|
||||||
|
rt_cfg.igd.expiration_time,
|
||||||
&ca.rx_open_ports
|
&ca.rx_open_ports
|
||||||
).await?;
|
).await?;
|
||||||
|
|
||||||
|
|
|
@ -1,56 +0,0 @@
|
||||||
use std::env;
|
|
||||||
use anyhow::{Result, Context, anyhow};
|
|
||||||
use std::time::Duration;
|
|
||||||
use log::*;
|
|
||||||
|
|
||||||
const epi: &'static str = "DIPLONAT_PRIVATE_IP";
|
|
||||||
const ert: &'static str = "DIPLONAT_REFRESH_TIME";
|
|
||||||
const eet: &'static str = "DIPLONAT_EXPIRATION_TIME";
|
|
||||||
const ecnd: &'static str = "DIPLONAT_CONSUL_NODE_NAME";
|
|
||||||
const ecu: &'static str = "DIPLONAT_CONSUL_URL";
|
|
||||||
|
|
||||||
pub struct Environment {
|
|
||||||
pub consul_node_name: String,
|
|
||||||
pub consul_url: String,
|
|
||||||
|
|
||||||
pub refresh_time: Duration,
|
|
||||||
pub expiration_time: Duration,
|
|
||||||
|
|
||||||
pub private_ip: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
/* @FIXME: Rewrite with Serde Envi */
|
|
||||||
impl Environment {
|
|
||||||
pub fn new() -> Result<Self> {
|
|
||||||
let ctx = Self {
|
|
||||||
consul_url: match env::var(ecu) { Ok(e) => e, Err(_) => "http://127.0.0.1:8500".to_string() },
|
|
||||||
consul_node_name: env::var(ecnd).with_context(|| format!("{} env var must be defined", ecnd))?,
|
|
||||||
private_ip: env::var(epi).with_context(|| format!("{} env var must be defined, eg: 192.168.0.18", epi))?,
|
|
||||||
refresh_time: Duration::from_secs(env::var(ert)
|
|
||||||
.with_context(|| format!("{} env var must be defined, eg: 60", ert))?
|
|
||||||
.parse()
|
|
||||||
.with_context(|| format!("{} env var must be an integer, eg: 60", ert))?),
|
|
||||||
expiration_time: Duration::from_secs(env::var(eet)
|
|
||||||
.with_context(|| format!("{} env var must be defined, eg: 300", eet))?
|
|
||||||
.parse()
|
|
||||||
.with_context(|| format!("{} env var must be an integer, eg: 300", eet))?),
|
|
||||||
};
|
|
||||||
|
|
||||||
if ctx.refresh_time.as_secs() * 2 > ctx.expiration_time.as_secs() {
|
|
||||||
return Err(anyhow!(
|
|
||||||
"Expiration time (currently: {}s) must be twice bigger than refresh time (currently: {}s)",
|
|
||||||
ctx.refresh_time.as_secs(),
|
|
||||||
ctx.expiration_time.as_secs()));
|
|
||||||
}
|
|
||||||
|
|
||||||
info!("Consul URL: {:#?}", ctx.consul_url);
|
|
||||||
info!("Consul node name: {:#?}", ctx.consul_node_name);
|
|
||||||
info!("Private IP address: {:#?}", ctx.private_ip);
|
|
||||||
info!("Refresh time: {:#?} seconds", ctx.refresh_time.as_secs());
|
|
||||||
info!("Expiration time: {:#?} seconds", ctx.expiration_time.as_secs());
|
|
||||||
|
|
||||||
return Ok(ctx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -25,8 +25,8 @@ impl IgdActor {
|
||||||
pub async fn new(priv_ip: &str, refresh: Duration, expire: Duration, rxp: &watch::Receiver<messages::PublicExposedPorts>) -> Result<Self> {
|
pub async fn new(priv_ip: &str, refresh: Duration, expire: Duration, rxp: &watch::Receiver<messages::PublicExposedPorts>) -> Result<Self> {
|
||||||
let gw = search_gateway(Default::default())
|
let gw = search_gateway(Default::default())
|
||||||
.await
|
.await
|
||||||
.context("Failed to find gateway")?;
|
.context("Failed to find IGD gateway")?;
|
||||||
info!("Gateway: {}", gw);
|
info!("IGD gateway: {}", gw);
|
||||||
|
|
||||||
let ctx = Self {
|
let ctx = Self {
|
||||||
gateway: gw,
|
gateway: gw,
|
||||||
|
|
|
@ -2,7 +2,6 @@ mod config;
|
||||||
mod consul;
|
mod consul;
|
||||||
mod consul_actor;
|
mod consul_actor;
|
||||||
mod diplonat;
|
mod diplonat;
|
||||||
mod environment;
|
|
||||||
mod fw;
|
mod fw;
|
||||||
mod fw_actor;
|
mod fw_actor;
|
||||||
mod igd_actor;
|
mod igd_actor;
|
||||||
|
|
Loading…
Reference in a new issue