atm0s_reverse_proxy_relayer/proxy/
http.rs

1use anyhow::anyhow;
2use tokio::net::TcpStream;
3
4use super::{ProxyDestination, ProxyDestinationDetector};
5
6#[derive(Debug, Default)]
7pub struct HttpDestinationDetector {}
8
9impl ProxyDestinationDetector for HttpDestinationDetector {
10    async fn determine(&self, stream: &mut TcpStream) -> anyhow::Result<ProxyDestination> {
11        let mut buf = [0; 4096];
12        let buf_len = stream.peek(&mut buf).await?;
13        log::info!("[HttpDomainDetector] check domain for {}", String::from_utf8_lossy(&buf[..buf_len]));
14        let mut headers = [httparse::EMPTY_HEADER; 64];
15        let mut req = httparse::Request::new(&mut headers);
16        let _ = req.parse(&buf[..buf_len])?;
17        let domain = req.headers.iter().find(|h| h.name.to_lowercase() == "host").ok_or(anyhow!("host header missing"))?.value;
18        // dont get the port
19        let domain = String::from_utf8_lossy(domain);
20        let domain = domain.split(':').next().expect("should have domain");
21        Ok(ProxyDestination {
22            domain: domain.to_string(),
23            service: None,
24            tls: false,
25        })
26    }
27}