mirror of
https://github.com/Grasscutters/Cultivation.git
synced 2025-12-15 08:34:43 +01:00
Configure and run rustfmt
This commit is contained in:
@@ -4,15 +4,15 @@
|
||||
*/
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
use std::{sync::Mutex, str::FromStr};
|
||||
use std::{str::FromStr, sync::Mutex};
|
||||
|
||||
use rcgen::*;
|
||||
use hudsucker::{
|
||||
async_trait::async_trait,
|
||||
certificate_authority::RcgenAuthority,
|
||||
hyper::{Body, Request, Response},
|
||||
*,
|
||||
};
|
||||
use rcgen::*;
|
||||
|
||||
use std::fs;
|
||||
use std::net::SocketAddr;
|
||||
@@ -22,10 +22,11 @@ use rustls_pemfile as pemfile;
|
||||
use tauri::http::Uri;
|
||||
|
||||
#[cfg(windows)]
|
||||
use registry::{Hive, Data, Security};
|
||||
use registry::{Data, Hive, Security};
|
||||
|
||||
async fn shutdown_signal() {
|
||||
tokio::signal::ctrl_c().await
|
||||
tokio::signal::ctrl_c()
|
||||
.await
|
||||
.expect("Failed to install CTRL+C signal handler");
|
||||
}
|
||||
|
||||
@@ -42,16 +43,18 @@ pub fn set_proxy_addr(addr: String) {
|
||||
|
||||
#[async_trait]
|
||||
impl HttpHandler for ProxyHandler {
|
||||
async fn handle_request(&mut self,
|
||||
_context: &HttpContext,
|
||||
mut request: Request<Body>,
|
||||
async fn handle_request(
|
||||
&mut self,
|
||||
_context: &HttpContext,
|
||||
mut request: Request<Body>,
|
||||
) -> RequestOrResponse {
|
||||
let uri = request.uri().to_string();
|
||||
let uri_path = request.uri().path();
|
||||
|
||||
|
||||
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).as_str()).unwrap();
|
||||
let new_uri =
|
||||
Uri::from_str(format!("{}{}", SERVER.lock().unwrap(), uri_path).as_str()).unwrap();
|
||||
// Set request URI to the new one.
|
||||
*request.uri_mut() = new_uri;
|
||||
}
|
||||
@@ -59,10 +62,13 @@ impl HttpHandler for ProxyHandler {
|
||||
RequestOrResponse::Request(request)
|
||||
}
|
||||
|
||||
async fn handle_response(&mut self,
|
||||
_context: &HttpContext,
|
||||
response: Response<Body>,
|
||||
) -> Response<Body> { response }
|
||||
async fn handle_response(
|
||||
&mut self,
|
||||
_context: &HttpContext,
|
||||
response: Response<Body>,
|
||||
) -> Response<Body> {
|
||||
response
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,20 +76,22 @@ impl HttpHandler for ProxyHandler {
|
||||
*/
|
||||
pub async fn create_proxy(proxy_port: u16, certificate_path: String) {
|
||||
// Get the certificate and private key.
|
||||
let mut private_key_bytes: &[u8] = &fs::read(format!("{}\\private.key", certificate_path)).expect("Could not read private key");
|
||||
let mut ca_cert_bytes: &[u8] = &fs::read(format!("{}\\cert.crt", certificate_path)).expect("Could not read certificate");
|
||||
let mut private_key_bytes: &[u8] =
|
||||
&fs::read(format!("{}\\private.key", certificate_path)).expect("Could not read private key");
|
||||
let mut ca_cert_bytes: &[u8] =
|
||||
&fs::read(format!("{}\\cert.crt", certificate_path)).expect("Could not read certificate");
|
||||
|
||||
// Parse the private key and certificate.
|
||||
let private_key = rustls::PrivateKey(
|
||||
pemfile::pkcs8_private_keys(&mut private_key_bytes)
|
||||
.expect("Failed to parse private key")
|
||||
.remove(0)
|
||||
.remove(0),
|
||||
);
|
||||
|
||||
let ca_cert = rustls::Certificate(
|
||||
pemfile::certs(&mut ca_cert_bytes)
|
||||
.expect("Failed to parse CA certificate")
|
||||
.remove(0)
|
||||
.remove(0),
|
||||
);
|
||||
|
||||
// Create the certificate authority.
|
||||
@@ -108,13 +116,23 @@ pub async fn create_proxy(proxy_port: u16, certificate_path: String) {
|
||||
#[cfg(windows)]
|
||||
pub fn connect_to_proxy(proxy_port: u16) {
|
||||
// Create 'ProxyServer' string.
|
||||
let server_string: String = format!("http=127.0.0.1:{};https=127.0.0.1:{}", proxy_port, proxy_port);
|
||||
let server_string: String = format!(
|
||||
"http=127.0.0.1:{};https=127.0.0.1:{}",
|
||||
proxy_port, proxy_port
|
||||
);
|
||||
|
||||
// Fetch the 'Internet Settings' registry key.
|
||||
let settings = Hive::CurrentUser.open(r"Software\Microsoft\Windows\CurrentVersion\Internet Settings", Security::Write).unwrap();
|
||||
let settings = Hive::CurrentUser
|
||||
.open(
|
||||
r"Software\Microsoft\Windows\CurrentVersion\Internet Settings",
|
||||
Security::Write,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Set registry values.
|
||||
settings.set_value("ProxyServer", &Data::String(server_string.parse().unwrap())).unwrap();
|
||||
settings
|
||||
.set_value("ProxyServer", &Data::String(server_string.parse().unwrap()))
|
||||
.unwrap();
|
||||
settings.set_value("ProxyEnable", &Data::U32(1)).unwrap();
|
||||
|
||||
println!("Connected to the proxy.");
|
||||
@@ -131,7 +149,12 @@ pub fn connect_to_proxy(_proxy_port: u16) {
|
||||
#[cfg(windows)]
|
||||
pub fn disconnect_from_proxy() {
|
||||
// Fetch the 'Internet Settings' registry key.
|
||||
let settings = Hive::CurrentUser.open(r"Software\Microsoft\Windows\CurrentVersion\Internet Settings", Security::Write).unwrap();
|
||||
let settings = Hive::CurrentUser
|
||||
.open(
|
||||
r"Software\Microsoft\Windows\CurrentVersion\Internet Settings",
|
||||
Security::Write,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Set registry values.
|
||||
settings.set_value("ProxyEnable", &Data::U32(0)).unwrap();
|
||||
@@ -157,7 +180,7 @@ pub fn generate_ca_files(path: &Path) {
|
||||
details.push(DnType::OrganizationName, "Grasscutters");
|
||||
details.push(DnType::CountryName, "CN");
|
||||
details.push(DnType::LocalityName, "CN");
|
||||
|
||||
|
||||
// Set details in the parameter.
|
||||
params.distinguished_name = details;
|
||||
// Set other properties.
|
||||
@@ -165,9 +188,9 @@ pub fn generate_ca_files(path: &Path) {
|
||||
params.key_usages = vec![
|
||||
KeyUsagePurpose::DigitalSignature,
|
||||
KeyUsagePurpose::KeyCertSign,
|
||||
KeyUsagePurpose::CrlSign
|
||||
KeyUsagePurpose::CrlSign,
|
||||
];
|
||||
|
||||
|
||||
// Create certificate.
|
||||
let cert = Certificate::from_params(params).unwrap();
|
||||
let cert_crt = cert.serialize_pem().unwrap();
|
||||
@@ -176,26 +199,37 @@ pub fn generate_ca_files(path: &Path) {
|
||||
// Make certificate directory.
|
||||
let cert_dir = path.join("ca");
|
||||
match fs::create_dir(&cert_dir) {
|
||||
Ok(_) => {},
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("{}", e);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Write the certificate to a file.
|
||||
let cert_path = cert_dir.join("cert.crt");
|
||||
match fs::write(&cert_path, cert_crt) {
|
||||
Ok(_) => println!("Wrote certificate to {}", cert_path.to_str().unwrap()),
|
||||
Err(e) => println!("Error writing certificate to {}: {}", cert_path.to_str().unwrap(), e),
|
||||
Err(e) => println!(
|
||||
"Error writing certificate to {}: {}",
|
||||
cert_path.to_str().unwrap(),
|
||||
e
|
||||
),
|
||||
}
|
||||
|
||||
// Write the private key to a file.
|
||||
let private_key_path = cert_dir.join("private.key");
|
||||
match fs::write(&private_key_path, private_key) {
|
||||
Ok(_) => println!("Wrote private key to {}", private_key_path.to_str().unwrap()),
|
||||
Err(e) => println!("Error writing private key to {}: {}", private_key_path.to_str().unwrap(), e),
|
||||
Ok(_) => println!(
|
||||
"Wrote private key to {}",
|
||||
private_key_path.to_str().unwrap()
|
||||
),
|
||||
Err(e) => println!(
|
||||
"Error writing private key to {}: {}",
|
||||
private_key_path.to_str().unwrap(),
|
||||
e
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
// Install certificate into the system's Root CA store.
|
||||
install_ca_files(&cert_path);
|
||||
}
|
||||
@@ -205,13 +239,27 @@ pub fn generate_ca_files(path: &Path) {
|
||||
*/
|
||||
#[cfg(windows)]
|
||||
pub fn install_ca_files(cert_path: &Path) {
|
||||
crate::system_helpers::run_command("certutil", vec!["-user", "-addstore", "Root", cert_path.to_str().unwrap()]);
|
||||
crate::system_helpers::run_command(
|
||||
"certutil",
|
||||
vec!["-user", "-addstore", "Root", cert_path.to_str().unwrap()],
|
||||
);
|
||||
println!("Installed certificate.");
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub fn install_ca_files(cert_path: &Path) {
|
||||
crate::system_helpers::run_command("security", vec!["add-trusted-cert", "-d", "-r", "trustRoot", "-k", "/Library/Keychains/System.keychain", cert_path.to_str().unwrap()]);
|
||||
crate::system_helpers::run_command(
|
||||
"security",
|
||||
vec![
|
||||
"add-trusted-cert",
|
||||
"-d",
|
||||
"-r",
|
||||
"trustRoot",
|
||||
"-k",
|
||||
"/Library/Keychains/System.keychain",
|
||||
cert_path.to_str().unwrap(),
|
||||
],
|
||||
);
|
||||
println!("Installed certificate.");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user