diff options
Diffstat (limited to 'talpid-core')
| -rw-r--r-- | talpid-core/Cargo.toml | 1 | ||||
| -rw-r--r-- | talpid-core/src/firewall/macos.rs | 33 | ||||
| -rw-r--r-- | talpid-core/src/firewall/mod.rs | 2 | ||||
| -rw-r--r-- | talpid-core/src/lib.rs | 4 | ||||
| -rw-r--r-- | talpid-core/src/net.rs | 42 | ||||
| -rw-r--r-- | talpid-core/src/process/openvpn.rs | 6 | ||||
| -rw-r--r-- | talpid-core/src/tunnel/mod.rs | 2 |
7 files changed, 28 insertions, 62 deletions
diff --git a/talpid-core/Cargo.toml b/talpid-core/Cargo.toml index 9b9cada258..ba70f49071 100644 --- a/talpid-core/Cargo.toml +++ b/talpid-core/Cargo.toml @@ -16,6 +16,7 @@ uuid = { version = "0.5", features = ["v4"] } openvpn-plugin = { version = "0.2", features = ["serialize"] } talpid-ipc = { path = "../talpid-ipc" } +talpid-types = { path = "../talpid-types" } [target.'cfg(unix)'.dependencies] libc = "0.2.20" diff --git a/talpid-core/src/firewall/macos.rs b/talpid-core/src/firewall/macos.rs index 5be5d4764e..8f0ba7a897 100644 --- a/talpid-core/src/firewall/macos.rs +++ b/talpid-core/src/firewall/macos.rs @@ -1,7 +1,7 @@ use super::{Firewall, SecurityPolicy}; -use net; use pfctl; use std::net::Ipv4Addr; +use talpid_types::net; // alias used to instantiate firewall implementation pub type ConcreteFirewall = PacketFilter; @@ -9,15 +9,6 @@ pub use pfctl::{Error, ErrorKind, Result}; const ANCHOR_NAME: &'static str = "talpid_core"; -impl From<net::TransportProtocol> for pfctl::Proto { - fn from(protocol: net::TransportProtocol) -> Self { - match protocol { - net::TransportProtocol::Udp => pfctl::Proto::Udp, - net::TransportProtocol::Tcp => pfctl::Proto::Tcp, - } - } -} - pub struct PacketFilter { pf: pfctl::PfCtl, pf_was_enabled: Option<bool>, @@ -85,11 +76,14 @@ impl PacketFilter { } fn get_relay_rule(relay_endpoint: net::Endpoint) -> Result<pfctl::FilterRule> { + let pfctl_endpoint = as_pfctl_endpoint(relay_endpoint); + let pfctl_proto = as_pfctl_proto(relay_endpoint.protocol); + pfctl::FilterRuleBuilder::default() .action(pfctl::FilterRuleAction::Pass) .direction(pfctl::Direction::Out) - .to(relay_endpoint.address) - .proto(relay_endpoint.protocol) + .to(pfctl_endpoint) + .proto(pfctl_proto) .keep_state(pfctl::StatePolicy::Keep) .tcp_flags(Self::get_tcp_flags()) .quick(true) @@ -174,3 +168,18 @@ impl PacketFilter { self.pf.try_remove_anchor(ANCHOR_NAME, pfctl::AnchorKind::Filter) } } + +fn as_pfctl_endpoint(relay_endpoint: net::Endpoint) -> pfctl::Endpoint { + pfctl::Endpoint::new( + pfctl::Ip::from(relay_endpoint.address.ip()), + pfctl::Port::from(relay_endpoint.address.port()) + ) +} + +fn as_pfctl_proto(protocol: net::TransportProtocol) -> pfctl::Proto { + match protocol { + net::TransportProtocol::Udp => pfctl::Proto::Udp, + net::TransportProtocol::Tcp => pfctl::Proto::Tcp, + } +} + diff --git a/talpid-core/src/firewall/mod.rs b/talpid-core/src/firewall/mod.rs index 16b8139453..acc0f21792 100644 --- a/talpid-core/src/firewall/mod.rs +++ b/talpid-core/src/firewall/mod.rs @@ -1,4 +1,4 @@ -use net::Endpoint; +use talpid_types::net::Endpoint; #[cfg(target_os = "macos")] #[path = "macos.rs"] diff --git a/talpid-core/src/lib.rs b/talpid-core/src/lib.rs index 271553c10c..c4cff6ebfc 100644 --- a/talpid-core/src/lib.rs +++ b/talpid-core/src/lib.rs @@ -30,6 +30,7 @@ extern crate jsonrpc_macros; extern crate uuid; extern crate talpid_ipc; +extern crate talpid_types; extern crate openvpn_plugin; #[cfg(target_os = "macos")] @@ -38,9 +39,6 @@ extern crate pfctl; /// Working with processes. pub mod process; -/// Network primitives. -pub mod net; - /// Abstracts over different VPN tunnel technologies pub mod tunnel; diff --git a/talpid-core/src/net.rs b/talpid-core/src/net.rs deleted file mode 100644 index 9a75859d61..0000000000 --- a/talpid-core/src/net.rs +++ /dev/null @@ -1,42 +0,0 @@ -use std::net::{IpAddr, SocketAddr}; -use std::str::FromStr; - -/// Represents a network layer IP address together with the transport layer protocol and port. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct Endpoint { - /// The address part of this endpoint, contains the IP and port. - 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<T: Into<IpAddr>>(address: T, port: u16, protocol: TransportProtocol) -> Self { - Endpoint { - address: SocketAddr::new(address.into(), port), - protocol: protocol, - } - } -} - -/// Representation of a transport protocol, either UDP or TCP. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub enum TransportProtocol { - /// Represents the UDP transport protocol. - Udp, - /// Represents the TCP transport protocol. - Tcp, -} - -impl FromStr for TransportProtocol { - type Err = (); - - fn from_str(s: &str) -> ::std::result::Result<TransportProtocol, Self::Err> { - match s { - "udp" => Ok(TransportProtocol::Udp), - "tcp" => Ok(TransportProtocol::Tcp), - _ => Err(()), - } - } -} diff --git a/talpid-core/src/process/openvpn.rs b/talpid-core/src/process/openvpn.rs index ce663809ce..c756757a75 100644 --- a/talpid-core/src/process/openvpn.rs +++ b/talpid-core/src/process/openvpn.rs @@ -1,11 +1,11 @@ use duct; -use net; - use std::ffi::{OsStr, OsString}; use std::fmt; use std::path::{Path, PathBuf}; +use talpid_types::net; + static BASE_ARGUMENTS: &[&[&str]] = &[ &["--client"], &["--nobind"], @@ -190,9 +190,9 @@ fn write_argument(fmt: &mut fmt::Formatter, arg: &str) -> fmt::Result { #[cfg(test)] mod tests { use super::OpenVpnCommand; - use net::{Endpoint, TransportProtocol}; use std::ffi::OsString; use std::net::Ipv4Addr; + use talpid_types::net::{Endpoint, TransportProtocol}; #[test] fn passes_one_remote() { diff --git a/talpid-core/src/tunnel/mod.rs b/talpid-core/src/tunnel/mod.rs index a2ab25a487..83212e6874 100644 --- a/talpid-core/src/tunnel/mod.rs +++ b/talpid-core/src/tunnel/mod.rs @@ -1,5 +1,4 @@ use mktemp; -use net; use openvpn_plugin::types::OpenVpnPluginEvent; @@ -11,6 +10,7 @@ use std::ffi::{OsStr, OsString}; use std::fs; use std::io::{self, Write}; use std::path::{Path, PathBuf}; +use talpid_types::net; /// A module for all OpenVPN related tunnel management. pub mod openvpn; |
