Full offline compatibility

This commit is contained in:
Thoronium
2023-03-05 19:13:09 -07:00
parent abf7a428f6
commit ea73feb8e9
9 changed files with 250 additions and 170 deletions

View File

@@ -157,7 +157,8 @@ fn enable_process_watcher(window: tauri::Window, process: String) {
let mut system = System::new_all();
loop {
thread::sleep(std::time::Duration::from_secs(5));
// Shorten loop timer to avoid user closing Cultivation before unpatching/proxy disconnecting
thread::sleep(std::time::Duration::from_secs(2));
// Refresh system info
system.refresh_all();

View File

@@ -12,7 +12,7 @@ use std::{path::PathBuf, str::FromStr, sync::Mutex};
use hudsucker::{
async_trait::async_trait,
certificate_authority::RcgenAuthority,
hyper::{Body, Request, Response},
hyper::{Body, Request, Response, StatusCode},
*,
};
use rcgen::*;
@@ -48,22 +48,33 @@ pub fn set_proxy_addr(addr: String) {
impl HttpHandler for ProxyHandler {
async fn handle_request(
&mut self,
_context: &HttpContext,
mut request: Request<Body>,
_ctx: &HttpContext,
mut req: Request<Body>,
) -> RequestOrResponse {
let uri = request.uri().to_string();
let uri_path_and_query = request.uri().path_and_query().unwrap().as_str();
let uri = req.uri().to_string();
if uri.contains("hoyoverse.com") || uri.contains("mihoyo.com") || uri.contains("yuanshen.com") {
// Create new URI.
let new_uri =
Uri::from_str(format!("{}{}", SERVER.lock().unwrap(), uri_path_and_query).as_str())
.unwrap();
// Set request URI to the new one.
*request.uri_mut() = new_uri;
// Handle CONNECTs
if req.method().as_str() == "CONNECT" {
let builder = Response::builder()
.header("DecryptEndpoint", "Created")
.status(StatusCode::OK);
let res = builder.body(()).unwrap();
// Respond to CONNECT
*res.body()
} else {
let uri_path_and_query = req.uri().path_and_query().unwrap().as_str();
// Create new URI.
let new_uri =
Uri::from_str(format!("{}{}", SERVER.lock().unwrap(), uri_path_and_query).as_str())
.unwrap();
// Set request URI to the new one.
*req.uri_mut() = new_uri;
}
}
RequestOrResponse::Request(request)
req.into()
}
async fn handle_response(
@@ -73,6 +84,10 @@ impl HttpHandler for ProxyHandler {
) -> Response<Body> {
response
}
async fn should_intercept(&mut self, _ctx: &HttpContext, _req: &Request<Body>) -> bool {
true
}
}
/**

View File

@@ -9,8 +9,13 @@ pub(crate) async fn query(site: &str) -> String {
.header(CONTENT_TYPE, "application/json")
.send()
.await
.unwrap();
response.text().await.unwrap()
.ok();
if response.is_some() {
return response.unwrap().text().await.unwrap();
} else {
false.to_string()
}
}
#[tauri::command]
@@ -23,9 +28,13 @@ pub(crate) async fn valid_url(url: String) -> bool {
.header(USER_AGENT, "cultivation")
.send()
.await
.unwrap();
.ok();
response.status().as_str() == "200"
if response.is_some() {
return response.unwrap().status().as_str() == "200";
} else {
false
}
}
#[tauri::command]