aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Garrelou <simon@sixfoisneuf.fr>2023-04-27 20:37:40 +0200
committerSimon Garrelou <simon@sixfoisneuf.fr>2023-04-27 20:37:40 +0200
commitdaae1688ee08c8ac3854f0b636b8863662f09152 (patch)
tree1a07c9a76f8c739c9003721cca7a9f5c8c9bde2c
parent8441ab0e15041f0411075fd705405db1f61bb90d (diff)
downloadwgmgr-daae1688ee08c8ac3854f0b636b8863662f09152.tar.gz
wgmgr-daae1688ee08c8ac3854f0b636b8863662f09152.zip
Use Result<> for Peer::new()
-rw-r--r--src/main.rs2
-rw-r--r--src/wg/peer.rs28
2 files changed, 13 insertions, 17 deletions
diff --git a/src/main.rs b/src/main.rs
index 0b652bb..25ef860 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -227,7 +227,7 @@ fn do_add(conf: &mut WireguardConfig, conf_path: String, peer_name: &String, ip:
227 None => {} 227 None => {}
228 } 228 }
229 229
230 let p = wg::peer::Peer::new(peer_name.clone(), ip); 230 let p = wg::peer::Peer::new(peer_name.clone(), ip)?;
231 conf.peers.push(p); 231 conf.peers.push(p);
232 232
233 let mut f = File::create(conf_path)?; 233 let mut f = File::create(conf_path)?;
diff --git a/src/wg/peer.rs b/src/wg/peer.rs
index 17722e5..c374ca0 100644
--- a/src/wg/peer.rs
+++ b/src/wg/peer.rs
@@ -6,7 +6,7 @@ use std::path::{Path};
6use std::process::{Command, Stdio}; 6use std::process::{Command, Stdio};
7use std::io::Write as ioWrite; 7use std::io::Write as ioWrite;
8 8
9use anyhow::Result; 9use anyhow::{Context, Result};
10 10
11use super::config::WireguardConfig; 11use super::config::WireguardConfig;
12 12
@@ -18,30 +18,30 @@ pub struct Peer {
18} 18}
19 19
20impl Peer { 20impl Peer {
21 pub fn new(name: String, ip: Ipv4Addr) -> Peer { 21 pub fn new(name: String, ip: Ipv4Addr) -> Result<Peer> {
22 let private = Command::new("wg") 22 let private = Command::new("wg")
23 .arg("genkey") 23 .arg("genkey")
24 .output() 24 .output()
25 .expect("Could not generate a private key. Is 'wg' installed?"); 25 .context("Could not generate a private key. Is 'wg' installed?")?;
26 26
27 let mut public = Command::new("wg") 27 let mut public = Command::new("wg")
28 .arg("pubkey") 28 .arg("pubkey")
29 .stdin(Stdio::piped()) 29 .stdin(Stdio::piped())
30 .stdout(Stdio::piped()) 30 .stdout(Stdio::piped())
31 .spawn() 31 .spawn()
32 .expect("Could not run 'wg'"); 32 .context("Could not run 'wg'")?;
33 33
34 let private_clone = private.stdout.clone(); 34 let private_clone = private.stdout.clone();
35 35
36 let mut stdin = public.stdin.take().expect("could not open wg stdin"); 36 let mut stdin = public.stdin.take().context("could not open wg stdin")?;
37 std::thread::spawn(move || { 37 std::thread::spawn(move || {
38 stdin.write_all(&private.stdout).unwrap(); 38 stdin.write_all(&private.stdout).unwrap();
39 }); 39 });
40 40
41 let output = public.wait_with_output().expect("could not read from 'wg pubkey'"); 41 let output = public.wait_with_output().context("could not read from 'wg pubkey'")?;
42 42
43 let private_key = String::from_utf8(private_clone).expect("could not decode private key"); 43 let private_key = String::from_utf8(private_clone).context("could not decode private key")?;
44 let public_key = String::from_utf8(output.stdout).expect("could not decode public key"); 44 let public_key = String::from_utf8(output.stdout).context("could not decode public key")?;
45 45
46 let private_key = private_key.trim(); 46 let private_key = private_key.trim();
47 let public_key = public_key.trim(); 47 let public_key = public_key.trim();
@@ -52,15 +52,11 @@ impl Peer {
52 public_key: String::from(public_key) 52 public_key: String::from(public_key)
53 }; 53 };
54 54
55 let mut f = match File::create(p.private_key_path()) { 55 let mut f = File::create(p.private_key_path()).context(format!("could not create file {}", p.private_key_path()))?;
56 Ok(f) => f,
57 Err(e) => {
58 panic!("Error creating private key file: {}", e);
59 }
60 };
61 write!(f, "{}", private_key).expect("could not write private key to file"); 56 write!(f, "{}", private_key).expect("could not write private key to file");
62 57
63 p 58
59 Ok(p)
64 } 60 }
65 61
66 fn private_key_folder(&self) -> String { 62 fn private_key_folder(&self) -> String {
@@ -147,4 +143,4 @@ impl PartialEq for Peer {
147 fn ne(&self, other: &Self) -> bool { 143 fn ne(&self, other: &Self) -> bool {
148 return self.ip.ne(&other.ip) 144 return self.ip.ne(&other.ip)
149 } 145 }
150} \ No newline at end of file 146}