Skip to main content

brainwires_proxy/transport/
mod.rs

1//! Transport abstractions — listeners accept connections, connectors forward to upstream.
2
3#[cfg(feature = "http")]
4pub mod http;
5#[cfg(feature = "http")]
6pub mod sse;
7
8pub mod tcp;
9pub mod unix;
10
11#[cfg(feature = "websocket")]
12pub mod websocket;
13
14use crate::error::ProxyResult;
15use crate::types::{ProxyRequest, ProxyResponse};
16use tokio::sync::{mpsc, oneshot};
17
18/// A connection received by a listener: the request plus a channel to send the response back.
19pub type InboundConnection = (ProxyRequest, oneshot::Sender<ProxyResponse>);
20
21/// Where a transport listener binds.
22#[derive(Debug, Clone)]
23pub enum ListenAddr {
24    Tcp(std::net::SocketAddr),
25    Unix(std::path::PathBuf),
26}
27
28/// Where a transport connector sends traffic.
29#[derive(Debug, Clone)]
30pub enum UpstreamTarget {
31    Url(url::Url),
32    Tcp { host: String, port: u16 },
33    Unix(std::path::PathBuf),
34}
35
36/// Accepts inbound connections and sends `(request, response_channel)` pairs
37/// through an mpsc channel for the proxy core to process.
38#[async_trait::async_trait]
39pub trait TransportListener: Send + Sync {
40    /// Start accepting connections. Sends each connection through `tx`.
41    /// Returns when the listener is shut down.
42    async fn listen(
43        &self,
44        tx: mpsc::Sender<InboundConnection>,
45        shutdown: tokio::sync::watch::Receiver<bool>,
46    ) -> ProxyResult<()>;
47
48    /// Human-readable transport name.
49    fn transport_name(&self) -> &str;
50}
51
52/// Forwards a `ProxyRequest` to an upstream target and returns the `ProxyResponse`.
53#[async_trait::async_trait]
54pub trait TransportConnector: Send + Sync {
55    /// Forward a request to the upstream and return the response.
56    async fn forward(&self, request: ProxyRequest) -> ProxyResult<ProxyResponse>;
57
58    /// Human-readable connector name.
59    fn connector_name(&self) -> &str;
60}