🐛 Fix http proxy

This commit is contained in:
LittleSheep 2024-02-13 22:56:22 +08:00
parent ed9434b85a
commit e40fe6049f
9 changed files with 34 additions and 25 deletions

1
Cargo.lock generated
View File

@ -352,6 +352,7 @@ dependencies = [
"percent-encoding",
"pin-project-lite",
"rand",
"rustls",
"serde",
"serde_json",
"serde_urlencoded",

View File

@ -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"

View File

@ -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"
# [[applications]]
# id = "script"
# exe = "./script.sh"
# workdir = "regions"

View File

@ -1,3 +1,3 @@
#!/bin/bash
echo "Good morning!"
echo "Good morning!" > ./kokodayo.txt

View File

@ -12,7 +12,7 @@ use super::responder::StaticResponderConfig;
pub struct Region {
pub id: String,
pub locations: Vec<Location>,
pub applications: Vec<Application>,
pub applications: Option<Vec<Application>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]

View File

@ -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<Client>,
) -> Result<HttpResponse, ProxyError> {
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::<String>();
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)
}

View File

@ -55,9 +55,9 @@ pub async fn handle(req: HttpRequest, client: web::Data<Client>) -> 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(),

View File

@ -29,7 +29,7 @@ pub fn build_single_proxy(cfg: ServerBindConfig) -> Result<Server, Box<dyn error
App::new()
.wrap(Logger::default())
.app_data(web::Data::new(Client::default()))
.route("/", web::to(route::handle))
.default_service(web::to(route::handle))
});
if cfg.tls {
Ok(server.bind_rustls_0_22(cfg.addr, use_rustls()?)?.run())

View File

@ -30,7 +30,7 @@ impl WardenInstance {
pub fn scan(&mut self, regions: Vec<Region>) {
self.applications = regions
.iter()
.flat_map(|item| item.applications.clone())
.flat_map(|item| item.applications.clone().unwrap_or_default())
.collect::<Vec<Application>>();
debug!(
applications = format!("{:?}", self.applications),