diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2018-10-10 10:34:31 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2018-10-10 12:36:00 +0200 |
| commit | 0cee580f39f763442772e36692164c27977d6e76 (patch) | |
| tree | 0a594e6e421e8f0f3e2310eda1896c8047f3f390 | |
| parent | 39048593373b5c1247286dfc2376ed7013a66ac4 (diff) | |
| download | mullvadvpn-0cee580f39f763442772e36692164c27977d6e76.tar.xz mullvadvpn-0cee580f39f763442772e36692164c27977d6e76.zip | |
Make relay constraints and parameters pretty printable
| -rw-r--r-- | mullvad-cli/src/cmds/relay.rs | 2 | ||||
| -rw-r--r-- | mullvad-types/src/custom_tunnel.rs | 12 | ||||
| -rw-r--r-- | mullvad-types/src/relay_constraints.rs | 75 | ||||
| -rw-r--r-- | mullvad-types/src/settings.rs | 2 | ||||
| -rw-r--r-- | talpid-types/src/net.rs | 27 |
5 files changed, 114 insertions, 4 deletions
diff --git a/mullvad-cli/src/cmds/relay.rs b/mullvad-cli/src/cmds/relay.rs index eda57070fb..e056505069 100644 --- a/mullvad-cli/src/cmds/relay.rs +++ b/mullvad-cli/src/cmds/relay.rs @@ -205,7 +205,7 @@ impl Relay { fn get(&self) -> Result<()> { let mut rpc = new_rpc_client()?; let constraints = rpc.get_settings()?.get_relay_settings(); - println!("Current constraints: {:#?}", constraints); + println!("Current constraints: {}", constraints); Ok(()) } diff --git a/mullvad-types/src/custom_tunnel.rs b/mullvad-types/src/custom_tunnel.rs index 5542ef9e71..9be3ec11f4 100644 --- a/mullvad-types/src/custom_tunnel.rs +++ b/mullvad-types/src/custom_tunnel.rs @@ -1,5 +1,7 @@ -use std::net::{IpAddr, ToSocketAddrs}; - +use std::{ + fmt, + net::{IpAddr, ToSocketAddrs}, +}; use talpid_types::net::{TunnelEndpoint, TunnelEndpointData}; error_chain!{ @@ -26,6 +28,12 @@ impl CustomTunnelEndpoint { } } +impl fmt::Display for CustomTunnelEndpoint { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{} over {}", self.host, self.tunnel) + } +} + /// Does a DNS lookup if the host isn't an IP. /// Returns the first IPv4 address if one exists, otherwise the first IPv6 address. /// Rust only provides means to resolve a socket addr, not just a host, for some reason. So diff --git a/mullvad-types/src/relay_constraints.rs b/mullvad-types/src/relay_constraints.rs index e9f8f80455..2a2507fa47 100644 --- a/mullvad-types/src/relay_constraints.rs +++ b/mullvad-types/src/relay_constraints.rs @@ -57,6 +57,17 @@ pub enum RelaySettings { Normal(RelayConstraints), } +impl fmt::Display for RelaySettings { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match self { + RelaySettings::CustomTunnelEndpoint(endpoint) => { + write!(f, "custom endpoint {}", endpoint) + } + RelaySettings::Normal(constraints) => constraints.fmt(f), + } + } +} + impl Default for RelaySettings { fn default() -> Self { RelaySettings::Normal(RelayConstraints::default()) @@ -95,6 +106,20 @@ impl RelayConstraints { } } +impl fmt::Display for RelayConstraints { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match self.tunnel { + Constraint::Any => write!(f, "any relay")?, + Constraint::Only(ref tunnel_constraint) => tunnel_constraint.fmt(f)?, + } + write!(f, " in ")?; + match self.location { + Constraint::Any => write!(f, "any location"), + Constraint::Only(ref location_constraint) => location_constraint.fmt(f), + } + } +} + #[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)] #[serde(rename_all = "snake_case")] @@ -107,6 +132,18 @@ pub enum LocationConstraint { Hostname(CountryCode, CityCode, Hostname), } +impl fmt::Display for LocationConstraint { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match self { + LocationConstraint::Country(country) => write!(f, "country {}", country), + LocationConstraint::City(country, city) => write!(f, "city {}, {}", city, country), + LocationConstraint::Hostname(country, city, hostname) => { + write!(f, "city {}, {}, hostname {}", city, country, hostname) + } + } + } +} + #[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)] pub enum TunnelConstraints { @@ -116,6 +153,21 @@ pub enum TunnelConstraints { Wireguard(WireguardConstraints), } +impl fmt::Display for TunnelConstraints { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match self { + TunnelConstraints::OpenVpn(openvpn_constraints) => { + write!(f, "OpenVPN over ")?; + openvpn_constraints.fmt(f) + } + TunnelConstraints::Wireguard(wireguard_constraints) => { + write!(f, "Wireguard over ")?; + wireguard_constraints.fmt(f) + } + } + } +} + impl Match<OpenVpnEndpointData> for TunnelConstraints { fn matches(&self, endpoint: &OpenVpnEndpointData) -> bool { match *self { @@ -140,6 +192,20 @@ pub struct OpenVpnConstraints { pub protocol: Constraint<TransportProtocol>, } +impl fmt::Display for OpenVpnConstraints { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match self.port { + Constraint::Any => write!(f, "any port")?, + Constraint::Only(port) => write!(f, "port {}", port)?, + } + write!(f, " over ")?; + match self.protocol { + Constraint::Any => write!(f, "any protocol"), + Constraint::Only(protocol) => write!(f, "{}", protocol), + } + } +} + impl Match<OpenVpnEndpointData> for OpenVpnConstraints { fn matches(&self, endpoint: &OpenVpnEndpointData) -> bool { self.port.matches(&endpoint.port) && self.protocol.matches(&endpoint.protocol) @@ -151,6 +217,15 @@ pub struct WireguardConstraints { pub port: Constraint<u16>, } +impl fmt::Display for WireguardConstraints { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match self.port { + Constraint::Any => write!(f, "any port"), + Constraint::Only(port) => write!(f, "port {}", port), + } + } +} + impl Match<WireguardEndpointData> for WireguardConstraints { fn matches(&self, endpoint: &WireguardEndpointData) -> bool { self.port.matches(&endpoint.port) diff --git a/mullvad-types/src/settings.rs b/mullvad-types/src/settings.rs index 231a1b638d..bca23e1786 100644 --- a/mullvad-types/src/settings.rs +++ b/mullvad-types/src/settings.rs @@ -136,7 +136,7 @@ impl Settings { let new_settings = self.relay_settings.merge(update); if self.relay_settings != new_settings { debug!( - "changing relay settings from {:?} to {:?}", + "changing relay settings from {} to {}", self.relay_settings, new_settings ); diff --git a/talpid-types/src/net.rs b/talpid-types/src/net.rs index 31edfb7d48..e00d0d9fe4 100644 --- a/talpid-types/src/net.rs +++ b/talpid-types/src/net.rs @@ -33,6 +33,21 @@ pub enum TunnelEndpointData { Wireguard(WireguardEndpointData), } +impl fmt::Display for TunnelEndpointData { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + match self { + TunnelEndpointData::OpenVpn(openvpn_data) => { + write!(f, "OpenVPN ")?; + openvpn_data.fmt(f) + } + TunnelEndpointData::Wireguard(wireguard_data) => { + write!(f, "Wireguard ")?; + wireguard_data.fmt(f) + } + } + } +} + impl TunnelEndpointData { pub fn port(self) -> u16 { match self { @@ -55,11 +70,23 @@ pub struct OpenVpnEndpointData { pub protocol: TransportProtocol, } +impl fmt::Display for OpenVpnEndpointData { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + write!(f, "{} port {}", self.protocol, self.port) + } +} + #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Deserialize, Serialize)] pub struct WireguardEndpointData { pub port: u16, } +impl fmt::Display for WireguardEndpointData { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + write!(f, "port {}", self.port) + } +} + /// Represents a network layer IP address together with the transport layer protocol and port. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
