mirror of
https://github.com/Grasscutters/Cultivation.git
synced 2026-02-04 17:32:10 +01:00
disconnect from proxy on game close
This commit is contained in:
76
src-tauri/Cargo.lock
generated
76
src-tauri/Cargo.lock
generated
@@ -600,6 +600,17 @@ dependencies = [
|
|||||||
"crossbeam-utils 0.8.8",
|
"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]]
|
[[package]]
|
||||||
name = "crossbeam-epoch"
|
name = "crossbeam-epoch"
|
||||||
version = "0.8.2"
|
version = "0.8.2"
|
||||||
@@ -615,6 +626,20 @@ dependencies = [
|
|||||||
"scopeguard",
|
"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]]
|
[[package]]
|
||||||
name = "crossbeam-utils"
|
name = "crossbeam-utils"
|
||||||
version = "0.7.2"
|
version = "0.7.2"
|
||||||
@@ -702,6 +727,7 @@ dependencies = [
|
|||||||
"rustls-pemfile",
|
"rustls-pemfile",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"sysinfo",
|
||||||
"tauri",
|
"tauri",
|
||||||
"tauri-build",
|
"tauri-build",
|
||||||
"tokio",
|
"tokio",
|
||||||
@@ -2042,7 +2068,7 @@ dependencies = [
|
|||||||
"async-io",
|
"async-io",
|
||||||
"async-lock",
|
"async-lock",
|
||||||
"crossbeam-channel",
|
"crossbeam-channel",
|
||||||
"crossbeam-epoch",
|
"crossbeam-epoch 0.8.2",
|
||||||
"crossbeam-utils 0.8.8",
|
"crossbeam-utils 0.8.8",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"num_cpus",
|
"num_cpus",
|
||||||
@@ -2162,6 +2188,15 @@ dependencies = [
|
|||||||
"winrt-notification",
|
"winrt-notification",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ntapi"
|
||||||
|
version = "0.3.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c28774a7fd2fbb4f0babd8237ce554b73af68021b5f695a3cebd6c59bac0980f"
|
||||||
|
dependencies = [
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-bigint"
|
name = "num-bigint"
|
||||||
version = "0.4.3"
|
version = "0.4.3"
|
||||||
@@ -2889,6 +2924,30 @@ dependencies = [
|
|||||||
"cty",
|
"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]]
|
[[package]]
|
||||||
name = "rcgen"
|
name = "rcgen"
|
||||||
version = "0.9.2"
|
version = "0.9.2"
|
||||||
@@ -3580,6 +3639,21 @@ dependencies = [
|
|||||||
"unicode-xid",
|
"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]]
|
[[package]]
|
||||||
name = "system-deps"
|
name = "system-deps"
|
||||||
version = "3.2.0"
|
version = "3.2.0"
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ tauri-build = { version = "1.0.0-rc.8", features = [] }
|
|||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
tauri = { version = "1.0.0-rc.9", features = ["api-all"] }
|
tauri = { version = "1.0.0-rc.9", features = ["api-all"] }
|
||||||
|
|
||||||
|
# For process info
|
||||||
|
sysinfo = "0.23.12"
|
||||||
|
|
||||||
# ZIP-archive library.
|
# ZIP-archive library.
|
||||||
zip-extract = "0.1.1"
|
zip-extract = "0.1.1"
|
||||||
zip = "0.6.2"
|
zip = "0.6.2"
|
||||||
|
|||||||
@@ -3,6 +3,12 @@ all(not(debug_assertions), target_os = "windows"),
|
|||||||
windows_subsystem = "windows"
|
windows_subsystem = "windows"
|
||||||
)]
|
)]
|
||||||
|
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
use std::thread;
|
||||||
|
use sysinfo::{ProcessExt, System, SystemExt};
|
||||||
|
|
||||||
use open;
|
use open;
|
||||||
use structs::{APIQuery};
|
use structs::{APIQuery};
|
||||||
|
|
||||||
@@ -14,20 +20,30 @@ mod proxy;
|
|||||||
mod web;
|
mod web;
|
||||||
mod structs;
|
mod structs;
|
||||||
|
|
||||||
|
lazy_static!{
|
||||||
|
static ref WATCH_GAME_PROCESS: Mutex<String> = {
|
||||||
|
let m = "".to_string();
|
||||||
|
Mutex::new(m)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
process_watcher();
|
||||||
|
|
||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
.invoke_handler(tauri::generate_handler![
|
.invoke_handler(tauri::generate_handler![
|
||||||
|
enable_process_watcher,
|
||||||
connect,
|
connect,
|
||||||
disconnect,
|
disconnect,
|
||||||
proxy::set_proxy_addr,
|
|
||||||
run_program,
|
run_program,
|
||||||
run_jar,
|
run_jar,
|
||||||
unzip::unzip,
|
|
||||||
file_helpers::rename,
|
|
||||||
file_helpers::dir_exists,
|
|
||||||
open_in_browser,
|
open_in_browser,
|
||||||
req_get,
|
req_get,
|
||||||
get_bg_file,
|
get_bg_file,
|
||||||
|
proxy::set_proxy_addr,
|
||||||
|
unzip::unzip,
|
||||||
|
file_helpers::rename,
|
||||||
|
file_helpers::dir_exists,
|
||||||
downloader::download_file,
|
downloader::download_file,
|
||||||
downloader::stop_download,
|
downloader::stop_download,
|
||||||
lang::get_lang,
|
lang::get_lang,
|
||||||
@@ -37,6 +53,47 @@ fn main() {
|
|||||||
.expect("error while running tauri application");
|
.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]
|
#[tauri::command]
|
||||||
async fn connect(port: u16) {
|
async fn connect(port: u16) {
|
||||||
// Log message to console.
|
// Log message to console.
|
||||||
|
|||||||
@@ -78,12 +78,23 @@ export default class ServerLaunchSection extends React.Component<IProps, IState>
|
|||||||
|
|
||||||
// Connect to proxy
|
// Connect to proxy
|
||||||
if (config.toggle_grasscutter) {
|
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
|
// Save last connected server and port
|
||||||
await setConfigOption('last_ip', this.state.ip)
|
await setConfigOption('last_ip', this.state.ip)
|
||||||
await setConfigOption('last_port', this.state.port)
|
await setConfigOption('last_port', this.state.port)
|
||||||
|
|
||||||
// Set IP
|
// Set IP
|
||||||
await invoke('set_proxy_addr', { addr: this.state.ip + ':' + this.state.port })
|
await invoke('set_proxy_addr', { addr: this.state.ip + ':' + this.state.port })
|
||||||
|
await invoke('enable_process_watcher', {
|
||||||
|
process: game_exe
|
||||||
|
})
|
||||||
|
|
||||||
// Connect to proxy
|
// Connect to proxy
|
||||||
await invoke('connect', { port: 8365 })
|
await invoke('connect', { port: 8365 })
|
||||||
|
|||||||
Reference in New Issue
Block a user