diff --git a/Cargo.lock b/Cargo.lock index c173933..bfa1870 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -680,7 +680,7 @@ dependencies = [ [[package]] name = "proxerver" -version = "0.1.0" +version = "0.1.1" dependencies = [ "base64", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 6806bcb..43d9dc0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "proxerver" -version = "0.1.0" +version = "0.1.1" edition = "2021" authors = ["doroved"] description = "User Friendly HTTP and HTTPS (HTTP over TLS) proxy server." diff --git a/src/http.rs b/src/http.rs index 41e266c..9ee9b67 100644 --- a/src/http.rs +++ b/src/http.rs @@ -15,7 +15,7 @@ use hyper::{ }; use std::{ net::{IpAddr, SocketAddr, ToSocketAddrs}, - sync::{Arc, Mutex}, + sync::{Arc, RwLock}, }; use tokio::{ io::{AsyncRead, AsyncWrite}, @@ -24,9 +24,9 @@ use tokio::{ #[derive(Debug, Clone)] pub(crate) struct Proxy { - pub allowed_credentials: Arc>>, - pub allowed_hosts: Arc>>, - pub secret_token: Arc>, + pub allowed_credentials: Arc>>, + pub allowed_hosts: Arc>>, + pub secret_token: Arc>, } impl Proxy { @@ -61,7 +61,7 @@ impl Proxy { async fn check_allowed_hosts(&self, req: &Request) -> Result<(), Response> { let host = req.uri().host().unwrap_or(""); - let allowed_hosts = self.allowed_hosts.lock().unwrap().to_vec(); + let allowed_hosts = self.allowed_hosts.read().unwrap().to_vec(); if !allowed_hosts.is_empty() && !is_host_allowed(host, &allowed_hosts) { return Err(Response::builder() .status(StatusCode::BAD_REQUEST) @@ -74,7 +74,7 @@ impl Proxy { async fn check_secret_token(&self, req: &Request) -> Result<(), Response> { let options = Opt::parse(); - let secret_token = self.secret_token.lock().unwrap().to_string(); + let secret_token = self.secret_token.read().unwrap().to_string(); if !secret_token.is_empty() && !options.no_http_token { if let Some(secret_token_header) = req.headers().get("x-http-secret-token") { if secret_token_header.to_str().unwrap_or_default().trim() @@ -96,7 +96,7 @@ impl Proxy { } async fn check_credentials(&self, req: &Request) -> Result<(), Response> { - let allowed_credentials = self.allowed_credentials.lock().unwrap().to_vec(); + let allowed_credentials = self.allowed_credentials.read().unwrap().to_vec(); if !allowed_credentials.is_empty() { if let Some(auth_header) = req.headers().get(PROXY_AUTHORIZATION) { let header_credentials = auth_header.to_str().unwrap_or_default(); @@ -167,9 +167,9 @@ pub async fn start_proxy( secret_token: String, ) -> Result<(), Box> { let proxy = Proxy { - allowed_credentials: Arc::new(Mutex::new(allowed_credentials)), - allowed_hosts: Arc::new(Mutex::new(allowed_hosts)), - secret_token: Arc::new(Mutex::new(secret_token)), + allowed_credentials: Arc::new(RwLock::new(allowed_credentials)), + allowed_hosts: Arc::new(RwLock::new(allowed_hosts)), + secret_token: Arc::new(RwLock::new(secret_token)), }; let make_service = make_service_fn(move |addr: &AddrStream| { diff --git a/src/utils.rs b/src/utils.rs index b5d5b07..52b7808 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -6,11 +6,11 @@ use rand::Rng; use sha2::{Digest, Sha256}; use std::net::{IpAddr, SocketAddr}; use std::process::Command; -use std::sync::Mutex; +use std::sync::RwLock; use wildmatch::WildMatch; lazy_static! { - pub static ref SERVER_IP: Mutex = Mutex::new("0.0.0.0".to_string()); + pub static ref SERVER_IP: RwLock = RwLock::new("0.0.0.0".to_string()); } pub fn get_rand_ipv4_socket_addr() -> SocketAddr { @@ -82,12 +82,12 @@ pub async fn get_server_ip() -> String { pub async fn update_server_ip() { let server_ip = get_server_ip().await; - let mut ip = SERVER_IP.lock().unwrap(); + let mut ip = SERVER_IP.write().unwrap(); *ip = server_ip; } pub fn get_current_server_ip() -> String { - SERVER_IP.lock().unwrap().clone() + SERVER_IP.read().unwrap().clone() } pub fn to_sha256(input: &str) -> String {