From 0384eee42c65b2e61822b2fc244d6b97655292fa Mon Sep 17 00:00:00 2001 From: Sapphire Date: Fri, 12 Dec 2025 00:43:12 -0600 Subject: [PATCH] Improve logging --- src/features/game_networking.rs | 1 + src/gui/run.rs | 4 +-- src/util/firewall.rs | 57 ++++++++++++++++++--------------- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/features/game_networking.rs b/src/features/game_networking.rs index 1b35a91..122b43a 100644 --- a/src/features/game_networking.rs +++ b/src/features/game_networking.rs @@ -46,6 +46,7 @@ impl GameNetworking { firewall: &Firewall, ) -> Result<(), Box> { let Some(exe_path) = system_info.get_game_exe_path() else { + log::warn!("Unable to find game executable path."); return Ok(()); }; firewall diff --git a/src/gui/run.rs b/src/gui/run.rs index 6a0bea6..ba612fe 100644 --- a/src/gui/run.rs +++ b/src/gui/run.rs @@ -12,8 +12,8 @@ fn app_creator( cc: &eframe::CreationContext<'_>, ) -> Result, Box> { // initialize COM just in case - if unsafe { CoInitializeEx(None, COINIT_APARTMENTTHREADED) }.is_err() { - log::error!("couldn't initialize COM"); + if let Err(e) = unsafe { CoInitializeEx(None, COINIT_APARTMENTTHREADED) }.ok() { + log::error!("couldn't initialize COM: {e}"); } // initialize App early to modify some things before returning it let mut app = Box::new(App::default()); diff --git a/src/util/firewall.rs b/src/util/firewall.rs index 42964b2..f868238 100644 --- a/src/util/firewall.rs +++ b/src/util/firewall.rs @@ -32,35 +32,42 @@ impl Firewall { direction: RuleDirection, protocol: RuleProtocol, ) -> Result<(), Box> { - let rules = unsafe { self.policy.Rules() }?; - unsafe { rules.Remove(&BSTR::from(name)) }?; - let rule: INetFwRule = unsafe { CoCreateInstance(&NetFwRule, None, CLSCTX_INPROC_SERVER) }?; - unsafe { rule.SetName(&BSTR::from(name)) }?; - match mode { - RuleMode::Executable(exe) => { - unsafe { rule.SetApplicationName(&BSTR::from(exe.to_string_lossy().to_string())) }? + let add_rule = || { + let rules = unsafe { self.policy.Rules() }?; + unsafe { rules.Remove(&BSTR::from(name)) }?; + let rule: INetFwRule = + unsafe { CoCreateInstance(&NetFwRule, None, CLSCTX_INPROC_SERVER) }?; + unsafe { rule.SetName(&BSTR::from(name)) }?; + match mode { + RuleMode::Executable(exe) => unsafe { + rule.SetApplicationName(&BSTR::from(exe.to_string_lossy().to_string())) + }?, + RuleMode::Address(ip) => unsafe { rule.SetRemoteAddresses(&BSTR::from(ip)) }?, } - RuleMode::Address(ip) => unsafe { rule.SetRemoteAddresses(&BSTR::from(ip)) }?, - } - match direction { - RuleDirection::In => unsafe { rule.SetDirection(NET_FW_RULE_DIR_IN) }?, - RuleDirection::Out => unsafe { rule.SetDirection(NET_FW_RULE_DIR_OUT) }?, - } - unsafe { rule.SetEnabled(true.into()) }?; - unsafe { rule.SetAction(NET_FW_ACTION_BLOCK) }?; - match protocol { - RuleProtocol::Any => unsafe { rule.SetProtocol(NET_FW_IP_PROTOCOL_ANY.0) }?, - RuleProtocol::Tcp => unsafe { rule.SetProtocol(NET_FW_IP_PROTOCOL_TCP.0) }?, - RuleProtocol::Udp => unsafe { rule.SetProtocol(NET_FW_IP_PROTOCOL_UDP.0) }?, - } - unsafe { rules.Add(&rule) }?; - Ok(()) + match direction { + RuleDirection::In => unsafe { rule.SetDirection(NET_FW_RULE_DIR_IN) }?, + RuleDirection::Out => unsafe { rule.SetDirection(NET_FW_RULE_DIR_OUT) }?, + } + unsafe { rule.SetEnabled(true.into()) }?; + unsafe { rule.SetAction(NET_FW_ACTION_BLOCK) }?; + match protocol { + RuleProtocol::Any => unsafe { rule.SetProtocol(NET_FW_IP_PROTOCOL_ANY.0) }?, + RuleProtocol::Tcp => unsafe { rule.SetProtocol(NET_FW_IP_PROTOCOL_TCP.0) }?, + RuleProtocol::Udp => unsafe { rule.SetProtocol(NET_FW_IP_PROTOCOL_UDP.0) }?, + } + unsafe { rules.Add(&rule) }?; + Ok(()) + }; + add_rule().inspect_err(|e| log::warn!("Failed to add rule '{name}': {e}")) } pub fn remove(&self, name: &str) -> Result<(), Box> { - let rules = unsafe { self.policy.Rules() }?; - unsafe { rules.Remove(&BSTR::from(name)) }?; - Ok(()) + let remove_rule = || { + let rules = unsafe { self.policy.Rules() }?; + unsafe { rules.Remove(&BSTR::from(name)) }?; + Ok(()) + }; + remove_rule().inspect_err(|e| log::warn!("Failed to remove rule '{name}': {e}")) } pub fn is_blocked(&self, name: &str) -> Result> {