bluebox 0.1.4

A fast DNS interceptor and cache for local networks
Documentation
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//! Remote URL blocklist loader.
//!
//! This module provides functionality to load blocklists from remote URLs
//! with caching support for offline fallback.

use std::io::BufReader;
use std::path::{Path, PathBuf};
use std::time::Duration;

use reqwest::Client;
use tokio::fs::{self, File};
use tokio::io::{AsyncReadExt, AsyncWriteExt};

use super::{ParseError, parser_for_format};
use crate::config::BlocklistFormat;

/// Default timeout for HTTP requests in seconds.
const DEFAULT_TIMEOUT_SECS: u64 = 30;

/// User-Agent header value for HTTP requests.
const USER_AGENT: &str = concat!("bluebox/", env!("CARGO_PKG_VERSION"));

/// Error type for remote blocklist loading operations.
#[derive(Debug, thiserror::Error)]
pub enum RemoteLoadError {
    /// HTTP request failed with a non-success status code.
    #[error("HTTP request failed for {url}: status {status}")]
    HttpStatus {
        /// URL that was requested.
        url: String,
        /// HTTP status code returned.
        status: u16,
    },

    /// Network error during HTTP request.
    #[error("network error fetching {url}")]
    Network {
        /// URL that was requested.
        url: String,
        /// Underlying reqwest error.
        #[source]
        source: reqwest::Error,
    },

    /// Timeout fetching the remote URL.
    #[error("timeout fetching {url}")]
    Timeout {
        /// URL that timed out.
        url: String,
    },

    /// Error parsing the blocklist content.
    #[error("parse error")]
    Parse(#[from] ParseError),

    /// Task join error from spawning a blocking task.
    #[error("task join error")]
    Join(#[from] tokio::task::JoinError),

    /// Cache not available for fallback.
    #[error("cache not available: {0:?}")]
    CacheUnavailable(PathBuf),

    /// I/O error during cache operations.
    #[error("cache I/O error for {path:?}")]
    CacheIo {
        /// Path to the cache file.
        path: PathBuf,
        /// Underlying I/O error.
        #[source]
        source: std::io::Error,
    },

    /// Failed to create HTTP client.
    #[error("failed to create HTTP client")]
    ClientBuild(#[source] reqwest::Error),
}

/// Loads blocklists from remote URLs.
pub struct RemoteLoader {
    client: Client,
    cache_dir: PathBuf,
}

impl RemoteLoader {
    /// Create a new remote loader with the specified cache directory.
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP client cannot be created.
    pub fn new(cache_dir: PathBuf) -> Result<Self, RemoteLoadError> {
        let client = Client::builder()
            .timeout(Duration::from_secs(DEFAULT_TIMEOUT_SECS))
            .user_agent(USER_AGENT)
            .gzip(true)
            .build()
            .map_err(RemoteLoadError::ClientBuild)?;

        Ok(Self { client, cache_dir })
    }

    /// Load a blocklist from a remote URL.
    ///
    /// This function fetches the content from the URL and parses it using the
    /// appropriate parser for the given format. Large responses are parsed in
    /// a blocking task to avoid blocking the async runtime.
    ///
    /// # Arguments
    ///
    /// * `url` - URL to fetch the blocklist from
    /// * `format` - Format of the blocklist content
    ///
    /// # Returns
    ///
    /// A vector of domain patterns extracted from the response.
    ///
    /// # Errors
    ///
    /// Returns a [`RemoteLoadError`] if:
    /// - The HTTP request fails ([`RemoteLoadError::Network`])
    /// - The server returns a non-success status ([`RemoteLoadError::HttpStatus`])
    /// - The request times out ([`RemoteLoadError::Timeout`])
    /// - The content cannot be parsed ([`RemoteLoadError::Parse`])
    pub async fn load(
        &self,
        url: &str,
        format: BlocklistFormat,
    ) -> Result<Vec<String>, RemoteLoadError> {
        let response = self.client.get(url).send().await.map_err(|err| {
            if err.is_timeout() {
                RemoteLoadError::Timeout {
                    url: url.to_string(),
                }
            } else {
                RemoteLoadError::Network {
                    url: url.to_string(),
                    source: err,
                }
            }
        })?;

        if !response.status().is_success() {
            return Err(RemoteLoadError::HttpStatus {
                url: url.to_string(),
                status: response.status().as_u16(),
            });
        }

        let content = response
            .text()
            .await
            .map_err(|err| RemoteLoadError::Network {
                url: url.to_string(),
                source: err,
            })?;

        // Parse in a blocking task to avoid blocking the async runtime
        let domains = tokio::task::spawn_blocking(move || {
            let parser = parser_for_format(format);
            let mut reader = BufReader::new(content.as_bytes());
            parser.parse(&mut reader)
        })
        .await??;

        Ok(domains)
    }

    /// Load a blocklist from a remote URL with caching support.
    ///
    /// This function attempts to fetch from the remote URL first. If successful,
    /// it caches the content for future offline use. If the fetch fails, it
    /// attempts to load from the cache as a fallback.
    ///
    /// # Arguments
    ///
    /// * `name` - Unique name for this blocklist (used for cache filename)
    /// * `url` - URL to fetch the blocklist from
    /// * `format` - Format of the blocklist content
    ///
    /// # Returns
    ///
    /// A vector of domain patterns extracted from the response or cache.
    ///
    /// # Errors
    ///
    /// Returns a [`RemoteLoadError`] if both the remote fetch and cache
    /// fallback fail.
    pub async fn load_cached(
        &self,
        name: &str,
        url: &str,
        format: BlocklistFormat,
    ) -> Result<Vec<String>, RemoteLoadError> {
        let cache_path = self.cache_path(name);

        // Try to load from remote
        match self.load_and_cache(url, format, &cache_path).await {
            Ok(patterns) => Ok(patterns),
            Err(err) => {
                tracing::warn!(
                    url = %url,
                    error = ?err,
                    "failed to fetch remote blocklist, trying cache"
                );
                self.load_from_cache(&cache_path, format).await
            }
        }
    }

    /// Load from remote and save to cache.
    async fn load_and_cache(
        &self,
        url: &str,
        format: BlocklistFormat,
        cache_path: &Path,
    ) -> Result<Vec<String>, RemoteLoadError> {
        let response = self.client.get(url).send().await.map_err(|err| {
            if err.is_timeout() {
                RemoteLoadError::Timeout {
                    url: url.to_string(),
                }
            } else {
                RemoteLoadError::Network {
                    url: url.to_string(),
                    source: err,
                }
            }
        })?;

        if !response.status().is_success() {
            return Err(RemoteLoadError::HttpStatus {
                url: url.to_string(),
                status: response.status().as_u16(),
            });
        }

        let content = response
            .text()
            .await
            .map_err(|err| RemoteLoadError::Network {
                url: url.to_string(),
                source: err,
            })?;

        // Save to cache (best effort, don't fail if cache write fails)
        if let Err(err) = self.save_cache(cache_path, &content).await {
            tracing::warn!(
                path = ?cache_path,
                error = ?err,
                "failed to save blocklist to cache"
            );
        }

        // Parse in a blocking task
        let domains = tokio::task::spawn_blocking(move || {
            let parser = parser_for_format(format);
            let mut reader = BufReader::new(content.as_bytes());
            parser.parse(&mut reader)
        })
        .await??;

        Ok(domains)
    }

    /// Save content to the cache file.
    async fn save_cache(&self, cache_path: &Path, content: &str) -> Result<(), RemoteLoadError> {
        // Ensure cache directory exists
        if let Some(parent) = cache_path.parent() {
            fs::create_dir_all(parent)
                .await
                .map_err(|err| RemoteLoadError::CacheIo {
                    path: parent.to_path_buf(),
                    source: err,
                })?;
        }

        let mut file = File::create(cache_path)
            .await
            .map_err(|err| RemoteLoadError::CacheIo {
                path: cache_path.to_path_buf(),
                source: err,
            })?;

        file.write_all(content.as_bytes())
            .await
            .map_err(|err| RemoteLoadError::CacheIo {
                path: cache_path.to_path_buf(),
                source: err,
            })?;

        file.flush().await.map_err(|err| RemoteLoadError::CacheIo {
            path: cache_path.to_path_buf(),
            source: err,
        })?;

        tracing::debug!(path = ?cache_path, "saved blocklist to cache");
        Ok(())
    }

    /// Load content from the cache file.
    async fn load_from_cache(
        &self,
        cache_path: &Path,
        format: BlocklistFormat,
    ) -> Result<Vec<String>, RemoteLoadError> {
        let mut file = File::open(cache_path).await.map_err(|err| {
            if err.kind() == std::io::ErrorKind::NotFound {
                RemoteLoadError::CacheUnavailable(cache_path.to_path_buf())
            } else {
                RemoteLoadError::CacheIo {
                    path: cache_path.to_path_buf(),
                    source: err,
                }
            }
        })?;

        let mut content = String::new();
        file.read_to_string(&mut content)
            .await
            .map_err(|err| RemoteLoadError::CacheIo {
                path: cache_path.to_path_buf(),
                source: err,
            })?;

        tracing::info!(path = ?cache_path, "loaded blocklist from cache");

        // Parse in a blocking task
        let domains = tokio::task::spawn_blocking(move || {
            let parser = parser_for_format(format);
            let mut reader = BufReader::new(content.as_bytes());
            parser.parse(&mut reader)
        })
        .await??;

        Ok(domains)
    }

    /// Get the cache file path for a blocklist name.
    fn cache_path(&self, name: &str) -> PathBuf {
        self.cache_dir.join(format!("{name}.cache"))
    }
}

/// Returns the default cache directory for blocklists.
///
/// - Linux: `~/.cache/bluebox/blocklists/`
/// - macOS: `~/Library/Caches/bluebox/blocklists/`
/// - Windows: `{FOLDERID_LocalAppData}\bluebox\blocklists\`
///
/// Falls back to `./cache/blocklists` if the cache directory cannot be determined.
#[must_use]
pub fn default_cache_dir() -> PathBuf {
    dirs::cache_dir().map_or_else(
        || PathBuf::from("./cache/blocklists"),
        |p| p.join("bluebox").join("blocklists"),
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    fn create_loader() -> (RemoteLoader, TempDir) {
        let temp_dir = TempDir::new().unwrap();
        let loader = RemoteLoader::new(temp_dir.path().to_path_buf()).unwrap();
        (loader, temp_dir)
    }

    #[tokio::test]
    async fn should_load_domains_format_from_url() {
        let mock_server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/blocklist.txt"))
            .respond_with(
                ResponseTemplate::new(200).set_body_string("# Comment\nexample.com\n*.ads.com"),
            )
            .mount(&mock_server)
            .await;

        let (loader, _temp) = create_loader();
        let url = format!("{}/blocklist.txt", mock_server.uri());

        let domains = loader.load(&url, BlocklistFormat::Domains).await.unwrap();

        assert_eq!(domains, vec!["example.com", "*.ads.com"]);
    }

    #[tokio::test]
    async fn should_load_hosts_format_from_url() {
        let mock_server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/hosts"))
            .respond_with(ResponseTemplate::new(200).set_body_string(
                "# Hosts file\n0.0.0.0 ads.example.com\n127.0.0.1 tracking.example.com",
            ))
            .mount(&mock_server)
            .await;

        let (loader, _temp) = create_loader();
        let url = format!("{}/hosts", mock_server.uri());

        let domains = loader.load(&url, BlocklistFormat::Hosts).await.unwrap();

        assert_eq!(domains, vec!["ads.example.com", "tracking.example.com"]);
    }

    #[tokio::test]
    async fn should_load_adblock_format_from_url() {
        let mock_server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/filter.txt"))
            .respond_with(ResponseTemplate::new(200).set_body_string(
                "! AdBlock comment\n||ads.example.com^\n||tracking.example.com^$third-party",
            ))
            .mount(&mock_server)
            .await;

        let (loader, _temp) = create_loader();
        let url = format!("{}/filter.txt", mock_server.uri());

        let domains = loader.load(&url, BlocklistFormat::Adblock).await.unwrap();

        assert_eq!(domains, vec!["ads.example.com", "tracking.example.com"]);
    }

    #[tokio::test]
    async fn should_return_http_status_error_when_not_found() {
        let mock_server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/notfound.txt"))
            .respond_with(ResponseTemplate::new(404))
            .mount(&mock_server)
            .await;

        let (loader, _temp) = create_loader();
        let url = format!("{}/notfound.txt", mock_server.uri());

        let result = loader.load(&url, BlocklistFormat::Domains).await;

        assert!(matches!(
            result,
            Err(RemoteLoadError::HttpStatus { status: 404, .. })
        ));
    }

    #[tokio::test]
    async fn should_return_http_status_error_when_server_error() {
        let mock_server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/error.txt"))
            .respond_with(ResponseTemplate::new(500))
            .mount(&mock_server)
            .await;

        let (loader, _temp) = create_loader();
        let url = format!("{}/error.txt", mock_server.uri());

        let result = loader.load(&url, BlocklistFormat::Domains).await;

        assert!(matches!(
            result,
            Err(RemoteLoadError::HttpStatus { status: 500, .. })
        ));
    }

    #[tokio::test]
    async fn should_return_network_error_when_connection_refused() {
        let (loader, _temp) = create_loader();
        // Use a port that's unlikely to be in use
        let url = "http://127.0.0.1:1/blocklist.txt";

        let result = loader.load(url, BlocklistFormat::Domains).await;

        assert!(matches!(result, Err(RemoteLoadError::Network { .. })));
    }

    #[tokio::test]
    async fn should_cache_response_on_successful_load() {
        let mock_server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/blocklist.txt"))
            .respond_with(ResponseTemplate::new(200).set_body_string("example.com"))
            .expect(1)
            .mount(&mock_server)
            .await;

        let (loader, temp_dir) = create_loader();
        let url = format!("{}/blocklist.txt", mock_server.uri());

        let domains = loader
            .load_cached("test", &url, BlocklistFormat::Domains)
            .await
            .unwrap();

        assert_eq!(domains, vec!["example.com"]);

        // Verify cache file was created
        let cache_path = temp_dir.path().join("test.cache");
        assert!(cache_path.exists());

        let cached_content = std::fs::read_to_string(&cache_path).unwrap();
        assert_eq!(cached_content, "example.com");
    }

    #[tokio::test]
    async fn should_fallback_to_cache_when_remote_fails() {
        let mock_server = MockServer::start().await;

        // First request succeeds
        Mock::given(method("GET"))
            .and(path("/blocklist.txt"))
            .respond_with(ResponseTemplate::new(200).set_body_string("example.com"))
            .expect(1)
            .mount(&mock_server)
            .await;

        let (loader, _temp) = create_loader();
        let url = format!("{}/blocklist.txt", mock_server.uri());

        // First load populates cache
        let domains = loader
            .load_cached("test", &url, BlocklistFormat::Domains)
            .await
            .unwrap();
        assert_eq!(domains, vec!["example.com"]);

        // Clear mocks and set up failure
        mock_server.reset().await;

        Mock::given(method("GET"))
            .and(path("/blocklist.txt"))
            .respond_with(ResponseTemplate::new(503))
            .expect(1)
            .mount(&mock_server)
            .await;

        // Second load should fall back to cache
        let domains = loader
            .load_cached("test", &url, BlocklistFormat::Domains)
            .await
            .unwrap();
        assert_eq!(domains, vec!["example.com"]);
    }

    #[tokio::test]
    async fn should_return_cache_unavailable_when_no_cache_exists() {
        let mock_server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/blocklist.txt"))
            .respond_with(ResponseTemplate::new(503))
            .mount(&mock_server)
            .await;

        let (loader, _temp) = create_loader();
        let url = format!("{}/blocklist.txt", mock_server.uri());

        let result = loader
            .load_cached("nonexistent", &url, BlocklistFormat::Domains)
            .await;

        assert!(matches!(result, Err(RemoteLoadError::CacheUnavailable(_))));
    }

    #[tokio::test]
    async fn should_handle_empty_response() {
        let mock_server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/empty.txt"))
            .respond_with(ResponseTemplate::new(200).set_body_string(""))
            .mount(&mock_server)
            .await;

        let (loader, _temp) = create_loader();
        let url = format!("{}/empty.txt", mock_server.uri());

        let domains = loader.load(&url, BlocklistFormat::Domains).await.unwrap();

        assert!(domains.is_empty());
    }

    #[tokio::test]
    async fn should_handle_comments_only_response() {
        let mock_server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/comments.txt"))
            .respond_with(ResponseTemplate::new(200).set_body_string("# Comment 1\n# Comment 2\n"))
            .mount(&mock_server)
            .await;

        let (loader, _temp) = create_loader();
        let url = format!("{}/comments.txt", mock_server.uri());

        let domains = loader.load(&url, BlocklistFormat::Domains).await.unwrap();

        assert!(domains.is_empty());
    }

    #[tokio::test]
    async fn should_include_user_agent_header() {
        let mock_server = MockServer::start().await;

        Mock::given(method("GET"))
            .and(path("/blocklist.txt"))
            .and(wiremock::matchers::header("User-Agent", USER_AGENT))
            .respond_with(ResponseTemplate::new(200).set_body_string("example.com"))
            .expect(1)
            .mount(&mock_server)
            .await;

        let (loader, _temp) = create_loader();
        let url = format!("{}/blocklist.txt", mock_server.uri());

        let domains = loader.load(&url, BlocklistFormat::Domains).await.unwrap();

        assert_eq!(domains, vec!["example.com"]);
    }

    #[tokio::test]
    async fn should_handle_redirect_responses() {
        let mock_server = MockServer::start().await;

        // First request redirects
        Mock::given(method("GET"))
            .and(path("/redirect"))
            .respond_with(
                ResponseTemplate::new(302)
                    .append_header("Location", format!("{}/actual", mock_server.uri())),
            )
            .mount(&mock_server)
            .await;

        // Actual location
        Mock::given(method("GET"))
            .and(path("/actual"))
            .respond_with(ResponseTemplate::new(200).set_body_string("example.com"))
            .mount(&mock_server)
            .await;

        let (loader, _temp) = create_loader();
        let url = format!("{}/redirect", mock_server.uri());

        let domains = loader.load(&url, BlocklistFormat::Domains).await.unwrap();

        assert_eq!(domains, vec!["example.com"]);
    }

    #[test]
    fn should_return_default_cache_dir() {
        let cache_dir = default_cache_dir();
        assert!(cache_dir.ends_with("bluebox/blocklists") || cache_dir.ends_with("blocklists"));
    }

    #[tokio::test]
    async fn should_handle_large_response() {
        use std::fmt::Write;

        let mock_server = MockServer::start().await;

        // Generate a large blocklist
        let mut content = String::new();
        for i in 0..10_000 {
            writeln!(content, "domain{i}.example.com").unwrap();
        }

        Mock::given(method("GET"))
            .and(path("/large.txt"))
            .respond_with(ResponseTemplate::new(200).set_body_string(content))
            .mount(&mock_server)
            .await;

        let (loader, _temp) = create_loader();
        let url = format!("{}/large.txt", mock_server.uri());

        let domains = loader.load(&url, BlocklistFormat::Domains).await.unwrap();

        assert_eq!(domains.len(), 10_000);
        assert_eq!(domains[0], "domain0.example.com");
        assert_eq!(domains[9999], "domain9999.example.com");
    }
}