Improve logging

This commit is contained in:
Sapphire
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,
) -> Result<(), Box<dyn Error>> {
let Some(exe_path) = system_info.get_game_exe_path() else {
log::warn!("Unable to find game executable path.");
return Ok(());
};
firewall
+2 -2
View File
@@ -12,8 +12,8 @@ fn app_creator(
cc: &eframe::CreationContext<'_>,
) -> Result<Box<dyn eframe::App>, Box<dyn std::error::Error + Send + Sync>> {
// 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());
+32 -25
View File
@@ -32,35 +32,42 @@ impl Firewall {
direction: RuleDirection,
protocol: RuleProtocol,
) -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
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<bool, Box<dyn Error>> {