proxybroker 0.4.2

Find, check, and serve public HTTP(S) and SOCKS4/5 proxies. Library + CLI.
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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
//! The `proxybroker` CLI — a thin shell over the library.
//!
//! - `grab` — scrape providers, no checking.
//! - `find` — scrape, check, and classify anonymity.
//! - `serve` — run a local rotating proxy server (requires the `server` feature).

use clap::{Parser, Subcommand, ValueEnum};
use futures_util::StreamExt;
use proxybroker::broker::{Broker, FindQuery, GrabQuery};
use proxybroker::types::{AnonLevel, ParseProtoError, Proto, TypeSpec};
use proxybroker::{Proxy, ProxyError, RetryPolicy};
use std::collections::HashSet;
use std::io::Write;
use std::path::PathBuf;
use std::time::Duration;
use tokio::io::AsyncWriteExt;

/// clap value parsers via the types' own `FromStr` (keeps clap out of the library's
/// always-compiled `types.rs`).
fn parse_proto(s: &str) -> Result<Proto, ParseProtoError> {
    s.parse()
}
fn parse_lvl(s: &str) -> Result<AnonLevel, ParseProtoError> {
    s.parse()
}

/// Shown in `--version`. The DB-IP attribution is required by CC BY 4.0 whenever the geo
/// data is bundled — see `LICENSE-DATA`.
const VERSION: &str = concat!(
    env!("CARGO_PKG_VERSION"),
    "\nIP Geolocation by DB-IP (https://db-ip.com), licensed CC BY 4.0"
);

#[derive(Parser)]
#[command(
    name = "proxybroker",
    version = VERSION,
    about = "Find, check, and serve public HTTP(S) and SOCKS4/5 proxies.",
    long_about = "Find, check, and serve public HTTP(S) and SOCKS4/5 proxies.\n\n\
        A Rust rewrite of proxybroker2. Geo data: DB-IP Country Lite (CC BY 4.0)."
)]
struct Cli {
    /// Log level (error, warn, info, debug, trace).
    #[arg(long, global = true, default_value = "warn")]
    log: String,

    /// Log output format: text (default) or json (line-delimited, for a log pipeline).
    #[arg(long, global = true, value_enum, default_value_t = LogFormat::Text)]
    log_format: LogFormat,

    /// Path to a MaxMind-format country database, overriding the bundled DB-IP one.
    #[arg(long, global = true, value_name = "PATH")]
    geo_db: Option<PathBuf>,

    /// Path to a MaxMind-format ASN database (e.g. GeoLite2-ASN.mmdb) to attribute each proxy to
    /// its Autonomous System. Off unless given — no ASN data is bundled.
    #[arg(long, global = true, value_name = "PATH")]
    asn_db: Option<PathBuf>,

    /// Load extra providers from YAML/JSON configs in this directory (appended to the
    /// bundled set). May be repeated. Pass --providers-only to use ONLY these.
    #[arg(long, global = true, value_name = "DIR")]
    provider_dir: Vec<PathBuf>,

    /// Use only the --provider-dir providers, ignoring the bundled registry.
    #[arg(long, global = true)]
    providers_only: bool,

    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Gather proxies from the providers without checking them.
    Grab(GrabArgs),
    /// Gather proxies and check that they work, classifying anonymity.
    Find(FindArgs),
    /// Check a list of proxies you already have (from stdin or --infile).
    Check(CheckArgs),
    /// Run a local proxy server that rotates through working proxies.
    #[cfg(feature = "server")]
    Serve(ServeArgs),
    /// Serve the live pool over MCP (stdio): get_proxy, pool_status, report_dead.
    #[cfg(feature = "mcp")]
    Mcp(McpArgs),
    /// Live terminal dashboard: a sortable pool table + latency sparklines (F4). Keep --log at the
    /// default `warn` — higher levels log to stderr over the dashboard.
    #[cfg(feature = "tui")]
    Top(TopArgs),
}

/// CLI mirror of [`proxybroker::server::Strategy`] (keeps clap's `ValueEnum` out of the library).
#[cfg(feature = "server")]
#[derive(Clone, Copy, Default, ValueEnum)]
enum SelectStrategy {
    /// Lowest error rate then fastest response.
    #[default]
    Best,
    /// Rotate through eligible proxies in order.
    RoundRobin,
    /// Uniform random pick.
    Random,
    /// Pin each client to one upstream (by IP, or --sticky-header).
    Sticky,
}

#[cfg(feature = "server")]
impl SelectStrategy {
    fn to_server(self) -> proxybroker::server::Strategy {
        use proxybroker::server::Strategy;
        match self {
            SelectStrategy::Best => Strategy::Best,
            SelectStrategy::RoundRobin => Strategy::RoundRobin,
            SelectStrategy::Random => Strategy::Random,
            SelectStrategy::Sticky => Strategy::Sticky,
        }
    }
}

#[cfg(feature = "server")]
#[derive(clap::Args, Default)]
struct ServeArgs {
    /// Address to listen on.
    #[arg(long, default_value = "127.0.0.1:8888")]
    host: String,

    /// Protocols to find for the pool. Required unless --load.
    #[arg(long, num_args = 1.., required_unless_present = "load", value_name = "TYPE", value_parser = parse_proto)]
    types: Vec<Proto>,

    /// Fill the pool from an NDJSON file of already-checked proxies (from a prior --save) instead
    /// of finding fresh ones. The pool serves these, then drains (no top-up).
    #[arg(long, value_name = "PATH", conflicts_with = "types")]
    load: Option<PathBuf>,

    /// Anonymity levels to accept for HTTP (e.g. High Anonymous). Default: any.
    #[arg(long, num_args = 1.., value_name = "LVL", value_parser = parse_lvl)]
    lvl: Vec<AnonLevel>,

    /// DNS blocklist zones; reject proxies listed in any (e.g. zen.spamhaus.org).
    #[arg(long, num_args = 1.., value_name = "ZONE")]
    dnsbl: Vec<String>,

    /// Use POST instead of GET for the pool-fill test request.
    #[arg(long)]
    post: bool,

    /// Require the anonymity level to match exactly.
    #[arg(long)]
    strict: bool,

    /// How to pick an upstream per request.
    #[arg(long, value_enum, default_value_t = SelectStrategy::Best)]
    strategy: SelectStrategy,

    /// With --strategy sticky, key the session on this request header instead of the client IP
    /// (HTTP requests only).
    #[arg(long, value_name = "HEADER")]
    sticky_header: Option<String>,

    /// Keep the pool topped up to this many working proxies.
    #[arg(long, default_value_t = 100)]
    limit: usize,

    /// Keep only proxies located in these ISO country codes.
    #[arg(long, visible_alias = "only-cc", num_args = 1.., value_delimiter = ',', value_name = "CC")]
    countries: Vec<String>,

    /// Per-request timeout in seconds.
    #[arg(long, default_value_t = 8)]
    timeout: u64,

    /// Drop a proxy once its error rate exceeds this (0.0–1.0).
    #[arg(long, default_value_t = 0.5)]
    max_error_rate: f64,

    /// Drop a proxy once its average response time (seconds) exceeds this.
    #[arg(long, default_value_t = 8.0)]
    max_resp_time: f64,

    /// Seconds a proxy is benched after a failure before it is re-probed.
    #[arg(long, default_value_t = 30)]
    fail_timeout: u64,

    /// Prefer proxies that support CONNECT:80 when otherwise equally ranked.
    #[arg(long)]
    prefer_connect: bool,

    /// Retry through another proxy when the upstream HTTP status is outside this set (e.g. 200 204
    /// 301 302), to dodge block pages. Empty = accept any status. HTTP requests only.
    #[arg(long, num_args = 1.., value_name = "CODE")]
    http_allowed_codes: Vec<u16>,

    /// Wait until the pool holds at least this many proxies before accepting clients.
    #[arg(long, default_value_t = 0)]
    min_queue: usize,

    /// TCP listen backlog (queued pending connections).
    #[arg(long, default_value_t = 1024)]
    backlog: u32,

    /// Maximum concurrently served client connections; beyond this, new connections wait in the
    /// backlog. Bounds descriptor/memory use against a client that opens and holds many sockets.
    #[arg(long, default_value_t = 1024)]
    max_clients: usize,

    /// Require clients to authenticate with `Proxy-Authorization: Basic base64(user:pass)` (also
    /// gates the SOCKS5 front-end via RFC 1929). Absent = open server.
    #[arg(long, value_name = "USER:PASS")]
    auth: Option<String>,

    /// Attempts (with different proxies) per client request.
    #[arg(long, default_value_t = 3)]
    max_tries: usize,

    /// Serve a Prometheus text metrics endpoint on this address (F1).
    #[cfg(feature = "metrics")]
    #[arg(long, value_name = "ADDR")]
    metrics: Option<std::net::SocketAddr>,

    /// Remember proxies across runs — warm-starts the pool from stored history and folds each fresh
    /// check back in. A file path uses SQLite (`store-sqlite`); a `redis://`/`rediss://` URL uses
    /// Redis (`store-redis`, Wave 9).
    #[arg(long, value_name = "PATH_OR_URL")]
    state: Option<String>,

    /// Adaptively re-check pooled proxies on a cadence proportional to their stability, keeping the
    /// pool fresh without re-running find (D3; needs the `persist` feature). Folds scores into
    /// `--state` when given; otherwise keeps them in memory (reset on restart).
    #[arg(long)]
    recheck: bool,

    /// Global re-check ceiling, checks/sec (the IP-block guard).
    #[arg(long, default_value_t = 5.0)]
    recheck_rate: f64,

    /// Shortest re-check cadence, seconds (a flaky proxy).
    #[arg(long, default_value_t = 60)]
    recheck_min: u64,

    /// Longest re-check cadence, seconds (a rock-solid proxy).
    #[arg(long, default_value_t = 3600)]
    recheck_max: u64,

    /// Score half-life for an unseen proxy, seconds.
    #[arg(long, default_value_t = 21600)]
    decay_halflife: u64,

    /// Live-reload the --load file: apply additions/removals to the running pool without a restart
    /// (E3; requires --load and the `watch` build feature).
    #[arg(long)]
    watch: bool,
}

#[derive(clap::Args)]
struct GrabArgs {
    /// Stop after this many proxies. 0 means unlimited.
    #[arg(long, default_value_t = 0)]
    limit: usize,

    /// Keep only proxies located in these ISO country codes (e.g. US GB DE).
    #[arg(long, visible_alias = "only-cc", num_args = 1.., value_delimiter = ',', value_name = "CC")]
    countries: Vec<String>,

    /// Output format.
    #[arg(long, value_enum, default_value_t = Format::Default)]
    format: Format,

    /// Render each proxy through this template instead — overrides --format. Tokens: {{proxy}}
    /// {{host}} {{port}} {{scheme}} {{protocols}} {{anon}} {{country}} {{asn}} {{asn_org}} {{duration}} {{error_rate}};
    /// unknown tokens pass through literally.
    #[arg(long, value_name = "TEMPLATE")]
    output_format: Option<String>,

    /// Write to this file instead of stdout.
    #[arg(long, value_name = "PATH")]
    outfile: Option<PathBuf>,
}

#[derive(clap::Args)]
struct FindArgs {
    /// Protocols to check (required). E.g. --types HTTP HTTPS SOCKS5 CONNECT:80.
    #[arg(long, num_args = 1.., required = true, value_name = "TYPE", value_parser = parse_proto)]
    types: Vec<Proto>,

    /// Anonymity levels to accept for HTTP (e.g. High Anonymous). Default: any.
    #[arg(long, num_args = 1.., value_name = "LVL", value_parser = parse_lvl)]
    lvl: Vec<AnonLevel>,

    /// Stop after this many working proxies. 0 means unlimited.
    #[arg(long, default_value_t = 0)]
    limit: usize,

    /// Keep only proxies located in these ISO country codes.
    #[arg(long, visible_alias = "only-cc", num_args = 1.., value_delimiter = ',', value_name = "CC")]
    countries: Vec<String>,

    /// Judge URLs to use instead of the bundled defaults.
    #[arg(long, num_args = 1.., value_name = "URL")]
    judges: Vec<String>,

    /// DNS blocklist zones; reject proxies listed in any (e.g. zen.spamhaus.org).
    #[arg(long, num_args = 1.., value_name = "ZONE")]
    dnsbl: Vec<String>,

    /// Per-request timeout in seconds.
    #[arg(long, default_value_t = 8)]
    timeout: u64,

    /// Maximum concurrent checks.
    #[arg(long, default_value_t = 200)]
    max_conn: usize,

    /// Attempts per protocol before giving up.
    #[arg(long, default_value_t = 3)]
    max_tries: usize,

    /// Use POST instead of GET for the test request.
    #[arg(long)]
    post: bool,

    /// Require the anonymity level to match exactly.
    #[arg(long)]
    strict: bool,

    /// Fallback endpoint to probe when no judge verifies. Enables graceful degradation; proxies
    /// confirmed via liveness report anonymity "None" (so combining it with --lvl yields nothing).
    #[arg(long, value_name = "URL")]
    liveness_url: Option<String>,

    /// Which errors trigger a retry: timeout (default), transient, or all.
    #[arg(long, value_enum, default_value_t = RetryOn::Timeout)]
    retry_on: RetryOn,

    /// Base backoff before a retry, in milliseconds (0 = no delay).
    #[arg(long, default_value_t = 0)]
    backoff_ms: u64,

    /// Accept proxies that forward the request (marker+IP) even if they strip Referer/Cookie,
    /// recording what they pass through as capabilities.
    #[arg(long)]
    relaxed_validity: bool,

    /// Keep only proxies that pass our Cookie header through (implies richer signal under
    /// --relaxed-validity).
    #[arg(long)]
    require_cookie: bool,

    /// Keep only proxies that pass our Referer header through.
    #[arg(long)]
    require_referer: bool,

    /// Keep only proxies with a confirmed CONNECT:25 (SMTP) tunnel.
    #[arg(long)]
    require_connect25: bool,

    /// Run honeypot detection on each proxy and record the verdict. The injected-header scan needs
    /// a judge that echoes raw request headers (Name: value); the bundled judges do not, so pair it
    /// with a raw-header-echo judge via --judges for real detection.
    #[arg(long)]
    trust_check: bool,

    /// Keep only proxies whose trust verdict is clean (implies --trust-check).
    #[arg(long)]
    require_trusted: bool,

    /// Print an aggregate summary (by protocol/anonymity/country) to stderr when done.
    #[arg(long)]
    show_stats: bool,

    /// Format for the --show-stats summary (stderr): text (default) or json. Inert without
    /// --show-stats.
    #[arg(long, value_enum, default_value_t = StatsFormat::Text)]
    stats_format: StatsFormat,

    /// Output format.
    #[arg(long, value_enum, default_value_t = Format::Default)]
    format: Format,

    /// Render each proxy through this template instead — overrides --format. Tokens: {{proxy}}
    /// {{host}} {{port}} {{scheme}} {{protocols}} {{anon}} {{country}} {{asn}} {{asn_org}} {{duration}} {{error_rate}};
    /// unknown tokens pass through literally.
    #[arg(long, value_name = "TEMPLATE")]
    output_format: Option<String>,

    /// Write to this file instead of stdout.
    #[arg(long, value_name = "PATH")]
    outfile: Option<PathBuf>,

    /// Also append every working proxy as NDJSON to this file (for `check --load` / `serve
    /// --load`). Independent of --format/--outfile.
    #[arg(long, value_name = "PATH")]
    save: Option<PathBuf>,

    /// Show a live progress bar (checked / working / avg) on stderr during find (F2; renders only
    /// when built with the `progress` feature).
    #[arg(long)]
    progress: bool,

    /// Remember proxies across runs in a SQLite DB at this path — each checked proxy is folded into
    /// its durable history. File path → SQLite (`store-sqlite`); `redis://` URL → Redis
    /// (`store-redis`).
    #[arg(long, value_name = "PATH_OR_URL")]
    state: Option<String>,
}

#[derive(clap::Args)]
struct CheckArgs {
    /// Protocols to check. E.g. --types HTTP HTTPS SOCKS5 CONNECT:80. Required unless --load.
    #[arg(long, num_args = 1.., required_unless_present = "load", value_name = "TYPE", value_parser = parse_proto)]
    types: Vec<Proto>,

    /// Read `host:port` addresses from this file instead of stdin.
    #[arg(long, value_name = "PATH")]
    infile: Option<PathBuf>,

    /// Load already-checked proxies from an NDJSON file (from a prior --save) and emit them
    /// WITHOUT re-checking. Stats restart from empty (a warm start, not a resumed history).
    #[arg(long, value_name = "PATH", conflicts_with_all = ["infile", "types"])]
    load: Option<PathBuf>,

    /// Anonymity levels to accept for HTTP (e.g. High Anonymous). Default: any.
    #[arg(long, num_args = 1.., value_name = "LVL", value_parser = parse_lvl)]
    lvl: Vec<AnonLevel>,

    /// Stop after this many working proxies. 0 means unlimited.
    #[arg(long, default_value_t = 0)]
    limit: usize,

    /// Keep only proxies located in these ISO country codes.
    #[arg(long, visible_alias = "only-cc", num_args = 1.., value_delimiter = ',', value_name = "CC")]
    countries: Vec<String>,

    /// Judge URLs to use instead of the bundled defaults.
    #[arg(long, num_args = 1.., value_name = "URL")]
    judges: Vec<String>,

    /// DNS blocklist zones; reject proxies listed in any (e.g. zen.spamhaus.org).
    #[arg(long, num_args = 1.., value_name = "ZONE")]
    dnsbl: Vec<String>,

    /// Per-request timeout in seconds.
    #[arg(long, default_value_t = 8)]
    timeout: u64,

    /// Maximum concurrent checks.
    #[arg(long, default_value_t = 200)]
    max_conn: usize,

    /// Attempts per protocol before giving up.
    #[arg(long, default_value_t = 3)]
    max_tries: usize,

    /// Use POST instead of GET for the test request.
    #[arg(long)]
    post: bool,

    /// Require the anonymity level to match exactly.
    #[arg(long)]
    strict: bool,

    /// Print an aggregate summary to stderr when done.
    #[arg(long)]
    show_stats: bool,

    /// Format for the --show-stats summary (stderr): text (default) or json. Inert without
    /// --show-stats.
    #[arg(long, value_enum, default_value_t = StatsFormat::Text)]
    stats_format: StatsFormat,

    /// Output format.
    #[arg(long, value_enum, default_value_t = Format::Default)]
    format: Format,

    /// Render each proxy through this template instead — overrides --format. Tokens: {{proxy}}
    /// {{host}} {{port}} {{scheme}} {{protocols}} {{anon}} {{country}} {{asn}} {{asn_org}} {{duration}} {{error_rate}};
    /// unknown tokens pass through literally.
    #[arg(long, value_name = "TEMPLATE")]
    output_format: Option<String>,

    /// Write to this file instead of stdout.
    #[arg(long, value_name = "PATH")]
    outfile: Option<PathBuf>,

    /// Also append every working proxy as NDJSON to this file (reloadable via --load). Independent
    /// of --format/--outfile.
    #[arg(long, value_name = "PATH")]
    save: Option<PathBuf>,
}

/// Log output format (F3).
#[derive(Clone, Copy, Default, ValueEnum)]
enum LogFormat {
    #[default]
    Text,
    Json,
}

/// Which errors `--retry-on` retries (A5). `factor`/`jitter`/`max_backoff` stay library-only.
#[derive(Clone, Copy, Default, ValueEnum)]
enum RetryOn {
    /// Retry only `Timeout` (parity default).
    #[default]
    Timeout,
    /// Retry the transient set: `Timeout`, `Reset`, `ConnFailed`, `EmptyRecv`.
    Transient,
    /// Retry every transient check-path error (adds `BadStatus`).
    All,
}

/// Assemble a [`RetryPolicy`] from the CLI knobs.
fn retry_policy(max_tries: usize, on: RetryOn, backoff_ms: u64) -> RetryPolicy {
    let mut p = match on {
        RetryOn::Timeout => RetryPolicy::tries(max_tries),
        RetryOn::Transient => RetryPolicy::transient(max_tries),
        RetryOn::All => RetryPolicy {
            max_tries,
            retry_on: HashSet::from([
                ProxyError::Timeout,
                ProxyError::Reset,
                ProxyError::ConnFailed,
                ProxyError::EmptyRecv,
                ProxyError::BadStatus,
            ]),
            ..Default::default()
        },
    };
    p.backoff = Duration::from_millis(backoff_ms);
    p
}

/// Format for the `--show-stats` summary (which always goes to stderr, orthogonal to `--format`).
#[derive(Clone, Copy, Default, ValueEnum)]
enum StatsFormat {
    /// The human-readable summary (unchanged).
    #[default]
    Text,
    /// A single JSON object.
    Json,
}

#[derive(Clone, Copy, ValueEnum)]
enum Format {
    /// `host:port`, one per line.
    Default,
    /// `host:port`, one per line (alias of default for grabbed proxies).
    Txt,
    /// One JSON object per line (NDJSON).
    Json,
    /// A single `[ {...}, {...} ]` JSON array document, emitted incrementally.
    JsonArray,
    /// `scheme://host:port`, one per line.
    Url,
    /// `host,port,protocols,anon,country,resp_time,error_rate`, with a header row.
    Csv,
}

/// Stateful output formatter: a one-time `prefix` (CSV header, JSON-array `[`), a per-proxy `item`
/// (which owns its own newline / array separator), and a one-time `suffix` (JSON-array `]`). One
/// source of format truth shared by both `write_stream` sink loops. Later waves' formats plug in as
/// new `Format` variants + arms here.
struct Emitter<'a> {
    format: Format,
    /// C6: when `Some`, `--output-format` template overrides `format` (always line output).
    template: Option<&'a str>,
    /// JSON-array separator state: `true` once at least one element has been emitted.
    started: bool,
}

impl<'a> Emitter<'a> {
    fn new(format: Format, template: Option<&'a str>) -> Self {
        Emitter {
            format,
            template,
            started: false,
        }
    }

    /// Bytes emitted once before the first proxy. `None` for the streaming line formats, and for a
    /// template (which is always plain line output — it ignores `json-array` wrapping).
    fn prefix(&self) -> Option<String> {
        if self.template.is_some() {
            return None;
        }
        match self.format {
            Format::JsonArray => Some("[".into()),
            Format::Csv => Some("host,port,protocols,anon,country,resp_time,error_rate\n".into()),
            Format::Default | Format::Txt | Format::Json | Format::Url => None,
        }
    }

    /// One proxy rendered as a self-contained chunk (including its trailing newline for line
    /// formats). `&mut self` so structural formats (JSON array) can track separator state.
    fn item(&mut self, proxy: &Proxy) -> String {
        if let Some(tmpl) = self.template {
            return format!("{}\n", render_template(tmpl, proxy));
        }
        match self.format {
            Format::Default | Format::Txt => format!("{}\n", proxy.addr()),
            Format::Json => format!("{}\n", serde_json::to_string(proxy).unwrap()),
            Format::Url => format!("{}://{}\n", scheme_str(proxy), proxy.addr()),
            Format::Csv => format!("{}\n", csv_row(proxy)),
            Format::JsonArray => {
                // Same per-object bytes as `Json`, wrapped: a leading `,` for every element after
                // the first, so the array streams out well-formed without buffering.
                let sep = if self.started { "," } else { "" };
                self.started = true;
                format!("{sep}{}", serde_json::to_string(proxy).unwrap())
            }
        }
    }

    /// Bytes emitted once after the last proxy. `None` for line formats and templates.
    fn suffix(&self) -> Option<String> {
        if self.template.is_some() {
            return None;
        }
        match self.format {
            Format::JsonArray => Some("]\n".into()),
            Format::Default | Format::Txt | Format::Json | Format::Url | Format::Csv => None,
        }
    }
}

/// `https` if the proxy can tunnel TLS, else `http` — the URL scheme for `--format url`. A grabbed
/// (unchecked) proxy has no confirmed types, so it falls back to `http`. `Scheme` has no
/// `Display`, so the two-arm choice is inlined here rather than widened into the library.
/// The proxy's own URL scheme for `--format url` and `{{scheme}}` — i.e. how a client dials the
/// proxy, which is what a consumer (`curl --proxy`, `requests`) needs. SOCKS proxies are `socks5`/
/// `socks4`; the whole HTTP family (`HTTP`, `HTTPS`, `CONNECT:*`) is reached over plain HTTP, so it
/// is `http`. The `HTTPS`/`CONNECT` capability describes *target* traffic the proxy can tunnel, not
/// a TLS endpoint — emitting `https://` there would tell tooling the proxy itself speaks TLS (it
/// does not) and mis-dispatch the connection.
fn scheme_str(p: &Proxy) -> &'static str {
    let protos = p.types();
    if protos.contains_key(&Proto::Socks5) {
        "socks5"
    } else if protos.contains_key(&Proto::Socks4) {
        "socks4"
    } else {
        "http"
    }
}

/// Confirmed protocols as `|`-joined wire names (never contains a comma). Shared by CSV + template.
fn proto_list(p: &Proxy) -> String {
    p.types()
        .keys()
        .map(|proto| proto.as_str())
        .collect::<Vec<_>>()
        .join("|")
}

/// The HTTP anonymity level as a wire string, or `""` if unchecked / not HTTP. Shared by CSV +
/// template.
fn anon_str(p: &Proxy) -> &'static str {
    p.types()
        .get(&Proto::Http)
        .and_then(|l| *l)
        .map(AnonLevel::as_str)
        .unwrap_or("")
}

/// The ISO country code, or `""` when geo is absent. Shared by CSV + template.
fn country_str(p: &Proxy) -> &str {
    p.geo.as_ref().map(|c| c.code.as_str()).unwrap_or("")
}

/// The ASN number as a string, or `""` when no `--asn-db` resolved it (C8). Template-only.
fn asn_str(p: &Proxy) -> String {
    p.asn
        .as_ref()
        .map(|a| a.number.to_string())
        .unwrap_or_default()
}

/// The ASN owner organization, or `""` when absent / unknown (C8). Template-only.
fn asn_org_str(p: &Proxy) -> &str {
    p.asn.as_ref().and_then(|a| a.org.as_deref()).unwrap_or("")
}

/// Render a proxy through a `--output-format` template (C6). A **closed** token set replaced
/// sequentially — tokens are distinct non-overlapping literals, so `str::replace` per token is
/// correct without a parser. Unknown `{{...}}` tokens are left literally (predictable, config-free).
fn render_template(tmpl: &str, p: &Proxy) -> String {
    tmpl.replace("{{proxy}}", &p.addr())
        .replace("{{host}}", &p.host.to_string())
        .replace("{{port}}", &p.port.to_string())
        .replace("{{scheme}}", scheme_str(p))
        .replace("{{protocols}}", &proto_list(p))
        .replace("{{anon}}", anon_str(p))
        .replace("{{country}}", country_str(p))
        .replace("{{asn}}", &asn_str(p))
        .replace("{{asn_org}}", asn_org_str(p))
        .replace("{{duration}}", &p.avg_resp_time().to_string())
        .replace("{{error_rate}}", &p.error_rate().to_string())
}

/// One CSV row for `--format csv`. Every field is comma-free by construction (protocols `|`-joined,
/// ISO country code only, numeric stats), so no quoting layer is needed — a deviation guarded by
/// the `csv_header_and_row` test (it fails if a field ever gains a comma).
fn csv_row(p: &Proxy) -> String {
    format!(
        "{},{},{},{},{},{},{}",
        p.host,
        p.port,
        proto_list(p),
        anon_str(p),
        country_str(p),
        p.avg_resp_time(),
        p.error_rate()
    )
}

#[tokio::main]
async fn main() -> std::process::ExitCode {
    let cli = Cli::parse();
    init_tracing(&cli.log, matches!(cli.log_format, LogFormat::Json));

    match run(cli).await {
        Ok(()) => std::process::ExitCode::SUCCESS,
        Err(e) => {
            eprintln!("error: {e}");
            std::process::ExitCode::FAILURE
        }
    }
}

async fn run(cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
    let mut builder = Broker::builder();

    // Providers: bundled registry plus any --provider-dir configs, or ONLY the configs when
    // --providers-only is set (mirrors proxybroker2's providers=[] + provider_dirs pattern).
    if !cli.provider_dir.is_empty() || cli.providers_only {
        let mut providers = if cli.providers_only {
            Vec::new()
        } else {
            proxybroker::provider::bundled_registry()
        };
        for dir in &cli.provider_dir {
            providers.extend(proxybroker::load_provider_dir(dir));
        }
        if providers.is_empty() {
            return Err(
                "no providers: --providers-only with no valid --provider-dir configs".into(),
            );
        }
        builder = builder.providers(providers);
    }

    #[cfg(feature = "geo")]
    {
        use proxybroker::geo::GeoDb;
        let db = match &cli.geo_db {
            Some(path) => Some(GeoDb::open(path)?),
            None => GeoDb::bundled().ok(),
        };
        if let Some(db) = db {
            builder = builder.geo(db);
        }
        // C8: ASN attribution is opt-in via --asn-db (a separate database from --geo-db); nothing
        // ASN-shaped is bundled, so without the flag proxies carry no `asn`.
        if let Some(path) = &cli.asn_db {
            builder = builder.asn_db(GeoDb::open(path)?);
        }
    }

    let broker = builder.build();

    match cli.command {
        Command::Grab(args) => grab(broker, args).await,
        Command::Find(args) => find(broker, args).await,
        Command::Check(args) => check(broker, args).await,
        #[cfg(feature = "server")]
        Command::Serve(args) => serve_cmd(broker, args).await,
        #[cfg(feature = "mcp")]
        Command::Mcp(args) => mcp_cmd(broker, args).await,
        #[cfg(feature = "tui")]
        Command::Top(args) => top_cmd(broker, args).await,
    }
}

#[cfg(feature = "server")]
async fn serve_cmd(broker: Broker, args: ServeArgs) -> Result<(), Box<dyn std::error::Error>> {
    use proxybroker::resolver::Resolver;
    use proxybroker::server::{serve, Pool, PoolConfig};
    use std::sync::Arc;

    let addr: std::net::SocketAddr = args.host.parse()?;
    if args.auth.is_none() && !addr.ip().is_loopback() {
        eprintln!(
            "warning: serving on {} without --auth — anyone who can reach this address can relay \
             traffic through the pool; pass --auth USER:PASS or bind to 127.0.0.1",
            addr
        );
    }
    // D2: install the upsert observer + read warm-start history from --state.
    let (broker, history) = open_state(broker, args.state.as_deref())?;
    let pool_config = PoolConfig {
        max_tries: args.max_tries,
        max_clients: args.max_clients,
        max_error_rate: args.max_error_rate,
        max_resp_time: args.max_resp_time,
        // Uppercased allow-list so the pool screens admissions (esp. the --load path, which never
        // ran find's country filter). None when no countries requested.
        countries: (!args.countries.is_empty())
            .then(|| args.countries.iter().map(|c| c.to_uppercase()).collect()),
        strategy: args.strategy.to_server(),
        sticky_header: args.sticky_header.clone(),
        fail_timeout: Duration::from_secs(args.fail_timeout),
        prefer_connect: args.prefer_connect,
        http_allowed_codes: (!args.http_allowed_codes.is_empty())
            .then(|| args.http_allowed_codes.clone()),
        ..Default::default()
    };

    // --load: fill the pool from a saved NDJSON file (already-checked proxies) and serve those,
    // no finding. The pool drains as it is used (no top-up); stats restart from empty.
    let pool = if let Some(path) = &args.load {
        let loaded = proxybroker::read_ndjson(std::io::BufReader::new(std::fs::File::open(path)?))?;
        eprintln!("loaded {} proxies from {}", loaded.len(), path.display());
        // D2: union the file with DB history; history wins on (host,port) since it carries score.
        Pool::from_proxies(union_dedup(history, loaded), pool_config)
    } else {
        // Find proxies to fill the pool, filtered by the serve flags (types/lvl/strict/post/
        // dnsbl/countries). The flag→query mapping lives in the pure `serve_query`. D2: seed the
        // pool with stored history first, then top up from the live find.
        let stream = broker.find(serve_query(&args)).await?;
        Pool::spawn(
            futures_util::stream::iter(history).chain(stream),
            pool_config,
        )
    };
    let resolver = Arc::new(Resolver::new(Duration::from_secs(args.timeout))?);
    // F1: an optional Prometheus endpoint alongside the proxy server. Cloned before `serve` takes
    // the pool; the handle lives until shutdown.
    #[cfg(feature = "metrics")]
    let _metrics = match args.metrics {
        Some(maddr) => {
            let h = proxybroker::serve_metrics(maddr, pool.clone()).await?;
            eprintln!("metrics on {}", h.local_addr());
            Some(h)
        }
        None => None,
    };
    // D3: adaptive re-check loop. Cloned before `serve` takes the pool; the handle lives until
    // shutdown (its Drop cancels the loop). Folds each outcome into a Store: the durable `--state`
    // backend when this build has one and `--state` was given, else an in-memory `MemoryStore`
    // (score bookkeeping only, reset on exit) so `--recheck` works with no database.
    #[cfg(feature = "persist")]
    let _rechecker = if args.recheck {
        let store = recheck_store(args.state.as_deref())?;
        let checker = broker.build_checker(&serve_query(&args)).await?;
        let cfg = proxybroker::RecheckConfig {
            min_interval: Duration::from_secs(args.recheck_min),
            max_interval: Duration::from_secs(args.recheck_max),
            rate_per_sec: args.recheck_rate,
            decay_halflife: Duration::from_secs(args.decay_halflife),
        };
        eprintln!(
            "adaptive re-checking enabled (rate {}/s)",
            args.recheck_rate
        );
        Some(proxybroker::spawn_rechecker(
            pool.clone(),
            checker,
            store,
            cfg,
        ))
    } else {
        None
    };
    // E3: live-reload the --load file into the running pool. Needs --load (nothing to watch
    // otherwise). Handle lives until shutdown (its Drop stops the watcher). Cloned before `serve`
    // takes the pool.
    #[cfg(feature = "watch")]
    let _watcher = if args.watch {
        match args.load.as_ref() {
            Some(path) => {
                eprintln!("live-reloading {} on change", path.display());
                Some(proxybroker::spawn_watch(pool.clone(), path.clone())?)
            }
            None => {
                eprintln!("--watch requires --load; live-reload disabled");
                None
            }
        }
    } else {
        None
    };
    // These flags are always present (clap mishandles #[cfg]-gated fields), so warn — rather than
    // silently no-op — when the build lacks the backing feature, matching --state's behavior.
    #[cfg(not(feature = "persist"))]
    if args.recheck {
        eprintln!("--recheck requires a persistence feature; rebuild with --features persist (or store-sqlite/store-redis)");
    }
    #[cfg(not(feature = "watch"))]
    if args.watch {
        eprintln!("--watch requires the watch feature; rebuild with --features watch");
    }
    let handle = serve(
        addr,
        pool,
        resolver,
        Duration::from_secs(args.timeout),
        args.min_queue,
        args.backlog,
        args.auth,
    )
    .await?;
    eprintln!(
        "proxybroker serving on {} — Ctrl-C to stop",
        handle.local_addr()
    );

    tokio::signal::ctrl_c().await?;
    handle.shutdown();
    eprintln!("shutting down");
    Ok(())
}

/// Build the pool-fill `FindQuery` from the serve flags. Pure (no I/O, no broker) so the
/// flag→query mapping is unit-testable offline — the filtering itself runs upstream in `find`.
/// Pool-fill args for `proxybroker mcp` — the subset of `ServeArgs` an MCP pool needs.
#[cfg(feature = "mcp")]
#[derive(clap::Args)]
struct McpArgs {
    /// Protocols to find for the pool. E.g. --types HTTP HTTPS.
    #[arg(long, num_args = 1.., required = true, value_name = "TYPE", value_parser = parse_proto)]
    types: Vec<Proto>,

    /// Stop filling the pool after this many working proxies.
    #[arg(long, default_value_t = 100)]
    limit: usize,

    /// Keep only proxies located in these ISO country codes (e.g. US GB DE).
    #[arg(long, visible_alias = "only-cc", num_args = 1.., value_delimiter = ',', value_name = "CC")]
    countries: Vec<String>,

    /// Per-request timeout in seconds.
    #[arg(long, default_value_t = 8)]
    timeout: u64,
    // No --max-error-rate / --max-resp-time: those PoolConfig thresholds only gate re-admission via
    // put_ok/put_failed (server relay / connector), which the MCP handlers never call — inert here.
}

/// Fill a pool via `find` (exactly like `serve`) then serve the three MCP tools over stdio. The
/// pool fills in the background, so `get_proxy` may return null until the first proxies land.
#[cfg(feature = "mcp")]
async fn mcp_cmd(broker: Broker, args: McpArgs) -> Result<(), Box<dyn std::error::Error>> {
    use proxybroker::server::{Pool, PoolConfig};

    let pool_config = PoolConfig {
        countries: (!args.countries.is_empty())
            .then(|| args.countries.iter().map(|c| c.to_uppercase()).collect()),
        ..Default::default()
    };
    let mut b = FindQuery::builder()
        .types(types_from(args.types.clone(), Vec::new()))
        .limit(args.limit.max(1))
        .timeout(Duration::from_secs(args.timeout));
    if !args.countries.is_empty() {
        b = b.countries(args.countries.clone());
    }
    let stream = broker.find(b.build()).await?;
    let pool = Pool::spawn(stream, pool_config);
    // stderr only — stdout is the MCP JSON-RPC channel.
    eprintln!(
        "mcp: filling pool (limit {}); serving get_proxy/pool_status/report_dead over stdio",
        args.limit
    );
    proxybroker::mcp::serve_stdio(pool).await
}

/// Pool-fill args for `proxybroker top` (F4) — the `mcp` subset plus a redraw interval + warm-start.
#[cfg(feature = "tui")]
#[derive(clap::Args)]
struct TopArgs {
    /// Protocols to find for the pool. E.g. --types HTTP HTTPS.
    #[arg(long, num_args = 1.., required = true, value_name = "TYPE", value_parser = parse_proto)]
    types: Vec<Proto>,

    /// Stop filling the pool after this many working proxies.
    #[arg(long, default_value_t = 100)]
    limit: usize,

    /// Keep only proxies located in these ISO country codes (e.g. US GB DE).
    #[arg(long, visible_alias = "only-cc", num_args = 1.., value_delimiter = ',', value_name = "CC")]
    countries: Vec<String>,

    /// Per-request timeout in seconds.
    #[arg(long, default_value_t = 8)]
    timeout: u64,

    /// Dashboard redraw interval in seconds.
    #[arg(long, default_value_t = 2)]
    refresh: u64,

    /// Warm-start the pool from stored history: a file path (SQLite) or a `redis://` URL (Redis).
    #[arg(long, value_name = "PATH_OR_URL")]
    state: Option<String>,
}

/// Fill a pool via `find` (optionally warm-started from `--state`) then render the live TUI dashboard.
#[cfg(feature = "tui")]
async fn top_cmd(broker: Broker, args: TopArgs) -> Result<(), Box<dyn std::error::Error>> {
    use proxybroker::server::{Pool, PoolConfig};

    let (broker, history) = open_state(broker, args.state.as_deref())?;
    let pool_config = PoolConfig {
        countries: (!args.countries.is_empty())
            .then(|| args.countries.iter().map(|c| c.to_uppercase()).collect()),
        ..Default::default()
    };
    let mut b = FindQuery::builder()
        .types(types_from(args.types.clone(), Vec::new()))
        .limit(args.limit.max(1))
        .timeout(Duration::from_secs(args.timeout));
    if !args.countries.is_empty() {
        b = b.countries(args.countries.clone());
    }
    let stream = broker.find(b.build()).await?;
    let pool = Pool::spawn(
        futures_util::stream::iter(history).chain(stream),
        pool_config,
    );
    proxybroker::tui::run_top(pool, Duration::from_secs(args.refresh)).await?;
    Ok(())
}

/// `serve` needs a positive limit (an unbounded pool would fill forever), so `0` maps to `1`,
/// matching api.py's `if limit <= 0: raise ValueError`.
#[cfg(feature = "server")]
fn serve_query(args: &ServeArgs) -> FindQuery {
    let mut b = FindQuery::builder()
        .types(types_from(args.types.clone(), args.lvl.clone()))
        .limit(args.limit.max(1))
        .dnsbl(args.dnsbl.clone())
        .timeout(Duration::from_secs(args.timeout))
        .post(args.post)
        .strict(args.strict);
    if !args.countries.is_empty() {
        b = b.countries(args.countries.clone());
    }
    b.build()
}

async fn grab(broker: Broker, args: GrabArgs) -> Result<(), Box<dyn std::error::Error>> {
    let query = GrabQuery {
        countries: (!args.countries.is_empty()).then_some(args.countries),
        // --limit 0 means unlimited. Mapped here, once, so the rest of the code never sees
        // 0-as-unlimited (which would otherwise make a `take(0)` yield nothing).
        limit: (args.limit > 0).then_some(args.limit),
    };
    let mut stream = broker.grab(query);
    write_stream(
        &mut stream,
        args.format,
        args.output_format.as_deref(),
        args.outfile.as_deref(),
        None, // grab has no --save
        None, // ...and no --progress
    )
    .await
}

/// Attach the requested anonymity levels to every requested type. `--lvl` applies only to
/// HTTP; for other protocols the checker ignores levels.
fn types_from(protos: Vec<Proto>, lvl: Vec<AnonLevel>) -> Vec<TypeSpec> {
    let levels = (!lvl.is_empty()).then_some(lvl);
    protos
        .into_iter()
        .map(|proto| TypeSpec {
            proto,
            levels: levels.clone(),
        })
        .collect()
}

/// Construct the `--state` [`Store`] backend from its spec: a `redis://`/`rediss://` URL → Redis,
/// anything else → a SQLite file path. Each arm errors clearly when its backing feature is absent.
#[cfg(any(feature = "store-sqlite", feature = "store-redis"))]
fn select_store(
    spec: &str,
) -> Result<std::sync::Arc<dyn proxybroker::Store>, Box<dyn std::error::Error>> {
    if spec.starts_with("redis://") || spec.starts_with("rediss://") {
        #[cfg(feature = "store-redis")]
        return Ok(std::sync::Arc::new(proxybroker::RedisStore::open(spec)?));
        #[cfg(not(feature = "store-redis"))]
        return Err(format!("--state {spec}: a redis:// URL needs --features store-redis").into());
    }
    #[cfg(feature = "store-sqlite")]
    return Ok(std::sync::Arc::new(proxybroker::SqliteStore::open(spec)?));
    #[cfg(not(feature = "store-sqlite"))]
    Err(format!("--state {spec}: a file path needs --features store-sqlite").into())
}

/// Pick the [`Store`](proxybroker::Store) a `serve --recheck` folds into (D3): the durable `--state`
/// backend when this build has one AND `--state` was given, else an in-memory [`MemoryStore`](proxybroker::MemoryStore)
/// whose score bookkeeping lives only for the process. The memory fallback is what lets `--recheck`
/// run with no database (a persist-only build, or a backend build with no `--state`). Gated on
/// `all(server, persist)` — only `serve_cmd` calls it.
#[cfg(all(feature = "server", feature = "persist"))]
fn recheck_store(
    state: Option<&str>,
) -> Result<std::sync::Arc<dyn proxybroker::Store>, Box<dyn std::error::Error>> {
    #[cfg(any(feature = "store-sqlite", feature = "store-redis"))]
    if let Some(spec) = state {
        return select_store(spec); // durable: the same handle open_state opens, chosen by URL scheme
    }
    let _ = state; // persist-only build, or --recheck without --state → memory fallback
    eprintln!("re-checking into memory only (no durable --state); scores reset on restart");
    Ok(std::sync::Arc::new(proxybroker::MemoryStore::new()))
}

/// Open the `--state` store (D2): return the broker with an upsert observer installed (every checked
/// proxy is folded into its durable row) plus the warm-start history read from the store. A no-op
/// returning empty history when `--state` is unset or no backend feature is built.
#[cfg(any(feature = "store-sqlite", feature = "store-redis"))]
fn open_state(
    broker: Broker,
    spec: Option<&str>,
) -> Result<(Broker, Vec<Proxy>), Box<dyn std::error::Error>> {
    match spec {
        Some(s) => {
            let store = select_store(s)?; // backend chosen by URL scheme
            let history = store.load()?;
            let writer = store.clone();
            let obs: proxybroker::broker::CheckObserver = std::sync::Arc::new(move |px: &Proxy| {
                if let Err(e) = writer.upsert(px) {
                    tracing::warn!(error = %e, "state upsert failed");
                }
            });
            Ok((broker.with_observer(Some(obs)), history))
        }
        None => Ok((broker, Vec::new())),
    }
}
#[cfg(not(any(feature = "store-sqlite", feature = "store-redis")))]
fn open_state(
    broker: Broker,
    spec: Option<&str>,
) -> Result<(Broker, Vec<Proxy>), Box<dyn std::error::Error>> {
    if spec.is_some() {
        eprintln!("--state requires a store backend; rebuild with --features store-sqlite (or store-redis)");
    }
    Ok((broker, Vec::new()))
}

/// Union two proxy sets deduped on `(host, port)`, `primary` winning a conflict (D2: DB history wins
/// over a `--load` snapshot because it carries reputation).
#[cfg(feature = "server")]
fn union_dedup(primary: Vec<Proxy>, secondary: Vec<Proxy>) -> Vec<Proxy> {
    let seen: std::collections::HashSet<_> = primary.iter().map(|p| (p.host, p.port)).collect();
    primary
        .into_iter()
        .chain(
            secondary
                .into_iter()
                .filter(|p| !seen.contains(&(p.host, p.port))),
        )
        .collect()
}

async fn find(broker: Broker, args: FindArgs) -> Result<(), Box<dyn std::error::Error>> {
    // D2: install the persistence upsert observer when --state is set (history unused by find,
    // which generates fresh candidates).
    let (broker, _history) = open_state(broker, args.state.as_deref())?;
    let mut builder = FindQuery::builder()
        .types(types_from(args.types, args.lvl))
        .limit(args.limit)
        .judges(args.judges)
        .dnsbl(args.dnsbl)
        .timeout(Duration::from_secs(args.timeout))
        .max_conn(args.max_conn)
        .retry(retry_policy(args.max_tries, args.retry_on, args.backoff_ms))
        .post(args.post)
        .strict(args.strict)
        .liveness_url(args.liveness_url);
    if !args.countries.is_empty() {
        builder = builder.countries(args.countries);
    }
    let mut query = builder.build();
    // A4 capability flags (pub fields; no builder setter needed).
    query.relaxed_validity = args.relaxed_validity;
    query.require_cookie = args.require_cookie;
    query.require_referer = args.require_referer;
    query.require_connect25 = args.require_connect25;
    query.trust_check = args.trust_check;
    query.require_trusted = args.require_trusted;

    // F2: a live bar during find. make_progress is a no-op unless the `progress` feature is on.
    let progress = make_progress(args.progress);

    let mut stream = broker.find(query).await?;
    write_stream(
        &mut stream,
        args.format,
        args.output_format.as_deref(),
        args.outfile.as_deref(),
        args.save.as_deref(),
        progress.as_ref(),
    )
    .await?;

    if args.show_stats {
        // Stats come from the stream itself, which aggregated EVERY checked proxy (working or
        // not) — not just the winners written above. Printed to stderr so it never mixes with
        // the proxy output on stdout. `stats()` is complete now: the stream is fully drained,
        // so all checks have finished and recorded.
        if let Some(s) = stream.stats() {
            match args.stats_format {
                StatsFormat::Text => eprint!("\n{s}"),
                StatsFormat::Json => eprintln!("{}", serde_json::to_string(&s)?),
            }
        }
    }
    Ok(())
}

async fn check(broker: Broker, args: CheckArgs) -> Result<(), Box<dyn std::error::Error>> {
    use tokio::io::AsyncReadExt;

    // --load: the proxies are already checked. Read them and stream straight to output, no broker,
    // no network. `--types` is optional here (enforced by clap), and unused.
    if let Some(path) = &args.load {
        let loaded = proxybroker::read_ndjson(std::io::BufReader::new(std::fs::File::open(path)?))?;
        // Honor --show-stats/--stats-format here too. This path has no broker/stream stats, so
        // compute a fresh summary over the loaded slice before it is moved into the stream. The
        // lossy timing fields aren't persisted (avg_resp_time/errors read 0), but total/working
        // and the protocol/anonymity/country breakdowns are meaningful for a saved pool.
        let stats = args
            .show_stats
            .then(|| proxybroker::Stats::from_proxies(&loaded));
        let mut stream = futures_util::stream::iter(loaded);
        write_stream(
            &mut stream,
            args.format,
            args.output_format.as_deref(),
            args.outfile.as_deref(),
            args.save.as_deref(),
            None,
        )
        .await?;
        if let Some(s) = stats {
            match args.stats_format {
                StatsFormat::Text => eprint!("\n{s}"),
                StatsFormat::Json => eprintln!("{}", serde_json::to_string(&s)?),
            }
        }
        return Ok(());
    }

    // Input: a file, or stdin by default.
    let text = match &args.infile {
        Some(path) => tokio::fs::read_to_string(path).await?,
        None => {
            let mut buf = String::new();
            tokio::io::stdin().read_to_string(&mut buf).await?;
            buf
        }
    };
    let proxies = proxybroker::parse_proxy_lines(&text);
    if proxies.is_empty() {
        eprintln!("no proxy addresses parsed from input");
    }

    let query = FindQuery {
        types: types_from(args.types, args.lvl),
        countries: (!args.countries.is_empty()).then_some(args.countries),
        limit: (args.limit > 0).then_some(args.limit),
        judges: args.judges,
        dnsbl: args.dnsbl,
        timeout: Duration::from_secs(args.timeout),
        max_conn: args.max_conn,
        retry: RetryPolicy::tries(args.max_tries),
        post: args.post,
        strict: args.strict,
        liveness_url: None, // --liveness-url is a find-only flag; check works from an explicit list
        relaxed_validity: false, // A4/A6 flags are find-only for now
        require_cookie: false,
        require_referer: false,
        require_connect25: false,
        trust_check: false,
        require_trusted: false,
    };

    let mut stream = broker
        .check(futures_util::stream::iter(proxies), query)
        .await?;
    write_stream(
        &mut stream,
        args.format,
        args.output_format.as_deref(),
        args.outfile.as_deref(),
        args.save.as_deref(),
        None,
    )
    .await?;

    if args.show_stats {
        if let Some(s) = stream.stats() {
            match args.stats_format {
                StatsFormat::Text => eprint!("\n{s}"),
                StatsFormat::Json => eprintln!("{}", serde_json::to_string(&s)?),
            }
        }
    }
    Ok(())
}

/// Drain a proxy stream to a file or stdout in the chosen format. Takes `&mut` so the caller
/// keeps the stream afterwards (e.g. to read `stats()`). When `save` is set, each streamed proxy
/// is also appended to that file as NDJSON (the C2 warm-start artifact), independent of `format`.
/// A one-line summary for the `--progress` bar (F2). Pure + testable.
#[cfg(feature = "progress")]
fn render_progress(s: &proxybroker::Stats) -> String {
    format!(
        "checked {} · working {} · avg {:.2}s",
        s.total, s.working, s.avg_resp_time
    )
}

/// A live progress bar (F2), or a no-op when the `progress` feature is off. Always drawn to stderr
/// so it never mixes with proxy output on stdout — same discipline as `--show-stats`.
struct Progress {
    #[cfg(feature = "progress")]
    bar: indicatif::ProgressBar,
}

impl Progress {
    fn inc(&self) {
        #[cfg(feature = "progress")]
        self.bar.inc(1);
    }
    fn tick(&self, _stats: &proxybroker::Stats) {
        #[cfg(feature = "progress")]
        self.bar.set_message(render_progress(_stats));
    }
    fn finish(&self) {
        #[cfg(feature = "progress")]
        self.bar.finish_and_clear();
    }
}

/// Build a spinner-style progress bar when `on` (F2). Spinner (not a percentage bar) because the
/// total is unknown for a streaming, possibly-unlimited `find`.
#[cfg(feature = "progress")]
fn make_progress(on: bool) -> Option<Progress> {
    on.then(|| {
        let bar = indicatif::ProgressBar::new_spinner();
        bar.set_draw_target(indicatif::ProgressDrawTarget::stderr());
        Progress { bar }
    })
}
#[cfg(not(feature = "progress"))]
fn make_progress(_on: bool) -> Option<Progress> {
    None
}

/// A stream that can report checked-so-far stats for the progress bar (F2). `ProxyStream` (from
/// `find`/`check`) does; a `--load` iterator has none.
trait DrainStats {
    fn drain_stats(&self) -> Option<proxybroker::Stats> {
        None
    }
}
impl DrainStats for proxybroker::ProxyStream {
    fn drain_stats(&self) -> Option<proxybroker::Stats> {
        self.stats()
    }
}
impl<I: Iterator<Item = Proxy>> DrainStats for futures_util::stream::Iter<I> {}

async fn write_stream<S>(
    stream: &mut S,
    format: Format,
    template: Option<&str>,
    outfile: Option<&std::path::Path>,
    save: Option<&std::path::Path>,
    progress: Option<&Progress>,
) -> Result<(), Box<dyn std::error::Error>>
where
    S: futures_util::Stream<Item = Proxy> + Unpin + DrainStats,
{
    // The save sink is the exact NDJSON bytes read_ndjson expects. Open once, before draining.
    // ponytail: blocking std::fs write inside the async loop — one small line per proxy at CLI
    // scale, not worth a spawn_blocking dance.
    let mut save_file = match save {
        Some(path) => Some(std::fs::File::create(path)?),
        None => None,
    };
    let mut save_line = |proxy: &Proxy| -> std::io::Result<()> {
        if let Some(f) = save_file.as_mut() {
            proxybroker::write_ndjson(f, std::slice::from_ref(proxy))?;
        }
        Ok(())
    };

    // Writing to a file is async I/O; stdout is a blocking lock. Keep them separate rather
    // than boxing a trait object over two very different sinks; the *format* logic lives only in
    // the shared `Emitter`, so the two branches differ only in their I/O mechanics.
    let mut emitter = Emitter::new(format, template);
    if let Some(path) = outfile {
        let mut file = tokio::fs::File::create(path).await?;
        if let Some(p) = emitter.prefix() {
            file.write_all(p.as_bytes()).await?;
        }
        let mut count = 0u64;
        match progress {
            None => {
                while let Some(proxy) = stream.next().await {
                    file.write_all(emitter.item(&proxy).as_bytes()).await?;
                    save_line(&proxy)?;
                    count += 1;
                }
            }
            Some(bar) => {
                // select! drops the (dropped-if-not-selected) next() future before the tick arm
                // runs, releasing the &mut borrow so stream.drain_stats() (&self) is legal (F2).
                let mut tick = tokio::time::interval(Duration::from_millis(120));
                loop {
                    tokio::select! {
                        maybe = stream.next() => match maybe {
                            Some(proxy) => {
                                file.write_all(emitter.item(&proxy).as_bytes()).await?;
                                save_line(&proxy)?;
                                count += 1;
                                bar.inc();
                            }
                            None => break,
                        },
                        _ = tick.tick() => {
                            if let Some(s) = stream.drain_stats() { bar.tick(&s); }
                        }
                    }
                }
                bar.finish();
            }
        }
        if let Some(s) = emitter.suffix() {
            file.write_all(s.as_bytes()).await?;
        }
        file.flush().await?;
        eprintln!("wrote {count} proxies to {}", path.display());
    } else {
        let stdout = std::io::stdout();
        let mut lock = stdout.lock();
        if let Some(p) = emitter.prefix() {
            write!(lock, "{p}")?;
        }
        match progress {
            None => {
                while let Some(proxy) = stream.next().await {
                    write!(lock, "{}", emitter.item(&proxy))?;
                    save_line(&proxy)?;
                }
            }
            Some(bar) => {
                let mut tick = tokio::time::interval(Duration::from_millis(120));
                loop {
                    tokio::select! {
                        maybe = stream.next() => match maybe {
                            Some(proxy) => {
                                write!(lock, "{}", emitter.item(&proxy))?;
                                save_line(&proxy)?;
                                bar.inc();
                            }
                            None => break,
                        },
                        _ = tick.tick() => {
                            if let Some(s) = stream.drain_stats() { bar.tick(&s); }
                        }
                    }
                }
                bar.finish();
            }
        }
        if let Some(s) = emitter.suffix() {
            write!(lock, "{s}")?;
        }
    }
    Ok(())
}

fn init_tracing(level: &str, json: bool) {
    use tracing_subscriber::{fmt, EnvFilter};
    let filter = EnvFilter::try_from_default_env()
        .unwrap_or_else(|_| EnvFilter::new(format!("proxybroker={level}")));
    let builder = fmt().with_env_filter(filter).with_writer(std::io::stderr);
    // --log-format json renders the whole log stream (incl. F3 check events) as line-delimited JSON.
    if json {
        let _ = builder.json().try_init();
    } else {
        let _ = builder.try_init();
    }
}

#[cfg(all(test, feature = "server"))]
mod tests {
    use super::*;

    // D3: `--recheck` without `--state` must fall back to an in-memory store — score bookkeeping
    // that does NOT persist across store handles (a durable file would). Confirms `serve_cmd`'s
    // store selection picks `MemoryStore` on the no-`--state` path.
    #[cfg(feature = "persist")]
    #[test]
    fn recheck_store_without_state_is_in_memory() {
        use std::collections::BTreeMap;
        let mut types = BTreeMap::new();
        types.insert(Proto::Http, None);
        let px = Proxy::restored("1.2.3.4".parse().unwrap(), 8080, types, 1, 0, 100.0);

        let first = recheck_store(None).unwrap();
        first.upsert(&px).unwrap();
        assert_eq!(first.load().unwrap().len(), 1);

        // A second no-`--state` handle starts empty — memory, not a shared durable file that would
        // still hold the first upsert.
        let second = recheck_store(None).unwrap();
        assert!(
            second.load().unwrap().is_empty(),
            "no --state must not persist across store handles"
        );
    }

    #[cfg(feature = "progress")]
    #[test]
    fn render_progress_formats_counts() {
        let s = proxybroker::Stats {
            total: 128,
            working: 34,
            avg_resp_time: 0.72,
            ..Default::default()
        };
        let out = render_progress(&s);
        assert!(out.contains("checked 128"), "{out}");
        assert!(out.contains("working 34"), "{out}");
        assert!(out.contains("0.72s"), "{out}");
    }

    #[test]
    fn serve_query_threads_lvl_and_strict() {
        // The four passthrough flags must reach the FindQuery that fills the pool; before B3 they
        // were dropped, so anonymity-filtered serving was impossible.
        let args = ServeArgs {
            types: vec![Proto::Http],
            lvl: vec![AnonLevel::High],
            strict: true,
            post: true,
            dnsbl: vec!["zen.spamhaus.org".into()],
            ..Default::default()
        };
        let q = serve_query(&args);
        assert_eq!(q.types[0].levels, Some(vec![AnonLevel::High]));
        assert!(q.strict);
        assert!(q.post);
        assert_eq!(q.dnsbl, vec!["zen.spamhaus.org".to_string()]);
    }

    fn serve_countries(argv: &[&str]) -> Vec<String> {
        match Cli::try_parse_from(argv).unwrap().command {
            Command::Serve(a) => a.countries,
            _ => panic!("expected serve"),
        }
    }

    #[test]
    fn only_cc_alias_splits_on_comma() {
        // --only-cc US,DE (comma) and --countries US DE (space) must be equivalent spellings.
        let comma = serve_countries(&[
            "proxybroker",
            "serve",
            "--types",
            "HTTP",
            "--only-cc",
            "US,DE",
        ]);
        let space = serve_countries(&[
            "proxybroker",
            "serve",
            "--types",
            "HTTP",
            "--countries",
            "US",
            "DE",
        ]);
        assert_eq!(comma, vec!["US".to_string(), "DE".to_string()]);
        assert_eq!(comma, space);
    }
}

#[cfg(test)]
mod format_tests {
    use super::*;
    use proxybroker::{AnonLevel, Country, Proto, Proxy};
    use std::collections::BTreeSet;

    /// A checked proxy with geo + one HTTP type at High + one recorded runtime.
    fn proxy_fixture() -> Proxy {
        let mut p = Proxy::new("1.2.3.4".parse().unwrap(), 8080, BTreeSet::new());
        p.geo = Some(Country {
            code: "US".into(),
            name: "United States".into(),
            ..Default::default()
        });
        p.add_type(Proto::Http, Some(AnonLevel::High));
        p.record_attempt(Some(0.42), None);
        p
    }

    #[test]
    fn emitter_default_is_addr_per_line() {
        let mut e = Emitter::new(Format::Default, None);
        assert!(e.prefix().is_none());
        assert_eq!(e.item(&proxy_fixture()), "1.2.3.4:8080\n");
        assert!(e.suffix().is_none());
    }

    #[test]
    fn emitter_json_is_ndjson() {
        let mut e = Emitter::new(Format::Json, None);
        assert!(e.prefix().is_none() && e.suffix().is_none());
        let line = e.item(&proxy_fixture());
        assert!(line.ends_with('\n'), "{line:?}");
        assert_eq!(
            line.matches('\n').count(),
            1,
            "exactly one object per line: {line:?}"
        );
        assert!(line.trim_end().starts_with('{'), "{line:?}");
    }

    #[test]
    fn url_format_prefixes_scheme() {
        // HTTP-only proxy → http.
        assert_eq!(
            Emitter::new(Format::Url, None).item(&proxy_fixture()),
            "http://1.2.3.4:8080\n"
        );
        // An HTTPS-capable (CONNECT-to-443) HTTP proxy is still dialed over plain HTTP → http.
        let mut https = Proxy::new("9.9.9.9".parse().unwrap(), 8080, BTreeSet::new());
        https.add_type(Proto::Https, None);
        assert_eq!(
            Emitter::new(Format::Url, None).item(&https),
            "http://9.9.9.9:8080\n"
        );
        // A SOCKS5 proxy speaks the SOCKS wire protocol → socks5, so the URL is directly usable.
        let mut p = Proxy::new("5.6.7.8".parse().unwrap(), 1080, BTreeSet::new());
        p.add_type(Proto::Socks5, None);
        assert_eq!(
            Emitter::new(Format::Url, None).item(&p),
            "socks5://5.6.7.8:1080\n"
        );
        // SOCKS4 likewise.
        let mut p4 = Proxy::new("5.6.7.8".parse().unwrap(), 1080, BTreeSet::new());
        p4.add_type(Proto::Socks4, None);
        assert_eq!(
            Emitter::new(Format::Url, None).item(&p4),
            "socks4://5.6.7.8:1080\n"
        );
    }

    #[test]
    fn csv_header_and_row() {
        let mut e = Emitter::new(Format::Csv, None);
        assert_eq!(
            e.prefix().unwrap(),
            "host,port,protocols,anon,country,resp_time,error_rate\n"
        );
        let row = e.item(&proxy_fixture());
        let row = row.trim_end();
        let fields: Vec<&str> = row.split(',').collect();
        // Exactly 7 fields ⇒ no field contained a comma (the no-quoting guard).
        assert_eq!(fields.len(), 7, "{row}");
        assert_eq!(&fields[..5], ["1.2.3.4", "8080", "HTTP", "High", "US"]);
    }

    #[test]
    fn csv_unchecked_proxy_has_empty_type_columns() {
        // A grabbed proxy: no confirmed types, no geo → the type/geo columns are empty.
        let p = Proxy::new("1.2.3.4".parse().unwrap(), 8080, BTreeSet::new());
        let row = Emitter::new(Format::Csv, None).item(&p);
        assert_eq!(row.trim_end(), "1.2.3.4,8080,,,,0,0");
    }

    #[test]
    fn json_array_emits_bracketed_comma_separated() {
        let mut e = Emitter::new(Format::JsonArray, None);
        let mut out = e.prefix().unwrap();
        out.push_str(&e.item(&proxy_fixture()));
        out.push_str(&e.item(&proxy_fixture()));
        out.push_str(&e.suffix().unwrap());
        assert!(out.starts_with('['), "{out}");
        assert!(out.ends_with("]\n"), "{out}");
        let v: Vec<serde_json::Value> = serde_json::from_str(out.trim_end()).unwrap();
        assert_eq!(v.len(), 2, "parses as a 2-element array: {out}");
    }

    #[test]
    fn json_array_empty_stream_is_empty_array() {
        let e = Emitter::new(Format::JsonArray, None);
        let out = format!("{}{}", e.prefix().unwrap(), e.suffix().unwrap());
        assert_eq!(out, "[]\n");
        assert!(
            serde_json::from_str::<Vec<serde_json::Value>>(out.trim_end())
                .unwrap()
                .is_empty()
        );
    }

    #[test]
    fn ndjson_still_one_object_per_line() {
        // C4 must not change the streaming NDJSON default: each element is a standalone object with
        // no array wrapping and no leading separator.
        let mut e = Emitter::new(Format::Json, None);
        let a = e.item(&proxy_fixture());
        let b = e.item(&proxy_fixture());
        assert!(
            a.trim_end().starts_with('{') && a.trim_end().ends_with('}'),
            "{a:?}"
        );
        assert!(
            b.starts_with('{'),
            "NDJSON element must not have a leading separator: {b:?}"
        );
    }

    #[test]
    fn template_renders_known_fields() {
        assert_eq!(
            render_template("{{proxy}}/{{country}}/{{duration}}", &proxy_fixture()),
            "1.2.3.4:8080/US/0.42"
        );
    }

    #[test]
    fn template_leaves_unknown_tokens_literal() {
        assert_eq!(render_template("{{nope}}", &proxy_fixture()), "{{nope}}");
    }

    #[test]
    fn template_renders_asn_tokens() {
        // With an --asn-db, {{asn}}/{{asn_org}} render the number and owner.
        let mut p = proxy_fixture();
        p.asn = Some(proxybroker::Asn {
            number: 15169,
            org: Some("Google LLC".into()),
        });
        assert_eq!(
            render_template("{{asn}} {{asn_org}}", &p),
            "15169 Google LLC"
        );
        // Absent ASN (the default: no --asn-db) renders both as empty.
        assert_eq!(render_template("{{asn}}{{asn_org}}", &proxy_fixture()), "");
    }

    #[test]
    fn output_format_overrides_format() {
        // A template forces line output even when the format is json-array.
        let mut e = Emitter::new(Format::JsonArray, Some("{{host}}"));
        assert!(e.prefix().is_none() && e.suffix().is_none());
        assert_eq!(e.item(&proxy_fixture()), "1.2.3.4\n");
    }
}