aboutsummaryrefslogtreecommitdiff
path: root/src/rm.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/rm.rs')
-rw-r--r--src/rm.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/rm.rs b/src/rm.rs
new file mode 100644
index 0000000..61880d2
--- /dev/null
+++ b/src/rm.rs
@@ -0,0 +1,36 @@
1use std::{fs::File, io::Write};
2
3use anyhow::{anyhow, Result, Context};
4
5use crate::wg::config::WireguardConfig;
6
7pub fn run(wg_conf: &mut WireguardConfig, wg_conf_path: String, peer_name: &String) -> Result<()> {
8 let mut del_index = 0;
9 let mut found = false;
10 let mut pk_path = String::new();
11
12 for (i, peer) in wg_conf.peers.iter().enumerate() {
13 if &peer.name == peer_name {
14 del_index = i;
15 found = true;
16 pk_path = peer.private_key_path().context("could not get private key path")?;
17 break;
18 }
19 }
20
21 if !found {
22 return Err(anyhow!("No such peer: {}", peer_name));
23 }
24
25 wg_conf.peers.remove(del_index);
26
27 let mut f = File::create(wg_conf_path.clone()).context(format!("error opening configuration file: {}", wg_conf_path))?;
28 let data = wg_conf.gen_config().context("error generating configuration")?;
29
30 f.write_all(data.as_bytes()).context(format!("error writing to file: {}", wg_conf_path))?;
31
32 std::fs::remove_file(pk_path).context(format!("could not remove file: {}", wg_conf_path))?;
33
34 Ok(())
35}
36