feat: init commit

This commit is contained in:
2024-03-24 18:03:20 +08:00
parent 0ae2b28434
commit db37ba5fd3
6 changed files with 2796 additions and 4 deletions

44
src/app.rs Normal file
View File

@@ -0,0 +1,44 @@
use async_trait::async_trait;
use http::HeaderName;
use log::debug;
use pingora::prelude::{HttpPeer, ProxyHttp, Result, Session};
use super::service::HostConfig;
pub struct ProxyApp {
host_configs: Vec<HostConfig>,
}
impl ProxyApp {
pub fn new(host_configs: Vec<HostConfig>) -> Self {
ProxyApp { host_configs }
}
}
#[async_trait]
impl ProxyHttp for ProxyApp {
type CTX = ();
fn new_ctx(&self) {}
async fn upstream_peer(&self, session: &mut Session, _ctx: &mut ()) -> Result<Box<HttpPeer>> {
let host_header = session
.get_header(HeaderName::from_static("host"))
.unwrap()
.to_str()
.expect("get host from http header failed");
debug!("host header: {host_header}");
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(
host_config.proxy_addr.as_str(),
host_config.proxy_tls,
host_config.proxy_hostname.clone(),
);
let peer = Box::new(proxy_to);
Ok(peer)
}
}