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