feat: updates

This commit is contained in:
2024-03-24 20:53:50 +08:00
parent db37ba5fd3
commit 9696af7a50
6 changed files with 98 additions and 23 deletions

View File

@@ -1,6 +1,9 @@
use async_trait::async_trait;
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use http::HeaderName;
use log::debug;
use log::{debug, info};
use pingora::{Error, ErrorType};
use pingora::prelude::{HttpPeer, ProxyHttp, Result, Session};
use super::service::HostConfig;
@@ -28,6 +31,10 @@ impl ProxyHttp for ProxyApp {
.expect("get host from http header failed");
debug!("host header: {host_header}");
if host_header == "localhost" || host_header.starts_with("localhost:") {
return Err(Error::new(ErrorType::CustomCode("bad host", 400)));
}
let host_config = self
.host_configs
.iter()
@@ -41,4 +48,34 @@ impl ProxyHttp for ProxyApp {
let peer = Box::new(proxy_to);
Ok(peer)
}
async fn request_filter(&self, session: &mut Session, _ctx: &mut Self::CTX) -> Result<bool>
where Self::CTX: Send + Sync,
{
let request_header = session.req_header();
let mut req = String::with_capacity(512);
req.push_str(request_header.method.as_str());
req.push(' ');
req.push_str(&request_header.uri.to_string());
req.push(' ');
req.push_str(&format!("{:?}\n", request_header.version));
let header_len = request_header.headers.len();
request_header.headers.iter().enumerate().for_each(|(i, (n, v))| {
req.push_str(
&format!("{}: {}{}",
n.as_str(),
v.to_str().unwrap_or("ERROR!BAD-VALUE!"),
if i < header_len - 1 { "\n" } else { "" }
)
);
});
let body = match session.read_request_body().await {
Ok(Some(body_bytes)) => Some(STANDARD.encode(body_bytes)),
_ => None,
};
info!("Request:\n{}\n\n{}", req, body.unwrap_or_else(|| "<None>".into()));
Ok(false)
}
}