From 1b4a7a2df1fc8ccab298bacf7be32a850765761c Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Sun, 2 Jun 2019 22:06:27 +0200 Subject: [PATCH] Parse strings using `std::net` to support peers with IPv6 addresses Also alter datatype for ports to `u16` as tcp port numbers are unsigned 16bit integers. --- src/wireguard.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/wireguard.rs b/src/wireguard.rs index 5288ee0..cc8fe7c 100644 --- a/src/wireguard.rs +++ b/src/wireguard.rs @@ -4,6 +4,7 @@ use crate::wireguard_config::PeerEntryHashMap; use log::{debug, trace}; use std::collections::HashMap; use std::convert::TryFrom; +use std::net::SocketAddr; const EMPTY: &str = "(none)"; @@ -11,7 +12,7 @@ const EMPTY: &str = "(none)"; pub(crate) struct LocalEndpoint { pub public_key: String, pub private_key: String, - pub local_port: u32, + pub local_port: u16, pub persistent_keepalive: bool, } @@ -19,7 +20,7 @@ pub(crate) struct LocalEndpoint { pub(crate) struct RemoteEndpoint { pub public_key: String, pub remote_ip: Option, - pub remote_port: Option, + pub remote_port: Option, pub local_ip: String, pub local_subnet: String, pub latest_handshake: u64, @@ -69,7 +70,7 @@ impl TryFrom<&str> for WireGuard { Endpoint::Local(LocalEndpoint { public_key: v[1].to_owned(), private_key: v[2].to_owned(), - local_port: v[3].parse::().unwrap(), + local_port: v[3].parse::().unwrap(), persistent_keepalive: to_bool(v[4]), }) } else { @@ -77,10 +78,11 @@ 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 toks: Vec<&str> = ip_and_port.split(':').collect(); + let addr: SocketAddr = ip_and_port.parse::().unwrap(); + ( - Some(toks[0].to_owned()), - Some(toks[1].parse::().unwrap()), + Some(addr.ip().to_string()), + Some(addr.port()), ) } else { (None, None)