diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/daemon_dbus.rs | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/src/daemon_dbus.rs b/src/daemon_dbus.rs index 5615bab..4e3d328 100644 --- a/src/daemon_dbus.rs +++ b/src/daemon_dbus.rs @@ -35,6 +35,9 @@ pub async fn start_dbus( let state_add_peer = state.clone(); let config_path_add_peer = config_path.clone(); + let state_rem_peer = state.clone(); + let config_path_rem_peer = config_path.clone(); + b.method_with_cr_async( "AddNetwork", ("name", "key", "ip", "listen_port", "maesch_port"), @@ -90,6 +93,20 @@ pub async fn start_dbus( ) }, ); + b.method_with_cr_async( + "RemovePeer", + ("network", "key"), + ("success",), + move |ctx, _, args: (String, String)| { + debug!("Received RemoveNetwork"); + handle_remove_peer( + ctx, + state_rem_peer.clone(), + config_path_rem_peer.clone(), + args, + ) + }, + ); }); cr.insert("/de/a/maesch", &[if_token], ()); @@ -117,6 +134,56 @@ pub async fn start_dbus( Ok((c, receive_token)) } +async fn handle_remove_peer( + mut ctx: Context, + state: Arc<RwLock<State>>, + config_path: PathBuf, + (nw_name, key): (String, String), +) -> PhantomData<(bool,)> { + let key = match Key::from_str(&key) { + Ok(k) => k, + Err(e) => { + warn!("RemovePeer with bad key: {e}"); + return ctx.reply(Err(MethodErr::invalid_arg(&e))); + } + }; + + let mut state_rw_guard = state.write().await; + + if let Some(nw) = state_rw_guard.conf.networks.get_mut(&nw_name) { + if let Some(_) = nw.peers.remove(&key) { + let wg_api = &state_rw_guard + .nw_handles + .get(&nw_name) + .expect("state.conf.networks and state.nw_handles desynced") + .0; + match wg_api.remove_peer(&key) { + Ok(_) => info!("Removed peer: {key}"), + Err(e) => { + error!("Couldn't remove peer from network: {e}"); + return ctx.reply(Err(MethodErr::failed(&e))); + } + } + } else { + warn!("Tried to remove non-existent peer"); + return ctx.reply(Err(MethodErr::invalid_arg("bad peer"))); + } + } else { + warn!("Tried to remove peer from non-existent network: {nw_name}"); + return ctx.reply(Err(MethodErr::invalid_arg("bad network"))); + } + + match write_config(&state_rw_guard.conf, &config_path) { + Ok(_) => info!("Synced config"), + Err(e) => { + error!("Couldn't sync config: {e}"); + return ctx.reply(Err(MethodErr::failed(&e))); + } + }; + + ctx.reply(Ok((true,))) +} + async fn handle_add_peer( mut ctx: Context, state: Arc<RwLock<State>>, @@ -262,7 +329,7 @@ async fn handle_add_peer( } }; - return ctx.reply(Ok((true,))); + ctx.reply(Ok((true,))) } async fn handle_remove_network( |