summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/daemon.rs104
1 files changed, 69 insertions, 35 deletions
diff --git a/src/daemon.rs b/src/daemon.rs
index ab84f03..e1404a0 100644
--- a/src/daemon.rs
+++ b/src/daemon.rs
@@ -147,7 +147,14 @@ pub fn daemon() -> Result<(), DaemonError> {
};
for (name, nw) in &config.networks {
- add_network(&mut state, name.clone(), nw.privkey.clone(), nw.address.clone(), nw.listen_port, &nw.peers)?;
+ add_network(
+ &mut state,
+ name.clone(),
+ nw.privkey.clone(),
+ nw.address.clone(),
+ nw.listen_port,
+ &nw.peers,
+ )?;
info!("loaded configuration for {0}", name);
}
info!("loaded all existing configurations");
@@ -161,7 +168,14 @@ pub fn daemon() -> Result<(), DaemonError> {
Ok(())
}
-fn add_network(state: &mut State, name: String, privkey: String, address: String, port: u16, peers: &HashMap<Key, PeerConfig>) -> Result<(), DaemonError> {
+fn add_network(
+ state: &mut State,
+ name: String,
+ privkey: String,
+ address: String,
+ port: u16,
+ peers: &HashMap<Key, PeerConfig>,
+) -> Result<(), DaemonError> {
let wg = WGApi::new(name.clone(), false)?;
let defguard_peers = peers
.iter()
@@ -249,46 +263,66 @@ async fn run_listeners(state: Arc<RwLock<State>>) -> Result<(), DaemonError> {
let state_ref = state.clone();
let if_token = cr.register("de.69owo.maesch", move |b| {
- let state_ref = state_ref.clone();
-
b.signal::<(String, String), _>("Proposal", ("network", "peer_data"));
- b.method_with_cr_async("AddNetwork",
- ("name", "key", "ip", "listen_port", "maesch_port"),
- ("success",), move |mut ctx, cr, (name, may_key, may_ip, may_lp, may_mp): (String, String, String, u16, u16)| async move
- {
- // NOTE: this is kinda stupid: we convert to a string later anyways, as thats what
- // defguard_wg takes...
- let key = Key::new(match may_key.as_str() {
- "" => rand::thread_rng().gen(),
- _ => match BASE64_STANDARD.decode(may_key) {
- Ok(v) if v.len() == 32 => v.try_into().unwrap(),
- _ => return ctx.reply(Err(MethodErr::invalid_arg("bad key")))
- },
- });
+ b.method_with_cr_async(
+ "AddNetwork",
+ ("name", "key", "ip", "listen_port", "maesch_port"),
+ ("success",),
+ move |mut ctx,
+ cr,
+ (name, may_key, may_ip, may_lp, may_mp): (
+ String,
+ String,
+ String,
+ u16,
+ u16,
+ )| {
+ let state_ref = state_ref.clone();
+ async move {
+ // NOTE: this is kinda stupid: we convert to a string later anyways, as thats what
+ // defguard_wg takes...
+ let key = Key::new(match may_key.as_str() {
+ "" => rand::thread_rng().gen(),
+ _ => match BASE64_STANDARD.decode(may_key) {
+ Ok(v) if v.len() == 32 => v.try_into().unwrap(),
+ _ => return ctx.reply(Err(MethodErr::invalid_arg("bad key"))),
+ },
+ });
- // we store the ip as the original string, but should validate it regardless
- let (ip, ip_string) = match may_ip.as_str() {
- "" => todo!(),
- _ => match IpAddrMask::from_str(&may_ip) {
- Err(_) => return ctx.reply(Err(MethodErr::invalid_arg("invalid ip"))),
- Ok(ip_mask) => (ip_mask.ip, may_ip),
- },
- };
+ // we store the ip as the original string, but should validate it regardless
+ let (ip, ip_string) = match may_ip.as_str() {
+ "" => todo!(),
+ _ => match IpAddrMask::from_str(&may_ip) {
+ Err(_) => {
+ return ctx.reply(Err(MethodErr::invalid_arg("invalid ip")))
+ }
+ Ok(ip_mask) => (ip_mask.ip, may_ip),
+ },
+ };
- let lp = if may_lp == 0 { 25565 } else { may_lp };
- let mp = if may_mp == 0 { 51820 } else { may_mp };
+ let lp = if may_lp == 0 { 25565 } else { may_lp };
+ let mp = if may_mp == 0 { 51820 } else { may_mp };
- let mut st_wr = state_ref.write().await;
+ let mut st_wr = state_ref.write().await;
- match add_network(&mut st_wr, name, key.to_string(), ip_string, lp, &HashMap::new()) {
- Ok(_) => (),
- Err(e) => return ctx.reply(Err(MethodErr::failed(&e))),
- };
+ match add_network(
+ &mut st_wr,
+ name,
+ key.to_string(),
+ ip_string,
+ lp,
+ &HashMap::new(),
+ ) {
+ Ok(_) => (),
+ Err(e) => return ctx.reply(Err(MethodErr::failed(&e))),
+ };
- //let listener = TcpListener::bind((ip, mp)).await?;
+ //let listener = TcpListener::bind((ip, mp)).await?;
- ctx.reply(Ok((true,)))
- });
+ ctx.reply(Ok((true,)))
+ }
+ },
+ );
});
cr.insert("/de/69owo/maesch", &[if_token], state.clone());