use crate::net::{ProxyVersions, TrustedProxies};
#[non_exhaustive]
#[derive(Clone, Debug, Default)]
pub enum Ingress {
#[default]
Direct,
Proxy(ProxySource),
}
impl Ingress {
pub(crate) fn proxy(&self) -> Option<&ProxySource> {
match self {
Ingress::Proxy(p) => Some(p),
Ingress::Direct => None,
}
}
}
#[must_use]
#[derive(Clone, Debug)]
pub struct ProxySource {
pub(crate) kind: ProxyKind,
pub(crate) trusted: TrustedProxies,
pub(crate) trust_scheme: bool,
pub(crate) trust_host: bool,
pub(crate) max_hops: u8,
pub(crate) missing: MissingHeader,
pub(crate) unknown: UnknownClient,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ProxyKind {
Headers(ForwardedSource),
#[allow(dead_code)] ProxyProtocol(ProxyVersions),
}
impl ProxySource {
pub fn forwarded_for(trusted: TrustedProxies) -> Self {
Self::headers(ForwardedSource::XForwardedFor, trusted)
}
pub fn headers(source: ForwardedSource, trusted: TrustedProxies) -> Self {
ProxySource {
kind: ProxyKind::Headers(source),
trusted,
trust_scheme: false,
trust_host: false,
max_hops: 16,
missing: MissingHeader::Refuse,
unknown: UnknownClient::Allow,
}
}
pub fn trust_scheme(mut self, on: bool) -> Self {
self.trust_scheme = on;
self
}
pub fn trust_host(mut self, on: bool) -> Self {
self.trust_host = on;
self
}
pub fn max_hops(mut self, n: u8) -> Self {
self.max_hops = n;
self
}
pub fn missing(mut self, policy: MissingHeader) -> Self {
self.missing = policy;
self
}
pub fn unknown(mut self, policy: UnknownClient) -> Self {
self.unknown = policy;
self
}
#[must_use]
pub fn trusted(&self) -> &TrustedProxies {
&self.trusted
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ForwardedSource {
Forwarded,
XForwardedFor,
SingleAddress(&'static str),
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum MissingHeader {
#[default]
Refuse,
UsePeer,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum UnknownClient {
#[default]
Allow,
Refuse,
}