From d5fa15e5d067e0c05ab0bd2b40af1dfaa80aedde Mon Sep 17 00:00:00 2001 From: Simon Garrelou Date: Thu, 25 May 2023 10:31:49 +0200 Subject: Simplify Wireguard config file discovery --- src/configuration.rs | 12 +++---- src/main.rs | 99 +++++----------------------------------------------- 2 files changed, 15 insertions(+), 96 deletions(-) diff --git a/src/configuration.rs b/src/configuration.rs index 173b8ff..839db24 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -5,24 +5,24 @@ use std::fs::read_to_string; #[derive(Deserialize)] pub struct Configuration { pub endpoint: String, + pub wgconf: String, pub port: Option, pub dns: Option, - pub wgconf: Option, } -pub fn find_configuration_file(argument: Option<&String>) -> Result { +pub fn find_configuration_file(argument: Option<&String>) -> Result { match argument { Some(p) => { if let Ok(t) = read_to_string(p) { - let _: Configuration = toml::from_str(&t).context("parsing configuration file")?; - return Ok(p.clone()) + let c: Configuration = toml::from_str(&t).context("parsing configuration file")?; + return Ok(c) } }, None => { // Try /etc/wgmgr.toml if let Ok(t) = read_to_string("/etc/wgmgr.toml") { - let _: Configuration = toml::from_str(&t).context("parsing /etc/wgmgr.toml")?; - return Ok(String::from("/etc/wgmgr.toml")) + let c: Configuration = toml::from_str(&t).context("parsing /etc/wgmgr.toml")?; + return Ok(c) } }, }; diff --git a/src/main.rs b/src/main.rs index 58c87e8..3f82d43 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ -use std::ffi::OsStr; -use std::fs::{read_to_string, self, File}; +use std::fs::File; use std::net::Ipv4Addr; use std::str::FromStr; use std::{env, process::exit}; @@ -7,9 +6,7 @@ use std::io::Write; use std::process::Command as processCommand; use anyhow::{Result, anyhow, Context}; -use clap::{command, ArgMatches}; -use clap::Command; -use clap::arg; +use clap::{arg, command, Command}; use wg::config::WireguardConfig; mod wg; @@ -17,12 +14,6 @@ mod configuration; fn main() { let matches = command!() - .arg( - arg!( - -w --wgconfig "Wireguard configuration file name" - ) - .required(false) - ) .arg( arg!( -c --config "wgmgr configuration file path" @@ -89,8 +80,8 @@ fn main() { // Find the configuration file for wgmgr - let wgmgr_conf_path = match configuration::find_configuration_file(matches.get_one::("config")) { - Ok(s) => s, + let wgmgr_conf = match configuration::find_configuration_file(matches.get_one::("config")) { + Ok(c) => c, Err(e) => { eprintln!("Could not find wgmgr configuration file. Place it at /etc/wgmgr.toml or use the --config flag."); eprintln!("{:?}", e); @@ -98,21 +89,12 @@ fn main() { } }; - let wgmgr_conf: configuration::Configuration = toml::from_str(&read_to_string(wgmgr_conf_path).unwrap()).unwrap(); - // Find the Wireguard configuration file - let wg_conf_path = match find_wg_config_file(&matches, &wgmgr_conf.wgconf) { - Ok(s) => s, - Err(e) => { - eprintln!("Wireguard configuration file error: {:?}", e); - exit(1); - } - }; - - let mut wg_conf = match wg::config::WireguardConfig::new(&wg_conf_path) { + // Load the Wireguard configuration file + let mut wg_conf = match wg::config::WireguardConfig::new(&wgmgr_conf.wgconf) { Ok(c) => c, Err(e) => { - eprintln!("Error loading the Wireguard configuration file '{}'.", wg_conf_path); + eprintln!("Error loading the Wireguard configuration file '{}'.", wgmgr_conf.wgconf); eprintln!("{:?}", e); exit(1); } @@ -151,7 +133,7 @@ fn main() { Some(("add", args)) => { let new_name = args.get_one::("NAME").unwrap().to_string(); - if let Err(e) = do_add(&mut wg_conf, wg_conf_path, &new_name, args.get_one::("ip")) { + if let Err(e) = do_add(&mut wg_conf, wgmgr_conf.wgconf, &new_name, args.get_one::("ip")) { eprintln!("Error adding peer: {:?}", e); exit(1); } @@ -162,7 +144,7 @@ fn main() { Some(("rm", args)) => { let rm_name = args.get_one::("NAME").unwrap().to_string(); - if let Err(e) = do_rm(&mut wg_conf, wg_conf_path, &rm_name) { + if let Err(e) = do_rm(&mut wg_conf, wgmgr_conf.wgconf, &rm_name) { eprintln!("Error removing peer: {:?}", e); exit(1); } @@ -184,69 +166,6 @@ fn main() { } -fn find_wg_config_file(matches: &ArgMatches, wgconf: &Option) -> Result { - // Top priority goes to the command-line argument - match matches.get_one::("wgconfig") { - Some(s) => { - if s.starts_with("/") { - return Ok(s.to_string()) - } - else { - return Ok(format!("/etc/wireguard/{}", s)) - } - }, - None => {} - } - - // Then, if the environment variable exists, we take it - match env::var("WG_CONF") { - Ok(s) => return Ok(s), - Err(_) => {} - }; - - // Then, is there is a "wgconf" in the wgmgr configuration file, we take that - match wgconf { - Some(s) => return Ok(s.clone()), - None => {} - }; - - // Otherwise, we will first try a simple "wg0.conf" - match read_to_string("/etc/wireguard/wg0.conf") { - Ok(_) => return Ok(String::from_str("/etc/wireguard/wg0.conf").unwrap()), - Err(_) => {} - } - - // Finally, we can try to see if there is a single ".conf" file in /etc/wireguard - match fs::read_dir("/etc/wireguard/") { - Ok(d) => { - let conf_files: Vec = d.filter(|e| { - if let Ok(e) = e { - if e.file_type().unwrap().is_dir() { - return false; - } - - match e.path().extension().and_then(OsStr::to_str) { - Some("conf") => { return true} , - _ => {return false}, - } - } - - return false; - }).map(|e| { - return e.unwrap(); - }).collect(); - - if conf_files.len() == 1 { - return Ok(String::from_str(conf_files[0].path().to_str().unwrap()).unwrap()); - } - else { - return Err(anyhow!("Could not determine the path to your WireGuard configuration file. Set the WG_CONF environment variable, or pass the '--config' parameter.")) - } - }, - Err(e) => return Err(anyhow!("Error listing /etc/wireguard/: {}", e)) - } -} - fn do_list(wg_conf: &wg::config::WireguardConfig) { let mut max_length = 0; for p in wg_conf.peers.iter() { -- cgit v1.2.3