From 1b4d57871e90f9f04b237a93091e07072d5ce0d8 Mon Sep 17 00:00:00 2001 From: Francesco Cogno Date: Wed, 15 May 2019 16:55:07 +0200 Subject: [PATCH] Started wireguard config file parse --- src/main.rs | 1 + src/wireguard_config.rs | 85 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/wireguard_config.rs diff --git a/src/main.rs b/src/main.rs index 1b810f3..05cafb6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,7 @@ use std::convert::TryFrom; use std::process::Command; use std::string::String; use wireguard::WireGuard; +mod wireguard_config; fn check_compliance(req: &Request) -> Result<(), Response> { if req.uri() != "/metrics" { diff --git a/src/wireguard_config.rs b/src/wireguard_config.rs new file mode 100644 index 0000000..bb6c369 --- /dev/null +++ b/src/wireguard_config.rs @@ -0,0 +1,85 @@ +use std::collections::HashMap; + +#[derive(Debug, Default, Clone)] +pub(crate) struct PeerEntry { + pub public_key: String, + pub name: Option, +} + +pub(crate) fn parse<'a>(txt: &'a str) -> HashMap { + let mut ht = HashMap::new(); + + let mut name = ""; + txt.lines().fold("", |prev, cur| { + if cur == "[Peer]" { + if prev.chars().next() == Some('#') { + name = prev; + } + } else if cur.starts_with("PublicKey") { + // public key found, use it as key + // TODO we must strip the PublicKey = first ! + ht.insert( + cur.to_owned(), + PeerEntry { + public_key: cur.to_owned(), + name: Some(name.to_owned()), + }, + ); + } + + cur + }); + + ht +} + +#[cfg(test)] +mod tests { + use super::*; + + const TEXT: &'static str = " +ListenPort = 51820 +PrivateKey = my_super_secret_private_key +# PreUp = iptables -t nat -A POSTROUTING -s 10.70.0.0/24 -o enp7s0 -j MASQUERADE +# PostDown = iptables -t nat -D POSTROUTING -s 10.70.0.0/24 -o enp7s0 -j MASQUERADE + +# OnePlus 6T +[Peer] +PublicKey = 2S7mA0vEMethCNQrJpJKE81/JmhgtB+tHHLYQhgM6kk= +AllowedIPs = 10.70.0.2/32 + +# varch.local (laptop) +[Peer] +PublicKey = qnoxQoQI8KKMupLnSSureORV0wMmH7JryZNsmGVISzU= +AllowedIPs = 10.70.0.3/32 + +# cantarch +[Peer] +PublicKey = L2UoJZN7RmEKsMmqaJgKG0m1S2Zs2wd2ptAf+kb3008= +AllowedIPs = 10.70.0.4/32 + +# frcognoarch +[Peer] +PublicKey = MdVOIPKt9K2MPj/sO2NlWQbOnFJ6L/qX80mmhQwsUlA= +AllowedIPs = 10.70.0.50/32 + +# frcognowin10 +[Peer] +PublicKey = lqYcojJMsIZXMUw1heAFbQHBoKjCEaeo7M1WXDh/KWc= +AllowedIPs = 10.70.0.40/32 + +# OnePlus 5T +[Peer] +PublicKey = 928vO9Lf4+Mo84cWu4k1oRyzf0AR7FTGoPKHGoTMSHk= +AllowedIPs = 10.70.0.80/32 +"; + + #[test] + fn test_parse() { + let a = parse(TEXT); + println!("{:?}", a); + } + + #[test] + fn test_parse_and_serialize() {} +}