~nickbp/originz

ref: d94f181c5fbfdf0f62b40ea46d3f301f4ec681e5 originz/src/filter/filter.rs -rw-r--r-- 11.4 KiB
d94f181cNick Parker Backport current benchmark to older code 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#![deny(warnings)]

use std::convert::TryFrom;
use std::path::{Path, PathBuf};
use std::time::Duration;
use std::vec::Vec;

use anyhow::{Context, Result};
use hyper::client::HttpConnector;
use hyper::{Body, Client, Uri};
use hyper_rustls::HttpsConnector;
use rustls::ClientConfig;
use sha2::{Digest, Sha256};
use tracing::{debug, warn};
use tracing_attributes::instrument;

use crate::client::hyper::Resolver;
use crate::filter::{downloader, path, reader};
use crate::http::Fetcher;

/// An iterator that goes over the parent domains of a provided child domain.
/// For example, www.domain.com => [www.domain.com, domain.com, com]
struct DomainParentIter<'a> {
    domain: &'a String,
    start_idx: usize,
}

impl<'a> DomainParentIter<'a> {
    fn new(domain: &'a String) -> DomainParentIter<'a> {
        DomainParentIter {
            domain,
            start_idx: 0,
        }
    }
}

impl<'a> Iterator for DomainParentIter<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<&'a str> {
        if self.start_idx >= self.domain.len() {
            // Seeked past end of domain string, nothing left
            None
        } else {
            // Collect this result: everything from start_idx
            let remainder = &self.domain[self.start_idx..];
            // Update start for next result
            match remainder.find('.') {
                Some(idx) => {
                    // idx is within remainder's address space, which starts at start_idx
                    // add 1 to seek past the '.' itself
                    self.start_idx += idx + 1;
                }
                None => {
                    self.start_idx = self.domain.len();
                }
            }
            Some(remainder)
        }
    }
}

pub struct Filter {
    overrides: Vec<reader::FileEntries>,
    blocks: Vec<reader::FileEntries>,
    download_dir: PathBuf,
    fetch_client: Client<HttpsConnector<HttpConnector<Resolver>>, Body>,
}

impl Filter {
    pub fn new(download_dir: PathBuf, resolver: Resolver) -> Result<Filter> {
        let fetch_client = build_fetch_client(resolver, 10000)?;
        Ok(Filter {
            overrides: vec![],
            blocks: vec![],
            download_dir,
            fetch_client,
        })
    }

    pub fn update_override(self: &mut Filter, override_path: &String) -> Result<()> {
        let file_entries = reader::read(reader::FileInfo {
            source_path: override_path.clone(),
            local_path: override_path.clone(),
            filter_type: reader::FilterType::OVERRIDE,
        })?;

        // Before adding new entry, check for existing entry to be replaced/updated.
        upsert_entries(&mut self.overrides, file_entries);
        Ok(())
    }

    #[instrument(skip(self))] // skip non-Debug stuff
    pub async fn update_block(self: &mut Filter, hosts_entry: &String) -> Result<()> {
        let download_path_str =
            update_url(&self.fetch_client, &self.download_dir, &hosts_entry).await?;
        let info = reader::FileInfo {
            source_path: hosts_entry.clone(),
            local_path: download_path_str,
            filter_type: reader::FilterType::BLOCK,
        };
        let file_entries = reader::read(info)?;
        if !file_entries.is_empty() {
            // Note: In theory we could dedupe entries across different blocks to save memory.
            // However this causes problems if we want to granularly update individual files.
            // For example if file A had a hostname that we omit from file B, and then file A is updated
            // to no longer mention that hostname, we'd want to reintroduce it into file B.

            // So for now the marginal gain likely isn't worth the complexity, but in the future we could
            // rebuild + dedupe a single monolithic tree each time ANY file is updated.
            // BUT this makes it harder to source filter decisions since everything will be merged.
            upsert_entries(&mut self.blocks, file_entries);
        }
        Ok(())
    }

    pub fn check(self: &Filter, host: &String) -> Option<(&reader::FileInfo, &reader::FileEntry)> {
        // Go over domains in ancestor order, checking all blocks for each ancestor.
        // For example check all files for 'www.example.com', then each again for 'example.com'.
        // This allows file B with 'www.example.com' to take precedence over file A with 'example.com'

        // Meanwhile if two files mention the exact same domain then the first file in the list wins.
        // So if file A says "127.0.0.1" and file B says "172.16.0.1" then "127.0.0.1" wins.

        for domain_str in DomainParentIter::new(&host) {
            let domain = domain_str.to_string();
            for override_entry in &self.overrides {
                match override_entry.get(&domain) {
                    // Found in an override file: Tell upstream to let it through or use provided override value
                    Some(entry) => return Some((override_entry.info(), entry)),
                    None => {}
                }
            }
            for block in &self.blocks {
                match block.get(&domain) {
                    // Found in block: Tell upstream to block it or use filter-provided override
                    Some(entry) => return Some((block.info(), entry)),
                    None => {}
                }
            }
        }

        return None;
    }
}

/// Build HTTP connector which queries our configured source DNS server.
/// In particular, AVOID querying the system DNS, which may just loop back to us.
fn build_fetch_client(
    resolver: Resolver,
    timeout_ms: u64,
) -> Result<Client<HttpsConnector<HttpConnector<Resolver>>, Body>> {
    let mut http_connector = HttpConnector::<_>::new_with_resolver(resolver);
    http_connector.set_connect_timeout(Some(Duration::from_millis(timeout_ms)));
    http_connector.set_happy_eyeballs_timeout(Some(Duration::from_millis(timeout_ms)));
    http_connector.set_keepalive(Some(Duration::from_secs(90)));
    // Required or else we get errors when trying to pass through https urls, see also HttpsConnector::new_():
    http_connector.enforce_http(false);

    // Build HTTPS connector that wraps HTTP connector. Allows HTTPS but doesn't require it.
    let mut https_config = ClientConfig::new();
    https_config.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
    match rustls_native_certs::load_native_certs() {
        Ok(certs) => {
            https_config.root_store = certs;
        }
        Err((Some(certs), e)) => {
            warn!(
                "Some TLS certificates failed to load, trying to continue without them: {:?}",
                e
            );
            https_config.root_store = certs;
        }
        Err((None, e)) => {
            return Err(e).with_context(|| "Failed to load native TLS cert store");
        }
    }
    https_config.ct_logs = Some(&ct_logs::LOGS);

    Ok(Client::builder()
        .build::<HttpsConnector<_>, Body>(HttpsConnector::from((http_connector, https_config))))
}

async fn update_url(
    fetch_client: &Client<HttpsConnector<HttpConnector<Resolver>>, Body>,
    download_dir: &PathBuf,
    hosts_path: &String,
) -> Result<String> {
    match Uri::try_from(hosts_path) {
        // Parsed as a URL, try to download
        Ok(host_uri) => {
            if host_uri.scheme() == None {
                // Filesystem paths can get parsed as URLs with no scheme
                return Ok(hosts_path.to_string());
            }
            let fetcher = Fetcher::new(10 * 1024 * 1024, None);
            // We download files to the exact SHA of the URL string we were provided.
            // This is an easy way to avoid filename collisions in URLs: example1.com/hosts vs example2.com/hosts
            // If the user changes the URL string then that changes the SHA, perfect for "cache invalidation" purposes.
            let hosts_path_sha = Sha256::digest(hosts_path.as_bytes());
            let download_path = Path::new(download_dir).join(format!(
                "{:x}.sha256.{}",
                hosts_path_sha,
                path::ZSTD_EXTENSION
            ));
            downloader::update_file(
                fetch_client,
                &fetcher,
                &hosts_path.to_string(),
                download_path.as_path(),
            )
            .await?;
            Ok(download_path
                .to_str()
                .with_context(|| format!("busted download path: {:?}", download_path))?
                .to_string())
        }
        // Couldn't parse as URL, assume it's a local file
        Err(_) => {
            debug!("file: {}", hosts_path);
            Ok(hosts_path.to_string())
        }
    }
}

fn upsert_entries(entries: &mut Vec<reader::FileEntries>, new_entry: reader::FileEntries) {
    // Before adding new entry, check for existing entry to be replaced/updated.
    for i in 0..entries.len() {
        let entry = entries.get(i).expect("incoherent vector size");
        if entry.info.local_path == new_entry.info.local_path {
            // Delete or replace existing version
            if new_entry.is_empty() {
                entries.remove(i);
            } else {
                entries.insert(i, new_entry);
            }
            return;
        }
    }
    // Add new entry
    if !new_entry.is_empty() {
        entries.push(new_entry);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn iter_empty() {
        let domain = "".to_string();
        let mut iter = DomainParentIter::new(&domain);
        assert_eq!(None, iter.next());
    }

    #[test]
    fn iter_com() {
        let domain = "com".to_string();
        let mut iter = DomainParentIter::new(&domain);
        assert_eq!(Some("com"), iter.next());
        assert_eq!(None, iter.next());
    }

    #[test]
    fn iter_domaincom() {
        let domain = "domain.com".to_string();
        let mut iter = DomainParentIter::new(&domain);
        assert_eq!(Some("domain.com"), iter.next());
        assert_eq!(Some("com"), iter.next());
        assert_eq!(None, iter.next());
    }

    #[test]
    fn iter_wwwdomaincom() {
        let domain = "www.domain.com".to_string();
        let mut iter = DomainParentIter::new(&domain);
        assert_eq!(Some("www.domain.com"), iter.next());
        assert_eq!(Some("domain.com"), iter.next());
        assert_eq!(Some("com"), iter.next());
        assert_eq!(None, iter.next());
    }

    #[test]
    fn iter_wwwngeeknz() {
        let domain = "www.n.geek.nz".to_string();
        let mut iter = DomainParentIter::new(&domain);
        assert_eq!(Some("www.n.geek.nz"), iter.next());
        assert_eq!(Some("n.geek.nz"), iter.next());
        assert_eq!(Some("geek.nz"), iter.next());
        assert_eq!(Some("nz"), iter.next());
        assert_eq!(None, iter.next());
    }

    #[test]
    fn iter_averylongteststringwithmanysegments() {
        let domain = "a.very-long.test.string.with-many.segments".to_string();
        let mut iter = DomainParentIter::new(&domain);
        assert_eq!(
            Some("a.very-long.test.string.with-many.segments"),
            iter.next()
        );
        assert_eq!(
            Some("very-long.test.string.with-many.segments"),
            iter.next()
        );
        assert_eq!(Some("test.string.with-many.segments"), iter.next());
        assert_eq!(Some("string.with-many.segments"), iter.next());
        assert_eq!(Some("with-many.segments"), iter.next());
        assert_eq!(Some("segments"), iter.next());
        assert_eq!(None, iter.next());
    }
}