atm0s_reverse_proxy_protocol/
proxy.rs

1use std::fmt::Display;
2
3use anyhow::anyhow;
4use derive_more::derive::{Deref, From};
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Hash, PartialEq, Eq, From, Deref, Clone, Copy)]
8pub struct AgentId(u64);
9
10impl Display for AgentId {
11    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12        f.write_fmt(format_args!("AgentId({:02x})", self.0))
13    }
14}
15
16impl AgentId {
17    pub fn try_from_domain(domain: &str) -> anyhow::Result<Self> {
18        let (first, _) = domain.as_bytes().split_at_checked(8).ok_or(anyhow!("domain should be at least 8 bytes"))?;
19        Ok(Self(u64::from_be_bytes(first.try_into()?)))
20    }
21}
22
23#[derive(Debug, Serialize, Deserialize)]
24pub struct ProxyDestination {
25    pub domain: String,
26    pub service: Option<u16>,
27    pub tls: bool,
28}
29
30impl ProxyDestination {
31    pub fn agent_id(&self) -> anyhow::Result<AgentId> {
32        AgentId::try_from_domain(&self.domain)
33    }
34}