Skip to main content

playwright_rs/protocol/
proxy.rs

1//! Network proxy settings
2//!
3//! This module defines the [`ProxySettings`] struct which encapsulates
4//! the configuration for proxies used for network requests.
5//!
6//! Proxy settings can be applied at both the browser launch level
7//! ([`LaunchOptions`](crate::api::LaunchOptions)) and the browser context level
8//! ([`BrowserContextOptions`](crate::protocol::BrowserContextOptions)).
9//!
10//! See: <https://playwright.dev/docs/api/class-browser#browser-new-context>
11
12use serde::{Deserialize, Serialize};
13
14/// Network proxy settings for browser contexts and browser launches.
15///
16/// HTTP and SOCKS proxies are supported. Example proxy URLs:
17/// - `http://myproxy.com:3128`
18/// - `socks5://myproxy.com:3128`
19///
20/// # Example
21///
22/// ```no_run
23/// use playwright_rs::protocol::ProxySettings;
24///
25/// let proxy = ProxySettings::new("http://proxy.example.com:8080")
26///     .bypass(".example.com, chromium.org")
27///     .username("user")
28///     .password("secret");
29/// ```
30///
31/// See: <https://playwright.dev/docs/api/class-browser#browser-new-context>
32#[derive(Debug, Clone, Serialize, Deserialize)]
33#[serde(rename_all = "camelCase")]
34#[non_exhaustive]
35pub struct ProxySettings {
36    /// Proxy server URL (e.g., "http://proxy:8080" or "socks5://proxy:1080")
37    pub server: String,
38
39    /// Comma-separated domains to bypass proxy (e.g., ".example.com, chromium.org")
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub bypass: Option<String>,
42
43    /// Proxy username for HTTP proxy authentication
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub username: Option<String>,
46
47    /// Proxy password for HTTP proxy authentication
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub password: Option<String>,
50}
51
52impl ProxySettings {
53    /// Proxy all traffic through the given server (e.g. "http://host:3128").
54    pub fn new(server: impl Into<String>) -> Self {
55        Self {
56            server: server.into(),
57            bypass: None,
58            username: None,
59            password: None,
60        }
61    }
62    /// Comma-separated domains to bypass the proxy for.
63    pub fn bypass(mut self, bypass: impl Into<String>) -> Self {
64        self.bypass = Some(bypass.into());
65        self
66    }
67    /// Proxy auth username.
68    pub fn username(mut self, username: impl Into<String>) -> Self {
69        self.username = Some(username.into());
70        self
71    }
72    /// Proxy auth password.
73    pub fn password(mut self, password: impl Into<String>) -> Self {
74        self.password = Some(password.into());
75        self
76    }
77}