game_networking: turn blocked into enum

Allows us to keep track of what's blocked without querying rules constantly.
This commit is contained in:
2025-12-12 09:36:21 +00:00
committed by futile
parent 53fd0112c2
commit 58fd6b7e9c
3 changed files with 53 additions and 38 deletions
+38 -29
View File
@@ -6,13 +6,22 @@ use crate::{
}, },
}; };
use std::error::Error; use std::error::Error;
use strum::{Display, EnumIter};
const FILTER_NAME_EXE: &str = "[GTA Tools] Block outbound traffic for all of GTA V"; const FILTER_NAME_EXE: &str = "[GTA Tools] Block outbound traffic for all of GTA V";
const FILTER_NAME_SAVE_SERVER: &str = "[GTA Tools] Block outbound traffic to Rockstar save server"; const FILTER_NAME_SAVE_SERVER: &str = "[GTA Tools] Block outbound traffic to Rockstar save server";
#[derive(Clone, Copy, Debug, Default, Display, EnumIter, PartialEq)]
pub enum BlockedStatus {
#[default]
NotBlocked,
ServerBlocked,
ExeBlocked,
}
#[derive(Debug)] #[derive(Debug)]
pub struct GameNetworking { pub struct GameNetworking {
pub blocked: bool, pub blocked: BlockedStatus,
} }
impl Default for GameNetworking { impl Default for GameNetworking {
@@ -20,9 +29,11 @@ impl Default for GameNetworking {
let firewall = Firewall::default(); let firewall = Firewall::default();
Self { Self {
blocked: if firewall.is_blocked(FILTER_NAME_SAVE_SERVER).unwrap() { blocked: if firewall.is_blocked(FILTER_NAME_SAVE_SERVER).unwrap() {
true BlockedStatus::ServerBlocked
} else if firewall.is_blocked(FILTER_NAME_EXE).unwrap() {
BlockedStatus::ExeBlocked
} else { } else {
firewall.is_blocked(FILTER_NAME_EXE).unwrap() BlockedStatus::NotBlocked
}, },
} }
} }
@@ -37,20 +48,20 @@ impl GameNetworking {
let Some(exe_path) = system_info.get_game_exe_path() else { let Some(exe_path) = system_info.get_game_exe_path() else {
return Ok(()); return Ok(());
}; };
firewall.add( firewall
FILTER_NAME_EXE, .add(
RuleMode::Executable(exe_path.to_path_buf()), FILTER_NAME_EXE,
RuleDirection::Out, RuleMode::Executable(exe_path.to_path_buf()),
RuleProtocol::Any, RuleDirection::Out,
)?; RuleProtocol::Any,
self.blocked = firewall.is_blocked(FILTER_NAME_EXE)?; )
Ok(()) .inspect(|_| self.blocked = BlockedStatus::ExeBlocked)
} }
pub fn unblock_exe(&mut self, firewall: &Firewall) -> Result<(), Box<dyn Error>> { pub fn unblock_exe(&mut self, firewall: &Firewall) -> Result<(), Box<dyn Error>> {
firewall.remove(FILTER_NAME_EXE)?; firewall
self.blocked = firewall.is_blocked(FILTER_NAME_EXE)?; .remove(FILTER_NAME_EXE)
Ok(()) .inspect(|_| self.blocked = BlockedStatus::NotBlocked)
} }
pub fn block_save_server( pub fn block_save_server(
@@ -58,20 +69,20 @@ impl GameNetworking {
save_server_ip: &str, save_server_ip: &str,
firewall: &Firewall, firewall: &Firewall,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
firewall.add( firewall
FILTER_NAME_SAVE_SERVER, .add(
RuleMode::Address(save_server_ip.to_owned()), FILTER_NAME_SAVE_SERVER,
RuleDirection::Out, RuleMode::Address(save_server_ip.to_owned()),
RuleProtocol::Any, RuleDirection::Out,
)?; RuleProtocol::Any,
self.blocked = firewall.is_blocked(FILTER_NAME_SAVE_SERVER)?; )
Ok(()) .inspect(|_| self.blocked = BlockedStatus::ServerBlocked)
} }
pub fn unblock_save_server(&mut self, firewall: &Firewall) -> Result<(), Box<dyn Error>> { pub fn unblock_save_server(&mut self, firewall: &Firewall) -> Result<(), Box<dyn Error>> {
firewall.remove(FILTER_NAME_SAVE_SERVER)?; firewall
self.blocked = firewall.is_blocked(FILTER_NAME_SAVE_SERVER)?; .remove(FILTER_NAME_SAVE_SERVER)
Ok(()) .inspect(|_| self.blocked = BlockedStatus::NotBlocked)
} }
pub fn ensure_block_exclusivity( pub fn ensure_block_exclusivity(
@@ -81,15 +92,13 @@ impl GameNetworking {
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
match block_method { match block_method {
BlockMethod::EntireGame => { BlockMethod::EntireGame => {
if firewall.is_blocked(FILTER_NAME_SAVE_SERVER)? { if self.blocked == BlockedStatus::ServerBlocked {
self.unblock_save_server(firewall)?; self.unblock_save_server(firewall)?;
self.blocked = firewall.is_blocked(FILTER_NAME_EXE)?;
} }
} }
BlockMethod::SaveServer => { BlockMethod::SaveServer => {
if firewall.is_blocked(FILTER_NAME_EXE)? { if self.blocked == BlockedStatus::ExeBlocked {
self.unblock_exe(firewall)?; self.unblock_exe(firewall)?;
self.blocked = firewall.is_blocked(FILTER_NAME_SAVE_SERVER)?;
} }
} }
} }
+13 -5
View File
@@ -1,5 +1,5 @@
use crate::{ use crate::{
features, features::{self, game_networking::BlockedStatus},
gui::{ gui::{
settings::{BlockMethod, ROCKSTAR_SAVE_SERVER, Settings}, settings::{BlockMethod, ROCKSTAR_SAVE_SERVER, Settings},
tools, tools,
@@ -163,10 +163,18 @@ impl App {
BlockMethod::SaveServer => ui.label("Rockstar save server access"), BlockMethod::SaveServer => ui.label("Rockstar save server access"),
}; };
ui.add_space(1.0); ui.add_space(1.0);
ui.create_indicator_dot(if self.game_networking.blocked { ui.create_indicator_dot(match self.game_networking.blocked {
colours::RED BlockedStatus::ExeBlocked
} else { if self.settings.block_method == BlockMethod::EntireGame =>
colours::GREEN {
colours::RED
}
BlockedStatus::ServerBlocked
if self.settings.block_method == BlockMethod::SaveServer =>
{
colours::RED
}
_ => colours::GREEN,
}); });
label label
}); });
+2 -4
View File
@@ -2,6 +2,7 @@ use crate::{
gui::{ gui::{
app::{App, WINDOW_SIZE}, app::{App, WINDOW_SIZE},
tools, tools,
ui_ext::UiExt,
}, },
util::consts::game::{EXE_ENHANCED, EXE_LEGACY}, util::consts::game::{EXE_ENHANCED, EXE_LEGACY},
}; };
@@ -26,10 +27,7 @@ impl App {
ui.label("blocked"); ui.label("blocked");
egui::ComboBox::from_id_salt("blocked") egui::ComboBox::from_id_salt("blocked")
.selected_text(self.game_networking.blocked.to_string()) .selected_text(self.game_networking.blocked.to_string())
.show_ui(ui, |ui| { .show_ui(ui, |ui| ui.build_menu(&mut self.game_networking.blocked));
ui.selectable_value(&mut self.game_networking.blocked, true, "true");
ui.selectable_value(&mut self.game_networking.blocked, false, "false");
});
}); });
if ui.add(egui::Button::new("force refresh theme")).clicked() { if ui.add(egui::Button::new("force refresh theme")).clicked() {
catppuccin_egui::set_theme(ui.ctx(), self.settings.theme.into()); catppuccin_egui::set_theme(ui.ctx(), self.settings.theme.into());