blob: 61880d235039cf3f09fa8aa29957b0767f1009f3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
use std::{fs::File, io::Write};
use anyhow::{anyhow, Result, Context};
use crate::wg::config::WireguardConfig;
pub fn run(wg_conf: &mut WireguardConfig, wg_conf_path: String, peer_name: &String) -> Result<()> {
let mut del_index = 0;
let mut found = false;
let mut pk_path = String::new();
for (i, peer) in wg_conf.peers.iter().enumerate() {
if &peer.name == peer_name {
del_index = i;
found = true;
pk_path = peer.private_key_path().context("could not get private key path")?;
break;
}
}
if !found {
return Err(anyhow!("No such peer: {}", peer_name));
}
wg_conf.peers.remove(del_index);
let mut f = File::create(wg_conf_path.clone()).context(format!("error opening configuration file: {}", wg_conf_path))?;
let data = wg_conf.gen_config().context("error generating configuration")?;
f.write_all(data.as_bytes()).context(format!("error writing to file: {}", wg_conf_path))?;
std::fs::remove_file(pk_path).context(format!("could not remove file: {}", wg_conf_path))?;
Ok(())
}
|