Skip to content

Commit

Permalink
Improved performance
Browse files Browse the repository at this point in the history
  • Loading branch information
doroved committed Oct 25, 2024
1 parent b214671 commit a8544df
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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."
Expand Down
20 changes: 10 additions & 10 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use hyper::{
};
use std::{
net::{IpAddr, SocketAddr, ToSocketAddrs},
sync::{Arc, Mutex},
sync::{Arc, RwLock},
};
use tokio::{
io::{AsyncRead, AsyncWrite},
Expand All @@ -24,9 +24,9 @@ use tokio::{

#[derive(Debug, Clone)]
pub(crate) struct Proxy {
pub allowed_credentials: Arc<Mutex<Vec<String>>>,
pub allowed_hosts: Arc<Mutex<Vec<String>>>,
pub secret_token: Arc<Mutex<String>>,
pub allowed_credentials: Arc<RwLock<Vec<String>>>,
pub allowed_hosts: Arc<RwLock<Vec<String>>>,
pub secret_token: Arc<RwLock<String>>,
}

impl Proxy {
Expand Down Expand Up @@ -61,7 +61,7 @@ impl Proxy {

async fn check_allowed_hosts(&self, req: &Request<Body>) -> Result<(), Response<Body>> {
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)
Expand All @@ -74,7 +74,7 @@ impl Proxy {
async fn check_secret_token(&self, req: &Request<Body>) -> Result<(), Response<Body>> {
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()
Expand All @@ -96,7 +96,7 @@ impl Proxy {
}

async fn check_credentials(&self, req: &Request<Body>) -> Result<(), Response<Body>> {
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();
Expand Down Expand Up @@ -167,9 +167,9 @@ pub async fn start_proxy(
secret_token: String,
) -> Result<(), Box<dyn std::error::Error>> {
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| {
Expand Down
8 changes: 4 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = Mutex::new("0.0.0.0".to_string());
pub static ref SERVER_IP: RwLock<String> = RwLock::new("0.0.0.0".to_string());
}

pub fn get_rand_ipv4_socket_addr() -> SocketAddr {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a8544df

Please sign in to comment.