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 proxyHTTPS
: 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
impl Proxy
Sourcepub fn new(proxy: &str) -> Result<Self, Error>
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 proxyhttps
: HTTPS CONNECT proxy (requires a TLS provider)socks4
: SOCKS4 (requires socks-proxy feature)socks4a
: SOCKS4A (requires socks-proxy feature)socks5
andsocks
: 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
Sourcepub fn builder(p: ProxyProtocol) -> ProxyBuilder
pub fn builder(p: ProxyProtocol) -> ProxyBuilder
Creates a proxy config using a builder.
Sourcepub fn try_from_env() -> Option<Self>
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.
Sourcepub fn protocol(&self) -> ProxyProtocol
pub fn protocol(&self) -> ProxyProtocol
The configured protocol.
Sourcepub fn is_from_env(&self) -> bool
pub fn is_from_env(&self) -> bool
Whether this proxy setting was created manually or from environment variables.
Sourcepub fn resolve_target(&self) -> bool
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§
impl Eq for Proxy
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.