ws2tcp-local-core 0.1.2

Core proxy library for ws2tcp-local.
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
use std::{
    collections::HashSet,
    fs::{self, FileTimes, OpenOptions},
    io::{Read, Seek, Write},
    path::{Path, PathBuf},
    sync::{
        Arc, RwLock,
        atomic::{AtomicU64, Ordering},
    },
    time::{Duration, SystemTime},
};

use anyhow::{Context, Result, anyhow, bail};
use base64::{Engine, engine::general_purpose::STANDARD};
use reqwest::{
    Client,
    header::{HeaderMap, LAST_MODIFIED},
};
use tracing::{info, warn};

use crate::settings::ProxyMode;

const GFWLIST_URL: &str = "https://gitlab.com/gfwlist/gfwlist/raw/master/gfwlist.txt";
const CACHE_DIR_NAME: &str = "ws2tcp-local";
const GFWLIST_CACHE_FILE: &str = "gfwlist.txt";

#[derive(Debug, Clone)]
pub(crate) struct RoutingRules {
    state: Arc<RwLock<RoutingRulesState>>,
    generation: Arc<AtomicU64>,
    custom_domain_rules: Option<PathBuf>,
    refresh_interval: Duration,
}

#[derive(Debug, Clone)]
enum RoutingRulesState {
    Domains {
        rules: DomainRules,
        custom_domain_rules: Option<PathBuf>,
    },
    GlobalProxy,
    DirectFallback,
}

impl RoutingRules {
    pub(crate) async fn load(
        proxy_mode: ProxyMode,
        custom_domain_rules: Option<&Path>,
        refresh_interval: Duration,
    ) -> Self {
        if proxy_mode == ProxyMode::Global {
            info!("using global proxy mode; skipping proxy routing rule download");
            return Self::new(
                RoutingRulesState::GlobalProxy,
                custom_domain_rules.map(Path::to_path_buf),
                refresh_interval,
            );
        }

        let mut loader = AutoRuleLoader::new(custom_domain_rules.map(Path::to_path_buf));
        let state = match loader.load_state().await {
            Ok(state) => state,
            Err(err) => {
                warn!(
                    url = GFWLIST_URL,
                    custom_domain_rules = custom_domain_rules.map(|path| path.display().to_string()),
                    error = %format_args!("{err:#}"),
                    "failed to load proxy routing rules; direct routing until rules are available"
                );
                RoutingRulesState::DirectFallback
            }
        };
        let rules = Self::new(
            state,
            custom_domain_rules.map(Path::to_path_buf),
            refresh_interval,
        );
        rules.spawn_auto_refresh(loader, refresh_interval, 0);
        rules
    }

    fn new(
        state: RoutingRulesState,
        custom_domain_rules: Option<PathBuf>,
        refresh_interval: Duration,
    ) -> Self {
        Self {
            state: Arc::new(RwLock::new(state)),
            generation: Arc::new(AtomicU64::new(0)),
            custom_domain_rules,
            refresh_interval,
        }
    }

    fn spawn_auto_refresh(
        &self,
        mut loader: AutoRuleLoader,
        refresh_interval: Duration,
        generation: u64,
    ) {
        let state = Arc::clone(&self.state);
        let current_generation = Arc::clone(&self.generation);

        tokio::spawn(async move {
            loop {
                tokio::time::sleep(refresh_interval).await;
                if current_generation.load(Ordering::Acquire) != generation {
                    return;
                }

                match loader.load_state().await {
                    Ok(next_state) => {
                        if current_generation.load(Ordering::Acquire) != generation {
                            return;
                        }
                        let mut guard = state.write().expect("routing rules lock poisoned");
                        *guard = next_state;
                    }
                    Err(err) => {
                        warn!(
                            url = GFWLIST_URL,
                            custom_domain_rules =
                                loader.custom_domain_rules_display(),
                            error = %format_args!("{err:#}"),
                            "failed to refresh proxy routing rules; keeping existing rules"
                        );
                    }
                }
            }
        });
    }

    pub(crate) fn set_mode(&self, mode: ProxyMode) {
        let generation = self.generation.fetch_add(1, Ordering::AcqRel) + 1;
        match mode {
            ProxyMode::Global => {
                *self.state.write().expect("routing rules lock poisoned") =
                    RoutingRulesState::GlobalProxy;
                info!("proxy mode dynamically changed to global");
            }
            ProxyMode::Auto => {
                let rules = self.clone();
                tokio::spawn(async move {
                    let mut loader = AutoRuleLoader::new(rules.custom_domain_rules.clone());
                    match loader.load_state().await {
                        Ok(next_state) => {
                            if rules.generation.load(Ordering::Acquire) != generation {
                                return;
                            }
                            *rules.state.write().expect("routing rules lock poisoned") = next_state;
                            info!("proxy mode dynamically changed to auto");
                            rules.spawn_auto_refresh(loader, rules.refresh_interval, generation);
                        }
                        Err(err) => {
                            if rules.generation.load(Ordering::Acquire) != generation {
                                return;
                            }
                            *rules.state.write().expect("routing rules lock poisoned") =
                                RoutingRulesState::DirectFallback;
                            warn!(error = %format_args!("{err:#}"), "failed to switch proxy mode to auto; using direct routing")
                        }
                    }
                });
            }
        }
    }

    pub(crate) fn should_proxy_host(&self, host: &str) -> bool {
        self.state
            .read()
            .expect("routing rules lock poisoned")
            .should_proxy_host(host)
    }

    fn mode(&self) -> &'static str {
        self.state
            .read()
            .expect("routing rules lock poisoned")
            .mode()
    }

    pub(crate) fn describe(&self) -> String {
        self.state
            .read()
            .expect("routing rules lock poisoned")
            .describe()
    }
}

#[derive(Debug)]
struct AutoRuleLoader {
    custom_domain_rules: Option<PathBuf>,
    custom_cache: Option<CustomDomainRulesCache>,
    gfwlist_cache: GfwlistCache,
}

#[derive(Debug, Clone)]
struct CustomDomainRulesCache {
    modified: SystemTime,
    domains: HashSet<String>,
}

#[derive(Debug)]
enum GfwlistCache {
    Disk(PathBuf),
    Memory {
        body: Option<Vec<u8>>,
        modified: Option<SystemTime>,
    },
}

impl AutoRuleLoader {
    fn new(custom_domain_rules: Option<PathBuf>) -> Self {
        Self {
            custom_domain_rules,
            custom_cache: None,
            gfwlist_cache: GfwlistCache::new(),
        }
    }

    async fn load_state(&mut self) -> Result<RoutingRulesState> {
        let rules = self.download_and_parse().await?;
        Ok(RoutingRulesState::from_domain_rules(
            rules,
            self.custom_domain_rules.as_deref(),
        ))
    }

    async fn download_and_parse(&mut self) -> Result<DomainRules> {
        let body = self.gfwlist_cache.load_gfwlist_body().await?;

        let mut rules = parse_gfwlist(&body)?;
        if let Some(custom_domains) = self.load_custom_domain_rules()? {
            rules.extend(custom_domains);
        }

        Ok(rules)
    }

    fn load_custom_domain_rules(&mut self) -> Result<Option<HashSet<String>>> {
        let Some(path) = self.custom_domain_rules.as_deref() else {
            return Ok(None);
        };
        let modified = file_modified_time(path).with_context(|| {
            format!(
                "failed to read custom domain rules timestamp {}",
                path.display()
            )
        })?;

        if let Some(cache) = &self.custom_cache
            && system_times_match_to_second(cache.modified, modified)
        {
            info!(
                path = %path.display(),
                custom_domain_count = cache.domains.len(),
                "using cached custom proxy routing rules"
            );
            return Ok(Some(cache.domains.clone()));
        }

        let domains = read_custom_domain_rules(path)?;
        let custom_count = domains.len();
        self.custom_cache = Some(CustomDomainRulesCache {
            modified,
            domains: domains.clone(),
        });
        info!(
            path = %path.display(),
            custom_domain_count = custom_count,
            "loaded custom proxy routing rules"
        );
        Ok(Some(domains))
    }

    fn custom_domain_rules_display(&self) -> Option<String> {
        self.custom_domain_rules
            .as_ref()
            .map(|path| path.display().to_string())
    }
}

impl RoutingRulesState {
    fn from_domain_rules(rules: DomainRules, custom_domain_rules: Option<&Path>) -> Self {
        info!(
            url = GFWLIST_URL,
            custom_domain_rules = custom_domain_rules.map(|path| path.display().to_string()),
            domain_count = rules.len(),
            "loaded proxy routing rules"
        );
        Self::Domains {
            rules,
            custom_domain_rules: custom_domain_rules.map(Path::to_path_buf),
        }
    }

    fn should_proxy_host(&self, host: &str) -> bool {
        match self {
            Self::Domains { rules, .. } => rules.matches(host),
            Self::GlobalProxy => true,
            Self::DirectFallback => false,
        }
    }

    fn mode(&self) -> &'static str {
        match self {
            Self::Domains { .. } | Self::DirectFallback => "auto",
            Self::GlobalProxy => "global",
        }
    }

    fn describe(&self) -> String {
        match self {
            Self::Domains {
                rules,
                custom_domain_rules: Some(path),
            } => format!(
                "{} domains from {} plus custom rules from {}",
                rules.len(),
                GFWLIST_URL,
                path.display()
            ),
            Self::Domains {
                rules,
                custom_domain_rules: None,
            } => format!("{} domains from {}", rules.len(), GFWLIST_URL),
            Self::GlobalProxy => "all domains via proxy; proxy mode is global".to_owned(),
            Self::DirectFallback => {
                format!("direct routing; failed to load {GFWLIST_URL}")
            }
        }
    }
}

impl GfwlistCache {
    fn new() -> Self {
        match gfwlist_cache_path().and_then(check_gfwlist_cache_access) {
            Ok(path) => Self::Disk(path),
            Err(err) => {
                warn!(
                    error = %format_args!("{err:#}"),
                    "gfwlist disk cache is unavailable; using in-memory cache"
                );
                Self::empty_memory()
            }
        }
    }

    fn empty_memory() -> Self {
        Self::Memory {
            body: None,
            modified: None,
        }
    }

    fn switch_to_memory(&mut self, error: &anyhow::Error) {
        warn!(
            error = %format_args!("{error:#}"),
            "gfwlist disk cache failed; switching to in-memory cache"
        );
        *self = Self::empty_memory();
    }

    fn memory_body_if_current(&self, remote_modified: Option<SystemTime>) -> Option<Vec<u8>> {
        let Self::Memory { body, modified } = self else {
            return None;
        };
        let remote_modified = remote_modified?;
        let cached_modified = (*modified)?;

        system_times_match_to_second(cached_modified, remote_modified)
            .then(|| body.clone())
            .flatten()
    }

    fn store_in_memory(&mut self, body: Vec<u8>, modified: Option<SystemTime>) {
        *self = Self::Memory {
            body: Some(body),
            modified,
        };
    }

    async fn load_gfwlist_body(&mut self) -> Result<Vec<u8>> {
        let client = Client::new();
        let remote_modified = fetch_remote_last_modified(&client).await?;

        if let Self::Disk(cache_path) = self {
            let cached = match remote_modified {
                Some(remote_modified) => match is_cache_current(cache_path, remote_modified) {
                    Ok(true) => Some(fs::read(&*cache_path).with_context(|| {
                        format!("failed to read cached gfwlist {}", cache_path.display())
                    })),
                    Ok(false) => None,
                    Err(err) => Some(Err(err)),
                },
                None => None,
            };

            if let Some(cached) = cached {
                match cached {
                    Ok(body) => {
                        info!(
                            cache_path = %cache_path.display(),
                            url = GFWLIST_URL,
                            "using cached gfwlist"
                        );
                        return Ok(body);
                    }
                    Err(err) => self.switch_to_memory(&err),
                }
            }
        }

        if let Some(body) = self.memory_body_if_current(remote_modified) {
            info!(url = GFWLIST_URL, "using in-memory cached gfwlist");
            return Ok(body);
        }

        let response = client
            .get(GFWLIST_URL)
            .send()
            .await
            .with_context(|| format!("failed to download {GFWLIST_URL}"))?
            .error_for_status()
            .with_context(|| format!("failed to download {GFWLIST_URL}"))?;
        let downloaded_modified = parse_last_modified(response.headers()).or(remote_modified);
        let body = response
            .bytes()
            .await
            .context("failed to read gfwlist response body")?
            .to_vec();

        if let Self::Disk(cache_path) = self
            && let Err(err) = write_gfwlist_cache(cache_path, &body, downloaded_modified)
        {
            self.switch_to_memory(&err);
        }
        if matches!(self, Self::Memory { .. }) {
            self.store_in_memory(body.clone(), downloaded_modified);
        }

        Ok(body)
    }
}

async fn fetch_remote_last_modified(client: &Client) -> Result<Option<SystemTime>> {
    let response = client
        .head(GFWLIST_URL)
        .send()
        .await
        .with_context(|| format!("failed to check remote gfwlist timestamp {GFWLIST_URL}"))?
        .error_for_status()
        .with_context(|| format!("failed to check remote gfwlist timestamp {GFWLIST_URL}"))?;

    Ok(parse_last_modified(response.headers()))
}

fn parse_last_modified(headers: &HeaderMap) -> Option<SystemTime> {
    headers
        .get(LAST_MODIFIED)
        .and_then(|value| value.to_str().ok())
        .and_then(|value| httpdate::parse_http_date(value).ok())
}

fn gfwlist_cache_path() -> Result<PathBuf> {
    let cache_dir = user_cache_dir()?;

    Ok(cache_dir.join(CACHE_DIR_NAME).join(GFWLIST_CACHE_FILE))
}

fn check_gfwlist_cache_access(cache_path: PathBuf) -> Result<PathBuf> {
    let parent = cache_path
        .parent()
        .context("gfwlist cache path has no parent directory")?;
    fs::create_dir_all(parent).with_context(|| {
        format!(
            "failed to create gfwlist cache directory {}",
            parent.display()
        )
    })?;

    if cache_path.exists() {
        OpenOptions::new()
            .read(true)
            .write(true)
            .open(&cache_path)
            .with_context(|| {
                format!(
                    "gfwlist cache is not readable and writable {}",
                    cache_path.display()
                )
            })?;
    }

    let probe_path = parent.join(format!(".cache-access-{}", std::process::id()));
    let probe_result = (|| -> Result<()> {
        let mut probe = OpenOptions::new()
            .read(true)
            .write(true)
            .create_new(true)
            .open(&probe_path)
            .with_context(|| {
                format!(
                    "gfwlist cache directory is not writable {}",
                    parent.display()
                )
            })?;
        probe.write_all(b"ok")?;
        probe.rewind()?;
        let mut contents = String::new();
        probe.read_to_string(&mut contents)?;
        if contents != "ok" {
            bail!("gfwlist cache access probe returned unexpected contents");
        }
        Ok(())
    })();
    let remove_result = fs::remove_file(&probe_path);
    probe_result?;
    remove_result.with_context(|| {
        format!(
            "failed to remove gfwlist cache access probe {}",
            probe_path.display()
        )
    })?;

    Ok(cache_path)
}

#[cfg(windows)]
fn user_cache_dir() -> Result<PathBuf> {
    if let Some(path) = std::env::var_os("LOCALAPPDATA") {
        return Ok(PathBuf::from(path));
    }

    let profile = std::env::var_os("USERPROFILE")
        .context("USERPROFILE is not set; cannot locate gfwlist cache")?;
    Ok(PathBuf::from(profile).join("AppData").join("Local"))
}

#[cfg(target_os = "macos")]
fn user_cache_dir() -> Result<PathBuf> {
    let home = std::env::var_os("HOME").context("HOME is not set; cannot locate gfwlist cache")?;
    Ok(PathBuf::from(home).join("Library").join("Caches"))
}

#[cfg(all(not(windows), not(target_os = "macos")))]
fn user_cache_dir() -> Result<PathBuf> {
    match std::env::var_os("XDG_CACHE_HOME") {
        Some(path) => Ok(PathBuf::from(path)),
        None => {
            let home =
                std::env::var_os("HOME").context("HOME is not set; cannot locate gfwlist cache")?;
            Ok(PathBuf::from(home).join(".cache"))
        }
    }
}

fn is_cache_current(cache_path: &Path, remote_modified: SystemTime) -> Result<bool> {
    let cache_modified = match file_modified_time(cache_path) {
        Ok(modified) => modified,
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(false),
        Err(err) => {
            return Err(err)
                .with_context(|| format!("failed to read gfwlist cache {}", cache_path.display()));
        }
    };

    Ok(system_times_match_to_second(
        cache_modified,
        remote_modified,
    ))
}

fn file_modified_time(path: &Path) -> std::io::Result<SystemTime> {
    fs::metadata(path)?.modified()
}

fn write_gfwlist_cache(
    cache_path: &Path,
    body: &[u8],
    remote_modified: Option<SystemTime>,
) -> Result<()> {
    if let Some(parent) = cache_path.parent() {
        fs::create_dir_all(parent).with_context(|| {
            format!(
                "failed to create gfwlist cache directory {}",
                parent.display()
            )
        })?;
    }

    fs::write(cache_path, body)
        .with_context(|| format!("failed to write gfwlist cache {}", cache_path.display()))?;

    if let Some(remote_modified) = remote_modified {
        fs::File::options()
            .write(true)
            .open(cache_path)
            .and_then(|file| file.set_times(FileTimes::new().set_modified(remote_modified)))
            .with_context(|| {
                format!(
                    "failed to update gfwlist cache timestamp {}",
                    cache_path.display()
                )
            })?;
    }

    info!(
        cache_path = %cache_path.display(),
        url = GFWLIST_URL,
        "updated gfwlist cache"
    );
    Ok(())
}

fn system_times_match_to_second(left: SystemTime, right: SystemTime) -> bool {
    left.duration_since(SystemTime::UNIX_EPOCH)
        .ok()
        .map(truncate_to_second)
        == right
            .duration_since(SystemTime::UNIX_EPOCH)
            .ok()
            .map(truncate_to_second)
}

fn truncate_to_second(duration: Duration) -> Duration {
    Duration::from_secs(duration.as_secs())
}

#[derive(Debug, Clone)]
pub(crate) struct DomainRules {
    domains: HashSet<String>,
}

impl DomainRules {
    fn new(domains: HashSet<String>) -> Result<Self> {
        if domains.is_empty() {
            bail!("gfwlist did not contain any usable domain rules");
        }

        Ok(Self { domains })
    }

    fn len(&self) -> usize {
        self.domains.len()
    }

    fn extend(&mut self, domains: HashSet<String>) {
        self.domains.extend(domains);
    }

    fn matches(&self, host: &str) -> bool {
        let host = normalize_host_for_match(host);
        if host.is_empty() {
            return false;
        }

        if self.domains.contains(&host) {
            return true;
        }

        host.match_indices('.')
            .any(|(idx, _)| self.domains.contains(&host[idx + 1..]))
    }
}

fn parse_gfwlist(encoded: &[u8]) -> Result<DomainRules> {
    let compact: Vec<u8> = encoded
        .iter()
        .copied()
        .filter(|byte| !byte.is_ascii_whitespace())
        .collect();
    let decoded = STANDARD
        .decode(compact)
        .context("failed to decode gfwlist")?;
    let text = String::from_utf8(decoded).context("decoded gfwlist is not valid UTF-8")?;
    parse_gfwlist_text(&text)
}

fn parse_gfwlist_text(text: &str) -> Result<DomainRules> {
    let domains = text
        .lines()
        .filter_map(parse_proxy_rule_domain)
        .collect::<HashSet<_>>();

    DomainRules::new(domains)
}

fn read_custom_domain_rules(path: &Path) -> Result<HashSet<String>> {
    let text = fs::read_to_string(path)
        .with_context(|| format!("failed to read custom domain rules {}", path.display()))?;
    Ok(parse_custom_domain_rules_text(&text))
}

fn parse_custom_domain_rules_text(text: &str) -> HashSet<String> {
    text.lines()
        .filter_map(parse_custom_domain_rule_domain)
        .collect()
}

fn parse_custom_domain_rule_domain(line: &str) -> Option<String> {
    let rule = line.split('#').next().unwrap_or_default().trim();
    let domain = normalize_host_for_match(rule);
    is_domain_like(&domain).then_some(domain)
}

fn parse_proxy_rule_domain(line: &str) -> Option<String> {
    let rule = line.strip_prefix("||").or_else(|| line.strip_prefix('.'))?;
    if rule.contains('*') {
        return None;
    }

    let domain = rule
        .split(['/', '^', '$'])
        .next()
        .unwrap_or_default()
        .trim_matches('.');
    let domain = normalize_host_for_match(domain);
    is_domain_like(&domain).then_some(domain)
}

fn normalize_host_for_match(host: &str) -> String {
    host.trim().trim_matches('.').to_ascii_lowercase()
}

fn is_domain_like(domain: &str) -> bool {
    if domain.is_empty() || domain.contains(':') || domain.parse::<std::net::IpAddr>().is_ok() {
        return false;
    }

    domain
        .bytes()
        .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-' || byte == b'.')
}

pub(crate) fn host_from_authority(authority: &str) -> Result<&str> {
    if let Some(rest) = authority.strip_prefix('[') {
        return rest
            .split_once("]:")
            .map(|(host, _)| host)
            .ok_or_else(|| anyhow!("IPv6 authority must be [host]:port"));
    }

    authority
        .rsplit_once(':')
        .map(|(host, _)| host)
        .ok_or_else(|| anyhow!("authority must include :port"))
}

impl std::fmt::Display for RoutingRules {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.mode())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use base64::engine::general_purpose::STANDARD;

    #[test]
    fn parses_gfwlist_domain_rules() {
        let text = "\
! comment
||example.com
||example.net/path
||example.org^
||example.edu$third-party
||wild*.blocked.test
.leading-dot.example
|http://ignored.example
";
        let encoded = STANDARD.encode(text);
        let rules = parse_gfwlist(encoded.as_bytes()).unwrap();

        assert!(rules.matches("example.com"));
        assert!(rules.matches("www.example.com"));
        assert!(rules.matches("example.net"));
        assert!(rules.matches("a.example.org"));
        assert!(rules.matches("example.edu"));
        assert!(rules.matches("www.leading-dot.example"));
        assert!(!rules.matches("wild.blocked.test"));
        assert!(!rules.matches("ignored.example"));
    }

    #[test]
    fn matches_case_insensitively_and_on_suffix_boundary() {
        let rules = parse_gfwlist_text("||example.com\n").unwrap();

        assert!(rules.matches("WWW.Example.Com."));
        assert!(!rules.matches("badexample.com"));
    }

    #[test]
    fn parses_custom_domain_rules() {
        let domains = parse_custom_domain_rules_text(
            "\
# One Squid dstdomain entry per line.
.paypal.com
.www.paypal.com

.googleadservices.com # inline comment
127.0.0.1
bad:domain
",
        );
        let rules = DomainRules::new(domains).unwrap();

        assert!(rules.matches("paypal.com"));
        assert!(rules.matches("checkout.paypal.com"));
        assert!(rules.matches("www.paypal.com"));
        assert!(rules.matches("pagead.googleadservices.com"));
        assert!(!rules.matches("127.0.0.1"));
        assert!(!rules.matches("bad:domain"));
    }

    #[test]
    fn custom_domain_rules_cache_reuses_unchanged_file() {
        let path = temp_custom_rules_path("custom-cache-reuses-unchanged");
        let modified = SystemTime::UNIX_EPOCH + Duration::from_secs(1_700_000_000);
        write_custom_rules_at(&path, ".first.example\n", modified);
        let mut loader = AutoRuleLoader::new(Some(path.clone()));

        let first = loader.load_custom_domain_rules().unwrap().unwrap();
        write_custom_rules_at(&path, ".second.example\n", modified);
        let second = loader.load_custom_domain_rules().unwrap().unwrap();
        let _ = fs::remove_file(&path);

        assert!(first.contains("first.example"));
        assert!(second.contains("first.example"));
        assert!(!second.contains("second.example"));
    }

    #[test]
    fn custom_domain_rules_cache_reloads_changed_file() {
        let path = temp_custom_rules_path("custom-cache-reloads-changed");
        let first_modified = SystemTime::UNIX_EPOCH + Duration::from_secs(1_700_000_000);
        let second_modified = first_modified + Duration::from_secs(2);
        write_custom_rules_at(&path, ".first.example\n", first_modified);
        let mut loader = AutoRuleLoader::new(Some(path.clone()));

        let first = loader.load_custom_domain_rules().unwrap().unwrap();
        write_custom_rules_at(&path, ".second.example\n", second_modified);
        let second = loader.load_custom_domain_rules().unwrap().unwrap();
        let _ = fs::remove_file(&path);

        assert!(first.contains("first.example"));
        assert!(!second.contains("first.example"));
        assert!(second.contains("second.example"));
    }

    #[test]
    fn parses_last_modified_header() {
        let mut headers = HeaderMap::new();
        headers.insert(
            LAST_MODIFIED,
            "Wed, 21 Oct 2015 07:28:00 GMT".parse().unwrap(),
        );

        assert_eq!(
            parse_last_modified(&headers).unwrap(),
            SystemTime::UNIX_EPOCH + Duration::from_secs(1_445_412_480)
        );
    }

    #[test]
    fn compares_timestamps_to_second_precision() {
        let timestamp = SystemTime::UNIX_EPOCH + Duration::from_secs(42);

        assert!(system_times_match_to_second(
            timestamp + Duration::from_millis(900),
            timestamp
        ));
        assert!(!system_times_match_to_second(
            timestamp + Duration::from_secs(1),
            timestamp
        ));
    }

    #[test]
    fn extracts_host_from_authority() {
        assert_eq!(
            host_from_authority("example.com:443").unwrap(),
            "example.com"
        );
        assert_eq!(
            host_from_authority("[2001:db8::1]:443").unwrap(),
            "2001:db8::1"
        );
    }

    #[test]
    fn global_proxy_matches_every_host() {
        let rules = RoutingRules::new(
            RoutingRulesState::GlobalProxy,
            None,
            Duration::from_secs(60),
        );

        assert!(rules.should_proxy_host("example.com"));
        assert_eq!(rules.to_string(), "global");
        assert_eq!(
            rules.describe(),
            "all domains via proxy; proxy mode is global"
        );
    }

    #[tokio::test]
    async fn global_proxy_load_skips_rule_files() {
        let rules = RoutingRules::load(
            ProxyMode::Global,
            Some(Path::new("/definitely/missing/custom-domains.txt")),
            Duration::from_secs(60),
        )
        .await;

        assert!(rules.should_proxy_host("example.com"));
        assert_eq!(rules.to_string(), "global");
    }

    #[test]
    fn auto_fallback_routes_direct_by_default() {
        let rules = RoutingRules::new(
            RoutingRulesState::DirectFallback,
            None,
            Duration::from_secs(60),
        );

        assert!(!rules.should_proxy_host("example.com"));
        assert_eq!(rules.to_string(), "auto");
        assert_eq!(
            rules.describe(),
            format!("direct routing; failed to load {GFWLIST_URL}")
        );
    }

    #[test]
    fn dynamically_switches_to_global_mode() {
        let rules = RoutingRules::new(
            RoutingRulesState::DirectFallback,
            None,
            Duration::from_secs(60),
        );

        rules.set_mode(ProxyMode::Global);

        assert!(rules.should_proxy_host("example.com"));
        assert_eq!(rules.to_string(), "global");
    }

    fn temp_custom_rules_path(name: &str) -> PathBuf {
        std::env::temp_dir().join(format!(
            "ws2tcp-local-test-{}-{name}.txt",
            std::process::id()
        ))
    }

    fn write_custom_rules_at(path: &Path, text: &str, modified: SystemTime) {
        fs::write(path, text).unwrap();
        fs::File::options()
            .write(true)
            .open(path)
            .and_then(|file| file.set_times(FileTimes::new().set_modified(modified)))
            .unwrap();
    }
}