Skip to main content

rama/
lib.rs

1//! ๐Ÿฆ™ **rama** (ใƒฉใƒž) is a modular network service and proxy framework for the ๐Ÿฆ€ Rust language.
2//!
3//! It gives you programmable control over how packets move through your stack so you can build:
4//!
5//! - production grade reverse and forward proxies
6//! - HTTP and TLS termination layers
7//! - security inspection and distortion proxies
8//! - high volume scraping and data extraction pipelines
9//! - custom HTTP clients with deep control over the wire
10//!
11//! rama is already used in production by companies at scale for use cases such as network
12//! security, data extraction, API gateways and routing.
13//!
14//! rama is async first and uses [`tokio`](https://tokio.rs) as its only async runtime.
15//!
16//! ---
17//!
18//! ## At a glance
19//!
20//! New here? Start with the [book](https://ramaproxy.org/book) for narrative explanations
21//! and the [examples](https://github.com/plabayo/rama/tree/main/examples) to see real code.
22//! Use this section to jump straight into the API.
23//!
24//! **Core abstractions** (always available, re-exported from
25//! [`rama-core`](https://docs.rs/rama-core)):
26//!
27//! - [`Service`] โ€” central trait: takes a request, produces a response or error
28//! - [`Layer`] โ€” wraps a [`Service`] to add behavior (timeouts, retries, telemetry, โ€ฆ)
29//! - support: [`error`], [`graceful`], [`rt`], [`combinators`], [`extensions`], [`matcher`],
30//!   [`stream`], [`io`], [`bytes`], [`futures`]
31//!
32//! **Networking (transports and primitives)**:
33//!
34//! - [`tcp`] ยท [`udp`] ยท `unix` โ€” listeners, connectors, sockets
35//! - [`net`] โ€” shared addresses / sockets / fingerprints (Apple-only items live in
36//!   `net::apple`)
37//! - [`dns`] โ€” DNS resolvers and types (native resolvers or opt-in HickoryDNS)
38//! - [`tls`] โ€” TLS via [rustls](`tls::rustls`) / [boring](`tls::boring`) and
39//!   [`tls::acme`](`tls::acme`)
40//!
41//! **Application protocols**:
42//!
43//! - [`http`] โ€” HTTP/1 + HTTP/2: [`http::server`], [`http::client`],
44//!   [`http::layer`], [`http::service`], [`http::tls`], [`http::ws`], [`http::grpc`],
45//!   [`http::proxy`]
46//! - [`proxy`] โ€” proxy primitives, [`proxy::socks5`], [`proxy::haproxy`]
47//!
48//! **Cross-cutting**:
49//!
50//! - [`telemetry`] โ€” tracing and OpenTelemetry
51//! - [`ua`] โ€” User-Agent emulation and fingerprinting
52//! - [`utils`] (incl. [`utils::tower`] for tower compat), [`crypto`], [`cli`]
53//!
54//! **Common entry points** for first-time readers:
55//!
56//! - HTTP client: [`http::client::EasyHttpWebClient`] +
57//!   [`http::service::client::HttpClientExt`]
58//! - HTTP server / web service: [`http::server`] and [`http::service::web`]
59//! - SOCKS5 server: [`proxy::socks5`]
60//! - Reverse / forward proxy: [`http::proxy`] (HTTP CONNECT) +
61//!   [`proxy::socks5`] / [`proxy::haproxy`]
62//! - L4 transparent proxy
63//!   - On Linux: rama services on top of `IP_TRANSPARENT` / `TPROXY`. See the
64//!     [`linux_tproxy_tcp`](https://github.com/plabayo/rama/blob/main/examples/linux_tproxy_tcp.rs)
65//!     example (with companion setup / cleanup scripts in the same directory).
66//!   - On macOS / iOS: platform-specific submodules of [`net`] bridge Apple
67//!     [NetworkExtension](https://developer.apple.com/documentation/networkextension)
68//!     flows into rama services and host โ†” extension IPC over
69//!     [Apple XPC](https://developer.apple.com/documentation/xpc). Available behind
70//!     the `net-apple-networkextension` and `net-apple-xpc` features (browse them
71//!     under [`net`] in the docs built on an Apple vendor target. See the
72//!     [`tproxy` example](https://github.com/plabayo/rama/tree/main/ffi/apple/examples/transparent_proxy)
73//!     for an end-to-end setup including the system-extension scaffolding.
74//!
75//! ---
76//!
77//! ## Who is rama for
78//!
79//! - **Developers and teams** who want fine grained control over transport, TLS and HTTP
80//!   while staying in safe Rust.
81//! - **Organisations** that need a partner to:
82//!   - maintain and evolve a proxy or network platform
83//!   - build custom features on top of rama
84//!   - get support and training for internal teams
85//!
86//! ---
87//!
88//! ## Getting started
89//!
90//! 1. Read the ["Why rama" chapter](https://ramaproxy.org/book/why_rama) for background.
91//! 2. Run one of the examples in
92//!    <https://github.com/plabayo/rama/tree/main/examples>.
93//! 3. Use the rama book at <https://ramaproxy.org/book> and the Rust docs at
94//!    <https://docs.rs/rama> or <https://ramaproxy.org/docs/rama> as references
95//!    while building your own stack.
96//!
97//! You can also use the `rama` binary if you want to use some of the rama features from the command line
98//! without writing your own Rust code. See <https://ramaproxy.org/book/deploy/rama-cli.html>.
99//!
100//! ---
101//!
102//! ## For organisations
103//!
104//! If your organisation relies on rama or plans to, the maintainers offer:
105//!
106//! - support and maintenance contracts
107//! - feature development with higher priority or extended scope
108//! - consulting and integration work around proxies, scraping and security
109//! - training and mentoring for your internal teams
110//!
111//! To discuss options, contact `hello@plabayo.tech`.
112//!
113//! Enterprise sponsorships are available via GitHub Sponsors and help fund development,
114//! maintenance and ecosystem work.
115//!
116//! ---
117//!
118//! ## Batteries included
119//!
120//! rama ships with batteries included for transports, HTTP, TLS, DNS, proxy protocols,
121//! telemetry, fingerprinting and more. Some highlights:
122//!
123//! - transports: TCP, UDP, Unix domain sockets, connection pooling and middleware
124//! - HTTP: HTTP 1 and 2 servers and clients, layers and middleware, metrics, tracing
125//! - TLS: Rustls and BoringSSL support
126//! - proxy protocols: HTTP connect, HTTPS connect, SOCKS5, HAProxy PROXY protocol
127//! - fingerprinting and user agent emulation for distortion and anti bot use cases
128//! - telemetry: tracing integration and OpenTelemetry metrics for HTTP and transport layers
129//!
130//! For a detailed and up to date overview see the feature table in the README and the
131//! relevant chapters in the rama book.
132//!
133//! ---
134//!
135//! ## Proxies and proxy focused use cases
136//!
137//! The primary focus of rama is to help you build proxies and proxy like services:
138//!
139//! - reverse proxies
140//! - TLS termination proxies
141//! - HTTP and HTTPS proxies
142//! - SOCKS5 proxies
143//! - SNI based routing proxies
144//! - MITM proxies
145//! - distortion proxies with UA emulation and fingerprinting
146//!
147//! The proxy chapters in the book start at
148//! <https://ramaproxy.org/book/proxies/intro.html>.
149//!
150//! Distortion support includes User Agent emulation for HTTP and TLS built on top of data
151//! collected by [`rama-fp`](https://github.com/plabayo/rama/tree/main/rama-fp) and exposed
152//! via <https://fp.ramaproxy.org>.
153//!
154//! ---
155//!
156//! ## Web services
157//!
158//! Even though proxies are the main focus, rama can also be used to build general purpose
159//! web services. Typical use cases:
160//!
161//! - dynamic HTTP endpoints
162//! - serving static files
163//! - websockets and Server Sent Events (SSE)
164//! - health and readiness endpoints for Kubernetes
165//! - metrics and control plane services
166//!
167//! rama gives you:
168//!
169//! - async method trait based services and layers
170//! - modular middleware to reuse across services and clients
171//! - full control from transport through TLS to HTTP and web protocols
172//!
173//! Learn more in the web servers chapter of the book:
174//! <https://ramaproxy.org/book/web_servers.html>.
175//!
176//! ### Datastar integration
177//!
178//! rama has built in support for [Datastar](https://data-star.dev) for reactive web
179//! applications using SSE. See the examples at
180//! <https://github.com/plabayo/rama/tree/main/examples> and the docs at:
181//!
182//! - <https://ramaproxy.org/docs/rama/http/sse/datastar/index.html>
183//! - <https://ramaproxy.org/docs/rama/http/service/web/extract/datastar/index.html>
184//! - <https://ramaproxy.org/docs/rama/http/service/web/response/struct.DatastarScript.html>
185//!
186//! ---
187//!
188//! ## Web clients
189//!
190//! A large part of rama is built on top of a service concept. A `Service` takes a `Request`
191//! and produces a `Response` or `Error`. Services can be leaf services or middlewares that
192//! wrap inner services.
193//!
194//! rama provides:
195//!
196//! - an `EasyHttpWebClient` for HTTP requests
197//! - many HTTP layers to tune timeouts, retries, telemetry and more
198//! - a high level `HttpClientExt` trait to build and send requests with a fluent API
199//!
200//! See the client example at
201//! <https://github.com/plabayo/rama/tree/main/examples/http_high_level_client.rs> and the
202//! docs at:
203//!
204//! - <https://ramaproxy.org/docs/rama/http/client/struct.EasyHttpWebClient.html>
205//! - <https://ramaproxy.org/docs/rama/http/service/client/trait.HttpClientExt.html>
206//!
207//! ---
208//!
209//! ## Ecosystem
210//!
211//! The `rama` crate can be used as the one and only dependency.
212//! However, as you can also read in the "DIY" chapter of the book
213//! at <https://ramaproxy.org/book/diy.html#empowering>, you are able
214//! to pick and choose not only what specific parts of `rama` you wish to use,
215//! but also in fact what specific (sub) crates.
216//!
217//! Here is a list of all `rama` crates:
218//!
219//! - [`rama`](https://crates.io/crates/rama): one crate to rule them all
220//! - [`rama-error`](https://crates.io/crates/rama-error): error utilities for rama and its users
221//! - [`rama-macros`](https://crates.io/crates/rama-macros): contains the procedural macros used by `rama`
222//! - [`rama-utils`](https://crates.io/crates/rama-utils): utilities crate for rama
223//! - [`rama-ws`](https://crates.io/crates/rama-ws): WebSocket (WS) support for rama
224//! - [`rama-core`](https://crates.io/crates/rama-core): core crate containing the service and layer traits
225//!   used by all other `rama` code, as well as some other _core_ utilities
226//! - [`rama-crypto`](https://crates.io/crates/rama-crypto): rama crypto primitives and dependencies
227//! - [`rama-net`](https://crates.io/crates/rama-net): rama network types and utilities
228//! - [`rama-net-apple-networkextension`](https://crates.io/crates/rama-net-apple-networkextension): Apple Network Extension support for rama
229//! - [`rama-net-apple-xpc`](https://crates.io/crates/rama-net-apple-xpc): Apple XPC support for rama
230//! - [`rama-dns`](https://crates.io/crates/rama-dns): DNS support for rama
231//! - [`rama-unix`](https://crates.io/crates/rama-unix): Unix (domain) socket support for rama
232//! - [`rama-tcp`](https://crates.io/crates/rama-tcp): TCP support for rama
233//! - [`rama-udp`](https://crates.io/crates/rama-udp): UDP support for rama
234//! - [`rama-tls-acme`](https://crates.io/crates/rama-tls-acme): ACME support for rama
235//! - [`rama-tls-boring`](https://crates.io/crates/rama-tls-boring): [Boring](https://github.com/plabayo/rama-boring) tls support for rama
236//! - [`rama-tls-rustls`](https://crates.io/crates/rama-tls-rustls): [Rustls](https://github.com/rustls/rustls) support for rama
237//! - [`rama-proxy`](https://crates.io/crates/rama-proxy): proxy types and utilities for rama
238//! - [`rama-socks5`](https://crates.io/crates/rama-socks5): SOCKS5 support for rama
239//! - [`rama-haproxy`](https://crates.io/crates/rama-haproxy): rama HAProxy support
240//! - [`rama-ua`](https://crates.io/crates/rama-ua): User-Agent (UA) support for `rama`
241//! - [`rama-http-types`](https://crates.io/crates/rama-http-types): http types and utilities
242//! - [`rama-http-headers`](https://crates.io/crates/rama-http-headers): typed http headers
243//! - [`rama-grpc`](https://crates.io/crates/rama-grpc): Grpc support for rama
244//! - [`rama-grpc-codegen`](https://crates.io/crates/rama-grpc-codegen): Grpc codegen support for rama
245//! - [`rama-http`](https://crates.io/crates/rama-http): rama http services, layers and utilities
246//! - [`rama-http-macros`](https://crates.io/crates/rama-http-macros): proc-macros powering the type-safe HTML templating in `rama-http::html`
247//! - [`rama-http-backend`](https://crates.io/crates/rama-http-backend): default http backend for `rama`
248//! - [`rama-http-core`](https://crates.io/crates/rama-http-core): http protocol implementation driving `rama-http-backend`
249//! - [`rama-tower`](https://crates.io/crates/rama-tower): provide [tower](https://github.com/tower-rs/tower) compatibility for `rama`
250//!
251//! `rama` crates that live in <https://github.com/plabayo/rama-boring> (forks of `cloudflare/boring`):
252//!
253//! - [`rama-boring`](https://crates.io/crates/rama-boring): BoringSSL bindings for rama
254//! - [`rama-boring-sys`](https://crates.io/crates/rama-boring-sys): FFI bindings to BoringSSL for rama
255//! - [`rama-boring-tokio`](https://crates.io/crates/rama-boring-tokio): an implementation of SSL streams for Tokio backed by BoringSSL in function of rama
256//!
257//! repositories in function of rama that aren't crates:
258//!
259//! - <https://github.com/plabayo/rama-boringssl>:
260//!   Fork of [mirror of BoringSSL](https://github.com/plabayo/rama-boringssl)
261//!   in function of [rama-boring](https://github.com/plabayo/rama-boring)
262//! - <https://github.com/plabayo/homebrew-rama>: Homebrew formula for the rama Cli tool
263//!
264//! Repositories that we maintain and are re exported by the root `rama` crate:
265//!
266//! - <https://github.com/plabayo/tokio-graceful>: Graceful shutdown util for Rust projects using the Tokio Async runtime.
267//!
268//! Community crates that extend the ecosystem are encouraged. If you publish a community
269//! crate, please prefix it with `rama-x` so it is easy to discover and clearly distinct
270//! from the official crates in this repository.
271//!
272//! ---
273//!
274//! ## Safety and compatibility
275//!
276//! - rama crates avoid `unsafe` Rust as much as possible and use it only where necessary.
277//! - Supply chain auditing is done with [`cargo vet`](https://github.com/mozilla/cargo-vet).
278//! - Tier 1 platforms include macOS, Linux and Windows on modern architectures.
279//! - The minimum supported Rust version (MSRV) is `1.93`.
280//!
281//! For details see the compatibility section in the README and the CI configuration in
282//! the repository.
283//!
284//! ---
285//!
286//! ## License
287//!
288//! rama is free and open source software, dual licensed under MIT and Apache 2.0.
289//!
290//! You can use rama for commercial and non commercial purposes. If rama becomes an
291//! important part of your stack, please consider supporting the project as a sponsor
292//! or partner.
293//!
294//! ---
295//!
296//! ## Community and links
297//!
298//! - Official website: <https://ramaproxy.org>
299//! - Rama Book index: <https://ramaproxy.org/book>
300//! - Rust docs: <https://docs.rs/rama> (latest release) and <https://ramaproxy.org/docs/rama> (edge)
301//! - Repository and issues: <https://github.com/plabayo/rama>
302//! - Discord: <https://discord.gg/29EetaSYCD>
303//! - FAQ: <https://ramaproxy.org/book/faq.html>
304//! - Netstack FM podcast: <https://netstack.fm> (podcast about networking and Rust)
305//!
306//! If you are not sure where to start, read "Why rama" in the book, run a proxy example and then
307//! iterate from there with the book and docs at hand.
308
309#![doc(
310    html_favicon_url = "https://raw.githubusercontent.com/plabayo/rama/main/docs/img/old_logo.png"
311)]
312#![doc(html_logo_url = "https://raw.githubusercontent.com/plabayo/rama/main/docs/img/old_logo.png")]
313#![cfg_attr(docsrs, feature(doc_cfg))]
314#![cfg_attr(test, allow(clippy::float_cmp))]
315
316#[doc(inline)]
317pub use ::rama_core::{
318    Layer, Service, ServiceInput, bytes, combinators, conversion, error, extensions, futures,
319    graceful, io, layer, matcher, rt, service, stream, username,
320};
321
322#[cfg(feature = "crypto")]
323#[cfg_attr(docsrs, doc(cfg(feature = "crypto")))]
324#[doc(inline)]
325pub use ::rama_crypto as crypto;
326
327#[cfg(all(target_family = "unix", feature = "net"))]
328#[cfg_attr(docsrs, doc(cfg(all(target_family = "unix", feature = "net"))))]
329#[doc(inline)]
330pub use ::rama_unix as unix;
331
332#[cfg(feature = "tcp")]
333#[cfg_attr(docsrs, doc(cfg(feature = "tcp")))]
334#[doc(inline)]
335pub use ::rama_tcp as tcp;
336
337#[cfg(feature = "udp")]
338#[cfg_attr(docsrs, doc(cfg(feature = "udp")))]
339#[doc(inline)]
340pub use ::rama_udp as udp;
341
342pub mod telemetry;
343
344#[cfg(any(feature = "rustls", feature = "boring", feature = "acme"))]
345pub mod tls;
346
347#[cfg(feature = "dns")]
348#[cfg_attr(docsrs, doc(cfg(feature = "dns")))]
349#[doc(inline)]
350pub use ::rama_dns as dns;
351
352#[cfg(feature = "net")]
353pub mod net {
354    #[cfg_attr(docsrs, doc(cfg(feature = "net")))]
355    #[doc(inline)]
356    pub use ::rama_net::*;
357
358    #[cfg(any(
359        all(doc, docsrs),
360        all(
361            target_vendor = "apple",
362            any(feature = "net-apple-networkextension", feature = "net-apple-xpc")
363        )
364    ))]
365    #[cfg_attr(docsrs, doc(cfg(target_vendor = "apple")))]
366    pub mod apple {
367        //! Apple (vendor) specific network modules
368
369        #[cfg(feature = "net-apple-networkextension")]
370        #[cfg_attr(docsrs, doc(cfg(feature = "net-apple-networkextension")))]
371        #[doc(inline)]
372        pub use ::rama_net_apple_networkextension as networkextension;
373
374        #[cfg(feature = "net-apple-xpc")]
375        #[cfg_attr(docsrs, doc(cfg(feature = "net-apple-xpc")))]
376        #[doc(inline)]
377        pub use ::rama_net_apple_xpc as xpc;
378    }
379}
380
381#[cfg(feature = "http")]
382#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
383pub mod http;
384
385#[cfg(any(feature = "proxy", feature = "haproxy", feature = "socks5"))]
386pub mod proxy {
387    //! rama proxy support
388
389    #[cfg(feature = "proxy")]
390    #[cfg_attr(docsrs, doc(cfg(feature = "proxy")))]
391    #[doc(inline)]
392    pub use ::rama_proxy::*;
393
394    #[cfg(feature = "haproxy")]
395    #[cfg_attr(docsrs, doc(cfg(feature = "haproxy")))]
396    #[doc(inline)]
397    pub use ::rama_haproxy as haproxy;
398
399    #[cfg(feature = "socks5")]
400    #[cfg_attr(docsrs, doc(cfg(feature = "socks5")))]
401    #[doc(inline)]
402    pub use ::rama_socks5 as socks5;
403}
404
405/// Application server gateway protocols (FastCGI, and similar).
406pub mod gateway {
407    #[cfg(feature = "fastcgi")]
408    #[cfg_attr(docsrs, doc(cfg(feature = "fastcgi")))]
409    #[doc(inline)]
410    pub use ::rama_fastcgi as fastcgi;
411}
412
413#[cfg(feature = "ua")]
414#[cfg_attr(docsrs, doc(cfg(feature = "ua")))]
415#[doc(inline)]
416pub use ::rama_ua as ua;
417
418#[cfg(feature = "cli")]
419#[cfg_attr(docsrs, doc(cfg(feature = "cli")))]
420pub mod cli;
421
422pub mod utils {
423    //! utilities for rama
424
425    #[doc(inline)]
426    pub use ::rama_utils::*;
427
428    #[cfg(feature = "tower")]
429    #[cfg_attr(docsrs, doc(cfg(feature = "tower")))]
430    #[doc(inline)]
431    pub use ::rama_tower as tower;
432}