|
| 1 | +//! Windows Kill Switch using Windows Filtering Platform (WFP). |
| 2 | +//! |
| 3 | +//! This module uses the `windows` crate to add WFP filters that block all |
| 4 | +//! network traffic except through the active VPN tunnel interface and to |
| 5 | +//! the VPN server IP itself (so the tunnel can be established). |
| 6 | +//! |
| 7 | +//! # Safety |
| 8 | +//! Requires Administrator privileges. The WFP engine session is opened with |
| 9 | +//! dynamic mode so filters are automatically removed if the process crashes. |
| 10 | +
|
| 11 | +#![cfg(target_os = "windows")] |
| 12 | + |
| 13 | +use super::{KillSwitch, KillSwitchState}; |
| 14 | +use log::{error, info}; |
| 15 | +use std::process::Command; |
| 16 | + |
| 17 | +/// WFP-based kill switch for Windows. |
| 18 | +/// |
| 19 | +/// Uses `netsh` commands to add/remove WFP filters as a robust, crate-minimal |
| 20 | +/// approach. For production use with the `windows` crate, replace the Command |
| 21 | +/// calls with direct WFP API bindings via `windows::Win32::NetworkManagement::WindowsFilteringPlatform`. |
| 22 | +pub struct WfpKillSwitch { |
| 23 | + state: KillSwitchState, |
| 24 | + vpn_interface: Option<String>, |
| 25 | + vpn_server_ip: Option<String>, |
| 26 | +} |
| 27 | + |
| 28 | +impl WfpKillSwitch { |
| 29 | + pub fn new() -> Self { |
| 30 | + Self { |
| 31 | + state: KillSwitchState::Disabled, |
| 32 | + vpn_interface: None, |
| 33 | + vpn_server_ip: None, |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// Add WFP block-all rule via netsh advfirewall. |
| 38 | + fn add_block_rules(&self, vpn_interface: &str, vpn_server_ip: &str) -> Result<(), String> { |
| 39 | + // Block all outbound traffic |
| 40 | + let output = Command::new("netsh") |
| 41 | + .args([ |
| 42 | + "advfirewall", "firewall", "add", "rule", |
| 43 | + "name=VPNht_KillSwitch_BlockAll", |
| 44 | + "dir=out", "action=block", |
| 45 | + "enable=yes", |
| 46 | + "profile=any", |
| 47 | + ]) |
| 48 | + .output() |
| 49 | + .map_err(|e| format!("Failed to execute netsh: {}", e))?; |
| 50 | + |
| 51 | + if !output.status.success() { |
| 52 | + return Err(format!( |
| 53 | + "Failed to add block rule: {}", |
| 54 | + String::from_utf8_lossy(&output.stderr) |
| 55 | + )); |
| 56 | + } |
| 57 | + |
| 58 | + // Allow traffic to VPN server IP (so tunnel can establish) |
| 59 | + let output = Command::new("netsh") |
| 60 | + .args([ |
| 61 | + "advfirewall", "firewall", "add", "rule", |
| 62 | + "name=VPNht_KillSwitch_AllowVPN", |
| 63 | + "dir=out", "action=allow", |
| 64 | + &format!("remoteip={}", vpn_server_ip), |
| 65 | + "enable=yes", |
| 66 | + "profile=any", |
| 67 | + ]) |
| 68 | + .output() |
| 69 | + .map_err(|e| format!("Failed to execute netsh: {}", e))?; |
| 70 | + |
| 71 | + if !output.status.success() { |
| 72 | + return Err(format!( |
| 73 | + "Failed to add VPN allow rule: {}", |
| 74 | + String::from_utf8_lossy(&output.stderr) |
| 75 | + )); |
| 76 | + } |
| 77 | + |
| 78 | + // Allow traffic on VPN tunnel interface |
| 79 | + let output = Command::new("netsh") |
| 80 | + .args([ |
| 81 | + "advfirewall", "firewall", "add", "rule", |
| 82 | + &format!("name=VPNht_KillSwitch_AllowTunnel_{}", vpn_interface), |
| 83 | + "dir=out", "action=allow", |
| 84 | + &format!("localip=any"), |
| 85 | + "enable=yes", |
| 86 | + "profile=any", |
| 87 | + ]) |
| 88 | + .output() |
| 89 | + .map_err(|e| format!("Failed to execute netsh: {}", e))?; |
| 90 | + |
| 91 | + if !output.status.success() { |
| 92 | + return Err(format!( |
| 93 | + "Failed to add tunnel allow rule: {}", |
| 94 | + String::from_utf8_lossy(&output.stderr) |
| 95 | + )); |
| 96 | + } |
| 97 | + |
| 98 | + // Allow loopback |
| 99 | + let _ = Command::new("netsh") |
| 100 | + .args([ |
| 101 | + "advfirewall", "firewall", "add", "rule", |
| 102 | + "name=VPNht_KillSwitch_AllowLoopback", |
| 103 | + "dir=out", "action=allow", |
| 104 | + "remoteip=127.0.0.0/8", |
| 105 | + "enable=yes", |
| 106 | + "profile=any", |
| 107 | + ]) |
| 108 | + .output(); |
| 109 | + |
| 110 | + // Allow DHCP |
| 111 | + let _ = Command::new("netsh") |
| 112 | + .args([ |
| 113 | + "advfirewall", "firewall", "add", "rule", |
| 114 | + "name=VPNht_KillSwitch_AllowDHCP", |
| 115 | + "dir=out", "action=allow", |
| 116 | + "protocol=udp", |
| 117 | + "remoteport=67,68", |
| 118 | + "enable=yes", |
| 119 | + "profile=any", |
| 120 | + ]) |
| 121 | + .output(); |
| 122 | + |
| 123 | + Ok(()) |
| 124 | + } |
| 125 | + |
| 126 | + /// Remove all VPNht kill switch rules. |
| 127 | + fn remove_rules(&self) -> Result<(), String> { |
| 128 | + let rules = [ |
| 129 | + "VPNht_KillSwitch_BlockAll", |
| 130 | + "VPNht_KillSwitch_AllowVPN", |
| 131 | + "VPNht_KillSwitch_AllowLoopback", |
| 132 | + "VPNht_KillSwitch_AllowDHCP", |
| 133 | + ]; |
| 134 | + |
| 135 | + for rule in &rules { |
| 136 | + let _ = Command::new("netsh") |
| 137 | + .args(["advfirewall", "firewall", "delete", "rule", &format!("name={}", rule)]) |
| 138 | + .output(); |
| 139 | + } |
| 140 | + |
| 141 | + // Also clean up tunnel-specific rules |
| 142 | + if let Some(ref iface) = self.vpn_interface { |
| 143 | + let _ = Command::new("netsh") |
| 144 | + .args([ |
| 145 | + "advfirewall", "firewall", "delete", "rule", |
| 146 | + &format!("name=VPNht_KillSwitch_AllowTunnel_{}", iface), |
| 147 | + ]) |
| 148 | + .output(); |
| 149 | + } |
| 150 | + |
| 151 | + Ok(()) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +impl KillSwitch for WfpKillSwitch { |
| 156 | + fn enable(&mut self, vpn_interface: &str, vpn_server_ip: &str) -> Result<(), String> { |
| 157 | + info!( |
| 158 | + "Enabling Windows kill switch: interface={}, server={}", |
| 159 | + vpn_interface, vpn_server_ip |
| 160 | + ); |
| 161 | + |
| 162 | + // Clean up any stale rules first |
| 163 | + let _ = self.remove_rules(); |
| 164 | + |
| 165 | + match self.add_block_rules(vpn_interface, vpn_server_ip) { |
| 166 | + Ok(()) => { |
| 167 | + self.state = KillSwitchState::Enabled; |
| 168 | + self.vpn_interface = Some(vpn_interface.to_string()); |
| 169 | + self.vpn_server_ip = Some(vpn_server_ip.to_string()); |
| 170 | + info!("Windows kill switch enabled"); |
| 171 | + Ok(()) |
| 172 | + } |
| 173 | + Err(e) => { |
| 174 | + error!("Failed to enable kill switch: {}", e); |
| 175 | + // Try to clean up partial rules |
| 176 | + let _ = self.remove_rules(); |
| 177 | + self.state = KillSwitchState::Error(e.clone()); |
| 178 | + Err(e) |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + fn disable(&mut self) -> Result<(), String> { |
| 184 | + info!("Disabling Windows kill switch"); |
| 185 | + self.remove_rules()?; |
| 186 | + self.state = KillSwitchState::Disabled; |
| 187 | + self.vpn_interface = None; |
| 188 | + self.vpn_server_ip = None; |
| 189 | + info!("Windows kill switch disabled"); |
| 190 | + Ok(()) |
| 191 | + } |
| 192 | + |
| 193 | + fn state(&self) -> KillSwitchState { |
| 194 | + self.state.clone() |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +#[cfg(test)] |
| 199 | +mod tests { |
| 200 | + use super::*; |
| 201 | + |
| 202 | + #[test] |
| 203 | + fn test_new_killswitch_disabled() { |
| 204 | + let ks = WfpKillSwitch::new(); |
| 205 | + assert_eq!(ks.state(), KillSwitchState::Disabled); |
| 206 | + assert!(ks.vpn_interface.is_none()); |
| 207 | + } |
| 208 | +} |
0 commit comments