feat: works

This commit is contained in:
2024-03-30 12:11:11 +08:00
parent 4aaec3aa95
commit 277843967b
4 changed files with 56 additions and 76 deletions

View File

@@ -2,7 +2,6 @@ use async_trait::async_trait;
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use http::HeaderName;
use log::{debug, info};
use pingora::{Error, ErrorType};
use pingora::prelude::{HttpPeer, ProxyHttp, Result, Session};
@@ -29,24 +28,30 @@ impl ProxyHttp for ProxyApp {
.unwrap()
.to_str()
.expect("get host from http header failed");
debug!("host header: {host_header}");
log::info!("host header: {host_header}");
if host_header == "localhost" || host_header.starts_with("localhost:") {
let hostname = if host_header.contains(':') {
host_header.chars().take_while(|c| c != &':').collect()
} else {
host_header.to_string()
};
if hostname == "localhost" {
return Err(Error::new(ErrorType::CustomCode("bad host", 400)));
}
let host_config = self
.host_configs
.iter()
.find(|x| x.proxy_hostname == host_header)
.unwrap_or_else(|| panic!("find config for: {} failed", host_header));
let proxy_to = HttpPeer::new(
.find(|x| x.proxy_hostname == hostname)
.unwrap_or_else(|| panic!("find config for: {} failed", hostname));
let peer = HttpPeer::new(
host_config.proxy_addr.as_str(),
host_config.proxy_tls,
host_config.proxy_hostname.clone(),
);
let peer = Box::new(proxy_to);
Ok(peer)
log::info!("Find peer: {:?}", peer._address);
Ok(Box::new(peer))
}
async fn request_filter(&self, session: &mut Session, _ctx: &mut Self::CTX) -> Result<bool>
@@ -75,7 +80,7 @@ impl ProxyHttp for ProxyApp {
_ => None,
};
info!("Request:\n{}\n\n{}", req, body.unwrap_or_else(|| "<None>".into()));
log::info!("Request:\n{}\n\n{}", req, body.unwrap_or_else(|| "<None>".into()));
Ok(false)
}
}