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//! ## Experimental status
103//!
104//! rama is considered experimental software for the foreseeable future. At the same time
105//! it is already used in production by the maintainers and by other organisations.
106//!
107//! Real world use helps shape the design and priorities. If you run rama in production,
108//! feedback via GitHub issues, email or Discord is very welcome.
109//!
110//! ---
111//!
112//! ## For organisations
113//!
114//! If your organisation relies on rama or plans to, the maintainers offer:
115//!
116//! - support and maintenance contracts
117//! - feature development with higher priority or extended scope
118//! - consulting and integration work around proxies, scraping and security
119//! - training and mentoring for your internal teams
120//!
121//! To discuss options, contact `hello@plabayo.tech`.
122//!
123//! Enterprise sponsorships are available via GitHub Sponsors and help fund development,
124//! maintenance and ecosystem work.
125//!
126//! ---
127//!
128//! ## Batteries included
129//!
130//! rama ships with batteries included for transports, HTTP, TLS, DNS, proxy protocols,
131//! telemetry, fingerprinting and more. Some highlights:
132//!
133//! - transports: TCP, UDP, Unix domain sockets, connection pooling and middleware
134//! - HTTP: HTTP 1 and 2 servers and clients, layers and middleware, metrics, tracing
135//! - TLS: Rustls and BoringSSL support
136//! - proxy protocols: HTTP connect, HTTPS connect, SOCKS5, HAProxy PROXY protocol
137//! - fingerprinting and user agent emulation for distortion and anti bot use cases
138//! - telemetry: tracing integration and OpenTelemetry metrics for HTTP and transport layers
139//!
140//! For a detailed and up to date overview see the feature table in the README and the
141//! relevant chapters in the rama book.
142//!
143//! ---
144//!
145//! ## Proxies and proxy focused use cases
146//!
147//! The primary focus of rama is to help you build proxies and proxy like services:
148//!
149//! - reverse proxies
150//! - TLS termination proxies
151//! - HTTP and HTTPS proxies
152//! - SOCKS5 proxies
153//! - SNI based routing proxies
154//! - MITM proxies
155//! - distortion proxies with UA emulation and fingerprinting
156//!
157//! The proxy chapters in the book start at
158//! <https://ramaproxy.org/book/proxies/intro.html>.
159//!
160//! Distortion support includes User Agent emulation for HTTP and TLS built on top of data
161//! collected by [`rama-fp`](https://github.com/plabayo/rama/tree/main/rama-fp) and exposed
162//! via <https://fp.ramaproxy.org>.
163//!
164//! ---
165//!
166//! ## Web services
167//!
168//! Even though proxies are the main focus, rama can also be used to build general purpose
169//! web services. Typical use cases:
170//!
171//! - dynamic HTTP endpoints
172//! - serving static files
173//! - websockets and Server Sent Events (SSE)
174//! - health and readiness endpoints for Kubernetes
175//! - metrics and control plane services
176//!
177//! rama gives you:
178//!
179//! - async method trait based services and layers
180//! - modular middleware to reuse across services and clients
181//! - full control from transport through TLS to HTTP and web protocols
182//!
183//! Learn more in the web servers chapter of the book:
184//! <https://ramaproxy.org/book/web_servers.html>.
185//!
186//! ### Datastar integration
187//!
188//! rama has built in support for [Datastar](https://data-star.dev) for reactive web
189//! applications using SSE. See the examples at
190//! <https://github.com/plabayo/rama/tree/main/examples> and the docs at:
191//!
192//! - <https://ramaproxy.org/docs/rama/http/sse/datastar/index.html>
193//! - <https://ramaproxy.org/docs/rama/http/service/web/extract/datastar/index.html>
194//! - <https://ramaproxy.org/docs/rama/http/service/web/response/struct.DatastarScript.html>
195//!
196//! ---
197//!
198//! ## Web clients
199//!
200//! A large part of rama is built on top of a service concept. A `Service` takes a `Request`
201//! and produces a `Response` or `Error`. Services can be leaf services or middlewares that
202//! wrap inner services.
203//!
204//! rama provides:
205//!
206//! - an `EasyHttpWebClient` for HTTP requests
207//! - many HTTP layers to tune timeouts, retries, telemetry and more
208//! - a high level `HttpClientExt` trait to build and send requests with a fluent API
209//!
210//! See the client example at
211//! <https://github.com/plabayo/rama/tree/main/examples/http_high_level_client.rs> and the
212//! docs at:
213//!
214//! - <https://ramaproxy.org/docs/rama/http/client/struct.EasyHttpWebClient.html>
215//! - <https://ramaproxy.org/docs/rama/http/service/client/trait.HttpClientExt.html>
216//!
217//! ---
218//!
219//! ## Ecosystem
220//!
221//! The `rama` crate can be used as the one and only dependency.
222//! However, as you can also read in the "DIY" chapter of the book
223//! at <https://ramaproxy.org/book/diy.html#empowering>, you are able
224//! to pick and choose not only what specific parts of `rama` you wish to use,
225//! but also in fact what specific (sub) crates.
226//!
227//! Here is a list of all `rama` crates:
228//!
229//! - [`rama`](https://crates.io/crates/rama): one crate to rule them all
230//! - [`rama-error`](https://crates.io/crates/rama-error): error utilities for rama and its users
231//! - [`rama-macros`](https://crates.io/crates/rama-macros): contains the procedural macros used by `rama`
232//! - [`rama-utils`](https://crates.io/crates/rama-utils): utilities crate for rama
233//! - [`rama-ws`](https://crates.io/crates/rama-ws): WebSocket (WS) support for rama
234//! - [`rama-core`](https://crates.io/crates/rama-core): core crate containing the service and layer traits
235//!   used by all other `rama` code, as well as some other _core_ utilities
236//! - [`rama-crypto`](https://crates.io/crates/rama-crypto): rama crypto primitives and dependencies
237//! - [`rama-net`](https://crates.io/crates/rama-net): rama network types and utilities
238//! - [`rama-net-apple-networkextension`](https://crates.io/crates/rama-net-apple-networkextension): Apple Network Extension support for rama
239//! - [`rama-net-apple-xpc`](https://crates.io/crates/rama-net-apple-xpc): Apple XPC support for rama
240//! - [`rama-dns`](https://crates.io/crates/rama-dns): DNS support for rama
241//! - [`rama-unix`](https://crates.io/crates/rama-unix): Unix (domain) socket support for rama
242//! - [`rama-tcp`](https://crates.io/crates/rama-tcp): TCP support for rama
243//! - [`rama-udp`](https://crates.io/crates/rama-udp): UDP support for rama
244//! - [`rama-tls-acme`](https://crates.io/crates/rama-tls-acme): ACME support for rama
245//! - [`rama-tls-boring`](https://crates.io/crates/rama-tls-boring): [Boring](https://github.com/plabayo/rama-boring) tls support for rama
246//! - [`rama-tls-rustls`](https://crates.io/crates/rama-tls-rustls): [Rustls](https://github.com/rustls/rustls) support for rama
247//! - [`rama-proxy`](https://crates.io/crates/rama-proxy): proxy types and utilities for rama
248//! - [`rama-socks5`](https://crates.io/crates/rama-socks5): SOCKS5 support for rama
249//! - [`rama-haproxy`](https://crates.io/crates/rama-haproxy): rama HAProxy support
250//! - [`rama-ua`](https://crates.io/crates/rama-ua): User-Agent (UA) support for `rama`
251//! - [`rama-http-types`](https://crates.io/crates/rama-http-types): http types and utilities
252//! - [`rama-http-headers`](https://crates.io/crates/rama-http-headers): typed http headers
253//! - [`rama-grpc`](https://crates.io/crates/rama-grpc): Grpc support for rama
254//! - [`rama-grpc-codegen`](https://crates.io/crates/rama-grpc-codegen): Grpc codegen support for rama
255//! - [`rama-http`](https://crates.io/crates/rama-http): rama http services, layers and utilities
256//! - [`rama-http-backend`](https://crates.io/crates/rama-http-backend): default http backend for `rama`
257//! - [`rama-http-core`](https://crates.io/crates/rama-http-core): http protocol implementation driving `rama-http-backend`
258//! - [`rama-tower`](https://crates.io/crates/rama-tower): provide [tower](https://github.com/tower-rs/tower) compatibility for `rama`
259//!
260//! `rama` crates that live in <https://github.com/plabayo/rama-boring> (forks of `cloudflare/boring`):
261//!
262//! - [`rama-boring`](https://crates.io/crates/rama-boring): BoringSSL bindings for rama
263//! - [`rama-boring-sys`](https://crates.io/crates/rama-boring-sys): FFI bindings to BoringSSL for rama
264//! - [`rama-boring-tokio`](https://crates.io/crates/rama-boring-tokio): an implementation of SSL streams for Tokio backed by BoringSSL in function of rama
265//!
266//! repositories in function of rama that aren't crates:
267//!
268//! - <https://github.com/plabayo/rama-boringssl>:
269//!   Fork of [mirror of BoringSSL](https://github.com/plabayo/rama-boringssl)
270//!   in function of [rama-boring](https://github.com/plabayo/rama-boring)
271//! - <https://github.com/plabayo/homebrew-rama>: Homebrew formula for the rama Cli tool
272//!
273//! Repositories that we maintain and are re exported by the root `rama` crate:
274//!
275//! - <https://github.com/plabayo/tokio-graceful>: Graceful shutdown util for Rust projects using the Tokio Async runtime.
276//!
277//! Community crates that extend the ecosystem are encouraged. If you publish a community
278//! crate, please prefix it with `rama-x` so it is easy to discover and clearly distinct
279//! from the official crates in this repository.
280//!
281//! ---
282//!
283//! ## Safety and compatibility
284//!
285//! - rama crates avoid `unsafe` Rust as much as possible and use it only where necessary.
286//! - Supply chain auditing is done with [`cargo vet`](https://github.com/mozilla/cargo-vet).
287//! - Tier 1 platforms include macOS, Linux and Windows on modern architectures.
288//! - The minimum supported Rust version (MSRV) is `1.93`.
289//!
290//! For details see the compatibility section in the README and the CI configuration in
291//! the repository.
292//!
293//! ---
294//!
295//! ## License
296//!
297//! rama is free and open source software, dual licensed under MIT and Apache 2.0.
298//!
299//! You can use rama for commercial and non commercial purposes. If rama becomes an
300//! important part of your stack, please consider supporting the project as a sponsor
301//! or partner.
302//!
303//! ---
304//!
305//! ## Community and links
306//!
307//! - Official website: <https://ramaproxy.org>
308//! - Rama Book index: <https://ramaproxy.org/book>
309//! - Rust docs: <https://docs.rs/rama> (latest release) and <https://ramaproxy.org/docs/rama> (edge)
310//! - Repository and issues: <https://github.com/plabayo/rama>
311//! - Discord: <https://discord.gg/29EetaSYCD>
312//! - FAQ: <https://ramaproxy.org/book/faq.html>
313//! - Netstack FM podcast: <https://netstack.fm> (podcast about networking and Rust)
314//!
315//! If you are not sure where to start, read "Why rama" in the book, run a proxy example and then
316//! iterate from there with the book and docs at hand.
317
318#![doc(
319    html_favicon_url = "https://raw.githubusercontent.com/plabayo/rama/main/docs/img/old_logo.png"
320)]
321#![doc(html_logo_url = "https://raw.githubusercontent.com/plabayo/rama/main/docs/img/old_logo.png")]
322#![cfg_attr(docsrs, feature(doc_cfg))]
323#![cfg_attr(test, allow(clippy::float_cmp))]
324#![cfg_attr(
325    not(test),
326    warn(clippy::print_stdout, clippy::dbg_macro),
327    deny(clippy::unwrap_used, clippy::expect_used)
328)]
329
330#[doc(inline)]
331pub use ::rama_core::{
332    Layer, Service, ServiceInput, bytes, combinators, conversion, error, extensions, futures,
333    graceful, io, layer, matcher, rt, service, stream, username,
334};
335
336#[cfg(feature = "crypto")]
337#[cfg_attr(docsrs, doc(cfg(feature = "crypto")))]
338#[doc(inline)]
339pub use ::rama_crypto as crypto;
340
341#[cfg(all(target_family = "unix", feature = "net"))]
342#[cfg_attr(docsrs, doc(cfg(all(target_family = "unix", feature = "net"))))]
343#[doc(inline)]
344pub use ::rama_unix as unix;
345
346#[cfg(feature = "tcp")]
347#[cfg_attr(docsrs, doc(cfg(feature = "tcp")))]
348#[doc(inline)]
349pub use ::rama_tcp as tcp;
350
351#[cfg(feature = "udp")]
352#[cfg_attr(docsrs, doc(cfg(feature = "udp")))]
353#[doc(inline)]
354pub use ::rama_udp as udp;
355
356pub mod telemetry;
357
358#[cfg(any(feature = "rustls", feature = "boring", feature = "acme"))]
359pub mod tls;
360
361#[cfg(feature = "dns")]
362#[cfg_attr(docsrs, doc(cfg(feature = "dns")))]
363#[doc(inline)]
364pub use ::rama_dns as dns;
365
366#[cfg(feature = "net")]
367pub mod net {
368    #[cfg_attr(docsrs, doc(cfg(feature = "net")))]
369    #[doc(inline)]
370    pub use ::rama_net::*;
371
372    #[cfg(any(
373        all(doc, docsrs),
374        all(
375            target_vendor = "apple",
376            any(feature = "net-apple-networkextension", feature = "net-apple-xpc")
377        )
378    ))]
379    #[cfg_attr(docsrs, doc(cfg(target_vendor = "apple")))]
380    pub mod apple {
381        //! Apple (vendor) specific network modules
382
383        #[cfg(feature = "net-apple-networkextension")]
384        #[cfg_attr(docsrs, doc(cfg(feature = "net-apple-networkextension")))]
385        #[doc(inline)]
386        pub use ::rama_net_apple_networkextension as networkextension;
387
388        #[cfg(feature = "net-apple-xpc")]
389        #[cfg_attr(docsrs, doc(cfg(feature = "net-apple-xpc")))]
390        #[doc(inline)]
391        pub use ::rama_net_apple_xpc as xpc;
392    }
393}
394
395#[cfg(feature = "http")]
396#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
397pub mod http;
398
399#[cfg(any(feature = "proxy", feature = "haproxy", feature = "socks5"))]
400pub mod proxy {
401    //! rama proxy support
402
403    #[cfg(feature = "proxy")]
404    #[cfg_attr(docsrs, doc(cfg(feature = "proxy")))]
405    #[doc(inline)]
406    pub use ::rama_proxy::*;
407
408    #[cfg(feature = "haproxy")]
409    #[cfg_attr(docsrs, doc(cfg(feature = "haproxy")))]
410    #[doc(inline)]
411    pub use ::rama_haproxy as haproxy;
412
413    #[cfg(feature = "socks5")]
414    #[cfg_attr(docsrs, doc(cfg(feature = "socks5")))]
415    #[doc(inline)]
416    pub use ::rama_socks5 as socks5;
417}
418
419#[cfg(feature = "ua")]
420#[cfg_attr(docsrs, doc(cfg(feature = "ua")))]
421#[doc(inline)]
422pub use ::rama_ua as ua;
423
424#[cfg(feature = "cli")]
425#[cfg_attr(docsrs, doc(cfg(feature = "cli")))]
426pub mod cli;
427
428pub mod utils {
429    //! utilities for rama
430
431    #[doc(inline)]
432    pub use ::rama_utils::*;
433
434    #[cfg(feature = "tower")]
435    #[cfg_attr(docsrs, doc(cfg(feature = "tower")))]
436    #[doc(inline)]
437    pub use ::rama_tower as tower;
438}