diff options
| -rw-r--r-- | talpid-core/src/firewall/macos/mod.rs | 7 | ||||
| -rw-r--r-- | talpid-core/src/firewall/mod.rs | 71 | ||||
| -rw-r--r-- | talpid-core/src/firewall/windows.rs | 7 |
3 files changed, 25 insertions, 60 deletions
diff --git a/talpid-core/src/firewall/macos/mod.rs b/talpid-core/src/firewall/macos/mod.rs index b5e405adb1..cb90074938 100644 --- a/talpid-core/src/firewall/macos/mod.rs +++ b/talpid-core/src/firewall/macos/mod.rs @@ -19,9 +19,6 @@ error_chain! { } } -/// alias used to instantiate firewall implementation -pub type ConcreteFirewall = PacketFilter; - const ANCHOR_NAME: &'static str = "mullvad"; /// The macOS firewall implementation. Acting as converter between the `Firewall` trait API @@ -32,7 +29,9 @@ pub struct PacketFilter { dns_monitor: DnsMonitor, } -impl Firewall<Error> for PacketFilter { +impl Firewall for PacketFilter { + type Error = Error; + fn new() -> Result<Self> { Ok(PacketFilter { pf: pfctl::PfCtl::new()?, diff --git a/talpid-core/src/firewall/mod.rs b/talpid-core/src/firewall/mod.rs index c0f839817d..cec1a66d94 100644 --- a/talpid-core/src/firewall/mod.rs +++ b/talpid-core/src/firewall/mod.rs @@ -1,35 +1,5 @@ use talpid_types::net::Endpoint; -/// macOS implementation of the firewall/security policy enforcer. -#[cfg(target_os = "macos")] -pub mod macos; -#[cfg(target_os = "macos")] -use self::macos as imp; - -/// Linux implementation of the firewall/security policy enforcer. -#[cfg(all(unix, not(target_os = "macos")))] -pub mod unix; -#[cfg(all(unix, not(target_os = "macos")))] -use self::unix as imp; - -/// Windows implementation of the firewall/security policy enforcer. -#[cfg(windows)] -pub mod windows; -#[cfg(windows)] -use self::windows as imp; - -error_chain!{ - errors { - /// Initialization error - FirewallInitError { - description("Failed to initialize firewall") - } - /// Firewall configuration error - FirewallConfigurationError { - description("Failed to configure firewall") - } - } -} /// A enum that describes firewall rules strategy #[derive(Debug, Clone, Eq, PartialEq)] @@ -54,38 +24,35 @@ pub enum SecurityPolicy { } /// Abstract firewall interaction trait -pub trait Firewall<E: ::std::error::Error> { +pub trait Firewall { + /// The error type thrown by the implementer of this trait + type Error: ::std::error::Error; + /// Create new instance of Firewall - fn new() -> ::std::result::Result<Self, E> + fn new() -> ::std::result::Result<Self, Self::Error> where Self: Sized; /// Enable firewall and set firewall rules based on SecurityPolicy - fn apply_policy(&mut self, policy: SecurityPolicy) -> ::std::result::Result<(), E>; + fn apply_policy(&mut self, policy: SecurityPolicy) -> ::std::result::Result<(), Self::Error>; /// Remove firewall rules applied by active SecurityPolicy and /// revert firewall to its original state - fn reset_policy(&mut self) -> ::std::result::Result<(), E>; + fn reset_policy(&mut self) -> ::std::result::Result<(), Self::Error>; } -/// An abstraction around platform specific firewall implementation -pub struct FirewallProxy(Box<Firewall<imp::Error>>); -impl Firewall<Error> for FirewallProxy { - fn new() -> Result<Self> { - let firewall = imp::ConcreteFirewall::new().chain_err(|| ErrorKind::FirewallInitError)?; - Ok(FirewallProxy(Box::new(firewall) as Box<Firewall<_>>)) - } +#[cfg(target_os = "macos")] +mod macos; +#[cfg(target_os = "macos")] +pub use self::macos::{Error, ErrorKind, PacketFilter as FirewallProxy, Result}; - fn apply_policy(&mut self, policy: SecurityPolicy) -> Result<()> { - self.0 - .apply_policy(policy) - .chain_err(|| ErrorKind::FirewallConfigurationError) - } +#[cfg(target_os = "linux")] +mod linux; +#[cfg(target_os = "linux")] +pub use self::linux::{Error, ErrorKind, Netfilter as FirewallProxy, Result}; - fn reset_policy(&mut self) -> Result<()> { - self.0 - .reset_policy() - .chain_err(|| ErrorKind::FirewallConfigurationError) - } -} +#[cfg(windows)] +mod windows; +#[cfg(windows)] +pub use self::windows::{Error, ErrorKind, Result, WindowsFirewall as FirewallProxy}; diff --git a/talpid-core/src/firewall/windows.rs b/talpid-core/src/firewall/windows.rs index bae506fdc5..4df9d87e6a 100644 --- a/talpid-core/src/firewall/windows.rs +++ b/talpid-core/src/firewall/windows.rs @@ -1,13 +1,12 @@ use super::{Firewall, SecurityPolicy}; -/// alias used to instantiate firewall implementation -pub type ConcreteFirewall = WindowsFirewall; - error_chain!{} /// The Windows implementation for the `Firewall` trait. pub struct WindowsFirewall; -impl Firewall<Error> for WindowsFirewall { +impl Firewall for WindowsFirewall { + type Error = Error; + fn new() -> Result<Self> { Ok(WindowsFirewall) } |
