diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2017-08-15 21:13:38 +0100 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2017-08-15 21:13:38 +0100 |
| commit | ca6d66bc34888e2ad151e14024efa7833f5cabf9 (patch) | |
| tree | ccd5b8a61d748e6a6e3a3bc95140dbe1e34a4507 | |
| parent | 1df61e1120a30a0705a27859ecdb3f6473e8909a (diff) | |
| parent | f557c77ccc460147b92b089d411df630ecead898 (diff) | |
| download | mullvadvpn-ca6d66bc34888e2ad151e14024efa7833f5cabf9.tar.xz mullvadvpn-ca6d66bc34888e2ad151e14024efa7833f5cabf9.zip | |
Merge branch 'remove-remote-addr'
| -rw-r--r-- | mullvad-daemon/src/main.rs | 10 | ||||
| -rw-r--r-- | talpid-core/src/net.rs | 198 | ||||
| -rw-r--r-- | talpid-core/src/process/openvpn.rs | 7 |
3 files changed, 15 insertions, 200 deletions
diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index dd35239d91..7ec19da7bd 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -35,6 +35,7 @@ use jsonrpc_core::futures::sync; use management_interface::{ManagementInterfaceServer, TunnelCommand}; use mullvad_types::states::{DaemonState, SecurityState, TargetState}; use std::io; +use std::net::Ipv4Addr; use std::path::PathBuf; use std::sync::{Arc, Mutex, mpsc}; @@ -69,9 +70,12 @@ error_chain!{ lazy_static! { // Temporary store of hardcoded remotes. static ref REMOTES: [Endpoint; 3] = [ - Endpoint::new("se5.mullvad.net", 1300, TransportProtocol::Udp), - Endpoint::new("se6.mullvad.net", 1300, TransportProtocol::Udp), - Endpoint::new("se7.mullvad.net", 1300, TransportProtocol::Udp), + // se5.mullvad.net + Endpoint::new(Ipv4Addr::new(193, 138, 219, 240), 1300, TransportProtocol::Udp), + // se6.mullvad.net + Endpoint::new(Ipv4Addr::new(193, 138, 219, 241), 1300, TransportProtocol::Udp), + // se7.mullvad.net + Endpoint::new(Ipv4Addr::new(185, 65, 132, 104), 1300, TransportProtocol::Udp), ]; } diff --git a/talpid-core/src/net.rs b/talpid-core/src/net.rs index 310adee6ca..116e3f2441 100644 --- a/talpid-core/src/net.rs +++ b/talpid-core/src/net.rs @@ -1,33 +1,19 @@ -use std::fmt; -use std::net::SocketAddr; -use std::str::FromStr; - - -error_chain! { - errors { - /// Error indicating parsing the address failed - AddrParse(s: String) { - description("Invalid address format") - display("Unable to parse address. {}", s) - } - } -} - +use std::net::{IpAddr, SocketAddr}; /// Represents a network layer IP address together with the transport layer protocol and port. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Endpoint { /// The address part of this endpoint, contains the IP and port. - pub address: RemoteAddr, + pub address: SocketAddr, /// The protocol part of this endpoint. pub protocol: TransportProtocol, } impl Endpoint { /// Constructs a new `Endpoint` from the given parameters. - pub fn new(address: &str, port: u16, protocol: TransportProtocol) -> Self { + pub fn new<T: Into<IpAddr>>(address: T, port: u16, protocol: TransportProtocol) -> Self { Endpoint { - address: RemoteAddr::new(address, port), + address: SocketAddr::new(address.into(), port), protocol: protocol, } } @@ -41,179 +27,3 @@ pub enum TransportProtocol { /// Represents the TCP transport protocol. Tcp, } - -/// Representation of a TCP or UDP endpoint. The IP level address is represented by either an IP -/// directly or a hostname/domain. The IP level address together with a port becomes a socket -/// address. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum RemoteAddr { - /// Endpoint represented by an IP and a port. - SocketAddr(SocketAddr), - /// Endpoint represented by a hostname or domain and a port. - Domain(String, u16), -} - -impl RemoteAddr { - /// Constructs a new `RemoteAddr` from the given address (hostname or domain) and port. To - /// construct a `RemoteAddr` based on IP rather than domain, use the From<SocketAddr> impl. - pub fn new(address: &str, port: u16) -> Self { - RemoteAddr::Domain(address.to_owned(), port) - } - - /// Returns the address associated with this `RemoteAddr`. If it is backed by an IP that will - /// be formatted as a string. - pub fn address(&self) -> String { - match *self { - RemoteAddr::SocketAddr(ref addr) => addr.ip().to_string(), - RemoteAddr::Domain(ref address, _) => address.to_owned(), - } - } - - /// Returns the port associated with this `RemoteAddr`. - pub fn port(&self) -> u16 { - match *self { - RemoteAddr::SocketAddr(addr) => addr.port(), - RemoteAddr::Domain(_, port) => port, - } - } - - fn from_domain_str(s: &str) -> Result<Self> { - let (address, port_str) = Self::split_at_last_colon(s)?; - let port = - u16::from_str(port_str) - .chain_err(|| ErrorKind::AddrParse(format!("Invalid port: \"{}\"", port_str)),)?; - if address.is_empty() || address.contains(':') { - let msg = format!("Invalid IP or domain: \"{}\"", address); - bail!(ErrorKind::AddrParse(msg)); - } - Ok(RemoteAddr::Domain(address.to_owned(), port)) - } - - fn split_at_last_colon(s: &str) -> Result<(&str, &str)> { - let mut iter = s.rsplitn(2, ':'); - let port = iter.next().unwrap(); - let address = iter.next() - .ok_or_else(|| Error::from(ErrorKind::AddrParse("No colon".to_owned())))?; - Ok((address, port)) - } -} - -impl From<SocketAddr> for RemoteAddr { - fn from(socket_addr: SocketAddr) -> Self { - RemoteAddr::SocketAddr(socket_addr) - } -} - -impl FromStr for RemoteAddr { - type Err = Error; - fn from_str(s: &str) -> Result<Self> { - if let Ok(addr) = SocketAddr::from_str(s) { - Ok(RemoteAddr::from(addr)) - } else { - Self::from_domain_str(s) - } - } -} - -impl fmt::Display for RemoteAddr { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - match *self { - RemoteAddr::SocketAddr(ref addr) => addr.fmt(fmt), - RemoteAddr::Domain(ref address, ref port) => write!(fmt, "{}:{}", address, port), - } - } -} - - -#[cfg(test)] -mod remote_addr_tests { - use super::*; - - use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6}; - use std::str::FromStr; - - #[test] - fn new_and_getters() { - let testee = RemoteAddr::new("a_domain", 543); - assert_eq!("a_domain", testee.address()); - assert_eq!(543, testee.port()); - } - - #[test] - fn from_socket_addr() { - let socket_addr = SocketAddr::from_str("10.0.1.1:76").unwrap(); - let testee: RemoteAddr = socket_addr.into(); - assert_eq!("10.0.1.1", testee.address()); - assert_eq!(76, testee.port()); - } - - #[test] - fn from_str() { - let testee = RemoteAddr::from_str("example.com:3333").unwrap(); - assert_eq!("example.com", testee.address()); - assert_eq!(3333, testee.port()); - } - - #[test] - fn from_ipv6_str_without_brackets() { - let result = RemoteAddr::from_str("fe80::1:1337"); - assert_matches!(result, Err(Error(ErrorKind::AddrParse(_), _))); - } - - #[test] - fn from_ipv6_str_with_brackets() { - let testee = RemoteAddr::from_str("[fe80::1]:1337").unwrap(); - assert_eq!("fe80::1", testee.address()); - assert_eq!(1337, testee.port()); - } - - #[test] - fn from_ipv6_str_without_port() { - let result = RemoteAddr::from_str("fe80::1"); - assert_matches!(result, Err(Error(ErrorKind::AddrParse(_), _))); - } - - #[test] - fn from_str_no_colon() { - let result = RemoteAddr::from_str("example.com"); - assert_matches!(result, Err(Error(ErrorKind::AddrParse(_), _))); - } - - #[test] - fn from_str_invalid_port_large() { - let result = RemoteAddr::from_str("example.com:99999"); - assert_matches!(result, Err(Error(ErrorKind::AddrParse(_), _))); - } - - #[test] - fn from_str_empty_address() { - let result = RemoteAddr::from_str(":100"); - assert_matches!(result, Err(Error(ErrorKind::AddrParse(_), _))); - } - - #[test] - fn from_str_empty_port() { - let result = RemoteAddr::from_str("example.com:"); - assert_matches!(result, Err(Error(ErrorKind::AddrParse(_), _))); - } - - #[test] - fn to_string_domain() { - let testee = RemoteAddr::new("example.com", 3333); - assert_eq!("example.com:3333", testee.to_string()); - } - - #[test] - fn to_string_ipv4() { - let socket_addr = SocketAddr::V4(SocketAddrV4::from_str("127.1.2.3:1337").unwrap()); - let testee = RemoteAddr::from(socket_addr); - assert_eq!("127.1.2.3:1337", testee.to_string()); - } - - #[test] - fn to_string_ipv6() { - let socket_addr = SocketAddr::V6(SocketAddrV6::from_str("[2001:beef::1]:9876").unwrap()); - let testee = RemoteAddr::from(socket_addr); - assert_eq!("[2001:beef::1]:9876", testee.to_string()); - } -} diff --git a/talpid-core/src/process/openvpn.rs b/talpid-core/src/process/openvpn.rs index 6b671b767c..ce663809ce 100644 --- a/talpid-core/src/process/openvpn.rs +++ b/talpid-core/src/process/openvpn.rs @@ -145,7 +145,7 @@ impl OpenVpnCommand { }, ); args.push("--remote".to_owned()); - args.push(endpoint.address.address()); + args.push(endpoint.address.ip().to_string()); args.push(endpoint.address.port().to_string()); } args @@ -192,15 +192,16 @@ mod tests { use super::OpenVpnCommand; use net::{Endpoint, TransportProtocol}; use std::ffi::OsString; + use std::net::Ipv4Addr; #[test] fn passes_one_remote() { - let remote = Endpoint::new("example.com", 3333, TransportProtocol::Udp); + let remote = Endpoint::new(Ipv4Addr::new(127, 0, 0, 1), 3333, TransportProtocol::Udp); let testee_args = OpenVpnCommand::new("").remote(remote).get_arguments(); assert!(testee_args.contains(&OsString::from("udp"))); - assert!(testee_args.contains(&OsString::from("example.com"))); + assert!(testee_args.contains(&OsString::from("127.0.0.1"))); assert!(testee_args.contains(&OsString::from("3333"))); } |
