Improve logging

This commit is contained in:
2025-12-12 09:36:21 +00:00
committed by futile
parent 89a320fd0f
commit 0384eee42c
3 changed files with 35 additions and 27 deletions
+1
View File
@@ -46,6 +46,7 @@ impl GameNetworking {
firewall: &Firewall, firewall: &Firewall,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
let Some(exe_path) = system_info.get_game_exe_path() else { let Some(exe_path) = system_info.get_game_exe_path() else {
log::warn!("Unable to find game executable path.");
return Ok(()); return Ok(());
}; };
firewall firewall
+2 -2
View File
@@ -12,8 +12,8 @@ fn app_creator(
cc: &eframe::CreationContext<'_>, cc: &eframe::CreationContext<'_>,
) -> Result<Box<dyn eframe::App>, Box<dyn std::error::Error + Send + Sync>> { ) -> Result<Box<dyn eframe::App>, Box<dyn std::error::Error + Send + Sync>> {
// initialize COM just in case // initialize COM just in case
if unsafe { CoInitializeEx(None, COINIT_APARTMENTTHREADED) }.is_err() { if let Err(e) = unsafe { CoInitializeEx(None, COINIT_APARTMENTTHREADED) }.ok() {
log::error!("couldn't initialize COM"); log::error!("couldn't initialize COM: {e}");
} }
// initialize App early to modify some things before returning it // initialize App early to modify some things before returning it
let mut app = Box::new(App::default()); let mut app = Box::new(App::default());
+32 -25
View File
@@ -32,35 +32,42 @@ impl Firewall {
direction: RuleDirection, direction: RuleDirection,
protocol: RuleProtocol, protocol: RuleProtocol,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
let rules = unsafe { self.policy.Rules() }?; let add_rule = || {
unsafe { rules.Remove(&BSTR::from(name)) }?; let rules = unsafe { self.policy.Rules() }?;
let rule: INetFwRule = unsafe { CoCreateInstance(&NetFwRule, None, CLSCTX_INPROC_SERVER) }?; unsafe { rules.Remove(&BSTR::from(name)) }?;
unsafe { rule.SetName(&BSTR::from(name)) }?; let rule: INetFwRule =
match mode { unsafe { CoCreateInstance(&NetFwRule, None, CLSCTX_INPROC_SERVER) }?;
RuleMode::Executable(exe) => { unsafe { rule.SetName(&BSTR::from(name)) }?;
unsafe { rule.SetApplicationName(&BSTR::from(exe.to_string_lossy().to_string())) }? 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) }?,
match direction { RuleDirection::Out => unsafe { rule.SetDirection(NET_FW_RULE_DIR_OUT) }?,
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) }?;
unsafe { rule.SetEnabled(true.into()) }?; match protocol {
unsafe { rule.SetAction(NET_FW_ACTION_BLOCK) }?; RuleProtocol::Any => unsafe { rule.SetProtocol(NET_FW_IP_PROTOCOL_ANY.0) }?,
match protocol { RuleProtocol::Tcp => unsafe { rule.SetProtocol(NET_FW_IP_PROTOCOL_TCP.0) }?,
RuleProtocol::Any => unsafe { rule.SetProtocol(NET_FW_IP_PROTOCOL_ANY.0) }?, RuleProtocol::Udp => unsafe { rule.SetProtocol(NET_FW_IP_PROTOCOL_UDP.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(())
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<dyn Error>> { pub fn remove(&self, name: &str) -> Result<(), Box<dyn Error>> {
let rules = unsafe { self.policy.Rules() }?; let remove_rule = || {
unsafe { rules.Remove(&BSTR::from(name)) }?; let rules = unsafe { self.policy.Rules() }?;
Ok(()) 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<bool, Box<dyn Error>> { pub fn is_blocked(&self, name: &str) -> Result<bool, Box<dyn Error>> {