Skip to main content

proxyapi/
lib.rs

1//! `proxyapi` — core library for the Proxelar MITM proxy.
2//!
3//! Provides HTTP/HTTPS forward and reverse proxy functionality with
4//! request/response interception via the [`HttpHandler`] trait.
5
6#![forbid(unsafe_code)]
7
8pub mod body;
9pub mod ca;
10pub mod error;
11pub mod event;
12pub(crate) mod handler;
13pub mod proxy;
14mod rewind;
15#[cfg(feature = "scripting")]
16pub mod scripting;
17
18use body::ProxyBody;
19use hyper::{Request, Response};
20use std::net::SocketAddr;
21
22pub use error::Error;
23pub use event::ProxyEvent;
24pub use handler::CapturingHandler;
25pub use proxy::{Proxy, ProxyConfig, ProxyMode};
26
27/// Returned by [`HttpHandler::handle_request`] to either forward or short-circuit.
28pub enum RequestOrResponse {
29    Request(Request<ProxyBody>),
30    Response(Response<ProxyBody>),
31}
32
33/// Metadata about the incoming connection.
34#[derive(Clone, Debug, Eq, Hash, PartialEq)]
35pub struct HttpContext {
36    pub remote_addr: SocketAddr,
37}
38
39/// Trait for intercepting and modifying proxied HTTP traffic.
40///
41/// Implementations must be `Clone` because the proxy clones the handler
42/// for each connection/request pair.
43#[async_trait::async_trait]
44pub trait HttpHandler: Clone + Send + Sync + 'static {
45    async fn handle_request(
46        &mut self,
47        ctx: &HttpContext,
48        req: Request<hyper::body::Incoming>,
49    ) -> RequestOrResponse;
50
51    async fn handle_response(
52        &mut self,
53        ctx: &HttpContext,
54        res: Response<hyper::body::Incoming>,
55    ) -> Response<ProxyBody>;
56}