Bugfix: comments no longer remove friendly names
This commit is contained in:
parent
abd25e81db
commit
4584bd6e50
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "prometheus_wireguard_exporter"
|
name = "prometheus_wireguard_exporter"
|
||||||
version = "3.4.1"
|
version = "3.4.2"
|
||||||
authors = ["Francesco Cogno <francesco.cogno@outlook.com>"]
|
authors = ["Francesco Cogno <francesco.cogno@outlook.com>"]
|
||||||
description = "Prometheus WireGuard Exporter"
|
description = "Prometheus WireGuard Exporter"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
[
|
|
||||||
{ "path": "/home/mindflavor", "recursive": true },
|
|
||||||
{ "path": "/home/mindflavor/.cargo", "recursive": false }
|
|
||||||
]
|
|
@ -10,7 +10,6 @@ pub(crate) struct PeerEntry<'a> {
|
|||||||
pub name: Option<&'a str>,
|
pub name: Option<&'a str>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn after_char(s: &str, c_split: char) -> &str {
|
fn after_char(s: &str, c_split: char) -> &str {
|
||||||
let mut p: usize = 0;
|
let mut p: usize = 0;
|
||||||
for c in s.chars().into_iter() {
|
for c in s.chars().into_iter() {
|
||||||
@ -23,6 +22,16 @@ fn after_char(s: &str, c_split: char) -> &str {
|
|||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn after_char_strip_comment(s: &str, c_split: char) -> &str {
|
||||||
|
let s = after_char(s, c_split);
|
||||||
|
|
||||||
|
if let Some(idx) = s.find('#') {
|
||||||
|
&s[..idx].trim()
|
||||||
|
} else {
|
||||||
|
s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn from_pound_line_to_key_value(line: &str) -> Option<(&str, &str)> {
|
fn from_pound_line_to_key_value(line: &str) -> Option<(&str, &str)> {
|
||||||
// since the pound sign is 1 byte the below slice will work
|
// since the pound sign is 1 byte the below slice will work
|
||||||
let line = &line[1..];
|
let line = &line[1..];
|
||||||
@ -42,7 +51,7 @@ impl<'a> TryFrom<&[&'a str]> for PeerEntry<'a> {
|
|||||||
type Error = PeerEntryParseError;
|
type Error = PeerEntryParseError;
|
||||||
|
|
||||||
fn try_from(lines: &[&'a str]) -> Result<PeerEntry<'a>, Self::Error> {
|
fn try_from(lines: &[&'a str]) -> Result<PeerEntry<'a>, Self::Error> {
|
||||||
debug!("PeerEntry::TryFrom called with lines == {:?}", lines);
|
debug!("PeerEntry::TryFrom called with lines == {:#?}", lines);
|
||||||
|
|
||||||
let mut public_key = "";
|
let mut public_key = "";
|
||||||
let mut allowed_ips = "";
|
let mut allowed_ips = "";
|
||||||
@ -52,12 +61,17 @@ impl<'a> TryFrom<&[&'a str]> for PeerEntry<'a> {
|
|||||||
let line_lowercase = line.to_lowercase();
|
let line_lowercase = line.to_lowercase();
|
||||||
|
|
||||||
if line_lowercase.starts_with("publickey") {
|
if line_lowercase.starts_with("publickey") {
|
||||||
public_key = after_char(line, '=').trim();
|
public_key = after_char_strip_comment(line, '=').trim();
|
||||||
|
debug!("public_key == {}", public_key);
|
||||||
} else if line_lowercase.starts_with("allowedips") {
|
} else if line_lowercase.starts_with("allowedips") {
|
||||||
allowed_ips = after_char(line, '=').trim();
|
allowed_ips = after_char_strip_comment(line, '=').trim();
|
||||||
|
debug!("allowed_ips == {}", allowed_ips);
|
||||||
} else if line.trim().starts_with('#') {
|
} else if line.trim().starts_with('#') {
|
||||||
if let Some((key, value)) = from_pound_line_to_key_value(line) {
|
if let Some((key, value)) = from_pound_line_to_key_value(line) {
|
||||||
// if it's a supported key, let' map it
|
// if it's a supported key, let' map it.
|
||||||
|
// we support one key now but this way
|
||||||
|
// we can support more in the future
|
||||||
|
#[allow(clippy::single_match)]
|
||||||
match key {
|
match key {
|
||||||
"friendly_name" => {
|
"friendly_name" => {
|
||||||
name = Some(value);
|
name = Some(value);
|
||||||
@ -71,12 +85,12 @@ impl<'a> TryFrom<&[&'a str]> for PeerEntry<'a> {
|
|||||||
// Sanity checks
|
// Sanity checks
|
||||||
// If there are more than one PublicKey or AllowedIPs we won't catch it. But
|
// If there are more than one PublicKey or AllowedIPs we won't catch it. But
|
||||||
// WireGuard won't be working either so we can live with this simplification.
|
// WireGuard won't be working either so we can live with this simplification.
|
||||||
if public_key == "" {
|
if public_key.is_empty() {
|
||||||
// we return a owned String for ergonomics. This will allocate but it's ok since it's not supposed
|
// we return a owned String for ergonomics. This will allocate but it's ok since it's not supposed
|
||||||
// to happen :)
|
// to happen :)
|
||||||
let lines_owned: Vec<String> = lines.iter().map(|line| (*line).to_string()).collect();
|
let lines_owned: Vec<String> = lines.iter().map(|line| (*line).to_string()).collect();
|
||||||
Err(PeerEntryParseError::PublicKeyNotFound { lines: lines_owned })
|
Err(PeerEntryParseError::PublicKeyNotFound { lines: lines_owned })
|
||||||
} else if allowed_ips == "" {
|
} else if allowed_ips.is_empty() {
|
||||||
let lines_owned: Vec<String> = lines.iter().map(|line| (*line).to_string()).collect();
|
let lines_owned: Vec<String> = lines.iter().map(|line| (*line).to_string()).collect();
|
||||||
Err(PeerEntryParseError::AllowedIPsEntryNotFound { lines: lines_owned })
|
Err(PeerEntryParseError::AllowedIPsEntryNotFound { lines: lines_owned })
|
||||||
} else {
|
} else {
|
||||||
@ -96,6 +110,7 @@ pub(crate) type PeerEntryHashMap<'a> = HashMap<&'a str, PeerEntry<'a>>;
|
|||||||
pub(crate) fn peer_entry_hashmap_try_from(
|
pub(crate) fn peer_entry_hashmap_try_from(
|
||||||
txt: &str,
|
txt: &str,
|
||||||
) -> Result<PeerEntryHashMap, PeerEntryParseError> {
|
) -> Result<PeerEntryHashMap, PeerEntryParseError> {
|
||||||
|
debug!("txt == {}", txt);
|
||||||
let mut hm = HashMap::new();
|
let mut hm = HashMap::new();
|
||||||
|
|
||||||
let mut v_blocks = Vec::new();
|
let mut v_blocks = Vec::new();
|
||||||
@ -116,7 +131,7 @@ pub(crate) fn peer_entry_hashmap_try_from(
|
|||||||
} else {
|
} else {
|
||||||
// push the line if we are in a block (only if not empty)
|
// push the line if we are in a block (only if not empty)
|
||||||
if let Some(inner_cur_block) = &mut cur_block {
|
if let Some(inner_cur_block) = &mut cur_block {
|
||||||
if line != "" {
|
if !line.is_empty() {
|
||||||
inner_cur_block.push(line);
|
inner_cur_block.push(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -158,7 +173,7 @@ PrivateKey = my_super_secret_private_key
|
|||||||
# This is a comment
|
# This is a comment
|
||||||
# This is a comment
|
# This is a comment
|
||||||
PublicKey = 2S7mA0vEMethCNQrJpJKE81/JmhgtB+tHHLYQhgM6kk=
|
PublicKey = 2S7mA0vEMethCNQrJpJKE81/JmhgtB+tHHLYQhgM6kk=
|
||||||
AllowedIPs = 10.70.0.2/32
|
AllowedIPs = 10.70.0.2/32 # this is a comment in AllowedIPs line
|
||||||
|
|
||||||
[Peer]
|
[Peer]
|
||||||
# friendly_name=varch.local (laptop)
|
# friendly_name=varch.local (laptop)
|
||||||
@ -179,7 +194,7 @@ AllowedIPs = 10.70.0.50/32
|
|||||||
# This is a comment
|
# This is a comment
|
||||||
# friendly_name = frcognowin10
|
# friendly_name = frcognowin10
|
||||||
# This is something
|
# This is something
|
||||||
PublicKey = lqYcojJMsIZXMUw1heAFbQHBoKjCEaeo7M1WXDh/KWc=
|
PublicKey = lqYcojJMsIZXMUw1heAFbQHBoKjCEaeo7M1WXDh/KWc= # other comment
|
||||||
AllowedIPs = 10.70.0.40/32
|
AllowedIPs = 10.70.0.40/32
|
||||||
|
|
||||||
[Peer]
|
[Peer]
|
||||||
@ -218,7 +233,7 @@ PrivateKey = my_super_secret_private_key
|
|||||||
[Peer]
|
[Peer]
|
||||||
# friendly_name=OnePlus 6T
|
# friendly_name=OnePlus 6T
|
||||||
PublicKey = 2S7mA0vEMethCNQrJpJKE81/JmhgtB+tHHLYQhgM6kk=
|
PublicKey = 2S7mA0vEMethCNQrJpJKE81/JmhgtB+tHHLYQhgM6kk=
|
||||||
AllowedIPs = 10.70.0.2/32
|
AllowedIPs = 10.70.0.2/32 # this is a comment
|
||||||
|
|
||||||
[Peer]
|
[Peer]
|
||||||
# friendly_name=varch.local (laptop)
|
# friendly_name=varch.local (laptop)
|
||||||
@ -265,13 +280,15 @@ PublicKey = L2UoJZN7RmEKsMmqaJgKG0m1S2Zs2wd2ptAf+kb3008=
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_parse_friendly_name() {
|
fn test_parse_friendly_name() {
|
||||||
let a: PeerEntryHashMap = peer_entry_hashmap_try_from(TEXT).unwrap();
|
let a: PeerEntryHashMap = peer_entry_hashmap_try_from(TEXT).unwrap();
|
||||||
|
|
||||||
|
let entry = a.get("lqYcojJMsIZXMUw1heAFbQHBoKjCEaeo7M1WXDh/KWc=");
|
||||||
|
let entry = entry.expect("this should have been Some (frcognowin10)!");
|
||||||
|
assert_eq!(Some("frcognowin10"), entry.name);
|
||||||
|
|
||||||
let entry = a.get("2S7mA0vEMethCNQrJpJKE81/JmhgtB+tHHLYQhgM6kk=");
|
let entry = a.get("2S7mA0vEMethCNQrJpJKE81/JmhgtB+tHHLYQhgM6kk=");
|
||||||
let entry = entry.expect("this should have been Some!");
|
let entry = entry.expect("this should have been Some!");
|
||||||
assert_eq!(Some("OnePlus 6T"), entry.name);
|
assert_eq!(Some("OnePlus 6T"), entry.name);
|
||||||
|
assert_eq!(entry.allowed_ips, "10.70.0.2/32");
|
||||||
let entry = a.get("lqYcojJMsIZXMUw1heAFbQHBoKjCEaeo7M1WXDh/KWc=");
|
|
||||||
let entry = entry.expect("this should have been Some!");
|
|
||||||
assert_eq!(Some("frcognowin10"), entry.name);
|
|
||||||
|
|
||||||
let entry = a.get("928vO9Lf4+Mo84cWu4k1oRyzf0AR7FTGoPKHGoTMSHk=");
|
let entry = a.get("928vO9Lf4+Mo84cWu4k1oRyzf0AR7FTGoPKHGoTMSHk=");
|
||||||
let entry = entry.expect("this should have been Some!");
|
let entry = entry.expect("this should have been Some!");
|
||||||
|
Loading…
Reference in New Issue
Block a user