atm0s_reverse_proxy_relayer/proxy/
rtsp.rs

1use anyhow::anyhow;
2use protocol::services::SERVICE_RTSP;
3use tokio::net::TcpStream;
4
5use super::{ProxyDestination, ProxyDestinationDetector};
6
7#[derive(Debug, Default)]
8pub struct RtspDestinationDetector {}
9
10impl ProxyDestinationDetector for RtspDestinationDetector {
11    async fn determine(&self, stream: &mut TcpStream) -> anyhow::Result<ProxyDestination> {
12        let mut buf = [0; 4096];
13        let buf_len = stream.peek(&mut buf).await?;
14        log::info!("[RtspDomainDetector] check domain for {}", String::from_utf8_lossy(&buf[..buf_len]));
15        let (message, _consumed): (rtsp_types::Message<Vec<u8>>, _) = rtsp_types::Message::parse(&buf[..buf_len])?;
16        log::info!("{:?}", message);
17        match message {
18            rtsp_types::Message::Request(req) => Ok(ProxyDestination {
19                domain: req
20                    .request_uri()
21                    .ok_or(anyhow!("missing request uri"))?
22                    .host()
23                    .map(|h| h.to_string())
24                    .ok_or(anyhow!("missing host header"))?,
25                service: Some(SERVICE_RTSP),
26                tls: false,
27            }),
28            _ => Err(anyhow!("invalid rtsp message")),
29        }
30    }
31}