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