scatter-proxy 0.9.0

Async request scheduler for unreliable SOCKS5 proxies — multi-path race for maximum throughput
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
use dashmap::DashMap;

use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use std::time::Duration;

use crate::error::ScatterProxyError;

/// State of a proxy in the manager.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProxyState {
    Unknown,
    Active,
    /// Proxy was eliminated due to sustained failure.  Not automatically
    /// recovered — requires a restart or explicit state reset.
    Dead,
    /// Proxy reached its per-node request limit and was voluntarily retired.
    /// Unlike `Dead`, `Retired` nodes can be reactivated on source refresh.
    Retired,
}

/// Internal info stored per proxy.
struct ProxyInfo {
    state: ProxyState,
}

/// Manages a set of SOCKS5 proxies: fetching from sources, normalizing URLs,
/// tracking state, and caching `reqwest::Client` instances.
pub struct ProxyManager {
    proxies: DashMap<String, ProxyInfo>,
    clients: DashMap<String, reqwest::Client>,
    proxy_timeout: Duration,
    /// Per-proxy WAF cookie values (`proxy_url` → cookie value).
    cookies: DashMap<String, String>,
    /// Per-proxy cumulative request counters.
    request_counts: DashMap<String, Arc<AtomicU32>>,
    /// Per-proxy semaphore ensuring at most one concurrent challenge solve.
    challenge_locks: DashMap<String, Arc<tokio::sync::Semaphore>>,
}

impl ProxyManager {
    /// Create a new `ProxyManager` with the given per-proxy connection timeout.
    pub fn new(proxy_timeout: Duration) -> Self {
        let proxies = DashMap::new();
        Self {
            proxies,
            clients: DashMap::new(),
            proxy_timeout,
            cookies: DashMap::new(),
            request_counts: DashMap::new(),
            challenge_locks: DashMap::new(),
        }
    }

    // ── Per-proxy cookie management ──────────────────────────────────────────

    /// Retrieve the stored cookie value for a proxy node, if any.
    pub fn get_cookie(&self, proxy_url: &str) -> Option<String> {
        self.cookies.get(proxy_url).map(|v| v.clone())
    }

    /// Store a cookie value for a proxy node, replacing any existing value.
    pub fn set_cookie(&self, proxy_url: &str, value: String) {
        self.cookies.insert(proxy_url.to_string(), value);
    }

    // ── Per-proxy request counting ───────────────────────────────────────────

    /// Atomically increment the request count for a proxy and return the new value.
    pub fn increment_request_count(&self, proxy_url: &str) -> u32 {
        self.request_counts
            .entry(proxy_url.to_string())
            .or_insert_with(|| Arc::new(AtomicU32::new(0)))
            .fetch_add(1, Ordering::Relaxed)
            + 1
    }

    /// Return the current request count for a proxy node.
    pub fn request_count(&self, proxy_url: &str) -> u32 {
        self.request_counts
            .get(proxy_url)
            .map(|c| c.load(Ordering::Relaxed))
            .unwrap_or(0)
    }

    /// Reset the request count for a proxy node and, if the node was `Retired`,
    /// restore it to `Active`.  `Dead` nodes are not restored.
    pub fn reset_request_count(&self, proxy_url: &str) {
        if let Some(counter) = self.request_counts.get(proxy_url) {
            counter.store(0, Ordering::Relaxed);
        }
        if matches!(self.get_state(proxy_url), ProxyState::Retired) {
            self.set_state(proxy_url, ProxyState::Active);
        }
    }

    // ── Per-proxy challenge serialization ────────────────────────────────────

    /// Acquire the per-proxy challenge lock (semaphore with 1 permit).
    ///
    /// Ensures at most one concurrent challenge solve per proxy node.
    pub fn challenge_lock(&self, proxy_url: &str) -> Arc<tokio::sync::Semaphore> {
        self.challenge_locks
            .entry(proxy_url.to_string())
            .or_insert_with(|| Arc::new(tokio::sync::Semaphore::new(1)))
            .clone()
    }

    /// Fetch proxy lists from the given source URLs, normalize each line, and
    /// add any new proxies to the manager.
    ///
    /// Returns the count of newly added proxies across all sources.
    /// Errors fetching individual sources are logged but do not cause the
    /// overall call to fail.
    pub async fn fetch_and_add(
        &self,
        sources: &[String],
        prefer_remote_dns: bool,
    ) -> Result<usize, ScatterProxyError> {
        let http_client = reqwest::Client::builder()
            .timeout(Duration::from_secs(30))
            .build()
            .map_err(|e| ScatterProxyError::Init(format!("failed to build HTTP client: {e}")))?;

        let mut added = 0usize;

        for source in sources {
            match http_client.get(source).send().await {
                Ok(resp) => match resp.text().await {
                    Ok(body) => {
                        for line in body.lines() {
                            let normalized = Self::normalize_proxy_url(line, prefer_remote_dns);
                            if normalized.is_empty() {
                                continue;
                            }
                            if !self.proxies.contains_key(&normalized) {
                                self.proxies.insert(
                                    normalized,
                                    ProxyInfo {
                                        state: ProxyState::Unknown,
                                    },
                                );
                                added += 1;
                            }
                        }
                    }
                    Err(e) => {
                        tracing::error!("failed to read body from source {source}: {e}");
                    }
                },
                Err(e) => {
                    tracing::error!("failed to fetch proxy source {source}: {e}");
                }
            }
        }

        Ok(added)
    }

    /// Get all proxy URLs currently in the manager.
    pub fn all_proxy_urls(&self) -> Vec<String> {
        self.proxies.iter().map(|r| r.key().clone()).collect()
    }

    /// Get proxy URLs that are eligible for scheduling (not `Dead` or `Retired`).
    pub fn get_active_proxies(&self) -> Vec<String> {
        self.proxies
            .iter()
            .filter(|r| !matches!(r.value().state, ProxyState::Dead | ProxyState::Retired))
            .map(|r| r.key().clone())
            .collect()
    }

    /// Set the state of a proxy.
    pub fn set_state(&self, proxy: &str, state: ProxyState) {
        if let Some(mut entry) = self.proxies.get_mut(proxy) {
            entry.state = state;
        }
    }

    /// Get the state of a proxy. Returns `Unknown` if the proxy is not tracked.
    pub fn get_state(&self, proxy: &str) -> ProxyState {
        self.proxies
            .get(proxy)
            .map(|r| r.value().state)
            .unwrap_or(ProxyState::Unknown)
    }

    /// Count proxies by state.
    ///
    /// Returns `(total, active_or_unknown, cooldown_placeholder, dead, retired)`.
    /// The cooldown placeholder is always 0 — cooldown tracking lives in a separate module.
    pub fn proxy_counts(&self) -> (usize, usize, usize, usize, usize) {
        let mut total = 0usize;
        let mut active_or_unknown = 0usize;
        let mut dead = 0usize;
        let mut retired = 0usize;

        for entry in self.proxies.iter() {
            total += 1;
            match entry.value().state {
                ProxyState::Dead => dead += 1,
                ProxyState::Retired => retired += 1,
                _ => active_or_unknown += 1,
            }
        }

        (total, active_or_unknown, 0, dead, retired)
    }

    /// Get or create a `reqwest::Client` configured to route through the given
    /// proxy URL.  Clients are cached for reuse.
    pub fn get_client(&self, proxy_url: &str) -> Result<reqwest::Client, ScatterProxyError> {
        if let Some(client) = self.clients.get(proxy_url) {
            return Ok(client.value().clone());
        }

        let proxy = reqwest::Proxy::all(proxy_url).map_err(|e| {
            ScatterProxyError::Init(format!("invalid proxy URL '{proxy_url}': {e}"))
        })?;

        let client = reqwest::Client::builder()
            .proxy(proxy)
            .timeout(self.proxy_timeout)
            .build()
            .map_err(|e| {
                ScatterProxyError::Init(format!(
                    "failed to build client for proxy '{proxy_url}': {e}"
                ))
            })?;

        self.clients.insert(proxy_url.to_string(), client.clone());
        Ok(client)
    }

    /// Normalize a raw proxy string to a full URL.
    ///
    /// Rules:
    /// - Empty lines and lines starting with `#` are returned as empty string.
    /// - `"socks5h://ip:port"` → returned as-is.
    /// - `"socks5://ip:port"` → replaced with `"socks5h://"` when
    ///   `prefer_remote_dns` is true, otherwise kept as-is.
    /// - Bare `"ip:port"` → prepended with `"socks5h://"` when
    ///   `prefer_remote_dns` is true, otherwise `"socks5://"`.
    pub fn normalize_proxy_url(raw: &str, prefer_remote_dns: bool) -> String {
        let trimmed = raw.trim();

        if trimmed.is_empty() || trimmed.starts_with('#') {
            return String::new();
        }

        if trimmed.starts_with("socks5h://") {
            return trimmed.to_string();
        }

        if let Some(rest) = trimmed.strip_prefix("socks5://") {
            if prefer_remote_dns {
                return format!("socks5h://{rest}");
            }
            return trimmed.to_string();
        }

        // Also handle http/https proxies as-is
        if trimmed.starts_with("http://") || trimmed.starts_with("https://") {
            return trimmed.to_string();
        }

        // Bare ip:port
        if prefer_remote_dns {
            format!("socks5h://{trimmed}")
        } else {
            format!("socks5://{trimmed}")
        }
    }

    /// Total number of proxies being tracked.
    #[allow(dead_code)]
    pub fn total_count(&self) -> usize {
        self.proxies.len()
    }
}

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

    // -----------------------------------------------------------------------
    // normalize_proxy_url
    // -----------------------------------------------------------------------

    #[test]
    fn normalize_bare_ip_port_prefer_remote_dns() {
        let result = ProxyManager::normalize_proxy_url("1.2.3.4:1080", true);
        assert_eq!(result, "socks5h://1.2.3.4:1080");
    }

    #[test]
    fn normalize_bare_ip_port_local_dns() {
        let result = ProxyManager::normalize_proxy_url("1.2.3.4:1080", false);
        assert_eq!(result, "socks5://1.2.3.4:1080");
    }

    #[test]
    fn normalize_socks5_to_socks5h_when_prefer_remote() {
        let result = ProxyManager::normalize_proxy_url("socks5://1.2.3.4:1080", true);
        assert_eq!(result, "socks5h://1.2.3.4:1080");
    }

    #[test]
    fn normalize_socks5_stays_when_not_prefer_remote() {
        let result = ProxyManager::normalize_proxy_url("socks5://1.2.3.4:1080", false);
        assert_eq!(result, "socks5://1.2.3.4:1080");
    }

    #[test]
    fn normalize_socks5h_unchanged() {
        let result = ProxyManager::normalize_proxy_url("socks5h://1.2.3.4:1080", true);
        assert_eq!(result, "socks5h://1.2.3.4:1080");
    }

    #[test]
    fn normalize_socks5h_unchanged_no_prefer() {
        let result = ProxyManager::normalize_proxy_url("socks5h://1.2.3.4:1080", false);
        assert_eq!(result, "socks5h://1.2.3.4:1080");
    }

    #[test]
    fn normalize_empty_string() {
        let result = ProxyManager::normalize_proxy_url("", true);
        assert!(result.is_empty());
    }

    #[test]
    fn normalize_whitespace_only() {
        let result = ProxyManager::normalize_proxy_url("   ", true);
        assert!(result.is_empty());
    }

    #[test]
    fn normalize_comment_line() {
        let result = ProxyManager::normalize_proxy_url("# this is a comment", true);
        assert!(result.is_empty());
    }

    #[test]
    fn normalize_trims_whitespace() {
        let result = ProxyManager::normalize_proxy_url("  1.2.3.4:1080  ", true);
        assert_eq!(result, "socks5h://1.2.3.4:1080");
    }

    #[test]
    fn normalize_http_proxy_passed_through() {
        let result = ProxyManager::normalize_proxy_url("http://proxy.example.com:8080", true);
        assert_eq!(result, "http://proxy.example.com:8080");
    }

    #[test]
    fn normalize_https_proxy_passed_through() {
        let result = ProxyManager::normalize_proxy_url("https://proxy.example.com:8080", false);
        assert_eq!(result, "https://proxy.example.com:8080");
    }

    #[test]
    fn normalize_socks5_with_auth() {
        let result = ProxyManager::normalize_proxy_url("socks5://user:pass@1.2.3.4:1080", true);
        assert_eq!(result, "socks5h://user:pass@1.2.3.4:1080");
    }

    // -----------------------------------------------------------------------
    // ProxyManager::new
    // -----------------------------------------------------------------------

    // -----------------------------------------------------------------------
    // State management
    // -----------------------------------------------------------------------

    #[test]
    fn get_state_returns_unknown_for_missing_proxy() {
        let mgr = ProxyManager::new(Duration::from_secs(5));
        assert_eq!(mgr.get_state("socks5h://9.9.9.9:1080"), ProxyState::Unknown);
    }

    #[test]
    fn set_and_get_state() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        pm.proxies.insert(
            "socks5h://1.2.3.4:1080".to_string(),
            ProxyInfo {
                state: ProxyState::Unknown,
            },
        );

        assert_eq!(pm.get_state("socks5h://1.2.3.4:1080"), ProxyState::Unknown);

        pm.set_state("socks5h://1.2.3.4:1080", ProxyState::Active);
        assert_eq!(pm.get_state("socks5h://1.2.3.4:1080"), ProxyState::Active);

        pm.set_state("socks5h://1.2.3.4:1080", ProxyState::Dead);
        assert_eq!(pm.get_state("socks5h://1.2.3.4:1080"), ProxyState::Dead);
    }

    #[test]
    fn set_state_on_missing_proxy_is_noop() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        pm.set_state("socks5h://nonexistent:1080", ProxyState::Dead);
        assert_eq!(pm.total_count(), 0);
    }

    // -----------------------------------------------------------------------
    // get_active_proxies
    // -----------------------------------------------------------------------

    #[test]
    fn get_active_proxies_excludes_dead() {
        let mgr = ProxyManager::new(Duration::from_secs(5));

        // Manually insert some proxies.
        mgr.proxies.insert(
            "socks5h://1.1.1.1:1080".into(),
            ProxyInfo {
                state: ProxyState::Active,
            },
        );
        mgr.proxies.insert(
            "socks5h://2.2.2.2:1080".into(),
            ProxyInfo {
                state: ProxyState::Dead,
            },
        );
        mgr.proxies.insert(
            "socks5h://3.3.3.3:1080".into(),
            ProxyInfo {
                state: ProxyState::Unknown,
            },
        );

        let active = mgr.get_active_proxies();
        assert_eq!(active.len(), 2);
        assert!(active.contains(&"socks5h://1.1.1.1:1080".to_string()));
        assert!(!active.contains(&"socks5h://2.2.2.2:1080".to_string()));
        assert!(active.contains(&"socks5h://3.3.3.3:1080".to_string()));
    }

    // -----------------------------------------------------------------------
    // proxy_counts
    // -----------------------------------------------------------------------

    #[test]
    fn proxy_counts_correct() {
        let pm = ProxyManager::new(Duration::from_secs(8));

        pm.proxies.insert(
            "a".to_string(),
            ProxyInfo {
                state: ProxyState::Active,
            },
        );
        pm.proxies.insert(
            "b".to_string(),
            ProxyInfo {
                state: ProxyState::Unknown,
            },
        );
        pm.proxies.insert(
            "c".to_string(),
            ProxyInfo {
                state: ProxyState::Dead,
            },
        );
        pm.proxies.insert(
            "d".to_string(),
            ProxyInfo {
                state: ProxyState::Dead,
            },
        );

        let (total, active_or_unknown, cooldown, dead, retired) = pm.proxy_counts();
        assert_eq!(total, 4);
        assert_eq!(active_or_unknown, 2);
        assert_eq!(cooldown, 0);
        assert_eq!(dead, 2);
        assert_eq!(retired, 0);
    }

    #[test]
    fn proxy_counts_empty() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        let (total, active, cooldown, dead, retired) = pm.proxy_counts();
        assert_eq!(total, 0);
        assert_eq!(active, 0);
        assert_eq!(cooldown, 0);
        assert_eq!(dead, 0);
        assert_eq!(retired, 0);
    }

    // -----------------------------------------------------------------------
    // get_client
    // -----------------------------------------------------------------------

    #[test]
    fn get_client_creates_and_caches() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        let client1 = pm.get_client("socks5h://127.0.0.1:1080").unwrap();
        let client2 = pm.get_client("socks5h://127.0.0.1:1080").unwrap();

        // Both should be the same cached client (we can't compare reqwest::Client
        // directly, but we can verify the cache contains one entry).
        assert_eq!(pm.clients.len(), 1);

        // Ensure we actually got clients back.
        drop(client1);
        drop(client2);
    }

    #[test]
    fn get_client_different_proxies_get_different_clients() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        let _c1 = pm.get_client("socks5h://127.0.0.1:1080").unwrap();
        let _c2 = pm.get_client("socks5h://127.0.0.1:1081").unwrap();
        assert_eq!(pm.clients.len(), 2);
    }

    #[test]
    fn get_client_invalid_url_returns_error() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        let result = pm.get_client("not a valid url at all");
        // reqwest may or may not reject this; if it does, we expect an Init error.
        // If reqwest is lenient, the test still passes.
        if let Err(e) = result {
            match e {
                ScatterProxyError::Init(msg) => {
                    assert!(msg.contains("proxy") || msg.contains("invalid"));
                }
                other => panic!("expected Init error, got: {other:?}"),
            }
        }
    }

    // -----------------------------------------------------------------------
    // all_proxy_urls
    // -----------------------------------------------------------------------

    #[test]
    fn all_proxy_urls_returns_all() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        pm.proxies.insert(
            "a".to_string(),
            ProxyInfo {
                state: ProxyState::Active,
            },
        );
        pm.proxies.insert(
            "b".to_string(),
            ProxyInfo {
                state: ProxyState::Dead,
            },
        );

        let urls = pm.all_proxy_urls();
        assert_eq!(urls.len(), 2);
        assert!(urls.contains(&"a".to_string()));
        assert!(urls.contains(&"b".to_string()));
    }

    // -----------------------------------------------------------------------
    // total_count
    // -----------------------------------------------------------------------

    #[test]
    fn total_count_tracks_insertions() {
        let mgr = ProxyManager::new(Duration::from_secs(5));
        assert_eq!(mgr.total_count(), 0);

        mgr.proxies.insert(
            "socks5h://1.1.1.1:1080".into(),
            ProxyInfo {
                state: ProxyState::Active,
            },
        );
        assert_eq!(mgr.total_count(), 1);

        mgr.proxies.insert(
            "socks5h://2.2.2.2:1080".into(),
            ProxyInfo {
                state: ProxyState::Dead,
            },
        );
        assert_eq!(mgr.total_count(), 2);
    }

    // -----------------------------------------------------------------------
    // ProxyState enum
    // -----------------------------------------------------------------------

    #[test]
    fn proxy_state_debug() {
        assert_eq!(format!("{:?}", ProxyState::Unknown), "Unknown");
        assert_eq!(format!("{:?}", ProxyState::Active), "Active");
        assert_eq!(format!("{:?}", ProxyState::Dead), "Dead");
        assert_eq!(format!("{:?}", ProxyState::Retired), "Retired");
    }

    #[test]
    fn proxy_state_clone_and_eq() {
        let s = ProxyState::Active;
        let s2 = s;
        assert_eq!(s, s2);
    }

    // -----------------------------------------------------------------------
    // ProxyState::Retired
    // -----------------------------------------------------------------------

    #[test]
    fn set_state_to_retired() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        pm.proxies.insert(
            "socks5h://1.2.3.4:1080".to_string(),
            ProxyInfo {
                state: ProxyState::Active,
            },
        );
        pm.set_state("socks5h://1.2.3.4:1080", ProxyState::Retired);
        assert_eq!(pm.get_state("socks5h://1.2.3.4:1080"), ProxyState::Retired);
    }

    #[test]
    fn get_active_proxies_excludes_retired() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        pm.proxies.insert(
            "a".to_string(),
            ProxyInfo {
                state: ProxyState::Active,
            },
        );
        pm.proxies.insert(
            "b".to_string(),
            ProxyInfo {
                state: ProxyState::Retired,
            },
        );
        pm.proxies.insert(
            "c".to_string(),
            ProxyInfo {
                state: ProxyState::Unknown,
            },
        );
        let active = pm.get_active_proxies();
        assert_eq!(active.len(), 2);
        assert!(active.contains(&"a".to_string()));
        assert!(!active.contains(&"b".to_string()));
        assert!(active.contains(&"c".to_string()));
    }

    #[test]
    fn proxy_counts_includes_retired() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        pm.proxies.insert(
            "a".to_string(),
            ProxyInfo {
                state: ProxyState::Active,
            },
        );
        pm.proxies.insert(
            "b".to_string(),
            ProxyInfo {
                state: ProxyState::Retired,
            },
        );
        pm.proxies.insert(
            "c".to_string(),
            ProxyInfo {
                state: ProxyState::Dead,
            },
        );

        let (total, active, cooldown, dead, retired) = pm.proxy_counts();
        assert_eq!(total, 3);
        assert_eq!(active, 1);
        assert_eq!(cooldown, 0);
        assert_eq!(dead, 1);
        assert_eq!(retired, 1);
    }

    // -----------------------------------------------------------------------
    // Per-proxy cookie management
    // -----------------------------------------------------------------------

    #[test]
    fn get_cookie_returns_none_when_not_set() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        assert!(pm.get_cookie("socks5h://1.2.3.4:1080").is_none());
    }

    #[test]
    fn set_and_get_cookie() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        pm.set_cookie("socks5h://1.2.3.4:1080", "abc123".to_string());
        assert_eq!(
            pm.get_cookie("socks5h://1.2.3.4:1080"),
            Some("abc123".to_string())
        );
    }

    #[test]
    fn set_cookie_overwrites_existing() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        pm.set_cookie("socks5h://1.2.3.4:1080", "first".to_string());
        pm.set_cookie("socks5h://1.2.3.4:1080", "second".to_string());
        assert_eq!(
            pm.get_cookie("socks5h://1.2.3.4:1080"),
            Some("second".to_string())
        );
    }

    #[test]
    fn cookies_are_per_proxy() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        pm.set_cookie("socks5h://1.1.1.1:1080", "cookie_a".to_string());
        pm.set_cookie("socks5h://2.2.2.2:1080", "cookie_b".to_string());
        assert_eq!(
            pm.get_cookie("socks5h://1.1.1.1:1080"),
            Some("cookie_a".to_string())
        );
        assert_eq!(
            pm.get_cookie("socks5h://2.2.2.2:1080"),
            Some("cookie_b".to_string())
        );
    }

    // -----------------------------------------------------------------------
    // Per-proxy request counting
    // -----------------------------------------------------------------------

    #[test]
    fn request_count_starts_at_zero() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        assert_eq!(pm.request_count("socks5h://1.2.3.4:1080"), 0);
    }

    #[test]
    fn increment_request_count_increments_correctly() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        assert_eq!(pm.increment_request_count("socks5h://1.2.3.4:1080"), 1);
        assert_eq!(pm.increment_request_count("socks5h://1.2.3.4:1080"), 2);
        assert_eq!(pm.increment_request_count("socks5h://1.2.3.4:1080"), 3);
        assert_eq!(pm.request_count("socks5h://1.2.3.4:1080"), 3);
    }

    #[test]
    fn request_counts_are_per_proxy() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        pm.increment_request_count("socks5h://1.1.1.1:1080");
        pm.increment_request_count("socks5h://1.1.1.1:1080");
        pm.increment_request_count("socks5h://2.2.2.2:1080");
        assert_eq!(pm.request_count("socks5h://1.1.1.1:1080"), 2);
        assert_eq!(pm.request_count("socks5h://2.2.2.2:1080"), 1);
    }

    #[test]
    fn reset_request_count_clears_count() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        pm.increment_request_count("socks5h://1.2.3.4:1080");
        pm.increment_request_count("socks5h://1.2.3.4:1080");
        pm.reset_request_count("socks5h://1.2.3.4:1080");
        assert_eq!(pm.request_count("socks5h://1.2.3.4:1080"), 0);
    }

    #[test]
    fn reset_request_count_restores_retired_to_active() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        pm.proxies.insert(
            "socks5h://1.2.3.4:1080".to_string(),
            ProxyInfo {
                state: ProxyState::Retired,
            },
        );
        pm.increment_request_count("socks5h://1.2.3.4:1080");
        pm.reset_request_count("socks5h://1.2.3.4:1080");
        assert_eq!(pm.get_state("socks5h://1.2.3.4:1080"), ProxyState::Active);
        assert_eq!(pm.request_count("socks5h://1.2.3.4:1080"), 0);
    }

    #[test]
    fn reset_request_count_does_not_restore_dead() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        pm.proxies.insert(
            "socks5h://1.2.3.4:1080".to_string(),
            ProxyInfo {
                state: ProxyState::Dead,
            },
        );
        pm.reset_request_count("socks5h://1.2.3.4:1080");
        assert_eq!(pm.get_state("socks5h://1.2.3.4:1080"), ProxyState::Dead);
    }

    // -----------------------------------------------------------------------
    // Challenge lock
    // -----------------------------------------------------------------------

    #[test]
    fn challenge_lock_returns_semaphore_with_one_permit() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        let sem = pm.challenge_lock("socks5h://1.2.3.4:1080");
        assert_eq!(sem.available_permits(), 1);
    }

    #[test]
    fn challenge_lock_same_proxy_returns_same_semaphore() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        let sem1 = pm.challenge_lock("socks5h://1.2.3.4:1080");
        let sem2 = pm.challenge_lock("socks5h://1.2.3.4:1080");
        // Both Arc references point to the same semaphore instance.
        assert!(std::ptr::eq(sem1.as_ref(), sem2.as_ref()));
    }

    // -----------------------------------------------------------------------
    // fetch_and_add (unit tests with no real HTTP server)
    // -----------------------------------------------------------------------

    #[tokio::test]
    async fn fetch_and_add_with_empty_sources() {
        let mgr = ProxyManager::new(Duration::from_secs(5));
        let added = mgr.fetch_and_add(&[], true).await.unwrap();
        assert_eq!(added, 0);
        assert_eq!(mgr.total_count(), 0);
    }

    #[tokio::test]
    async fn fetch_and_add_with_unreachable_source_logs_error() {
        let pm = ProxyManager::new(Duration::from_secs(8));
        // This source won't be reachable but should not cause a hard failure.
        let added = pm
            .fetch_and_add(
                &["http://127.0.0.1:1/nonexistent-proxy-list".to_string()],
                true,
            )
            .await
            .unwrap();
        assert_eq!(added, 0);
        assert_eq!(pm.total_count(), 0);
    }

    // -----------------------------------------------------------------------
    // normalize edge cases
    // -----------------------------------------------------------------------

    #[test]
    fn normalize_comment_with_leading_whitespace() {
        // After trimming, starts with '#'
        let result = ProxyManager::normalize_proxy_url("  # comment", true);
        assert!(result.is_empty());
    }

    #[test]
    fn normalize_ipv6_bare() {
        let result = ProxyManager::normalize_proxy_url("[::1]:1080", true);
        assert_eq!(result, "socks5h://[::1]:1080");
    }

    #[test]
    fn normalize_socks5_uppercase_not_matched_becomes_bare() {
        // "SOCKS5://..." doesn't match the lowercase prefix, so it's treated as bare.
        let result = ProxyManager::normalize_proxy_url("SOCKS5://1.2.3.4:1080", true);
        assert_eq!(result, "socks5h://SOCKS5://1.2.3.4:1080");
    }
}