Skip to main content

Proxy

Struct Proxy 

Source
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

Source

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);
Source

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);
Source

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();
Source

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: user must be 1-255 bytes and password 0-255 bytes (RFC 1929 wire-format limits). Empty user is 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();

Trait Implementations§

Source§

impl Clone for Proxy

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Proxy

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Proxy

Source§

impl Hash for Proxy

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Proxy

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Proxy

Auto Trait Implementations§

§

impl Freeze for Proxy

§

impl RefUnwindSafe for Proxy

§

impl Send for Proxy

§

impl Sync for Proxy

§

impl Unpin for Proxy

§

impl UnsafeUnpin for Proxy

§

impl UnwindSafe for Proxy

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.