diff --git a/.editorconfig b/.editorconfig index 667b613..2acebee 100644 --- a/.editorconfig +++ b/.editorconfig @@ -3,11 +3,11 @@ root = true [*] charset = utf-8 end_of_line = crlf -indent_size = 4 +indent_size = 2 indent_style = space insert_final_newline = false max_line_length = 120 -tab_width = 4 +tab_width = 2 trim_trailing_whitespace = false [*.rs] diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 39b509c..1637326 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -671,6 +671,7 @@ dependencies = [ "serde_json", "tauri", "tauri-build", + "tokio", "tokio-rustls", "tokio-tungstenite", "tracing", @@ -3302,6 +3303,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" +dependencies = [ + "libc", +] + [[package]] name = "siphasher" version = "0.3.10" @@ -3877,6 +3887,7 @@ dependencies = [ "num_cpus", "once_cell", "pin-project-lite", + "signal-hook-registry", "socket2", "winapi", ] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 860ce06..138b5cb 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -18,10 +18,10 @@ 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"] } -# zip library +# ZIP-archive library. zip-extract = "0.1.1" -# For creating a "global" downloads list +# For creating a "global" downloads list. lazy_static = "1.4.0" # Access to the Windows Registry. @@ -38,6 +38,7 @@ hudsucker = "0.17.2" tracing = "0.1.21" tokio-rustls = "0.23.0" tokio-tungstenite = "0.17.0" +tokio = { version = "1.18.2", features = ["signal"] } rustls-pemfile = "1.0.0" reqwest = { version = "0.11.3", features = ["stream"] } futures-util = "0.3.14" diff --git a/src-tauri/resources/cert.crt b/src-tauri/resources/cert.crt new file mode 100644 index 0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Sjbdyu4o5SDqj7lmr263YgM4ZnVOQR9CaWwCL21C65tpgHa8Grm +hNil9REdnpM7br4H0yeX2nFjOYI8sUguxNle3ojTFLl0sWXZIPJE/koEaaHGSJD1 +XR72llJbbExYbTzaEV3uw7sJsuwldMC/QL+oWm/Jnwc2WfLTl3HjLOaK9r/smF/E +RtYk5yo7J6pMALrIP7SPHpFooez5JHn2ucP42HcUwOXmrDIOUt6gJQ4w8DBE46Bo +oMKSHK2k0g== +-----END CERTIFICATE----- \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index a8cd0b9..571679b 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -4,6 +4,7 @@ windows_subsystem = "windows" )] use open; +use tokio::sync::oneshot::Sender; use structs::{APIQuery}; mod file_helpers; @@ -38,17 +39,20 @@ fn main() { #[tauri::command] async fn connect(port: u16) { // Log message to console. - println!("Connecting to proxy..."); - - // Create and start a proxy. - proxy::create_proxy(port).await; + println!("Connecting to proxy..."); // Change proxy settings. proxy::connect_to_proxy(port); + + // Create and start a proxy. + proxy::create_proxy(port).await; } #[tauri::command] fn disconnect() { + // Log message to console. + println!("Disconnecting from proxy..."); + // Change proxy settings. proxy::disconnect_from_proxy(); } diff --git a/src-tauri/src/proxy.rs b/src-tauri/src/proxy.rs index 6c3383f..9c70891 100644 --- a/src-tauri/src/proxy.rs +++ b/src-tauri/src/proxy.rs @@ -3,6 +3,7 @@ * https://github.com/omjadas/hudsucker/blob/main/examples/log.rs */ +use std::borrow::Borrow; use hudsucker::{ async_trait::async_trait, certificate_authority::RcgenAuthority, @@ -14,12 +15,12 @@ use std::net::SocketAddr; use registry::{Hive, Data, Security}; use rustls_pemfile as pemfile; +use tauri::http::Uri; +use tokio::sync::oneshot::Sender; -/** - * Application shutdown handler. - */ -async fn shutdown_signal() { - disconnect_from_proxy(); +async unsafe fn shutdown_signal() { + tokio::signal::ctrl_c().await + .expect("Failed to install CTRL+C signal handler"); } #[derive(Clone)] @@ -31,11 +32,32 @@ impl HttpHandler for ProxyHandler { _context: &HttpContext, request: Request ) -> RequestOrResponse { - println!("{:?}", request.uri().path()); - RequestOrResponse::Request(request) + // Get request parts. + let (parts, body) = request.into_parts(); + + // Parse request URI. + let mut uri = parts.uri.clone(); + let path = uri.to_string(); + + // Check URI against constraints. + if path.contains("hoyoverse.com") || path.contains("mihoyo.com") || path.contains("yuanshen.com") { + let mut new_uri = String::new(); + new_uri.push_str("127.0.0.1"); + new_uri.push_str(uri.path()); + + uri = Uri::from_static(new_uri.as_str()); + } + + let builder = Request::builder() + .method(&parts.method) + .uri(&uri) + .version(parts.version); + let modified = builder.body(body).unwrap(); + + RequestOrResponse::Request(modified) } - async fn handle_response(&mut self, + async fn handle_response(&mut self, _context: &HttpContext, response: Response ) -> Response { response } @@ -73,10 +95,11 @@ pub(crate) async fn create_proxy(proxy_port: u16) { .with_ca(authority) .with_http_handler(ProxyHandler) .build(); - - // Create the proxy & listen for errors. - proxy.start(shutdown_signal()).await - .expect("Failed to start proxy"); + + // Start the proxy. + unsafe { + tokio::spawn(proxy.start(shutdown_signal())); + } } /** @@ -109,4 +132,6 @@ pub(crate) fn disconnect_from_proxy() { // Set registry values. settings.set_value("ProxyEnable", &Data::U32(0)).unwrap(); } + + println!("Disconnected from proxy."); } \ No newline at end of file diff --git a/src/ui/Test.tsx b/src/ui/Test.tsx index ebae74b..b4a60ba 100644 --- a/src/ui/Test.tsx +++ b/src/ui/Test.tsx @@ -3,7 +3,7 @@ import React from 'react' import {invoke} from '@tauri-apps/api/tauri' async function startProxy() { - await invoke('connect') + await invoke('connect', { port: 2222 }) } async function stopProxy() {