aboutsummaryrefslogtreecommitdiff
path: root/src/add.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/add.rs')
-rw-r--r--src/add.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/add.rs b/src/add.rs
new file mode 100644
index 0000000..8ccf53b
--- /dev/null
+++ b/src/add.rs
@@ -0,0 +1,33 @@
1use std::{net::Ipv4Addr, str::FromStr, fs::File};
2use std::io::Write;
3use anyhow::{anyhow, Result};
4use crate::wg::{config::WireguardConfig, peer::Peer};
5
6pub fn run(wg_conf: &mut WireguardConfig, wg_conf_path: String, peer_name: &String, ip: Option<&String>) -> Result<()> {
7 let ip = match ip {
8 Some(s) => {
9 Ipv4Addr::from_str(s.as_str())?
10 },
11 None => {
12 match wg_conf.next_free_ip() {
13 Ok(i) => i,
14 Err(e) => {
15 return Err(e);
16 }
17 }
18 }
19 };
20
21 match wg_conf.get_peer(peer_name.as_str()) {
22 Some(_) => { return Err(anyhow!("There is already a peer named {}", peer_name)); },
23 None => {}
24 }
25
26 let p = Peer::new(peer_name.clone(), ip)?;
27 wg_conf.peers.push(p);
28
29 let mut f = File::create(wg_conf_path)?;
30 write!(f, "{}", wg_conf.gen_config()?)?;
31
32 Ok(())
33} \ No newline at end of file