aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Garrelou <simon@sixfoisneuf.fr>2023-05-24 21:31:32 +0200
committerSimon Garrelou <simon@sixfoisneuf.fr>2023-05-24 21:31:32 +0200
commit0edf4d09a6fca89de7f98b12d24def74a12ca7f3 (patch)
tree428847fcb4921dd9d81d5d5d947cd26243ea6d12
parente67c54d731c3a0ae95948421dea71ab5e78526a7 (diff)
downloadwgmgr-0edf4d09a6fca89de7f98b12d24def74a12ca7f3.tar.gz
wgmgr-0edf4d09a6fca89de7f98b12d24def74a12ca7f3.zip
Add wgmgr.conf loading
-rw-r--r--Cargo.lock171
-rw-r--r--Cargo.toml2
-rw-r--r--src/configuration.rs31
-rw-r--r--src/main.rs55
-rw-r--r--src/wg/peer.rs19
5 files changed, 239 insertions, 39 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6c862da..5edeb14 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4,9 +4,9 @@ version = 3
4 4
5[[package]] 5[[package]]
6name = "anstream" 6name = "anstream"
7version = "0.3.0" 7version = "0.3.2"
8source = "registry+https://github.com/rust-lang/crates.io-index" 8source = "registry+https://github.com/rust-lang/crates.io-index"
9checksum = "9e579a7752471abc2a8268df8b20005e3eadd975f585398f17efcfd8d4927371" 9checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163"
10dependencies = [ 10dependencies = [
11 "anstyle", 11 "anstyle",
12 "anstyle-parse", 12 "anstyle-parse",
@@ -43,9 +43,9 @@ dependencies = [
43 43
44[[package]] 44[[package]]
45name = "anstyle-wincon" 45name = "anstyle-wincon"
46version = "1.0.0" 46version = "1.0.1"
47source = "registry+https://github.com/rust-lang/crates.io-index" 47source = "registry+https://github.com/rust-lang/crates.io-index"
48checksum = "4bcd8291a340dd8ac70e18878bc4501dd7b4ff970cfa21c207d36ece51ea88fd" 48checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188"
49dependencies = [ 49dependencies = [
50 "anstyle", 50 "anstyle",
51 "windows-sys", 51 "windows-sys",
@@ -53,9 +53,15 @@ dependencies = [
53 53
54[[package]] 54[[package]]
55name = "anyhow" 55name = "anyhow"
56version = "1.0.70" 56version = "1.0.71"
57source = "registry+https://github.com/rust-lang/crates.io-index" 57source = "registry+https://github.com/rust-lang/crates.io-index"
58checksum = "7de8ce5e0f9f8d88245311066a578d72b7af3e7088f32783804676302df237e4" 58checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8"
59
60[[package]]
61name = "autocfg"
62version = "1.1.0"
63source = "registry+https://github.com/rust-lang/crates.io-index"
64checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
59 65
60[[package]] 66[[package]]
61name = "bitflags" 67name = "bitflags"
@@ -71,18 +77,18 @@ checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
71 77
72[[package]] 78[[package]]
73name = "clap" 79name = "clap"
74version = "4.2.2" 80version = "4.3.0"
75source = "registry+https://github.com/rust-lang/crates.io-index" 81source = "registry+https://github.com/rust-lang/crates.io-index"
76checksum = "9b802d85aaf3a1cdb02b224ba472ebdea62014fccfcb269b95a4d76443b5ee5a" 82checksum = "93aae7a4192245f70fe75dd9157fc7b4a5bf53e88d30bd4396f7d8f9284d5acc"
77dependencies = [ 83dependencies = [
78 "clap_builder", 84 "clap_builder",
79] 85]
80 86
81[[package]] 87[[package]]
82name = "clap_builder" 88name = "clap_builder"
83version = "4.2.2" 89version = "4.3.0"
84source = "registry+https://github.com/rust-lang/crates.io-index" 90source = "registry+https://github.com/rust-lang/crates.io-index"
85checksum = "14a1a858f532119338887a4b8e1af9c60de8249cd7bafd68036a489e261e37b6" 91checksum = "4f423e341edefb78c9caba2d9c7f7687d0e72e89df3ce3394554754393ac3990"
86dependencies = [ 92dependencies = [
87 "anstream", 93 "anstream",
88 "anstyle", 94 "anstyle",
@@ -94,9 +100,9 @@ dependencies = [
94 100
95[[package]] 101[[package]]
96name = "clap_lex" 102name = "clap_lex"
97version = "0.4.1" 103version = "0.5.0"
98source = "registry+https://github.com/rust-lang/crates.io-index" 104source = "registry+https://github.com/rust-lang/crates.io-index"
99checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" 105checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b"
100 106
101[[package]] 107[[package]]
102name = "colorchoice" 108name = "colorchoice"
@@ -126,12 +132,28 @@ dependencies = [
126] 132]
127 133
128[[package]] 134[[package]]
135name = "hashbrown"
136version = "0.12.3"
137source = "registry+https://github.com/rust-lang/crates.io-index"
138checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
139
140[[package]]
129name = "hermit-abi" 141name = "hermit-abi"
130version = "0.3.1" 142version = "0.3.1"
131source = "registry+https://github.com/rust-lang/crates.io-index" 143source = "registry+https://github.com/rust-lang/crates.io-index"
132checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" 144checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
133 145
134[[package]] 146[[package]]
147name = "indexmap"
148version = "1.9.3"
149source = "registry+https://github.com/rust-lang/crates.io-index"
150checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
151dependencies = [
152 "autocfg",
153 "hashbrown",
154]
155
156[[package]]
135name = "io-lifetimes" 157name = "io-lifetimes"
136version = "1.0.10" 158version = "1.0.10"
137source = "registry+https://github.com/rust-lang/crates.io-index" 159source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -165,15 +187,21 @@ dependencies = [
165 187
166[[package]] 188[[package]]
167name = "libc" 189name = "libc"
168version = "0.2.141" 190version = "0.2.144"
169source = "registry+https://github.com/rust-lang/crates.io-index" 191source = "registry+https://github.com/rust-lang/crates.io-index"
170checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5" 192checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
171 193
172[[package]] 194[[package]]
173name = "linux-raw-sys" 195name = "linux-raw-sys"
174version = "0.3.1" 196version = "0.3.8"
175source = "registry+https://github.com/rust-lang/crates.io-index" 197source = "registry+https://github.com/rust-lang/crates.io-index"
176checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f" 198checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519"
199
200[[package]]
201name = "memchr"
202version = "2.5.0"
203source = "registry+https://github.com/rust-lang/crates.io-index"
204checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
177 205
178[[package]] 206[[package]]
179name = "once_cell" 207name = "once_cell"
@@ -182,10 +210,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
182checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" 210checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
183 211
184[[package]] 212[[package]]
213name = "proc-macro2"
214version = "1.0.58"
215source = "registry+https://github.com/rust-lang/crates.io-index"
216checksum = "fa1fb82fc0c281dd9671101b66b771ebbe1eaf967b96ac8740dcba4b70005ca8"
217dependencies = [
218 "unicode-ident",
219]
220
221[[package]]
222name = "quote"
223version = "1.0.27"
224source = "registry+https://github.com/rust-lang/crates.io-index"
225checksum = "8f4f29d145265ec1c483c7c654450edde0bfe043d3938d6972630663356d9500"
226dependencies = [
227 "proc-macro2",
228]
229
230[[package]]
185name = "rustix" 231name = "rustix"
186version = "0.37.11" 232version = "0.37.19"
187source = "registry+https://github.com/rust-lang/crates.io-index" 233source = "registry+https://github.com/rust-lang/crates.io-index"
188checksum = "85597d61f83914ddeba6a47b3b8ffe7365107221c2e557ed94426489fefb5f77" 234checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d"
189dependencies = [ 235dependencies = [
190 "bitflags", 236 "bitflags",
191 "errno", 237 "errno",
@@ -197,9 +243,32 @@ dependencies = [
197 243
198[[package]] 244[[package]]
199name = "serde" 245name = "serde"
200version = "1.0.160" 246version = "1.0.163"
247source = "registry+https://github.com/rust-lang/crates.io-index"
248checksum = "2113ab51b87a539ae008b5c6c02dc020ffa39afd2d83cffcb3f4eb2722cebec2"
249dependencies = [
250 "serde_derive",
251]
252
253[[package]]
254name = "serde_derive"
255version = "1.0.163"
201source = "registry+https://github.com/rust-lang/crates.io-index" 256source = "registry+https://github.com/rust-lang/crates.io-index"
202checksum = "bb2f3770c8bce3bcda7e149193a069a0f4365bda1fa5cd88e03bca26afc1216c" 257checksum = "8c805777e3930c8883389c602315a24224bcc738b63905ef87cd1420353ea93e"
258dependencies = [
259 "proc-macro2",
260 "quote",
261 "syn",
262]
263
264[[package]]
265name = "serde_spanned"
266version = "0.6.2"
267source = "registry+https://github.com/rust-lang/crates.io-index"
268checksum = "93107647184f6027e3b7dcb2e11034cf95ffa1e3a682c67951963ac69c1c007d"
269dependencies = [
270 "serde",
271]
203 272
204[[package]] 273[[package]]
205name = "strsim" 274name = "strsim"
@@ -208,6 +277,57 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
208checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" 277checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
209 278
210[[package]] 279[[package]]
280name = "syn"
281version = "2.0.16"
282source = "registry+https://github.com/rust-lang/crates.io-index"
283checksum = "a6f671d4b5ffdb8eadec19c0ae67fe2639df8684bd7bc4b83d986b8db549cf01"
284dependencies = [
285 "proc-macro2",
286 "quote",
287 "unicode-ident",
288]
289
290[[package]]
291name = "toml"
292version = "0.7.4"
293source = "registry+https://github.com/rust-lang/crates.io-index"
294checksum = "d6135d499e69981f9ff0ef2167955a5333c35e36f6937d382974566b3d5b94ec"
295dependencies = [
296 "serde",
297 "serde_spanned",
298 "toml_datetime",
299 "toml_edit",
300]
301
302[[package]]
303name = "toml_datetime"
304version = "0.6.2"
305source = "registry+https://github.com/rust-lang/crates.io-index"
306checksum = "5a76a9312f5ba4c2dec6b9161fdf25d87ad8a09256ccea5a556fef03c706a10f"
307dependencies = [
308 "serde",
309]
310
311[[package]]
312name = "toml_edit"
313version = "0.19.10"
314source = "registry+https://github.com/rust-lang/crates.io-index"
315checksum = "2380d56e8670370eee6566b0bfd4265f65b3f432e8c6d85623f728d4fa31f739"
316dependencies = [
317 "indexmap",
318 "serde",
319 "serde_spanned",
320 "toml_datetime",
321 "winnow",
322]
323
324[[package]]
325name = "unicode-ident"
326version = "1.0.8"
327source = "registry+https://github.com/rust-lang/crates.io-index"
328checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4"
329
330[[package]]
211name = "utf8parse" 331name = "utf8parse"
212version = "0.2.1" 332version = "0.2.1"
213source = "registry+https://github.com/rust-lang/crates.io-index" 333source = "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"
287version = "0.48.0" 409version = "0.48.0"
288source = "registry+https://github.com/rust-lang/crates.io-index" 410source = "registry+https://github.com/rust-lang/crates.io-index"
289checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" 411checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a"
412
413[[package]]
414name = "winnow"
415version = "0.4.6"
416source = "registry+https://github.com/rust-lang/crates.io-index"
417checksum = "61de7bac303dc551fe038e2b3cef0f571087a47571ea6e79a87692ac99b99699"
418dependencies = [
419 "memchr",
420]
diff --git a/Cargo.toml b/Cargo.toml
index 8b25ae0..6c52be6 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -9,3 +9,5 @@ edition = "2021"
9anyhow = "1.0.70" 9anyhow = "1.0.70"
10clap = { version = "4.2.2", features = ["cargo"] } 10clap = { version = "4.2.2", features = ["cargo"] }
11ipnetwork = "0.20.0" 11ipnetwork = "0.20.0"
12serde = { version = "1.0", features = ["derive"] }
13toml = "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 @@
1use serde::Deserialize;
2use anyhow::{Result, anyhow, Context};
3use std::fs::read_to_string;
4
5#[derive(Deserialize)]
6pub struct Configuration {
7 pub endpoint: String,
8 pub port: Option<u32>,
9 pub dns: Option<String>,
10 pub wgconf: Option<String>,
11}
12
13pub 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;
13use wg::config::WireguardConfig; 13use wg::config::WireguardConfig;
14 14
15mod wg; 15mod wg;
16mod configuration;
16 17
17fn main() { 18fn 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
166fn find_wg_config_file(matches: &ArgMatches) -> Result<String> { 187fn 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
236fn do_config(wg_conf: &wg::config::WireguardConfig, peer_name: String, is_full: bool) -> Result<()> { 263fn 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)