diff options
author | Simon Garrelou <simon@sixfoisneuf.fr> | 2023-05-24 21:31:32 +0200 |
---|---|---|
committer | Simon Garrelou <simon@sixfoisneuf.fr> | 2023-05-24 21:31:32 +0200 |
commit | 0edf4d09a6fca89de7f98b12d24def74a12ca7f3 (patch) | |
tree | 428847fcb4921dd9d81d5d5d947cd26243ea6d12 | |
parent | e67c54d731c3a0ae95948421dea71ab5e78526a7 (diff) | |
download | wgmgr-0edf4d09a6fca89de7f98b12d24def74a12ca7f3.tar.gz wgmgr-0edf4d09a6fca89de7f98b12d24def74a12ca7f3.zip |
Add wgmgr.conf loading
-rw-r--r-- | Cargo.lock | 171 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/configuration.rs | 31 | ||||
-rw-r--r-- | src/main.rs | 55 | ||||
-rw-r--r-- | src/wg/peer.rs | 19 |
5 files changed, 239 insertions, 39 deletions
@@ -4,9 +4,9 @@ version = 3 | |||
4 | 4 | ||
5 | [[package]] | 5 | [[package]] |
6 | name = "anstream" | 6 | name = "anstream" |
7 | version = "0.3.0" | 7 | version = "0.3.2" |
8 | source = "registry+https://github.com/rust-lang/crates.io-index" | 8 | source = "registry+https://github.com/rust-lang/crates.io-index" |
9 | checksum = "9e579a7752471abc2a8268df8b20005e3eadd975f585398f17efcfd8d4927371" | 9 | checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163" |
10 | dependencies = [ | 10 | dependencies = [ |
11 | "anstyle", | 11 | "anstyle", |
12 | "anstyle-parse", | 12 | "anstyle-parse", |
@@ -43,9 +43,9 @@ dependencies = [ | |||
43 | 43 | ||
44 | [[package]] | 44 | [[package]] |
45 | name = "anstyle-wincon" | 45 | name = "anstyle-wincon" |
46 | version = "1.0.0" | 46 | version = "1.0.1" |
47 | source = "registry+https://github.com/rust-lang/crates.io-index" | 47 | source = "registry+https://github.com/rust-lang/crates.io-index" |
48 | checksum = "4bcd8291a340dd8ac70e18878bc4501dd7b4ff970cfa21c207d36ece51ea88fd" | 48 | checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188" |
49 | dependencies = [ | 49 | dependencies = [ |
50 | "anstyle", | 50 | "anstyle", |
51 | "windows-sys", | 51 | "windows-sys", |
@@ -53,9 +53,15 @@ dependencies = [ | |||
53 | 53 | ||
54 | [[package]] | 54 | [[package]] |
55 | name = "anyhow" | 55 | name = "anyhow" |
56 | version = "1.0.70" | 56 | version = "1.0.71" |
57 | source = "registry+https://github.com/rust-lang/crates.io-index" | 57 | source = "registry+https://github.com/rust-lang/crates.io-index" |
58 | checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4" | 58 | checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" |
59 | |||
60 | [[package]] | ||
61 | name = "autocfg" | ||
62 | version = "1.1.0" | ||
63 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
64 | checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" | ||
59 | 65 | ||
60 | [[package]] | 66 | [[package]] |
61 | name = "bitflags" | 67 | name = "bitflags" |
@@ -71,18 +77,18 @@ checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" | |||
71 | 77 | ||
72 | [[package]] | 78 | [[package]] |
73 | name = "clap" | 79 | name = "clap" |
74 | version = "4.2.2" | 80 | version = "4.3.0" |
75 | source = "registry+https://github.com/rust-lang/crates.io-index" | 81 | source = "registry+https://github.com/rust-lang/crates.io-index" |
76 | checksum = "9b802d85aaf3a1cdb02b224ba472ebdea62014fccfcb269b95a4d76443b5ee5a" | 82 | checksum = "93aae7a4192245f70fe75dd9157fc7b4a5bf53e88d30bd4396f7d8f9284d5acc" |
77 | dependencies = [ | 83 | dependencies = [ |
78 | "clap_builder", | 84 | "clap_builder", |
79 | ] | 85 | ] |
80 | 86 | ||
81 | [[package]] | 87 | [[package]] |
82 | name = "clap_builder" | 88 | name = "clap_builder" |
83 | version = "4.2.2" | 89 | version = "4.3.0" |
84 | source = "registry+https://github.com/rust-lang/crates.io-index" | 90 | source = "registry+https://github.com/rust-lang/crates.io-index" |
85 | checksum = "14a1a858f532119338887a4b8e1af9c60de8249cd7bafd68036a489e261e37b6" | 91 | checksum = "4f423e341edefb78c9caba2d9c7f7687d0e72e89df3ce3394554754393ac3990" |
86 | dependencies = [ | 92 | dependencies = [ |
87 | "anstream", | 93 | "anstream", |
88 | "anstyle", | 94 | "anstyle", |
@@ -94,9 +100,9 @@ dependencies = [ | |||
94 | 100 | ||
95 | [[package]] | 101 | [[package]] |
96 | name = "clap_lex" | 102 | name = "clap_lex" |
97 | version = "0.4.1" | 103 | version = "0.5.0" |
98 | source = "registry+https://github.com/rust-lang/crates.io-index" | 104 | source = "registry+https://github.com/rust-lang/crates.io-index" |
99 | checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" | 105 | checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" |
100 | 106 | ||
101 | [[package]] | 107 | [[package]] |
102 | name = "colorchoice" | 108 | name = "colorchoice" |
@@ -126,12 +132,28 @@ dependencies = [ | |||
126 | ] | 132 | ] |
127 | 133 | ||
128 | [[package]] | 134 | [[package]] |
135 | name = "hashbrown" | ||
136 | version = "0.12.3" | ||
137 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
138 | checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" | ||
139 | |||
140 | [[package]] | ||
129 | name = "hermit-abi" | 141 | name = "hermit-abi" |
130 | version = "0.3.1" | 142 | version = "0.3.1" |
131 | source = "registry+https://github.com/rust-lang/crates.io-index" | 143 | source = "registry+https://github.com/rust-lang/crates.io-index" |
132 | checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" | 144 | checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" |
133 | 145 | ||
134 | [[package]] | 146 | [[package]] |
147 | name = "indexmap" | ||
148 | version = "1.9.3" | ||
149 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
150 | checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" | ||
151 | dependencies = [ | ||
152 | "autocfg", | ||
153 | "hashbrown", | ||
154 | ] | ||
155 | |||
156 | [[package]] | ||
135 | name = "io-lifetimes" | 157 | name = "io-lifetimes" |
136 | version = "1.0.10" | 158 | version = "1.0.10" |
137 | source = "registry+https://github.com/rust-lang/crates.io-index" | 159 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -165,15 +187,21 @@ dependencies = [ | |||
165 | 187 | ||
166 | [[package]] | 188 | [[package]] |
167 | name = "libc" | 189 | name = "libc" |
168 | version = "0.2.141" | 190 | version = "0.2.144" |
169 | source = "registry+https://github.com/rust-lang/crates.io-index" | 191 | source = "registry+https://github.com/rust-lang/crates.io-index" |
170 | checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" | 192 | checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" |
171 | 193 | ||
172 | [[package]] | 194 | [[package]] |
173 | name = "linux-raw-sys" | 195 | name = "linux-raw-sys" |
174 | version = "0.3.1" | 196 | version = "0.3.8" |
175 | source = "registry+https://github.com/rust-lang/crates.io-index" | 197 | source = "registry+https://github.com/rust-lang/crates.io-index" |
176 | checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f" | 198 | checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" |
199 | |||
200 | [[package]] | ||
201 | name = "memchr" | ||
202 | version = "2.5.0" | ||
203 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
204 | checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" | ||
177 | 205 | ||
178 | [[package]] | 206 | [[package]] |
179 | name = "once_cell" | 207 | name = "once_cell" |
@@ -182,10 +210,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
182 | checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" | 210 | checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" |
183 | 211 | ||
184 | [[package]] | 212 | [[package]] |
213 | name = "proc-macro2" | ||
214 | version = "1.0.58" | ||
215 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
216 | checksum = "fa1fb82fc0c281dd9671101b66b771ebbe1eaf967b96ac8740dcba4b70005ca8" | ||
217 | dependencies = [ | ||
218 | "unicode-ident", | ||
219 | ] | ||
220 | |||
221 | [[package]] | ||
222 | name = "quote" | ||
223 | version = "1.0.27" | ||
224 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
225 | checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500" | ||
226 | dependencies = [ | ||
227 | "proc-macro2", | ||
228 | ] | ||
229 | |||
230 | [[package]] | ||
185 | name = "rustix" | 231 | name = "rustix" |
186 | version = "0.37.11" | 232 | version = "0.37.19" |
187 | source = "registry+https://github.com/rust-lang/crates.io-index" | 233 | source = "registry+https://github.com/rust-lang/crates.io-index" |
188 | checksum = "85597d61f83914ddeba6a47b3b8ffe7365107221c2e557ed94426489fefb5f77" | 234 | checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d" |
189 | dependencies = [ | 235 | dependencies = [ |
190 | "bitflags", | 236 | "bitflags", |
191 | "errno", | 237 | "errno", |
@@ -197,9 +243,32 @@ dependencies = [ | |||
197 | 243 | ||
198 | [[package]] | 244 | [[package]] |
199 | name = "serde" | 245 | name = "serde" |
200 | version = "1.0.160" | 246 | version = "1.0.163" |
247 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
248 | checksum = "2113ab51b87a539ae008b5c6c02dc020ffa39afd2d83cffcb3f4eb2722cebec2" | ||
249 | dependencies = [ | ||
250 | "serde_derive", | ||
251 | ] | ||
252 | |||
253 | [[package]] | ||
254 | name = "serde_derive" | ||
255 | version = "1.0.163" | ||
201 | source = "registry+https://github.com/rust-lang/crates.io-index" | 256 | source = "registry+https://github.com/rust-lang/crates.io-index" |
202 | checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c" | 257 | checksum = "8c805777e3930c8883389c602315a24224bcc738b63905ef87cd1420353ea93e" |
258 | dependencies = [ | ||
259 | "proc-macro2", | ||
260 | "quote", | ||
261 | "syn", | ||
262 | ] | ||
263 | |||
264 | [[package]] | ||
265 | name = "serde_spanned" | ||
266 | version = "0.6.2" | ||
267 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
268 | checksum = "93107647184f6027e3b7dcb2e11034cf95ffa1e3a682c67951963ac69c1c007d" | ||
269 | dependencies = [ | ||
270 | "serde", | ||
271 | ] | ||
203 | 272 | ||
204 | [[package]] | 273 | [[package]] |
205 | name = "strsim" | 274 | name = "strsim" |
@@ -208,6 +277,57 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
208 | checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" | 277 | checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" |
209 | 278 | ||
210 | [[package]] | 279 | [[package]] |
280 | name = "syn" | ||
281 | version = "2.0.16" | ||
282 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
283 | checksum = "a6f671d4b5ffdb8eadec19c0ae67fe2639df8684bd7bc4b83d986b8db549cf01" | ||
284 | dependencies = [ | ||
285 | "proc-macro2", | ||
286 | "quote", | ||
287 | "unicode-ident", | ||
288 | ] | ||
289 | |||
290 | [[package]] | ||
291 | name = "toml" | ||
292 | version = "0.7.4" | ||
293 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
294 | checksum = "d6135d499e69981f9ff0ef2167955a5333c35e36f6937d382974566b3d5b94ec" | ||
295 | dependencies = [ | ||
296 | "serde", | ||
297 | "serde_spanned", | ||
298 | "toml_datetime", | ||
299 | "toml_edit", | ||
300 | ] | ||
301 | |||
302 | [[package]] | ||
303 | name = "toml_datetime" | ||
304 | version = "0.6.2" | ||
305 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
306 | checksum = "5a76a9312f5ba4c2dec6b9161fdf25d87ad8a09256ccea5a556fef03c706a10f" | ||
307 | dependencies = [ | ||
308 | "serde", | ||
309 | ] | ||
310 | |||
311 | [[package]] | ||
312 | name = "toml_edit" | ||
313 | version = "0.19.10" | ||
314 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
315 | checksum = "2380d56e8670370eee6566b0bfd4265f65b3f432e8c6d85623f728d4fa31f739" | ||
316 | dependencies = [ | ||
317 | "indexmap", | ||
318 | "serde", | ||
319 | "serde_spanned", | ||
320 | "toml_datetime", | ||
321 | "winnow", | ||
322 | ] | ||
323 | |||
324 | [[package]] | ||
325 | name = "unicode-ident" | ||
326 | version = "1.0.8" | ||
327 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
328 | checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" | ||
329 | |||
330 | [[package]] | ||
211 | name = "utf8parse" | 331 | name = "utf8parse" |
212 | version = "0.2.1" | 332 | version = "0.2.1" |
213 | source = "registry+https://github.com/rust-lang/crates.io-index" | 333 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -220,6 +340,8 @@ dependencies = [ | |||
220 | "anyhow", | 340 | "anyhow", |
221 | "clap", | 341 | "clap", |
222 | "ipnetwork", | 342 | "ipnetwork", |
343 | "serde", | ||
344 | "toml", | ||
223 | ] | 345 | ] |
224 | 346 | ||
225 | [[package]] | 347 | [[package]] |
@@ -287,3 +409,12 @@ name = "windows_x86_64_msvc" | |||
287 | version = "0.48.0" | 409 | version = "0.48.0" |
288 | source = "registry+https://github.com/rust-lang/crates.io-index" | 410 | source = "registry+https://github.com/rust-lang/crates.io-index" |
289 | checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" | 411 | checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" |
412 | |||
413 | [[package]] | ||
414 | name = "winnow" | ||
415 | version = "0.4.6" | ||
416 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
417 | checksum = "61de7bac303dc551fe038e2b3cef0f571087a47571ea6e79a87692ac99b99699" | ||
418 | dependencies = [ | ||
419 | "memchr", | ||
420 | ] | ||
@@ -9,3 +9,5 @@ edition = "2021" | |||
9 | anyhow = "1.0.70" | 9 | anyhow = "1.0.70" |
10 | clap = { version = "4.2.2", features = ["cargo"] } | 10 | clap = { version = "4.2.2", features = ["cargo"] } |
11 | ipnetwork = "0.20.0" | 11 | ipnetwork = "0.20.0" |
12 | serde = { version = "1.0", features = ["derive"] } | ||
13 | toml = "0.7.4" | ||
diff --git a/src/configuration.rs b/src/configuration.rs new file mode 100644 index 0000000..173b8ff --- /dev/null +++ b/src/configuration.rs | |||
@@ -0,0 +1,31 @@ | |||
1 | use serde::Deserialize; | ||
2 | use anyhow::{Result, anyhow, Context}; | ||
3 | use std::fs::read_to_string; | ||
4 | |||
5 | #[derive(Deserialize)] | ||
6 | pub struct Configuration { | ||
7 | pub endpoint: String, | ||
8 | pub port: Option<u32>, | ||
9 | pub dns: Option<String>, | ||
10 | pub wgconf: Option<String>, | ||
11 | } | ||
12 | |||
13 | pub fn find_configuration_file(argument: Option<&String>) -> Result<String> { | ||
14 | match argument { | ||
15 | Some(p) => { | ||
16 | if let Ok(t) = read_to_string(p) { | ||
17 | let _: Configuration = toml::from_str(&t).context("parsing configuration file")?; | ||
18 | return Ok(p.clone()) | ||
19 | } | ||
20 | }, | ||
21 | None => { | ||
22 | // Try /etc/wgmgr.toml | ||
23 | if let Ok(t) = read_to_string("/etc/wgmgr.toml") { | ||
24 | let _: Configuration = toml::from_str(&t).context("parsing /etc/wgmgr.toml")?; | ||
25 | return Ok(String::from("/etc/wgmgr.toml")) | ||
26 | } | ||
27 | }, | ||
28 | }; | ||
29 | |||
30 | Err(anyhow!("Could not find a valid configuration file for wgmgr.")) | ||
31 | } | ||
diff --git a/src/main.rs b/src/main.rs index 6bc7e85..58c87e8 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -13,12 +13,19 @@ use clap::arg; | |||
13 | use wg::config::WireguardConfig; | 13 | use wg::config::WireguardConfig; |
14 | 14 | ||
15 | mod wg; | 15 | mod wg; |
16 | mod configuration; | ||
16 | 17 | ||
17 | fn main() { | 18 | fn main() { |
18 | let matches = command!() | 19 | let matches = command!() |
19 | .arg( | 20 | .arg( |
20 | arg!( | 21 | arg!( |
21 | -w --wg-config <WGCONFIG> "Wireguard configuration file name" | 22 | -w --wgconfig <WGCONFIG> "Wireguard configuration file name" |
23 | ) | ||
24 | .required(false) | ||
25 | ) | ||
26 | .arg( | ||
27 | arg!( | ||
28 | -c --config <CONFIG> "wgmgr configuration file path" | ||
22 | ) | 29 | ) |
23 | .required(false) | 30 | .required(false) |
24 | ) | 31 | ) |
@@ -79,11 +86,25 @@ fn main() { | |||
79 | .about("Run 'wg', but with the client names") | 86 | .about("Run 'wg', but with the client names") |
80 | ) | 87 | ) |
81 | .get_matches(); | 88 | .get_matches(); |
89 | |||
90 | |||
91 | // Find the configuration file for wgmgr | ||
92 | let wgmgr_conf_path = match configuration::find_configuration_file(matches.get_one::<String>("config")) { | ||
93 | Ok(s) => s, | ||
94 | Err(e) => { | ||
95 | eprintln!("Could not find wgmgr configuration file. Place it at /etc/wgmgr.toml or use the --config flag."); | ||
96 | eprintln!("{:?}", e); | ||
97 | exit(1); | ||
98 | } | ||
99 | }; | ||
100 | |||
101 | let wgmgr_conf: configuration::Configuration = toml::from_str(&read_to_string(wgmgr_conf_path).unwrap()).unwrap(); | ||
82 | 102 | ||
83 | let wg_conf_path = match find_wg_config_file(&matches) { | 103 | // Find the Wireguard configuration file |
104 | let wg_conf_path = match find_wg_config_file(&matches, &wgmgr_conf.wgconf) { | ||
84 | Ok(s) => s, | 105 | Ok(s) => s, |
85 | Err(e) => { | 106 | Err(e) => { |
86 | eprintln!("Wireguard configuration file error: {}", e); | 107 | eprintln!("Wireguard configuration file error: {:?}", e); |
87 | exit(1); | 108 | exit(1); |
88 | } | 109 | } |
89 | }; | 110 | }; |
@@ -91,8 +112,8 @@ fn main() { | |||
91 | let mut wg_conf = match wg::config::WireguardConfig::new(&wg_conf_path) { | 112 | let mut wg_conf = match wg::config::WireguardConfig::new(&wg_conf_path) { |
92 | Ok(c) => c, | 113 | Ok(c) => c, |
93 | Err(e) => { | 114 | Err(e) => { |
94 | eprintln!("Error loading the Wireguard configuration file '{}'", wg_conf_path); | 115 | eprintln!("Error loading the Wireguard configuration file '{}'.", wg_conf_path); |
95 | eprintln!("{}", e); | 116 | eprintln!("{:?}", e); |
96 | exit(1); | 117 | exit(1); |
97 | } | 118 | } |
98 | }; | 119 | }; |
@@ -119,8 +140,8 @@ fn main() { | |||
119 | None => false | 140 | None => false |
120 | }; | 141 | }; |
121 | 142 | ||
122 | if let Err(e) = do_config(&wg_conf, peer_name.to_string(), is_full) { | 143 | if let Err(e) = do_config(&wg_conf, &wgmgr_conf, peer_name.to_string(), is_full) { |
123 | eprintln!("Error generating configuration for {}: {}", peer_name.to_string(), e); | 144 | eprintln!("Error generating configuration for {}: {:?}", peer_name.to_string(), e); |
124 | exit(1); | 145 | exit(1); |
125 | } | 146 | } |
126 | }, | 147 | }, |
@@ -131,7 +152,7 @@ fn main() { | |||
131 | Some(("add", args)) => { | 152 | Some(("add", args)) => { |
132 | let new_name = args.get_one::<String>("NAME").unwrap().to_string(); | 153 | let new_name = args.get_one::<String>("NAME").unwrap().to_string(); |
133 | if let Err(e) = do_add(&mut wg_conf, wg_conf_path, &new_name, args.get_one::<String>("ip")) { | 154 | 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); | 155 | eprintln!("Error adding peer: {:?}", e); |
135 | exit(1); | 156 | exit(1); |
136 | } | 157 | } |
137 | else { | 158 | else { |
@@ -142,7 +163,7 @@ fn main() { | |||
142 | Some(("rm", args)) => { | 163 | Some(("rm", args)) => { |
143 | let rm_name = args.get_one::<String>("NAME").unwrap().to_string(); | 164 | let rm_name = args.get_one::<String>("NAME").unwrap().to_string(); |
144 | if let Err(e) = do_rm(&mut wg_conf, wg_conf_path, &rm_name) { | 165 | if let Err(e) = do_rm(&mut wg_conf, wg_conf_path, &rm_name) { |
145 | eprintln!("Error removing peer: {}", e); | 166 | eprintln!("Error removing peer: {:?}", e); |
146 | exit(1); | 167 | exit(1); |
147 | } | 168 | } |
148 | 169 | ||
@@ -151,7 +172,7 @@ fn main() { | |||
151 | 172 | ||
152 | Some(("wg", _)) => { | 173 | Some(("wg", _)) => { |
153 | if let Err(e) = do_wg(&wg_conf) { | 174 | if let Err(e) = do_wg(&wg_conf) { |
154 | eprintln!("{}", e); | 175 | eprintln!("{:?}", e); |
155 | exit(1); | 176 | exit(1); |
156 | } | 177 | } |
157 | } | 178 | } |
@@ -163,9 +184,9 @@ fn main() { | |||
163 | 184 | ||
164 | } | 185 | } |
165 | 186 | ||
166 | fn find_wg_config_file(matches: &ArgMatches) -> Result<String> { | 187 | fn find_wg_config_file(matches: &ArgMatches, wgconf: &Option<String>) -> Result<String> { |
167 | // Top priority goes to the command-line argument | 188 | // Top priority goes to the command-line argument |
168 | match matches.get_one::<String>("wg-config") { | 189 | match matches.get_one::<String>("wgconfig") { |
169 | Some(s) => { | 190 | Some(s) => { |
170 | if s.starts_with("/") { | 191 | if s.starts_with("/") { |
171 | return Ok(s.to_string()) | 192 | return Ok(s.to_string()) |
@@ -183,6 +204,12 @@ fn find_wg_config_file(matches: &ArgMatches) -> Result<String> { | |||
183 | Err(_) => {} | 204 | Err(_) => {} |
184 | }; | 205 | }; |
185 | 206 | ||
207 | // Then, is there is a "wgconf" in the wgmgr configuration file, we take that | ||
208 | match wgconf { | ||
209 | Some(s) => return Ok(s.clone()), | ||
210 | None => {} | ||
211 | }; | ||
212 | |||
186 | // Otherwise, we will first try a simple "wg0.conf" | 213 | // Otherwise, we will first try a simple "wg0.conf" |
187 | match read_to_string("/etc/wireguard/wg0.conf") { | 214 | match read_to_string("/etc/wireguard/wg0.conf") { |
188 | Ok(_) => return Ok(String::from_str("/etc/wireguard/wg0.conf").unwrap()), | 215 | Ok(_) => return Ok(String::from_str("/etc/wireguard/wg0.conf").unwrap()), |
@@ -233,7 +260,7 @@ fn do_list(wg_conf: &wg::config::WireguardConfig) { | |||
233 | } | 260 | } |
234 | } | 261 | } |
235 | 262 | ||
236 | fn do_config(wg_conf: &wg::config::WireguardConfig, peer_name: String, is_full: bool) -> Result<()> { | 263 | fn do_config(wg_conf: &wg::config::WireguardConfig, conf: &configuration::Configuration, peer_name: String, is_full: bool) -> Result<()> { |
237 | let peer = match wg_conf.get_peer(peer_name.as_str()) { | 264 | let peer = match wg_conf.get_peer(peer_name.as_str()) { |
238 | Some(p) => p, | 265 | Some(p) => p, |
239 | None => { | 266 | None => { |
@@ -241,7 +268,7 @@ fn do_config(wg_conf: &wg::config::WireguardConfig, peer_name: String, is_full: | |||
241 | } | 268 | } |
242 | }; | 269 | }; |
243 | 270 | ||
244 | println!("{}", peer.gen_config(wg_conf, is_full)?); | 271 | println!("{}", peer.gen_config(wg_conf, conf.dns.clone(), conf.endpoint.clone(), conf.port, is_full)?); |
245 | 272 | ||
246 | Ok(()) | 273 | Ok(()) |
247 | } | 274 | } |
diff --git a/src/wg/peer.rs b/src/wg/peer.rs index 7b703fc..bb85a5e 100644 --- a/src/wg/peer.rs +++ b/src/wg/peer.rs | |||
@@ -89,23 +89,32 @@ impl Peer { | |||
89 | Ok(pk) | 89 | Ok(pk) |
90 | } | 90 | } |
91 | 91 | ||
92 | pub fn gen_config(&self, conf: &WireguardConfig, is_full: bool) -> Result<String>{ | 92 | pub fn gen_config(&self, wg_conf: &WireguardConfig, dns: Option<String>, endpoint: String, port: Option<u32>, is_full: bool) -> Result<String>{ |
93 | let mut res = String::new(); | 93 | let mut res = String::new(); |
94 | 94 | ||
95 | writeln!(res, "[Interface]")?; | 95 | writeln!(res, "[Interface]")?; |
96 | writeln!(res, "PrivateKey = {}", self.private_key()?)?; | 96 | writeln!(res, "PrivateKey = {}", self.private_key()?)?; |
97 | writeln!(res, "Address = {}/32", self.ip)?; | 97 | writeln!(res, "Address = {}/32", self.ip)?; |
98 | writeln!(res, "DNS = TODO\n")?; | ||
99 | 98 | ||
99 | if let Some(s) = dns { | ||
100 | writeln!(res, "DNS = {}", s)?; | ||
101 | } | ||
102 | |||
103 | writeln!(res, "")?; | ||
100 | writeln!(res, "[Peer]")?; | 104 | writeln!(res, "[Peer]")?; |
101 | writeln!(res, "PublicKey = {}", conf.public_key().context("error getting server public key")?)?; | 105 | writeln!(res, "PublicKey = {}", wg_conf.public_key().context("error getting server public key")?)?; |
102 | 106 | ||
103 | let allowed_ips = match is_full { | 107 | let allowed_ips = match is_full { |
104 | true => String::from("0.0.0.0/0"), | 108 | true => String::from("0.0.0.0/0"), |
105 | false => conf.network.to_string() | 109 | false => wg_conf.network.to_string() |
106 | }; | 110 | }; |
107 | writeln!(res, "AllowedIPs = {}", allowed_ips)?; | 111 | writeln!(res, "AllowedIPs = {}", allowed_ips)?; |
108 | writeln!(res, "Endpoint = TODO")?; | 112 | |
113 | let port = match port { | ||
114 | Some(p) => p, | ||
115 | None => 51820, | ||
116 | }; | ||
117 | writeln!(res, "Endpoint = {}:{}", endpoint, port)?; | ||
109 | writeln!(res, "PersistentKeepAlive = 25")?; | 118 | writeln!(res, "PersistentKeepAlive = 25")?; |
110 | 119 | ||
111 | Ok(res) | 120 | Ok(res) |