Skip to main content

ras_browser/domain/
browser_profile.rs

1use std::collections::HashSet;
2use std::path::PathBuf;
3
4use ras_types::{ActionTimeout, DomainPattern};
5use serde::{Deserialize, Serialize};
6use url::Url;
7
8#[derive(Debug, Clone, Default, Serialize, Deserialize)]
9pub struct BrowserProfile {
10    pub user_data_dir: Option<PathBuf>,
11    pub headless: bool,
12    pub allowed_domains: AllowedDomains,
13    pub prohibited_domains: Vec<DomainPattern>,
14    pub block_ip_addresses: bool,
15    pub viewport_width: u32,
16    pub viewport_height: u32,
17    pub headers: Vec<(String, String)>,
18    pub proxy: Option<Url>,
19    pub action_timeout: ActionTimeout,
20    pub stealth: bool,
21}
22
23#[derive(Debug, Clone, Default, Serialize, Deserialize)]
24pub struct AllowedDomains {
25    patterns: Vec<DomainPattern>,
26    cache: HashSet<String>,
27}
28
29impl AllowedDomains {
30    #[must_use]
31    pub fn new(patterns: Vec<DomainPattern>) -> Self {
32        let cache = patterns.iter().map(|p| p.as_str().to_owned()).collect();
33        Self { patterns, cache }
34    }
35
36    #[must_use]
37    pub fn is_empty(&self) -> bool {
38        self.patterns.is_empty()
39    }
40
41    #[must_use]
42    pub fn allows(&self, url: &Url) -> bool {
43        if self.patterns.is_empty() {
44            return true;
45        }
46        if let Some(host) = url.host_str()
47            && self.cache.contains(host)
48        {
49            return true;
50        }
51        self.patterns.iter().any(|p| p.matches_url(url))
52    }
53}