-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame_networking.rs
More file actions
100 lines (91 loc) · 2.98 KB
/
game_networking.rs
File metadata and controls
100 lines (91 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use crate::{
gui::settings::BlockMethod,
util::{
firewall::{Firewall, RuleDirection, RuleMode, RuleProtocol},
system_info::SystemInfo,
},
};
use anyhow::Result;
use strum::{Display, EnumIter};
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";
#[derive(Clone, Copy, Debug, Default, Display, EnumIter, PartialEq)]
pub enum BlockedStatus {
#[default]
Unblocked,
Server,
Executable,
}
#[derive(Debug)]
pub struct GameNetworking {
pub blocked: BlockedStatus,
}
impl Default for GameNetworking {
fn default() -> Self {
let firewall = Firewall::default();
Self {
blocked: if firewall.is_blocked(FILTER_NAME_SAVE_SERVER).unwrap() {
BlockedStatus::Server
} else if firewall.is_blocked(FILTER_NAME_EXE).unwrap() {
BlockedStatus::Executable
} else {
BlockedStatus::Unblocked
},
}
}
}
impl GameNetworking {
pub fn block_exe(&mut self, system_info: &mut SystemInfo, firewall: &Firewall) -> Result<()> {
let Some(exe_path) = system_info.get_game_exe_path() else {
log::warn!("Unable to find game executable path.");
return Ok(());
};
firewall
.add(
FILTER_NAME_EXE,
RuleMode::Executable(exe_path.to_path_buf()),
RuleDirection::Out,
RuleProtocol::Any,
)
.inspect(|_| self.blocked = BlockedStatus::Executable)
}
pub fn unblock_exe(&mut self, firewall: &Firewall) -> Result<()> {
firewall
.remove(FILTER_NAME_EXE)
.inspect(|_| self.blocked = BlockedStatus::Unblocked)
}
pub fn block_save_server(&mut self, save_server_ip: &str, firewall: &Firewall) -> Result<()> {
firewall
.add(
FILTER_NAME_SAVE_SERVER,
RuleMode::Address(save_server_ip.to_owned()),
RuleDirection::Out,
RuleProtocol::Any,
)
.inspect(|_| self.blocked = BlockedStatus::Server)
}
pub fn unblock_save_server(&mut self, firewall: &Firewall) -> Result<()> {
firewall
.remove(FILTER_NAME_SAVE_SERVER)
.inspect(|_| self.blocked = BlockedStatus::Unblocked)
}
pub fn ensure_block_exclusivity(
&mut self,
block_method: BlockMethod,
firewall: &Firewall,
) -> Result<()> {
match block_method {
BlockMethod::EntireGame => {
if self.blocked == BlockedStatus::Server {
self.unblock_save_server(firewall)?;
}
}
BlockMethod::SaveServer => {
if self.blocked == BlockedStatus::Executable {
self.unblock_exe(firewall)?;
}
}
}
Ok(())
}
}