From e40fe6049fa4681b28c2a8a79de1e23cf657a723 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 13 Feb 2024 22:56:22 +0800 Subject: [PATCH] :bug: Fix http proxy --- Cargo.lock | 1 + Cargo.toml | 2 +- regions/index.toml | 16 ++++++++++------ regions/script.sh | 2 +- src/proxies/config.rs | 2 +- src/proxies/responder.rs | 28 ++++++++++++++++------------ src/proxies/route.rs | 4 ++-- src/proxies/server.rs | 2 +- src/warden/mod.rs | 2 +- 9 files changed, 34 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fb75eba..c129a8d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -352,6 +352,7 @@ dependencies = [ "percent-encoding", "pin-project-lite", "rand", + "rustls", "serde", "serde_json", "serde_urlencoded", diff --git a/Cargo.toml b/Cargo.toml index f72e974..b30e787 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ actix-files = "0.6.5" actix-proxy = "0.2.0" actix-web = { version = "4.5.1", features = ["rustls-0_22"] } actix-web-httpauth = "0.8.1" -awc = "3.4.0" +awc = { version = "3.4.0", features = ["tls-rustls-0_22"] } config = { version = "0.14.0", features = ["toml"] } lazy_static = "1.4.0" mime = "0.3.17" diff --git a/regions/index.toml b/regions/index.toml index 899c626..f2b9261 100644 --- a/regions/index.toml +++ b/regions/index.toml @@ -5,10 +5,14 @@ id = "root" hosts = ["localhost"] paths = ["/"] [[locations.destinations]] -id = "static" -uri = "files://regions?index=index.html" +id = "hypertext" +uri = "https://postman-echo.com/get" +# [[locations.destinations]] +# id = "static" +# uri = "files://regions?index=index.html" -[[applications]] -id = "script" -exe = "./script.sh" -workdir = "regions" \ No newline at end of file + +# [[applications]] +# id = "script" +# exe = "./script.sh" +# workdir = "regions" \ No newline at end of file diff --git a/regions/script.sh b/regions/script.sh index eca748e..b88fa7a 100755 --- a/regions/script.sh +++ b/regions/script.sh @@ -1,3 +1,3 @@ #!/bin/bash -echo "Good morning!" +echo "Good morning!" > ./kokodayo.txt diff --git a/src/proxies/config.rs b/src/proxies/config.rs index 60d4b31..1f4d935 100644 --- a/src/proxies/config.rs +++ b/src/proxies/config.rs @@ -12,7 +12,7 @@ use super::responder::StaticResponderConfig; pub struct Region { pub id: String, pub locations: Vec, - pub applications: Vec, + pub applications: Option>, } #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/src/proxies/responder.rs b/src/proxies/responder.rs index 3213695..43e4f42 100644 --- a/src/proxies/responder.rs +++ b/src/proxies/responder.rs @@ -5,7 +5,8 @@ use std::{ use actix_files::{NamedFile}; use actix_proxy::IntoHttpResponse; use actix_web::{HttpRequest, HttpResponse, web}; -use actix_web::http::Method; +use actix_web::http::{header, Method}; +use actix_web::http::header::HeaderValue; use awc::Client; use tracing::log::warn; use crate::proxies::ProxyError; @@ -15,29 +16,32 @@ pub async fn respond_hypertext( req: HttpRequest, client: web::Data, ) -> Result { - let ip = req.peer_addr().unwrap().ip().to_string(); - let proto = req.uri().scheme_str().unwrap(); - let host = req.uri().host().unwrap(); + let conn = req.connection_info(); + let ip = conn.realip_remote_addr().unwrap_or("0.0.0.0"); + let proto = conn.scheme(); + let host = conn.host(); let mut headers = req.headers().clone(); - headers.insert("Server".parse().unwrap(), "RoadSign".parse().unwrap()); - headers.insert("X-Forward-For".parse().unwrap(), ip.parse().unwrap()); - headers.insert("X-Forwarded-Proto".parse().unwrap(), proto.parse().unwrap()); - headers.insert("X-Forwarded-Host".parse().unwrap(), host.parse().unwrap()); - headers.insert("X-Real-IP".parse().unwrap(), ip.parse().unwrap()); + headers.insert(header::X_FORWARDED_FOR, ip.parse().unwrap()); + headers.insert(header::X_FORWARDED_PROTO, proto.parse().unwrap()); + headers.insert(header::X_FORWARDED_HOST, host.parse().unwrap()); headers.insert( - "Forwarded".parse().unwrap(), + header::FORWARDED, format!("by={};for={};host={};proto={}", ip, ip, host, proto) .parse() .unwrap(), ); - let res = client.get(uri).send().await; + let append_part = req.uri().to_string().chars().skip(1).collect::(); + let target_url = format!("{}{}", uri, append_part); + + let res = client.request(req.method().clone(), target_url).send().await; return match res { Ok(result) => { let mut res = result.into_http_response(); - res.headers_mut().insert("Server".parse().unwrap(), "RoadSign".parse().unwrap()); + res.headers_mut().insert(header::SERVER, HeaderValue::from_static("RoadSign")); + res.headers_mut().remove(header::CONTENT_ENCODING); Ok(res) } diff --git a/src/proxies/route.rs b/src/proxies/route.rs index 3e37b5f..a3ad210 100644 --- a/src/proxies/route.rs +++ b/src/proxies/route.rs @@ -55,9 +55,9 @@ pub async fn handle(req: HttpRequest, client: web::Data) -> HttpResponse let loc = location.clone(); let end = destination.clone(); - let ip = match req.peer_addr() { + let ip = match req.connection_info().realip_remote_addr() { None => "unknown".to_string(), - Some(val) => val.ip().to_string() + Some(val) => val.to_string(), }; let ua = match req.headers().get(header::USER_AGENT) { None => "unknown".to_string(), diff --git a/src/proxies/server.rs b/src/proxies/server.rs index fdab9a9..1195f66 100644 --- a/src/proxies/server.rs +++ b/src/proxies/server.rs @@ -29,7 +29,7 @@ pub fn build_single_proxy(cfg: ServerBindConfig) -> Result) { self.applications = regions .iter() - .flat_map(|item| item.applications.clone()) + .flat_map(|item| item.applications.clone().unwrap_or_default()) .collect::>(); debug!( applications = format!("{:?}", self.applications),