Allow for dynamic usage of the proxy port

This commit is contained in:
KingRainbow44
2022-05-10 19:05:25 -04:00
parent d8323b09d5
commit 12928ca0e1
2 changed files with 19 additions and 13 deletions

View File

@@ -10,23 +10,23 @@ mod proxy;
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![downloader::download_file])
.invoke_handler(tauri::generate_handler![run_program])
.invoke_handler(tauri::generate_handler![connect, disconnect])
.run(tauri::generate_context!())
.expect("error while running tauri application");
.invoke_handler(tauri::generate_handler![downloader::download_file])
.invoke_handler(tauri::generate_handler![run_program])
.invoke_handler(tauri::generate_handler![connect, disconnect])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
#[tauri::command]
async fn connect() {
async fn connect(port: u16) {
// Log message to console.
println!("Connecting to proxy...");
// Create and start a proxy.
proxy::create_proxy().await;
proxy::create_proxy(port).await;
// Change proxy settings.
proxy::connect_to_proxy();
proxy::connect_to_proxy(port);
}
#[tauri::command]
@@ -38,5 +38,6 @@ fn disconnect() {
#[tauri::command]
fn run_program(path: String) {
// Open the program from the specified path.
opener::open(path.clone()).expect("Failed to open program");
opener::open(path.clone())
.expect("Failed to open program");
}

View File

@@ -44,7 +44,7 @@ impl HttpHandler for ProxyHandler {
/**
* Starts an HTTP(S) proxy server.
*/
pub(crate) async fn create_proxy() {
pub(crate) async fn create_proxy(proxy_port: u16) {
// Get the certificate and private key.
let mut private_key_bytes: &[u8] = include_bytes!("../resources/private-key.pem");
let mut ca_cert_bytes: &[u8] = include_bytes!("../resources/ca-certificate.pem");
@@ -68,7 +68,7 @@ pub(crate) async fn create_proxy() {
// Create an instance of the proxy.
let proxy = ProxyBuilder::new()
.with_addr(SocketAddr::from(([127, 0, 0, 1], 8537)))
.with_addr(SocketAddr::from(([0, 0, 0, 0], proxy_port)))
.with_rustls_client()
.with_ca(authority)
.with_http_handler(ProxyHandler)
@@ -82,15 +82,20 @@ pub(crate) async fn create_proxy() {
/**
* Connects to the local HTTP(S) proxy server.
*/
pub(crate) fn connect_to_proxy() {
pub(crate) fn connect_to_proxy(proxy_port: u16) {
if cfg!(target_os = "windows") {
// Create 'ProxyServer' string.
let server_string: String = format!("http=127.0.0.1:{};https=127.0.0.1:{};ftp=127.0.0.1:{}", proxy_port, 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();
// Set registry values.
settings.set_value("ProxyServer", &Data::String("http=127.0.0.1:8537;https=127.0.0.1:8537;ftp=127.0.0.1:8537".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.");
}
/**