Skip to content

Commit

Permalink
Strip link-local zone ids from IPv6 socket addrs
Browse files Browse the repository at this point in the history
This is a fairly ugly hack to temporarily work around the issue #10.
Until now Rust doesn't support so-called zone-ids[1] in link-local IPv6
socket-addresses and has a pending RFC on this topic. As I'm
encountering this issue on one of my machines I decided to work around
this issue (for now) by removing the zone-id from the IPv6 address.

This won't harm any other cases as `regex.replace_all` simply returns
the input if the regex doesn't match:

```
>> extern crate regex;
>> let re = regex::Regex::new(r"^\[(?P<ip>[A-Fa-f0-9:]+)%(.*)\]:(?P<port>[0-9]+)$");
>> let rs = re.replace_all("fairly unrelated stuff", "[$ip]:$port");
>> rs
"fairly unrelated stuff"
```

Please note that (1) this regex isn't RFC-compliant[2] and is just a
heuristic to remove zone-ids that currently break this exporter and
(2) this is something that probably shouldn't be merged as-is. I mainly
pushed this since I needed a workaround for this issue (and others
probably too), but it may be better to wait for proper support from the
language.

[1] https://tools.ietf.org/html/rfc4007#section-11
  • Loading branch information
Ma27 committed Nov 2, 2019
1 parent 09aa62a commit 9a54a8f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 27 deletions.
35 changes: 10 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ failure = "0.1.5"
hyper = "0.12.29"
http = "0.1.17"
prometheus_exporter_base = "0.2.0"

regex = "1.3.1"
4 changes: 3 additions & 1 deletion src/wireguard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use prometheus_exporter_base::PrometheusCounter;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::net::SocketAddr;
use regex::Regex;

const EMPTY: &str = "(none)";

Expand Down Expand Up @@ -77,7 +78,8 @@ impl TryFrom<&str> for WireGuard {
let public_key = v[1].to_owned();

let (remote_ip, remote_port) = if let Some(ip_and_port) = to_option_string(v[3]) {
let addr: SocketAddr = ip_and_port.parse::<SocketAddr>().unwrap();
let re = Regex::new(r"^\[(?P<ip>[A-Fa-f0-9:]+)%(.*)\]:(?P<port>[0-9]+)$").unwrap();
let addr: SocketAddr = re.replace_all(&ip_and_port, "[$ip]:$port").parse::<SocketAddr>().unwrap();

(Some(addr.ip().to_string()), Some(addr.port()))
} else {
Expand Down

0 comments on commit 9a54a8f

Please sign in to comment.