Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse strings using std::net to support peers with IPv6 addresses #5

Merged
merged 1 commit into from
Jun 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/wireguard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ 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)";

#[derive(Default, Debug, Clone)]
pub(crate) struct LocalEndpoint {
pub public_key: String,
pub private_key: String,
pub local_port: u32,
pub local_port: u16,
pub persistent_keepalive: bool,
}

#[derive(Debug, Clone)]
pub(crate) struct RemoteEndpoint {
pub public_key: String,
pub remote_ip: Option<String>,
pub remote_port: Option<u32>,
pub remote_port: Option<u16>,
pub local_ip: String,
pub local_subnet: String,
pub latest_handshake: u64,
Expand Down Expand Up @@ -69,18 +70,19 @@ 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::<u32>().unwrap(),
local_port: v[3].parse::<u16>().unwrap(),
persistent_keepalive: to_bool(v[4]),
})
} else {
// remote endpoint
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::<SocketAddr>().unwrap();

(
Some(toks[0].to_owned()),
Some(toks[1].parse::<u32>().unwrap()),
Some(addr.ip().to_string()),
Some(addr.port()),
)
} else {
(None, None)
Expand Down