pub struct Proxy { /* private fields */ }Expand description
Proxy configuration. Supports HTTP CONNECT proxies (Proxy::new_http)
and SOCKS5 proxies (Proxy::new_socks5).
SOCKS5 encodes targets per RFC 1928: IPv4 literals as ATYP 0x01, IPv6
literals as ATYP 0x04, and anything else as a domain name (ATYP 0x03)
with DNS resolution left to the proxy. The latter enables routing
through Tor, including .onion addresses.
For HTTP CONNECT proxies, when credentials are provided, the Basic authentication type is used for Proxy-Authorization.
Implementations§
Source§impl Proxy
impl Proxy
Sourcepub fn new_http<S: AsRef<str>>(proxy: S) -> Result<Self, Error>
pub fn new_http<S: AsRef<str>>(proxy: S) -> Result<Self, Error>
Creates a new Proxy configuration for an HTTP proxy supporting the CONNECT command.
Supported proxy format is:
[http://][user[:password]@]host[:port]The default port is 8080.
§Example
let proxy = bitreq::Proxy::new_http("user:password@localhost:1080").unwrap();
let request = bitreq::post("http://example.com").with_proxy(proxy);Sourcepub fn new_socks5<S: AsRef<str>>(proxy: S) -> Result<Self, Error>
pub fn new_socks5<S: AsRef<str>>(proxy: S) -> Result<Self, Error>
Creates a new Proxy configuration for a SOCKS5 proxy.
Supported proxy formats:
[socks5://]host[:port]
[socks5h://]host[:port]Both socks5:// and socks5h:// are accepted and behave identically.
IPv4 and IPv6 literal destinations are encoded with their proper ATYP
(0x01 / 0x04); anything else is sent as a domain name (ATYP 0x03),
leaving DNS resolution to the proxy. The latter is the socks5h
privacy expectation and is required for routing through Tor
(including .onion addresses).
The default port is 1080.
§Example
let proxy = bitreq::Proxy::new_socks5("127.0.0.1:9050").unwrap();
let request = bitreq::post("http://example.com").with_proxy(proxy);Sourcepub fn new_socks5_with_credentials<S: AsRef<str>>(
proxy: S,
user: String,
password: String,
) -> Result<Self, Error>
pub fn new_socks5_with_credentials<S: AsRef<str>>( proxy: S, user: String, password: String, ) -> Result<Self, Error>
Creates a new SOCKS5 proxy with username/password credentials.
Credentials trigger RFC 1929 username/password authentication during the SOCKS5 handshake. Tor uses credentials for circuit isolation: connections with different credentials are routed through separate circuits, preventing correlation.
§Example
let proxy = bitreq::Proxy::new_socks5_with_credentials(
"127.0.0.1:9050", "session-1".to_string(), "x".to_string()
).unwrap();Sourcepub fn set_credentials(
&mut self,
user: String,
password: String,
) -> Result<(), Error>
pub fn set_credentials( &mut self, user: String, password: String, ) -> Result<(), Error>
Sets username/password credentials on this proxy.
For SOCKS5, credentials trigger RFC 1929 username/password
authentication during the handshake; for Tor, distinct credentials
also trigger separate circuits, so rotating them on a long-lived
Proxy lets a caller cycle isolated circuits without rebuilding
from a URL. For HTTP CONNECT proxies, credentials are sent as
Basic auth in the Proxy-Authorization header (RFC 7617).
Length limits depend on the proxy kind:
- SOCKS5:
usermust be 1-255 bytes andpassword0-255 bytes (RFC 1929 wire-format limits). Emptyuseris rejected. - HTTP: no protocol-level length limit; practical limits come from server-side HTTP header size, which is server-specific.
Returns Error::BadProxy when these limits are violated.
§Example
let mut proxy = bitreq::Proxy::new_socks5("127.0.0.1:9050").unwrap();
proxy.set_credentials("session-1".to_string(), "x".to_string()).unwrap();
// ... later, for a fresh circuit:
proxy.set_credentials("session-2".to_string(), "x".to_string()).unwrap();