diff options
author | Simon Garrelou <simon@sixfoisneuf.fr> | 2023-05-24 16:02:09 +0200 |
---|---|---|
committer | Simon Garrelou <simon@sixfoisneuf.fr> | 2023-05-24 16:02:09 +0200 |
commit | e67c54d731c3a0ae95948421dea71ab5e78526a7 (patch) | |
tree | 900653c5764de1a7071065a4bd26503d1ecd20da | |
parent | 43fea8e62bd7e678a56436d1702eed01c79f4767 (diff) | |
download | wgmgr-e67c54d731c3a0ae95948421dea71ab5e78526a7.tar.gz wgmgr-e67c54d731c3a0ae95948421dea71ab5e78526a7.zip |
conf -> wg_conf
-rw-r--r-- | src/main.rs | 66 |
1 files changed, 33 insertions, 33 deletions
diff --git a/src/main.rs b/src/main.rs index 05b05a2..6bc7e85 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -18,7 +18,7 @@ fn main() { | |||
18 | let matches = command!() | 18 | let matches = command!() |
19 | .arg( | 19 | .arg( |
20 | arg!( | 20 | arg!( |
21 | -c --config <CONFIG> "Configuration file name" | 21 | -w --wg-config <WGCONFIG> "Wireguard configuration file name" |
22 | ) | 22 | ) |
23 | .required(false) | 23 | .required(false) |
24 | ) | 24 | ) |
@@ -80,18 +80,18 @@ fn main() { | |||
80 | ) | 80 | ) |
81 | .get_matches(); | 81 | .get_matches(); |
82 | 82 | ||
83 | let conf_path = match find_config_file(&matches) { | 83 | let wg_conf_path = match find_wg_config_file(&matches) { |
84 | Ok(s) => s, | 84 | Ok(s) => s, |
85 | Err(e) => { | 85 | Err(e) => { |
86 | eprintln!("Configuration file error: {}", e); | 86 | eprintln!("Wireguard configuration file error: {}", e); |
87 | exit(1); | 87 | exit(1); |
88 | } | 88 | } |
89 | }; | 89 | }; |
90 | 90 | ||
91 | let mut conf = match wg::config::WireguardConfig::new(&conf_path) { | 91 | let mut wg_conf = match wg::config::WireguardConfig::new(&wg_conf_path) { |
92 | Ok(c) => c, | 92 | Ok(c) => c, |
93 | Err(e) => { | 93 | Err(e) => { |
94 | eprintln!("Error loading the configuration file '{}'", conf_path); | 94 | eprintln!("Error loading the Wireguard configuration file '{}'", wg_conf_path); |
95 | eprintln!("{}", e); | 95 | eprintln!("{}", e); |
96 | exit(1); | 96 | exit(1); |
97 | } | 97 | } |
@@ -99,7 +99,7 @@ fn main() { | |||
99 | 99 | ||
100 | match matches.subcommand() { | 100 | match matches.subcommand() { |
101 | Some(("ls", _)) => { | 101 | Some(("ls", _)) => { |
102 | do_list(&conf); | 102 | do_list(&wg_conf); |
103 | } | 103 | } |
104 | 104 | ||
105 | Some(("config", args)) => { | 105 | Some(("config", args)) => { |
@@ -119,7 +119,7 @@ fn main() { | |||
119 | None => false | 119 | None => false |
120 | }; | 120 | }; |
121 | 121 | ||
122 | if let Err(e) = do_config(&conf, peer_name.to_string(), is_full) { | 122 | if let Err(e) = do_config(&wg_conf, peer_name.to_string(), is_full) { |
123 | eprintln!("Error generating configuration for {}: {}", peer_name.to_string(), e); | 123 | eprintln!("Error generating configuration for {}: {}", peer_name.to_string(), e); |
124 | exit(1); | 124 | exit(1); |
125 | } | 125 | } |
@@ -130,7 +130,7 @@ fn main() { | |||
130 | 130 | ||
131 | Some(("add", args)) => { | 131 | Some(("add", args)) => { |
132 | let new_name = args.get_one::<String>("NAME").unwrap().to_string(); | 132 | let new_name = args.get_one::<String>("NAME").unwrap().to_string(); |
133 | if let Err(e) = do_add(&mut conf, conf_path, &new_name, args.get_one::<String>("ip")) { | 133 | if let Err(e) = do_add(&mut wg_conf, wg_conf_path, &new_name, args.get_one::<String>("ip")) { |
134 | eprintln!("Error adding peer: {}", e); | 134 | eprintln!("Error adding peer: {}", e); |
135 | exit(1); | 135 | exit(1); |
136 | } | 136 | } |
@@ -141,7 +141,7 @@ fn main() { | |||
141 | 141 | ||
142 | Some(("rm", args)) => { | 142 | Some(("rm", args)) => { |
143 | let rm_name = args.get_one::<String>("NAME").unwrap().to_string(); | 143 | let rm_name = args.get_one::<String>("NAME").unwrap().to_string(); |
144 | if let Err(e) = do_rm(&mut conf, conf_path, &rm_name) { | 144 | if let Err(e) = do_rm(&mut wg_conf, wg_conf_path, &rm_name) { |
145 | eprintln!("Error removing peer: {}", e); | 145 | eprintln!("Error removing peer: {}", e); |
146 | exit(1); | 146 | exit(1); |
147 | } | 147 | } |
@@ -150,7 +150,7 @@ fn main() { | |||
150 | }, | 150 | }, |
151 | 151 | ||
152 | Some(("wg", _)) => { | 152 | Some(("wg", _)) => { |
153 | if let Err(e) = do_wg(&conf) { | 153 | if let Err(e) = do_wg(&wg_conf) { |
154 | eprintln!("{}", e); | 154 | eprintln!("{}", e); |
155 | exit(1); | 155 | exit(1); |
156 | } | 156 | } |
@@ -163,9 +163,9 @@ fn main() { | |||
163 | 163 | ||
164 | } | 164 | } |
165 | 165 | ||
166 | fn find_config_file(matches: &ArgMatches) -> Result<String> { | 166 | fn find_wg_config_file(matches: &ArgMatches) -> Result<String> { |
167 | // Top priority goes to the command-line argument | 167 | // Top priority goes to the command-line argument |
168 | match matches.get_one::<String>("config") { | 168 | match matches.get_one::<String>("wg-config") { |
169 | Some(s) => { | 169 | Some(s) => { |
170 | if s.starts_with("/") { | 170 | if s.starts_with("/") { |
171 | return Ok(s.to_string()) | 171 | return Ok(s.to_string()) |
@@ -220,39 +220,39 @@ fn find_config_file(matches: &ArgMatches) -> Result<String> { | |||
220 | } | 220 | } |
221 | } | 221 | } |
222 | 222 | ||
223 | fn do_list(conf: &wg::config::WireguardConfig) { | 223 | fn do_list(wg_conf: &wg::config::WireguardConfig) { |
224 | let mut max_length = 0; | 224 | let mut max_length = 0; |
225 | for p in conf.peers.iter() { | 225 | for p in wg_conf.peers.iter() { |
226 | if p.name.len() > max_length { | 226 | if p.name.len() > max_length { |
227 | max_length = p.name.len(); | 227 | max_length = p.name.len(); |
228 | } | 228 | } |
229 | } | 229 | } |
230 | 230 | ||
231 | for p in conf.peers.iter() { | 231 | for p in wg_conf.peers.iter() { |
232 | println!("{:max_length$} | {}", p.name, p.ip); | 232 | println!("{:max_length$} | {}", p.name, p.ip); |
233 | } | 233 | } |
234 | } | 234 | } |
235 | 235 | ||
236 | fn do_config(conf: &wg::config::WireguardConfig, peer_name: String, is_full: bool) -> Result<()> { | 236 | fn do_config(wg_conf: &wg::config::WireguardConfig, peer_name: String, is_full: bool) -> Result<()> { |
237 | let peer = match conf.get_peer(peer_name.as_str()) { | 237 | let peer = match wg_conf.get_peer(peer_name.as_str()) { |
238 | Some(p) => p, | 238 | Some(p) => p, |
239 | None => { | 239 | None => { |
240 | return Err(anyhow!("No such peer: {}", peer_name)); | 240 | return Err(anyhow!("No such peer: {}", peer_name)); |
241 | } | 241 | } |
242 | }; | 242 | }; |
243 | 243 | ||
244 | println!("{}", peer.gen_config(conf, is_full)?); | 244 | println!("{}", peer.gen_config(wg_conf, is_full)?); |
245 | 245 | ||
246 | Ok(()) | 246 | Ok(()) |
247 | } | 247 | } |
248 | 248 | ||
249 | fn do_add(conf: &mut WireguardConfig, conf_path: String, peer_name: &String, ip: Option<&String>) -> Result<()> { | 249 | fn do_add(wg_conf: &mut WireguardConfig, wg_conf_path: String, peer_name: &String, ip: Option<&String>) -> Result<()> { |
250 | let ip = match ip { | 250 | let ip = match ip { |
251 | Some(s) => { | 251 | Some(s) => { |
252 | Ipv4Addr::from_str(s.as_str())? | 252 | Ipv4Addr::from_str(s.as_str())? |
253 | }, | 253 | }, |
254 | None => { | 254 | None => { |
255 | match conf.next_free_ip() { | 255 | match wg_conf.next_free_ip() { |
256 | Ok(i) => i, | 256 | Ok(i) => i, |
257 | Err(e) => { | 257 | Err(e) => { |
258 | return Err(e); | 258 | return Err(e); |
@@ -261,26 +261,26 @@ fn do_add(conf: &mut WireguardConfig, conf_path: String, peer_name: &String, ip: | |||
261 | } | 261 | } |
262 | }; | 262 | }; |
263 | 263 | ||
264 | match conf.get_peer(peer_name.as_str()) { | 264 | match wg_conf.get_peer(peer_name.as_str()) { |
265 | Some(_) => { return Err(anyhow!("There is already a peer named {}", peer_name)); }, | 265 | Some(_) => { return Err(anyhow!("There is already a peer named {}", peer_name)); }, |
266 | None => {} | 266 | None => {} |
267 | } | 267 | } |
268 | 268 | ||
269 | let p = wg::peer::Peer::new(peer_name.clone(), ip)?; | 269 | let p = wg::peer::Peer::new(peer_name.clone(), ip)?; |
270 | conf.peers.push(p); | 270 | wg_conf.peers.push(p); |
271 | 271 | ||
272 | let mut f = File::create(conf_path)?; | 272 | let mut f = File::create(wg_conf_path)?; |
273 | write!(f, "{}", conf.gen_config()?)?; | 273 | write!(f, "{}", wg_conf.gen_config()?)?; |
274 | 274 | ||
275 | Ok(()) | 275 | Ok(()) |
276 | } | 276 | } |
277 | 277 | ||
278 | fn do_rm(conf: &mut WireguardConfig, conf_path: String, peer_name: &String) -> Result<()> { | 278 | fn do_rm(wg_conf: &mut WireguardConfig, wg_conf_path: String, peer_name: &String) -> Result<()> { |
279 | let mut del_index = 0; | 279 | let mut del_index = 0; |
280 | let mut found = false; | 280 | let mut found = false; |
281 | let mut pk_path = String::new(); | 281 | let mut pk_path = String::new(); |
282 | 282 | ||
283 | for (i, peer) in conf.peers.iter().enumerate() { | 283 | for (i, peer) in wg_conf.peers.iter().enumerate() { |
284 | if &peer.name == peer_name { | 284 | if &peer.name == peer_name { |
285 | del_index = i; | 285 | del_index = i; |
286 | found = true; | 286 | found = true; |
@@ -293,19 +293,19 @@ fn do_rm(conf: &mut WireguardConfig, conf_path: String, peer_name: &String) -> R | |||
293 | return Err(anyhow!("No such peer: {}", peer_name)); | 293 | return Err(anyhow!("No such peer: {}", peer_name)); |
294 | } | 294 | } |
295 | 295 | ||
296 | conf.peers.remove(del_index); | 296 | wg_conf.peers.remove(del_index); |
297 | 297 | ||
298 | let mut f = File::create(conf_path.clone()).context(format!("error opening configuration file: {}", conf_path))?; | 298 | let mut f = File::create(wg_conf_path.clone()).context(format!("error opening configuration file: {}", wg_conf_path))?; |
299 | let data = conf.gen_config().context("error generating configuration")?; | 299 | let data = wg_conf.gen_config().context("error generating configuration")?; |
300 | 300 | ||
301 | f.write_all(data.as_bytes()).context(format!("error writing to file: {}", conf_path))?; | 301 | f.write_all(data.as_bytes()).context(format!("error writing to file: {}", wg_conf_path))?; |
302 | 302 | ||
303 | std::fs::remove_file(pk_path).context(format!("could not remove file: {}", conf_path))?; | 303 | std::fs::remove_file(pk_path).context(format!("could not remove file: {}", wg_conf_path))?; |
304 | 304 | ||
305 | Ok(()) | 305 | Ok(()) |
306 | } | 306 | } |
307 | 307 | ||
308 | fn do_wg(conf: &WireguardConfig) -> Result<()> { | 308 | fn do_wg(wg_conf: &WireguardConfig) -> Result<()> { |
309 | let wg = processCommand::new("wg") | 309 | let wg = processCommand::new("wg") |
310 | .env("WG_COLOR_MODE", "always") | 310 | .env("WG_COLOR_MODE", "always") |
311 | .output() | 311 | .output() |
@@ -318,7 +318,7 @@ fn do_wg(conf: &WireguardConfig) -> Result<()> { | |||
318 | 318 | ||
319 | let mut out = String::from_utf8(wg.stdout).context("error parsing stdout")?; | 319 | let mut out = String::from_utf8(wg.stdout).context("error parsing stdout")?; |
320 | 320 | ||
321 | for peer in conf.peers.iter() { | 321 | for peer in wg_conf.peers.iter() { |
322 | out = out.replace(peer.public_key.as_str(), peer.name.as_str()); | 322 | out = out.replace(peer.public_key.as_str(), peer.name.as_str()); |
323 | } | 323 | } |
324 | 324 | ||