Skip to main content

http_request/request/proxy/
struct.rs

1use super::*;
2
3/// Proxy protocol family.
4///
5/// Distinguishes the wire format used to reach the proxy server:
6/// plain `HTTP CONNECT` for HTTP proxies, TLS-wrapped `HTTP CONNECT` for
7/// HTTPS proxies, and SOCKS5 handshake for SOCKS5 proxies.
8#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9pub enum ProxyType {
10    /// HTTP proxy (plain TCP, CONNECT method).
11    Http,
12    /// HTTPS proxy (TLS-wrapped CONNECT method).
13    Https,
14    /// SOCKS5 proxy.
15    Socks5,
16}
17
18/// Proxy configuration for an outgoing HTTP / WebSocket request.
19///
20/// `Proxy` is a value type with no `Arc<RwLock>` wrapping — exactly like
21/// `hyperlane-core`'s plain fields. Construct one with the convenience
22/// constructors below, then pass to `RequestBuilder::proxy(...)` or assign
23/// directly to `RequestConfig::proxy`.
24///
25/// # Examples
26///
27/// ```ignore
28/// use http_request::Proxy;
29///
30/// let p = Proxy::https("proxy.example.com", 7890);
31/// let auth = Proxy::socks5("127.0.0.1", 1080).auth("user", "pass");
32/// ```
33#[derive(Clone, Debug, Eq, PartialEq)]
34pub struct Proxy {
35    /// Proxy protocol family.
36    pub proxy_type: ProxyType,
37    /// Proxy server hostname or IP.
38    pub host: String,
39    /// Proxy server port.
40    pub port: u16,
41    /// Optional username for proxy auth.
42    pub username: Option<String>,
43    /// Optional password for proxy auth.
44    pub password: Option<String>,
45}
46
47impl Proxy {
48    /// Plain HTTP proxy.
49    pub fn http<H: AsRef<str>>(host: H, port: u16) -> Self {
50        Self::new(ProxyType::Http, host, port)
51    }
52
53    /// HTTPS proxy (TLS-wrapped).
54    pub fn https<H: AsRef<str>>(host: H, port: u16) -> Self {
55        Self::new(ProxyType::Https, host, port)
56    }
57
58    /// SOCKS5 proxy.
59    pub fn socks5<H: AsRef<str>>(host: H, port: u16) -> Self {
60        Self::new(ProxyType::Socks5, host, port)
61    }
62
63    /// Attach username / password to this proxy.
64    pub fn auth<U: AsRef<str>, P: AsRef<str>>(mut self, username: U, password: P) -> Self {
65        self.username = Some(username.as_ref().to_owned());
66        self.password = Some(password.as_ref().to_owned());
67        self
68    }
69
70    fn new<H: AsRef<str>>(proxy_type: ProxyType, host: H, port: u16) -> Self {
71        Self {
72            proxy_type,
73            host: host.as_ref().to_owned(),
74            port,
75            username: None,
76            password: None,
77        }
78    }
79}
80
81/// Async tunnel stream wrapping another async stream, with a buffer of
82/// pre-read bytes that are returned before delegating to the inner stream.
83pub struct ProxyTunnelStream {
84    pub(super) inner: BoxAsyncReadWrite,
85    pub(super) pre_read_data: Vec<u8>,
86}
87
88/// Sync tunnel stream wrapping another sync stream, with a buffer of
89/// pre-read bytes that are returned before delegating to the inner stream.
90pub struct SyncProxyTunnelStream {
91    pub(super) inner: BoxReadWrite,
92    pub(super) pre_read_data: Vec<u8>,
93}