Struct Proxy

Source
pub struct Proxy { /* private fields */ }
Expand description

Proxy server settings

This struct represents a proxy server configuration that can be used to route HTTP/HTTPS requests through a proxy server. It supports various proxy protocols including HTTP CONNECT, HTTPS CONNECT, SOCKS4, SOCKS4A, and SOCKS5.

§Protocol Support

  • HTTP: HTTP CONNECT proxy
  • HTTPS: HTTPS CONNECT proxy (requires a TLS provider)
  • SOCKS4: SOCKS4 proxy (requires socks-proxy feature)
  • SOCKS4A: SOCKS4A proxy (requires socks-proxy feature)
  • SOCKS5: SOCKS5 proxy (requires socks-proxy feature)

§DNS Resolution

The resolve_target setting controls where DNS resolution happens:

  • When true: DNS resolution happens locally before connecting to the proxy. The resolved IP address is sent to the proxy.
  • When false: The hostname is sent to the proxy, which performs DNS resolution.

Default behavior:

  • For SOCKS4: true (local resolution required)
  • For all other protocols: false (proxy performs resolution)

§Examples

use ureq::{Proxy, ProxyProtocol};

// Create a proxy from a URI string
let proxy = Proxy::new("http://localhost:8080").unwrap();

// Create a proxy using the builder pattern
let proxy = Proxy::builder(ProxyProtocol::Socks5)
    .host("proxy.example.com")
    .port(1080)
    .username("user")
    .password("pass")
    .resolve_target(true)  // Force local DNS resolution
    .build()
    .unwrap();

// Read proxy settings from environment variables
if let Some(proxy) = Proxy::try_from_env() {
    // Use proxy from environment
}

Implementations§

Source§

impl Proxy

Source

pub fn new(proxy: &str) -> Result<Self, Error>

Create a proxy from a uri.

§Arguments:
  • proxy - a str of format <protocol>://<user>:<password>@<host>:port . All parts except host are optional.
§Protocols
  • http: HTTP CONNECT proxy
  • https: HTTPS CONNECT proxy (requires a TLS provider)
  • socks4: SOCKS4 (requires socks-proxy feature)
  • socks4a: SOCKS4A (requires socks-proxy feature)
  • socks5 and socks: SOCKS5 (requires socks-proxy feature)
§Examples proxy formats
  • http://127.0.0.1:8080
  • socks5://john:smith@socks.google.com
  • john:smith@socks.google.com:8000
  • localhost
Source

pub fn builder(p: ProxyProtocol) -> ProxyBuilder

Creates a proxy config using a builder.

Source

pub fn try_from_env() -> Option<Self>

Read proxy settings from environment variables.

The environment variable is expected to contain a proxy URI. The following environment variables are attempted:

  • ALL_PROXY
  • HTTPS_PROXY
  • HTTP_PROXY

Returns None if no environment variable is set or the URI is invalid.

Source

pub fn protocol(&self) -> ProxyProtocol

The configured protocol.

Source

pub fn uri(&self) -> &Uri

The proxy uri

Source

pub fn host(&self) -> &str

The host part of the proxy uri

Source

pub fn port(&self) -> u16

The port of the proxy uri

Source

pub fn username(&self) -> Option<&str>

The username of the proxy uri

Source

pub fn password(&self) -> Option<&str>

The password of the proxy uri

Source

pub fn is_from_env(&self) -> bool

Whether this proxy setting was created manually or from environment variables.

Source

pub fn resolve_target(&self) -> bool

Whether to resolve target locally before calling the proxy.

  • true - resolve the DNS before calling proxy.
  • false - send the target host to the proxy and let it resolve.

Defaults to false for all proxies protocols except SOCKS4. I.e. the normal case is to let the proxy resolve the target host.

Trait Implementations§

Source§

impl Clone for Proxy

Source§

fn clone(&self) -> Proxy

Returns a duplicate of the value. Read more
1.0.0 · 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 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: &Proxy) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Proxy

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 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,