disconnect from proxy on game close

This commit is contained in:
SpikeHD
2022-05-16 22:55:29 -07:00
parent b55767b276
commit cbd0c77b9c
4 changed files with 150 additions and 5 deletions

76
src-tauri/Cargo.lock generated
View File

@@ -600,6 +600,17 @@ dependencies = [
"crossbeam-utils 0.8.8",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6455c0ca19f0d2fbf751b908d5c55c1f5cbc65e03c4225427254b46890bdde1e"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-epoch 0.9.8",
"crossbeam-utils 0.8.8",
]
[[package]]
name = "crossbeam-epoch"
version = "0.8.2"
@@ -615,6 +626,20 @@ dependencies = [
"scopeguard",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1145cf131a2c6ba0615079ab6a638f7e1973ac9c2634fcbeaaad6114246efe8c"
dependencies = [
"autocfg",
"cfg-if 1.0.0",
"crossbeam-utils 0.8.8",
"lazy_static",
"memoffset 0.6.5",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.7.2"
@@ -702,6 +727,7 @@ dependencies = [
"rustls-pemfile",
"serde",
"serde_json",
"sysinfo",
"tauri",
"tauri-build",
"tokio",
@@ -2042,7 +2068,7 @@ dependencies = [
"async-io",
"async-lock",
"crossbeam-channel",
"crossbeam-epoch",
"crossbeam-epoch 0.8.2",
"crossbeam-utils 0.8.8",
"futures-util",
"num_cpus",
@@ -2162,6 +2188,15 @@ dependencies = [
"winrt-notification",
]
[[package]]
name = "ntapi"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c28774a7fd2fbb4f0babd8237ce554b73af68021b5f695a3cebd6c59bac0980f"
dependencies = [
"winapi",
]
[[package]]
name = "num-bigint"
version = "0.4.3"
@@ -2889,6 +2924,30 @@ dependencies = [
"cty",
]
[[package]]
name = "rayon"
version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd99e5772ead8baa5215278c9b15bf92087709e9c1b2d1f97cdb5a183c933a7d"
dependencies = [
"autocfg",
"crossbeam-deque",
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "258bcdb5ac6dad48491bb2992db6b7cf74878b0384908af124823d118c99683f"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils 0.8.8",
"num_cpus",
]
[[package]]
name = "rcgen"
version = "0.9.2"
@@ -3580,6 +3639,21 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "sysinfo"
version = "0.23.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56b1e20ee77901236c389ff74618a899ff5fd34719a7ff0fd1d64f0acca5179a"
dependencies = [
"cfg-if 1.0.0",
"core-foundation-sys",
"libc",
"ntapi",
"once_cell",
"rayon",
"winapi",
]
[[package]]
name = "system-deps"
version = "3.2.0"

View File

@@ -18,6 +18,9 @@ tauri-build = { version = "1.0.0-rc.8", features = [] }
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.0.0-rc.9", features = ["api-all"] }
# For process info
sysinfo = "0.23.12"
# ZIP-archive library.
zip-extract = "0.1.1"
zip = "0.6.2"

View File

@@ -3,6 +3,12 @@ all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use lazy_static::lazy_static;
use std::sync::Mutex;
use std::thread;
use sysinfo::{ProcessExt, System, SystemExt};
use open;
use structs::{APIQuery};
@@ -14,20 +20,30 @@ mod proxy;
mod web;
mod structs;
lazy_static!{
static ref WATCH_GAME_PROCESS: Mutex<String> = {
let m = "".to_string();
Mutex::new(m)
};
}
fn main() {
process_watcher();
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
enable_process_watcher,
connect,
disconnect,
proxy::set_proxy_addr,
run_program,
run_jar,
unzip::unzip,
file_helpers::rename,
file_helpers::dir_exists,
open_in_browser,
req_get,
get_bg_file,
proxy::set_proxy_addr,
unzip::unzip,
file_helpers::rename,
file_helpers::dir_exists,
downloader::download_file,
downloader::stop_download,
lang::get_lang,
@@ -37,6 +53,47 @@ fn main() {
.expect("error while running tauri application");
}
fn process_watcher() {
// Every 5 seconds, see if the game process is still running.
// If it is not, then we assume the game has closed and disable the proxy
// to prevent any requests from being sent to the game.
// Start in thread so as to not block the main thread.
thread::spawn(|| {
let mut s = System::new_all();
loop {
// Refresh system info
s.refresh_all();
// Grab the game process name
let mut proc = WATCH_GAME_PROCESS.lock().unwrap().to_string();
if !&proc.is_empty() {
let mut proc_with_name = s.processes_by_exact_name(&proc);
let mut exists = false;
for p in proc_with_name {
exists = true;
break;
}
// If the game process closes, disable the proxy.
if !exists {
*WATCH_GAME_PROCESS.lock().unwrap() = "".to_string();
disconnect();
}
}
std::thread::sleep(std::time::Duration::from_secs(5));
}
});
}
#[tauri::command]
fn enable_process_watcher(process: String) {
*WATCH_GAME_PROCESS.lock().unwrap() = process;
}
#[tauri::command]
async fn connect(port: u16) {
// Log message to console.

View File

@@ -78,12 +78,23 @@ export default class ServerLaunchSection extends React.Component<IProps, IState>
// Connect to proxy
if (config.toggle_grasscutter) {
let game_exe = config.game_install_path
if (game_exe.includes('\\')) {
game_exe = game_exe.substring(config.game_install_path.lastIndexOf('\\') + 1)
} else {
game_exe = game_exe.substring(config.game_install_path.lastIndexOf('/') + 1)
}
// Save last connected server and port
await setConfigOption('last_ip', this.state.ip)
await setConfigOption('last_port', this.state.port)
// Set IP
await invoke('set_proxy_addr', { addr: this.state.ip + ':' + this.state.port })
await invoke('enable_process_watcher', {
process: game_exe
})
// Connect to proxy
await invoke('connect', { port: 8365 })