game_networking: turn blocked into enum
Allows us to keep track of what's blocked without querying rules constantly.
This commit is contained in:
@@ -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
|
||||||
|
.add(
|
||||||
FILTER_NAME_EXE,
|
FILTER_NAME_EXE,
|
||||||
RuleMode::Executable(exe_path.to_path_buf()),
|
RuleMode::Executable(exe_path.to_path_buf()),
|
||||||
RuleDirection::Out,
|
RuleDirection::Out,
|
||||||
RuleProtocol::Any,
|
RuleProtocol::Any,
|
||||||
)?;
|
)
|
||||||
self.blocked = firewall.is_blocked(FILTER_NAME_EXE)?;
|
.inspect(|_| self.blocked = BlockedStatus::ExeBlocked)
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
.add(
|
||||||
FILTER_NAME_SAVE_SERVER,
|
FILTER_NAME_SAVE_SERVER,
|
||||||
RuleMode::Address(save_server_ip.to_owned()),
|
RuleMode::Address(save_server_ip.to_owned()),
|
||||||
RuleDirection::Out,
|
RuleDirection::Out,
|
||||||
RuleProtocol::Any,
|
RuleProtocol::Any,
|
||||||
)?;
|
)
|
||||||
self.blocked = firewall.is_blocked(FILTER_NAME_SAVE_SERVER)?;
|
.inspect(|_| self.blocked = BlockedStatus::ServerBlocked)
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-4
@@ -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 {
|
||||||
|
BlockedStatus::ExeBlocked
|
||||||
|
if self.settings.block_method == BlockMethod::EntireGame =>
|
||||||
|
{
|
||||||
colours::RED
|
colours::RED
|
||||||
} else {
|
}
|
||||||
colours::GREEN
|
BlockedStatus::ServerBlocked
|
||||||
|
if self.settings.block_method == BlockMethod::SaveServer =>
|
||||||
|
{
|
||||||
|
colours::RED
|
||||||
|
}
|
||||||
|
_ => colours::GREEN,
|
||||||
});
|
});
|
||||||
label
|
label
|
||||||
});
|
});
|
||||||
|
|||||||
+2
-4
@@ -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());
|
||||||
|
|||||||
Reference in New Issue
Block a user