use http::{HeaderName, HeaderValue};
use n0_error::stack_error;
use strum::VariantArray;
#[cfg(feature = "server")]
pub(crate) const WEBSOCKET_UPGRADE_PROTOCOL: &str = "websocket";
#[cfg(feature = "server")] pub(crate) const SUPPORTED_WEBSOCKET_VERSION: &str = "13";
pub const RELAY_PATH: &str = "/relay";
pub const RELAY_PROBE_PATH: &str = "/ping";
pub const CLIENT_AUTH_HEADER: HeaderName = HeaderName::from_static("x-iroh-relay-client-auth-v1");
#[derive(
Clone,
Copy,
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Default,
strum::VariantArray,
strum::EnumString,
strum::Display,
strum::IntoStaticStr,
)]
#[strum(parse_err_ty = UnsupportedRelayProtocolVersion, parse_err_fn = strum_err_fn)]
pub enum ProtocolVersion {
#[strum(serialize = "iroh-relay-v1")]
V1,
#[default]
#[strum(serialize = "iroh-relay-v2")]
V2,
}
impl ProtocolVersion {
pub fn all() -> impl Iterator<Item = &'static str> {
Self::VARIANTS
.iter()
.map(ProtocolVersion::to_str)
.rev()
}
pub fn all_joined() -> String {
Self::all().collect::<Vec<_>>().join(", ")
}
pub fn all_as_header_value() -> HeaderValue {
HeaderValue::from_bytes(Self::all_joined().as_bytes()).expect("valid header name")
}
pub fn to_str(&self) -> &'static str {
self.into()
}
pub fn match_from_str(s: &str) -> Option<Self> {
Self::try_from(s).ok()
}
pub fn to_header_value(&self) -> HeaderValue {
HeaderValue::from_static(self.to_str())
}
}
#[stack_error(derive)]
#[error("Relay protocol version is not supported")]
pub struct UnsupportedRelayProtocolVersion;
fn strum_err_fn(_item: &str) -> UnsupportedRelayProtocolVersion {
UnsupportedRelayProtocolVersion::new()
}