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 intercept;
14pub mod proxy;
15mod rewind;
16#[cfg(feature = "scripting")]
17pub mod scripting;
18
19use body::ProxyBody;
20use hyper::{Request, Response};
21use std::net::SocketAddr;
22
23pub use error::Error;
24pub use event::ProxyEvent;
25pub use handler::CapturingHandler;
26pub use intercept::{InterceptConfig, InterceptDecision};
27pub use proxy::{Proxy, ProxyConfig, ProxyMode};
28
29/// Returned by [`HttpHandler::handle_request`] to either forward or short-circuit.
30pub enum RequestOrResponse {
31    Request(Request<ProxyBody>),
32    Response(Response<ProxyBody>),
33}
34
35/// Metadata about the incoming connection.
36#[derive(Clone, Debug, Eq, Hash, PartialEq)]
37pub struct HttpContext {
38    pub remote_addr: SocketAddr,
39}
40
41/// Trait for intercepting and modifying proxied HTTP traffic.
42///
43/// Implementations must be `Clone` because the proxy clones the handler
44/// for each connection/request pair.
45#[async_trait::async_trait]
46pub trait HttpHandler: Clone + Send + Sync + 'static {
47    async fn handle_request(
48        &mut self,
49        ctx: &HttpContext,
50        req: Request<hyper::body::Incoming>,
51    ) -> RequestOrResponse;
52
53    async fn handle_response(
54        &mut self,
55        ctx: &HttpContext,
56        res: Response<hyper::body::Incoming>,
57    ) -> Response<ProxyBody>;
58}