Skip to main content

rama/cli/service/
mod.rs

1//! Reusable CLI [`Service`]s.
2//!
3//! These services are used by rama's binary
4//! distribution and that can be added to your own rama-driven CLI application as well.
5//!
6//! [`Service`]: crate::Service
7
8pub mod echo;
9pub mod fs;
10pub mod geo;
11pub mod http_security;
12pub mod ip;
13pub mod wire_names;
14
15#[cfg(feature = "quic")]
16use {
17    crate::{Service, quic},
18    std::convert::Infallible,
19};
20
21use crate::{
22    cli::ForwardKind,
23    combinators::Either7,
24    http::{
25        headers::forwarded::{
26            CFConnectingIp, ClientIp, TrueClientIp, XClientIp, XForwardedFor, XRealIp,
27        },
28        layer::forwarded::GetForwardedHeaderLayer,
29    },
30};
31
32/// What the echo and ip builders produce next to their TCP service: an HTTP/3 service for
33/// accepted QUIC connections, or nothing without the `quic` feature.
34#[cfg(feature = "quic")]
35trait MaybeHttp3Service: Service<quic::Connection, Output = (), Error = Infallible> {}
36#[cfg(feature = "quic")]
37impl<S: Service<quic::Connection, Output = (), Error = Infallible>> MaybeHttp3Service for S {}
38#[cfg(not(feature = "quic"))]
39trait MaybeHttp3Service {}
40#[cfg(not(feature = "quic"))]
41impl<T> MaybeHttp3Service for T {}
42
43/// The forwarded-header extraction layer selected by a [`ForwardKind`].
44///
45/// `None` means no extraction: the default, and `HaProxy` (which forwards at
46/// the transport layer instead).
47type HttpForwardedLayer = Option<
48    Either7<
49        GetForwardedHeaderLayer,
50        GetForwardedHeaderLayer<XForwardedFor>,
51        GetForwardedHeaderLayer<XClientIp>,
52        GetForwardedHeaderLayer<ClientIp>,
53        GetForwardedHeaderLayer<XRealIp>,
54        GetForwardedHeaderLayer<CFConnectingIp>,
55        GetForwardedHeaderLayer<TrueClientIp>,
56    >,
57>;
58
59/// Build the HTTP forwarded-header extraction layer selected by `forward`,
60/// shared by the echo and fs CLI services.
61pub(super) fn http_forwarded_layer(forward: Option<&ForwardKind>) -> HttpForwardedLayer {
62    match forward {
63        None | Some(ForwardKind::HaProxy) => None,
64        Some(ForwardKind::Forwarded) => Some(Either7::A(GetForwardedHeaderLayer::forwarded())),
65        Some(ForwardKind::XForwardedFor) => {
66            Some(Either7::B(GetForwardedHeaderLayer::x_forwarded_for()))
67        }
68        Some(ForwardKind::XClientIp) => {
69            Some(Either7::C(GetForwardedHeaderLayer::<XClientIp>::new()))
70        }
71        Some(ForwardKind::ClientIp) => Some(Either7::D(GetForwardedHeaderLayer::<ClientIp>::new())),
72        Some(ForwardKind::XRealIp) => Some(Either7::E(GetForwardedHeaderLayer::<XRealIp>::new())),
73        Some(ForwardKind::CFConnectingIp) => {
74            Some(Either7::F(GetForwardedHeaderLayer::<CFConnectingIp>::new()))
75        }
76        Some(ForwardKind::TrueClientIp) => {
77            Some(Either7::G(GetForwardedHeaderLayer::<TrueClientIp>::new()))
78        }
79    }
80}