vanguards-rs 1.0.1

Enhanced security for Tor hidden services through vanguard relay selection
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
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
//! Control module for Tor controller interaction and main application loop.
//!
//! This module provides the core functionality for connecting to Tor's control port,
//! authenticating, handling events, and managing the vanguard protection lifecycle.
//!
//! # Overview
//!
//! The control module handles:
//!
//! - **Connection Management**: Connect via TCP or Unix socket with auto-detection
//! - **Authentication**: Password, cookie, and interactive authentication
//! - **Consensus Processing**: Parse consensus weights and update vanguard state
//! - **Event Handling**: Register and dispatch events to protection components
//! - **Circuit Management**: Close circuits when attacks are detected
//! - **Signal Handling**: Handle SIGHUP for configuration reload
//!
//! # Architecture
//!
//! The control module orchestrates all protection components through a central event loop:
//!
//! ```text
//! ┌──────────────────────────────────────────────────────────────────────────┐
//! │                         Main Event Loop                                  │
//! │                                                                          │
//! │  ┌──────────────┐     ┌──────────────┐     ┌──────────────────────────┐  │
//! │  │  Tor Control │───▶│   Event      │───▶│   Protection Components  │  │
//! │  │    Socket    │    │  Dispatcher  │      │                          │  │
//! │  └──────────────┘    └──────────────┘      │  ┌────────────────────┐  │  │
//! │         │                   │              │  │ VanguardState      │  │  │
//! │         │                   │              │  │ (guard management) │  │  │
//! │         ▼                   │              │  └────────────────────┘  │  │
//! │  ┌──────────────┐           │              │  ┌────────────────────┐  │  │
//! │  │ Authenticate │           │              │  │ BandwidthStats     │  │  │
//! │  │ (password/   │           │              │  │ (attack detection) │  │  │
//! │  │  cookie)     │           │              │  └────────────────────┘  │  │
//! │  └──────────────┘           │              │  ┌────────────────────┐  │  │
//! │                             │              │  │ RendGuard          │  │  │
//! │                             │              │  │ (RP monitoring)    │  │  │
//! │                             │              │  └────────────────────┘  │  │
//! │                             │              │  ┌────────────────────┐  │  │
//! │                             │              │  │ PathVerify         │  │  │
//! │                             │              │  │ (path validation)  │  │  │
//! │                             │              │  └────────────────────┘  │  │
//! │                             │              └──────────────────────────┘  │
//! │                             │                                            │
//! │                             ▼                                            │
//! │                    ┌──────────────────┐                                  │
//! │                    │ Circuit Actions  │                                  │
//! │                    │ (close on attack)│                                  │
//! │                    └──────────────────┘                                  │
//! └─────────────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Main Loop Flow
//!
//! The main application loop follows this sequence:
//!
//! ```text
//!                    ┌─────────────────┐
//!                    │     Start       │
//!                    └────────┬────────┘
//!//!//!                    ┌─────────────────┐
//!                    │ Connect to Tor  │◀──────────────────┐
//!                    │ (socket/port)   │                   │
//!                    └────────┬────────┘                   │
//!                             │                            │
//!                             ▼                            │
//!                    ┌─────────────────┐                   │
//!                    │  Authenticate   │                   │
//!                    │ (auto-detect)   │                   │
//!                    └────────┬────────┘                   │
//!                             │                            │
//!                             ▼                            │
//!                    ┌─────────────────┐                   │
//!                    │ Load/Create     │                   │
//!                    │ Vanguard State  │                   │
//!                    └────────┬────────┘                   │
//!                             │                            │
//!                             ▼                            │
//!                    ┌─────────────────┐                   │
//!                    │ Subscribe to    │                   │
//!                    │ Tor Events      │                   │
//!                    └────────┬────────┘                   │
//!                             │                            │
//!                             ▼                            │
//!              ┌──────────────────────────────┐            │
//!              │      Event Processing Loop   │            │
//!              │  ┌────────────────────────┐  │            │
//!              │  │ Receive Event          │  │            │
//!              │  └───────────┬────────────┘  │            │
//!              │              │               │            │
//!              │              ▼               │            │
//!              │  ┌────────────────────────┐  │            │
//!              │  │ Dispatch to Handlers   │  │            │
//!              │  │ (CIRC, CIRC_BW, etc.)  │  │            │
//!              │  └───────────┬────────────┘  │            │
//!              │              │               │            │
//!              │              ▼               │            │
//!              │  ┌────────────────────────┐  │            │
//!              │  │ Check Circuit Limits   │  │            │
//!              │  │ (close if attack)      │  │            │
//!              │  └───────────┬────────────┘  │            │
//!              │              │               │            │
//!              │              ▼               │            │
//!              │         [Continue]           │            │
//!              └──────────────┬───────────────┘            │
//!                             │                            │
//!                    [Connection Lost]                     │
//!                             │                            │
//!                             ▼                            │
//!                    ┌─────────────────┐                   │
//!                    │ Reconnect?      │───────────────────┘
//!                    │ (retry limit)   │
//!                    └────────┬────────┘
//!                             │ [Limit Reached]
//!//!                    ┌─────────────────┐
//!                    │      Exit       │
//!                    └─────────────────┘
//! ```
//!
//! # Event Handling State Diagram
//!
//! Events are dispatched to different handlers based on type:
//!
//! ```text
//!                         ┌─────────────┐
//!                         │ Tor Event   │
//!                         └──────┬──────┘
//!//!          ┌─────────────────────┼─────────────────────┐
//!          │                     │                     │
//!          ▼                     ▼                     ▼
//!    ┌───────────┐        ┌───────────┐        ┌───────────┐
//!    │   CIRC    │        │  CIRC_BW  │        │ NEWCONSENS│
//!    └─────┬─────┘        └─────┬─────┘        └─────┬─────┘
//!          │                    │                    │
//!          ▼                    ▼                    ▼
//!    ┌───────────┐        ┌───────────┐        ┌───────────┐
//!    │RendGuard  │        │BandGuards │        │ Update    │
//!    │BandGuards │        │(bandwidth │        │ Vanguard  │
//!    │CBTVerify  │        │ tracking) │        │ State     │
//!    │PathVerify │        └───────────┘        └───────────┘
//!    │LogGuard   │
//!    └───────────┘
//!
//!          ┌─────────────────────┼─────────────────────┐
//!          │                     │                     │
//!          ▼                     ▼                     ▼
//!    ┌───────────┐        ┌───────────┐        ┌───────────┐
//!    │  ORCONN   │        │   GUARD   │        │  SIGNAL   │
//!    └─────┬─────┘        └─────┬─────┘        └─────┬─────┘
//!          │                    │                    │
//!          ▼                    ▼                    ▼
//!    ┌───────────┐        ┌───────────┐        ┌───────────┐
//!    │BandGuards │        │PathVerify │        │ Reapply   │
//!    │PathVerify │        │(guard     │        │ Vanguards │
//!    │(conn      │        │ tracking) │        │ (SIGHUP)  │
//!    │ tracking) │        └───────────┘        └───────────┘
//!    └───────────┘
//! ```
//!
//! # What This Module Does NOT Do
//!
//! - **Direct relay communication**: Use stem-rs client module for ORPort connections
//! - **Descriptor parsing**: Consensus parsing is limited to bandwidth weights
//! - **Guard selection**: Use [`crate::node_selection`] for bandwidth-weighted selection
//! - **State persistence**: Use [`crate::vanguards::VanguardState`] for state file I/O
//!
//! # Example
//!
//! Running the main application loop:
//!
//! ```rust,no_run
//! use vanguards_rs::config::Config;
//! use vanguards_rs::control::run_main;
//!
//! # async fn example() -> Result<(), vanguards_rs::error::Error> {
//! // Load configuration
//! let config = Config::default();
//!
//! // Run the main loop (blocks until shutdown)
//! run_main(config).await?;
//! # Ok(())
//! # }
//! ```
//!
//! # Security Considerations
//!
//! - Passwords are prompted interactively if not provided (never logged)
//! - Circuit closure decisions are logged for audit purposes
//! - State files contain guard fingerprints (protect accordingly)
//! - Reconnection attempts are rate-limited to prevent DoS
//!
//! # See Also
//!
//! - [`crate::config`] - Configuration management
//! - [`crate::vanguards`] - Vanguard state management
//! - [`crate::bandguards`] - Bandwidth-based attack detection
//! - [`crate::rendguard`] - Rendezvous point monitoring
//! - [`crate::pathverify`] - Circuit path verification
//! - [Python vanguards control](https://github.com/mikeperry-tor/vanguards) - Original implementation
//! - [Tor Control Protocol](https://spec.torproject.org/control-spec) - Protocol specification

use std::collections::HashMap;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;

use stem_rs::controller::{CircuitId, Controller};
use stem_rs::descriptor::router_status::RouterStatusEntry;
use stem_rs::events::ParsedEvent;
use stem_rs::version::Version;
use stem_rs::EventType;

use crate::bandguards::BandwidthStats;
use crate::cbtverify::TimeoutStats;
use crate::config::{Config, LogLevel};
use crate::error::{Error, Result};
use crate::logger::plog;
use crate::logguard::LogGuard;
use crate::node_selection::{BwWeightedGenerator, FlagsRestriction, NodeRestrictionList, Position};
use crate::pathverify::PathVerify;
use crate::vanguards::{ExcludeNodes, VanguardState};

/// Library version string.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

/// Minimum Tor version required for CIRC_BW and CIRC_MINOR events.
#[allow(dead_code)]
const MIN_TOR_VERSION_FOR_BW: &str = "0.3.4.10";

/// Minimum Tor version required for HSLayer2Nodes support.
#[allow(dead_code)]
const MIN_TOR_VERSION_FOR_VANGUARDS: &str = "0.3.3.0";

/// Global flag for close circuits configuration.
///
/// When true, detected attacks will result in circuit closure.
/// When false, attacks are logged but circuits remain open (monitoring mode).
static CLOSE_CIRCUITS: AtomicBool = AtomicBool::new(true);

/// Sets the global close circuits flag.
///
/// Controls whether circuits are actually closed when attacks are detected.
/// Set to `false` for monitoring-only mode where attacks are logged but
/// circuits are not closed.
///
/// # Arguments
///
/// * `value` - `true` to enable circuit closure, `false` for monitoring only
///
/// # Thread Safety
///
/// This function uses atomic operations and is safe to call from any thread.
///
/// # Example
///
/// ```rust
/// use vanguards_rs::control::{set_close_circuits, get_close_circuits};
///
/// // Enable monitoring-only mode
/// set_close_circuits(false);
/// assert!(!get_close_circuits());
///
/// // Re-enable circuit closure
/// set_close_circuits(true);
/// assert!(get_close_circuits());
/// ```
pub fn set_close_circuits(value: bool) {
    CLOSE_CIRCUITS.store(value, Ordering::SeqCst);
}

/// Gets the global close circuits flag.
///
/// Returns whether circuits will be closed when attacks are detected.
///
/// # Returns
///
/// `true` if circuit closure is enabled, `false` if in monitoring-only mode.
///
/// # Thread Safety
///
/// This function uses atomic operations and is safe to call from any thread.
///
/// # Example
///
/// ```rust
/// use vanguards_rs::control::get_close_circuits;
///
/// if get_close_circuits() {
///     println!("Circuit closure is enabled");
/// } else {
///     println!("Monitoring-only mode");
/// }
/// ```
pub fn get_close_circuits() -> bool {
    CLOSE_CIRCUITS.load(Ordering::SeqCst)
}

/// Authenticates with Tor using any available method.
///
/// Attempts authentication in this order:
/// 1. No authentication (if control port is open)
/// 2. Password authentication (if provided)
/// 3. Cookie authentication
///
/// If password authentication fails and no password was provided,
/// prompts the user interactively for a password.
///
/// # Arguments
///
/// * `controller` - The Tor controller to authenticate
/// * `password` - Optional password for authentication
///
/// # Errors
///
/// Returns [`Error::Control`] if authentication fails.
pub async fn authenticate_any(controller: &mut Controller, password: Option<&str>) -> Result<()> {
    let result = controller.authenticate(password).await;

    match result {
        Ok(()) => {
            let version = controller.get_version().await?;
            plog(
                LogLevel::Notice,
                &format!(
                    "Vanguards {} connected to Tor {} using stem-rs",
                    VERSION, version
                ),
            );
            Ok(())
        }
        Err(stem_rs::Error::Authentication(stem_rs::AuthError::MissingPassword)) => {
            // Prompt for password interactively
            let passwd = prompt_password()?;
            controller.authenticate(Some(&passwd)).await?;
            let version = controller.get_version().await?;
            plog(
                LogLevel::Notice,
                &format!(
                    "Vanguards {} connected to Tor {} using stem-rs",
                    VERSION, version
                ),
            );
            Ok(())
        }
        Err(e) => Err(Error::Control(e)),
    }
}

/// Prompts the user for a password interactively.
fn prompt_password() -> Result<String> {
    eprint!("Controller password: ");
    let mut password = String::new();
    std::io::stdin()
        .read_line(&mut password)
        .map_err(Error::Io)?;
    Ok(password.trim().to_string())
}

/// Parses consensus bandwidth weights from a cached-microdesc-consensus file.
///
/// Bandwidth weights are used by Tor clients to select relays proportionally
/// to their contribution to the network. These weights are essential for
/// the bandwidth-weighted guard selection algorithm.
///
/// # Weight Keys
///
/// Common weight keys include:
///
/// | Key | Description |
/// |-----|-------------|
/// | `Wgg` | Weight for Guard-flagged nodes in guard position |
/// | `Wgm` | Weight for Guard-flagged nodes in middle position |
/// | `Wmm` | Weight for non-flagged nodes in middle position |
/// | `Wme` | Weight for Exit-flagged nodes in middle position |
/// | `Wee` | Weight for Exit-flagged nodes in exit position |
///
/// # Arguments
///
/// * `consensus_filename` - Path to the cached-microdesc-consensus file
///
/// # Returns
///
/// A HashMap mapping weight keys (e.g., "Wmm") to their integer values
/// (typically in range 0-10000, representing parts per 10000).
///
/// # Errors
///
/// Returns [`Error::Consensus`] if:
/// - The file cannot be opened or read
/// - No `bandwidth-weights` line is found in the file
///
/// # File Format
///
/// The function looks for a line starting with `bandwidth-weights ` followed
/// by space-separated key=value pairs:
///
/// ```text
/// bandwidth-weights Wbd=0 Wbe=0 Wbg=4194 Wbm=10000 ...
/// ```
///
/// # Example
///
/// ```rust,no_run
/// use std::path::Path;
/// use vanguards_rs::control::get_consensus_weights;
///
/// # fn example() -> Result<(), vanguards_rs::error::Error> {
/// let weights = get_consensus_weights(Path::new("/var/lib/tor/cached-microdesc-consensus"))?;
///
/// if let Some(wmm) = weights.get("Wmm") {
///     println!("Middle position weight: {}", wmm);
/// }
/// # Ok(())
/// # }
/// ```
///
/// # See Also
///
/// - [`BwWeightedGenerator`] - Uses these weights
/// - [dir-spec.txt](https://spec.torproject.org/dir-spec) - Consensus format specification
pub fn get_consensus_weights(consensus_filename: &Path) -> Result<HashMap<String, i64>> {
    let file = std::fs::File::open(consensus_filename).map_err(|e| {
        Error::Consensus(format!(
            "cannot read {}: {}",
            consensus_filename.display(),
            e
        ))
    })?;
    let reader = BufReader::new(file);

    let mut weights = HashMap::new();

    for line in reader.lines() {
        let line = line.map_err(|e| Error::Consensus(format!("read error: {}", e)))?;
        if line.starts_with("bandwidth-weights ") {
            // Parse bandwidth-weights line
            // Format: bandwidth-weights Wbd=0 Wbe=0 Wbg=4194 Wbm=10000 ...
            for part in line.split_whitespace().skip(1) {
                if let Some((key, value)) = part.split_once('=') {
                    if let Ok(v) = value.parse::<i64>() {
                        weights.insert(key.to_string(), v);
                    }
                }
            }
            break;
        }
    }

    if weights.is_empty() {
        return Err(Error::Consensus(
            "no bandwidth-weights found in consensus".to_string(),
        ));
    }

    Ok(weights)
}

/// Attempts to close a circuit, optionally dumping logs first.
///
/// This function is called when an attack is detected and a circuit needs
/// to be closed. If logguard is enabled, it dumps the log queue for the
/// circuit before closing to aid in post-incident analysis.
///
/// # Arguments
///
/// * `controller` - The Tor controller
/// * `circ_id` - The circuit ID to close
/// * `logguard` - Optional log guard for pre-close log dumping
///
/// # Behavior
///
/// 1. If logguard is provided, dumps buffered logs for the circuit
/// 2. If `close_circuits` global flag is true, sends CLOSECIRCUIT command
/// 3. Logs success or failure of the close operation
///
/// # Global Flag
///
/// The `close_circuits` flag (set via [`set_close_circuits`]) controls whether
/// circuits are actually closed. When false, the function logs but doesn't close.
/// This is useful for testing or monitoring-only mode.
///
/// # Example
///
/// ```rust,no_run
/// use stem_rs::controller::Controller;
/// use vanguards_rs::control::try_close_circuit;
///
/// # async fn example() -> Result<(), vanguards_rs::error::Error> {
/// let mut controller = Controller::from_port("127.0.0.1:9051".parse().unwrap()).await?;
/// controller.authenticate(None).await?;
///
/// // Close circuit without log dumping
/// try_close_circuit(&mut controller, "42", None).await;
/// # Ok(())
/// # }
/// ```
///
/// # See Also
///
/// - [`set_close_circuits`] - Control whether circuits are actually closed
/// - [`LogGuard::dump_log_queue`] - Log dumping implementation
pub async fn try_close_circuit(
    controller: &mut Controller,
    circ_id: &str,
    logguard: Option<&mut LogGuard>,
) {
    // Dump logs before closing
    if let Some(lg) = logguard {
        lg.dump_log_queue(circ_id, "Pre");
    }

    if get_close_circuits() {
        let circuit_id = CircuitId::new(circ_id);
        match controller.close_circuit(&circuit_id).await {
            Ok(()) => {
                plog(
                    LogLevel::Info,
                    &format!("We force-closed circuit {}", circ_id),
                );
            }
            Err(e) => {
                plog(
                    LogLevel::Info,
                    &format!("Failed to close circuit {}: {}", circ_id, e),
                );
            }
        }
    }
}

/// Configures Tor with the current vanguard settings.
///
/// Sets Tor configuration options to enforce the vanguard guard layers.
/// This function is called after vanguard state is updated to apply
/// the new guard sets to Tor.
///
/// # Configuration Options Set
///
/// | Option | Description | Condition |
/// |--------|-------------|-----------|
/// | `NumEntryGuards` | Number of layer 1 guards | If > 0 |
/// | `NumDirectoryGuards` | Number of directory guards | If > 0 |
/// | `GuardLifetime` | Layer 1 guard lifetime | If > 0 days |
/// | `HSLayer2Nodes` | Layer 2 guard fingerprints | Always |
/// | `HSLayer3Nodes` | Layer 3 guard fingerprints | If num_layer3 > 0 |
///
/// # Arguments
///
/// * `controller` - The Tor controller
/// * `state` - The current vanguard state containing guard sets
/// * `config` - The vanguards configuration
///
/// # Returns
///
/// Returns `Ok(())` on successful configuration.
///
/// # Errors
///
/// Returns [`Error::Control`] if Tor configuration fails. This typically
/// indicates an incompatible Tor version (requires 0.3.3.x or newer).
///
/// # Tor Version Requirements
///
/// - `HSLayer2Nodes`: Requires Tor 0.3.3.0+
/// - `HSLayer3Nodes`: Requires Tor 0.3.3.0+
///
/// # Example
///
/// ```rust,no_run
/// use stem_rs::controller::Controller;
/// use vanguards_rs::vanguards::VanguardState;
/// use vanguards_rs::config::Config;
/// use vanguards_rs::control::configure_tor;
///
/// # async fn example() -> Result<(), vanguards_rs::error::Error> {
/// let mut controller = Controller::from_port("127.0.0.1:9051".parse().unwrap()).await?;
/// controller.authenticate(None).await?;
///
/// let state = VanguardState::new("/tmp/vanguards.state");
/// let config = Config::default();
///
/// configure_tor(&mut controller, &state, &config).await?;
/// # Ok(())
/// # }
/// ```
///
/// # See Also
///
/// - [`VanguardState::layer2_guardset`] - Get layer 2 fingerprint string
/// - [`VanguardState::layer3_guardset`] - Get layer 3 fingerprint string
pub async fn configure_tor(
    controller: &mut Controller,
    state: &VanguardState,
    config: &Config,
) -> Result<()> {
    let vg_config = &config.vanguards;

    // Set NumEntryGuards and NumDirectoryGuards if configured
    if vg_config.num_layer1_guards > 0 {
        controller
            .set_conf("NumEntryGuards", &vg_config.num_layer1_guards.to_string())
            .await?;
        controller
            .set_conf(
                "NumDirectoryGuards",
                &vg_config.num_layer1_guards.to_string(),
            )
            .await?;
    }

    // Set GuardLifetime if configured
    if vg_config.layer1_lifetime_days > 0 {
        controller
            .set_conf(
                "GuardLifetime",
                &format!("{} days", vg_config.layer1_lifetime_days),
            )
            .await?;
    }

    // Set HSLayer2Nodes
    let layer2_guardset = state.layer2_guardset();
    controller
        .set_conf("HSLayer2Nodes", &layer2_guardset)
        .await
        .inspect_err(|_e| {
            plog(
                LogLevel::Error,
                "Vanguards requires Tor 0.3.3.x (and ideally 0.3.4.x or newer).",
            );
        })?;

    // Set HSLayer3Nodes if configured
    if vg_config.num_layer3_guards > 0 {
        let layer3_guardset = state.layer3_guardset();
        controller
            .set_conf("HSLayer3Nodes", &layer3_guardset)
            .await?;
    }

    plog(
        LogLevel::Info,
        &format!("Layer2 guards: {}", layer2_guardset),
    );
    if vg_config.num_layer3_guards > 0 {
        plog(
            LogLevel::Info,
            &format!("Layer3 guards: {}", state.layer3_guardset()),
        );
    }

    Ok(())
}

/// Handles a new consensus event by updating vanguard state.
///
/// This function is called when a new consensus is received from Tor. It performs
/// a complete update cycle for all protection components.
///
/// # Processing Steps
///
/// ```text
/// ┌─────────────────────────────────────────────────────────────┐
/// │                  new_consensus_event()                       │
/// │                                                              │
/// │  1. Get router list from Tor (GETINFO ns/all)               │
/// │  2. Get ExcludeNodes configuration                          │
/// │  3. Parse consensus weights from cached-microdesc-consensus │
/// │  4. Update vanguard state:                                  │
/// │     • Remove guards no longer in consensus                  │
/// │     • Remove expired guards                                 │
/// │     • Remove excluded guards                                │
/// │     • Replenish guard layers                                │
/// │  5. Update rendguard use counts                             │
/// │  6. Configure Tor with new HSLayer2/3Nodes                  │
/// │  7. Write state to file                                     │
/// └─────────────────────────────────────────────────────────────┘
/// ```
///
/// # Arguments
///
/// * `controller` - The Tor controller for querying and configuration
/// * `state` - The vanguard state to update
/// * `config` - The vanguards configuration
///
/// # Returns
///
/// Returns `Ok(())` on successful update.
///
/// # Errors
///
/// - [`Error::DescriptorUnavailable`] - Tor doesn't have descriptors yet (retry later)
/// - [`Error::Consensus`] - Failed to parse consensus file
/// - [`Error::Config`] - DataDirectory not configured in Tor
/// - [`Error::Control`] - Failed to configure Tor
///
/// # Example
///
/// ```rust,no_run
/// use stem_rs::controller::Controller;
/// use vanguards_rs::vanguards::VanguardState;
/// use vanguards_rs::config::Config;
/// use vanguards_rs::control::new_consensus_event;
///
/// # async fn example() -> Result<(), vanguards_rs::error::Error> {
/// let mut controller = Controller::from_port("127.0.0.1:9051".parse().unwrap()).await?;
/// controller.authenticate(None).await?;
///
/// let mut state = VanguardState::new("/tmp/vanguards.state");
/// let config = Config::default();
///
/// new_consensus_event(&mut controller, &mut state, &config).await?;
/// # Ok(())
/// # }
/// ```
///
/// # See Also
///
/// - [`get_consensus_weights`] - Consensus weight parsing
/// - [`configure_tor`] - Tor configuration
/// - [`VanguardState::replenish_layers`] - Guard replenishment
pub async fn new_consensus_event(
    controller: &mut Controller,
    state: &mut VanguardState,
    config: &Config,
) -> Result<()> {
    // Get routers from Tor
    let routers = get_network_statuses(controller).await?;

    // Get ExcludeNodes configuration
    let exclude_nodes_conf = controller
        .get_conf("ExcludeNodes")
        .await
        .ok()
        .and_then(|v| v.first().cloned())
        .unwrap_or_default();
    let geoip_exclude = controller
        .get_conf("GeoIPExcludeUnknown")
        .await
        .ok()
        .and_then(|v| v.first().cloned());
    let exclude = ExcludeNodes::parse(&exclude_nodes_conf, geoip_exclude.as_deref());

    // Get DataDirectory for consensus file
    let data_dir = controller
        .get_conf("DataDirectory")
        .await?
        .first()
        .cloned()
        .ok_or_else(|| {
            Error::Config("You must set a DataDirectory location option in your torrc.".to_string())
        })?;

    let consensus_file = Path::new(&data_dir).join("cached-microdesc-consensus");
    let weights = get_consensus_weights(&consensus_file)?;

    // Update vanguard state
    consensus_update(state, &routers, &weights, &exclude, config)?;

    // Configure Tor if vanguards enabled
    if config.enable_vanguards {
        configure_tor(controller, state, config).await?;
    }

    // Write state to file
    let state_path = Path::new(&state.state_file);
    state.write_to_file(state_path).map_err(|e| {
        plog(
            LogLevel::Error,
            &format!("Cannot write state to {}: {}", state.state_file, e),
        );
        e
    })?;

    Ok(())
}

/// Updates vanguard state based on new consensus.
fn consensus_update(
    state: &mut VanguardState,
    routers: &[RouterStatusEntry],
    weights: &HashMap<String, i64>,
    exclude: &ExcludeNodes,
    config: &Config,
) -> Result<()> {
    // Sort routers by measured bandwidth
    let mut sorted_routers: Vec<RouterStatusEntry> = routers.to_vec();
    sorted_routers.sort_by(|a, b| {
        let bw_a = a.measured.or(a.bandwidth).unwrap_or(0);
        let bw_b = b.measured.or(b.bandwidth).unwrap_or(0);
        bw_b.cmp(&bw_a)
    });

    // Create router map for lookups
    let router_map: HashMap<String, &RouterStatusEntry> = sorted_routers
        .iter()
        .map(|r| (r.fingerprint.clone(), r))
        .collect();

    // Create consensus fingerprint set
    let consensus_fps: std::collections::HashSet<String> = sorted_routers
        .iter()
        .map(|r| r.fingerprint.clone())
        .collect();

    // Create generator for vanguard selection
    let restriction = FlagsRestriction::new(
        vec![
            "Fast".to_string(),
            "Stable".to_string(),
            "Valid".to_string(),
        ],
        vec!["Authority".to_string()],
    );
    let restrictions = NodeRestrictionList::new(vec![Box::new(restriction)]);
    let generator = BwWeightedGenerator::new(
        sorted_routers.clone(),
        restrictions,
        weights.clone(),
        Position::Middle,
    )?;

    if state.enable_vanguards {
        // Remove guards that are no longer in consensus
        VanguardState::remove_down_from_layer(&mut state.layer2, &consensus_fps);
        VanguardState::remove_down_from_layer(&mut state.layer3, &consensus_fps);

        // Remove expired guards
        VanguardState::remove_expired_from_layer(&mut state.layer2);
        VanguardState::remove_expired_from_layer(&mut state.layer3);

        // Remove excluded guards
        VanguardState::remove_excluded_from_layer(&mut state.layer2, &router_map, exclude);
        VanguardState::remove_excluded_from_layer(&mut state.layer3, &router_map, exclude);

        // Replenish guard layers
        state.replenish_layers(&generator, exclude, &config.vanguards)?;
    }

    // Create generator for rendguard (with Exit flag allowed)
    let rend_restriction = FlagsRestriction::new(
        vec!["Fast".to_string(), "Valid".to_string()],
        vec!["Authority".to_string()],
    );
    let rend_restrictions = NodeRestrictionList::new(vec![Box::new(rend_restriction)]);
    let mut rend_generator = BwWeightedGenerator::new(
        sorted_routers,
        rend_restrictions,
        weights.clone(),
        Position::Middle,
    )?;

    // Repair exit weights for RP selection
    rend_generator.repair_exits();

    // Update rendguard use counts
    state
        .rendguard
        .xfer_use_counts(&rend_generator, &config.rendguard);

    Ok(())
}

/// Gets network statuses from Tor.
async fn get_network_statuses(controller: &mut Controller) -> Result<Vec<RouterStatusEntry>> {
    let response = controller
        .get_info("ns/all")
        .await
        .map_err(|e| Error::DescriptorUnavailable(format!("Cannot get network statuses: {}", e)))?;

    parse_network_statuses(&response)
}

/// Parses network status entries from GETINFO ns/all response.
fn parse_network_statuses(response: &str) -> Result<Vec<RouterStatusEntry>> {
    use chrono::Utc;
    use stem_rs::descriptor::router_status::RouterStatusEntryType;

    let mut routers = Vec::new();
    let mut current_router: Option<RouterStatusEntry> = None;

    for line in response.lines() {
        if line.starts_with("r ") {
            // Save previous router if any
            if let Some(router) = current_router.take() {
                routers.push(router);
            }

            // Parse r line: r nickname identity digest published IP ORPort DirPort
            let parts: Vec<&str> = line.split_whitespace().collect();
            if parts.len() >= 8 {
                let nickname = parts[1].to_string();
                let fingerprint = decode_base64_fingerprint(parts[2]);
                let address = parts[5]
                    .parse()
                    .unwrap_or_else(|_| "0.0.0.0".parse().unwrap());
                let or_port = parts[6].parse().unwrap_or(9001);

                current_router = Some(RouterStatusEntry::new(
                    RouterStatusEntryType::V3,
                    nickname,
                    fingerprint,
                    Utc::now(),
                    address,
                    or_port,
                ));
            }
        } else if let Some(stripped) = line.strip_prefix("s ") {
            // Parse s line: s Flag1 Flag2 ...
            if let Some(ref mut router) = current_router {
                router.flags = stripped.split_whitespace().map(|s| s.to_string()).collect();
            }
        } else if let Some(stripped) = line.strip_prefix("w ") {
            // Parse w line: w Bandwidth=X Measured=Y
            if let Some(ref mut router) = current_router {
                for part in stripped.split_whitespace() {
                    if let Some((key, value)) = part.split_once('=') {
                        if let Ok(v) = value.parse::<u64>() {
                            match key {
                                "Bandwidth" => router.bandwidth = Some(v),
                                "Measured" => router.measured = Some(v),
                                _ => {}
                            }
                        }
                    }
                }
            }
        }
    }

    // Don't forget the last router
    if let Some(router) = current_router {
        routers.push(router);
    }

    Ok(routers)
}

/// Decodes a base64-encoded fingerprint to hex.
fn decode_base64_fingerprint(b64: &str) -> String {
    // Add padding if needed
    let padded = match b64.len() % 4 {
        0 => b64.to_string(),
        2 => format!("{}==", b64),
        3 => format!("{}=", b64),
        _ => b64.to_string(),
    };

    // Decode base64
    let decoded = base64_decode(&padded).unwrap_or_default();

    // Convert to hex
    decoded.iter().map(|b| format!("{:02X}", b)).collect()
}

/// Simple base64 decoder.
fn base64_decode(input: &str) -> Option<Vec<u8>> {
    const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    let input = input.trim_end_matches('=');
    let mut output = Vec::new();
    let mut buffer = 0u32;
    let mut bits = 0;

    for c in input.bytes() {
        let value = ALPHABET.iter().position(|&x| x == c)? as u32;
        buffer = (buffer << 6) | value;
        bits += 6;

        if bits >= 8 {
            bits -= 8;
            output.push((buffer >> bits) as u8);
            buffer &= (1 << bits) - 1;
        }
    }

    Some(output)
}

/// Handles a signal event from Tor.
///
/// Processes signals received by the Tor daemon and takes appropriate action.
/// Currently handles SIGHUP/RELOAD to reapply vanguard configuration.
///
/// # Supported Signals
///
/// | Signal | Action |
/// |--------|--------|
/// | `RELOAD` (SIGHUP) | Reapply vanguard configuration to Tor |
///
/// # Arguments
///
/// * `controller` - The Tor controller
/// * `state` - The vanguard state
/// * `config` - The vanguards configuration
/// * `signal` - The signal name (e.g., "RELOAD")
///
/// # Returns
///
/// Returns `Ok(())` on successful handling.
///
/// # Errors
///
/// Returns [`Error::Control`] if reconfiguring Tor fails.
///
/// # Example
///
/// ```rust,no_run
/// use stem_rs::controller::Controller;
/// use vanguards_rs::vanguards::VanguardState;
/// use vanguards_rs::config::Config;
/// use vanguards_rs::control::signal_event;
///
/// # async fn example() -> Result<(), vanguards_rs::error::Error> {
/// let mut controller = Controller::from_port("127.0.0.1:9051".parse().unwrap()).await?;
/// controller.authenticate(None).await?;
///
/// let state = VanguardState::new("/tmp/vanguards.state");
/// let config = Config::default();
///
/// signal_event(&mut controller, &state, &config, "RELOAD").await?;
/// # Ok(())
/// # }
/// ```
pub async fn signal_event(
    controller: &mut Controller,
    state: &VanguardState,
    config: &Config,
    signal: &str,
) -> Result<()> {
    if signal == "RELOAD" {
        plog(LogLevel::Notice, "Tor got SIGHUP. Reapplying vanguards.");
        configure_tor(controller, state, config).await?;
    }
    Ok(())
}

/// Application state shared across event handlers.
///
/// `AppState` aggregates all the stateful components needed during the main
/// event loop. It is passed to event handlers to allow them to update their
/// respective state and access configuration.
///
/// # Components
///
/// ```text
/// ┌─────────────────────────────────────────────┐
/// │                  AppState                   │
/// │                                             │
/// │  ┌─────────────────┐  ┌─────────────────┐   │
/// │  │ VanguardState   │  │ BandwidthStats  │   │
/// │  │ • layer2 guards │  │ • circuit stats │   │
/// │  │ • layer3 guards │  │ • conn tracking │   │
/// │  │ • rendguard     │  │ • attack detect │   │
/// │  └─────────────────┘  └─────────────────┘   │
/// │                                             │
/// │  ┌─────────────────┐  ┌─────────────────┐   │
/// │  │ TimeoutStats    │  │ LogGuard        │   │
/// │  │ • CBT tracking  │  │ • log buffering │   │
/// │  │ • timeout rates │  │ • warn detection│   │
/// │  └─────────────────┘  └─────────────────┘   │
/// │                                             │
/// │  ┌─────────────────┐  ┌─────────────────┐   │
/// │  │ PathVerify      │  │ Config          │   │
/// │  │ • path checking │  │ • all settings  │   │
/// │  │ • guard verify  │  │ • thresholds    │   │
/// │  └─────────────────┘  └─────────────────┘   │
/// └────────────────────────────────────────────┘
/// ```
///
/// # Thread Safety
///
/// `AppState` is not thread-safe. It is designed to be used within a single
/// async task (the main event loop). For concurrent access, wrap in appropriate
/// synchronization primitives.
///
/// # Example
///
/// ```rust,no_run
/// use vanguards_rs::control::AppState;
/// use vanguards_rs::vanguards::VanguardState;
/// use vanguards_rs::config::Config;
///
/// // Create state for the event loop
/// let vanguard_state = VanguardState::new("/var/lib/tor/vanguards.state");
/// let config = Config::default();
/// let app_state = AppState::new(vanguard_state, config);
/// ```
///
/// # See Also
///
/// - [`VanguardState`] - Guard layer management
/// - [`BandwidthStats`] - Bandwidth attack detection
/// - [`TimeoutStats`] - Circuit build timeout verification
/// - [`LogGuard`] - Log buffering and analysis
/// - [`PathVerify`] - Circuit path verification
pub struct AppState {
    /// Vanguard state containing guard layers and rendguard.
    pub vanguard_state: VanguardState,
    /// Bandwidth statistics for attack detection.
    pub bandwidth_stats: BandwidthStats,
    /// Circuit build timeout statistics.
    pub timeout_stats: TimeoutStats,
    /// Optional log guard for log buffering and analysis.
    pub logguard: Option<LogGuard>,
    /// Optional path verifier for circuit path validation.
    pub pathverify: Option<PathVerify>,
    /// Application configuration.
    pub config: Config,
}

impl AppState {
    /// Creates a new application state with the given vanguard state and configuration.
    ///
    /// Initializes bandwidth and timeout statistics to empty state. LogGuard and
    /// PathVerify are initialized later in the control loop based on configuration.
    ///
    /// # Arguments
    ///
    /// * `vanguard_state` - The vanguard state (loaded from file or newly created)
    /// * `config` - The application configuration
    ///
    /// # Returns
    ///
    /// A new `AppState` with initialized statistics and the provided state/config.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use vanguards_rs::control::AppState;
    /// use vanguards_rs::vanguards::VanguardState;
    /// use vanguards_rs::config::Config;
    ///
    /// let state = VanguardState::new("/tmp/vanguards.state");
    /// let config = Config::default();
    /// let app_state = AppState::new(state, config);
    /// ```
    pub fn new(vanguard_state: VanguardState, config: Config) -> Self {
        Self {
            vanguard_state,
            bandwidth_stats: BandwidthStats::new(),
            timeout_stats: TimeoutStats::new(),
            logguard: None,
            pathverify: None,
            config,
        }
    }
}

/// Connects to Tor's control port.
///
/// Attempts connection in this order:
/// 1. Unix socket if configured
/// 2. TCP port if configured
/// 3. Default Unix socket /run/tor/control
/// 4. Default TCP port 127.0.0.1:9051
async fn connect_to_tor(config: &Config) -> Result<Controller> {
    // Try configured socket first
    if let Some(ref socket_path) = config.control_socket {
        match Controller::from_socket_file(socket_path.as_path()).await {
            Ok(controller) => {
                plog(
                    LogLevel::Notice,
                    &format!("Connected to Tor via socket {}", socket_path.display()),
                );
                return Ok(controller);
            }
            Err(e) => {
                return Err(Error::Control(e));
            }
        }
    }

    // Try configured port
    if let Some(port) = config.control_port {
        let addr = format!("{}:{}", config.control_ip, port);
        match Controller::from_port(
            addr.parse()
                .map_err(|e| Error::Config(format!("Invalid control address: {}", e)))?,
        )
        .await
        {
            Ok(controller) => {
                plog(
                    LogLevel::Notice,
                    &format!("Connected to Tor via control port {}", addr),
                );
                return Ok(controller);
            }
            Err(e) => {
                return Err(Error::Control(e));
            }
        }
    }

    // Try default socket
    if let Ok(controller) = Controller::from_socket_file(Path::new("/run/tor/control")).await {
        plog(
            LogLevel::Notice,
            "Connected to Tor via /run/tor/control socket",
        );
        return Ok(controller);
    }

    // Try default port
    let addr = format!("{}:9051", config.control_ip);
    match Controller::from_port(
        addr.parse()
            .map_err(|e| Error::Config(format!("Invalid control address: {}", e)))?,
    )
    .await
    {
        Ok(controller) => {
            plog(
                LogLevel::Notice,
                &format!("Connected to Tor via {} control port", addr),
            );
            Ok(controller)
        }
        Err(e) => Err(Error::Control(e)),
    }
}

/// Gets the list of event types to subscribe to based on configuration.
fn get_event_types(config: &Config, tor_version: &Version) -> Vec<EventType> {
    let mut events = Vec::new();

    // Always subscribe to these if vanguards or rendguard enabled
    if config.enable_vanguards || config.enable_rendguard {
        events.push(EventType::NewConsensus);
        events.push(EventType::Signal);
    }

    // Rendguard needs CIRC events
    if config.enable_rendguard {
        events.push(EventType::Circ);
    }

    // Bandguards events
    if config.enable_bandguards {
        events.push(EventType::Circ);
        events.push(EventType::Bw);
        events.push(EventType::OrConn);
        events.push(EventType::NetworkLiveness);

        // CIRC_BW and CIRC_MINOR require Tor 0.3.4.10+
        let min_version = Version::new(0, 3, 4).with_patch(10);
        if *tor_version >= min_version {
            events.push(EventType::CircBw);
            events.push(EventType::CircMinor);
        } else {
            plog(
                LogLevel::Notice,
                "In order for bandwidth-based protections to be enabled, you must use Tor 0.3.4.10 or newer.",
            );
        }
    }

    // CBT verify events
    if config.enable_cbtverify {
        events.push(EventType::Circ);
        events.push(EventType::BuildTimeoutSet);
    }

    // Path verify events
    if config.enable_pathverify {
        events.push(EventType::Circ);
        events.push(EventType::CircMinor);
        events.push(EventType::OrConn);
        events.push(EventType::Guard);
        events.push(EventType::ConfChanged);
    }

    // Log guard events
    if config.enable_logguard {
        events.push(EventType::Circ);
        events.push(EventType::Warn);

        // Add log events based on dump level
        let log_events = LogGuard::get_log_event_types(config.logguard.dump_level);
        for event_name in log_events {
            match event_name {
                "DEBUG" => events.push(EventType::Debug),
                "INFO" => events.push(EventType::Info),
                "NOTICE" => events.push(EventType::Notice),
                "WARN" => events.push(EventType::Warn),
                "ERR" => events.push(EventType::Err),
                _ => {}
            }
        }
    }

    // Deduplicate
    events.sort_by_key(|e| format!("{:?}", e));
    events.dedup();

    events
}

/// Handles a circuit event, dispatching to all enabled handlers.
fn handle_circ_event(state: &mut AppState, event: &stem_rs::events::CircuitEvent, arrived_at: f64) {
    let circ_id = &event.id.0;
    let status = format!("{:?}", event.status);
    let purpose = event.purpose.as_ref().map(|p| format!("{:?}", p));
    let hs_state = event.hs_state.as_ref().map(|s| format!("{:?}", s));
    let reason = event.reason.as_ref().map(|r| format!("{:?}", r));
    let path: Vec<String> = event.path.iter().map(|(fp, _)| fp.clone()).collect();

    // Rendguard: check for HS_SERVICE_REND in HSSR_CONNECTING
    if state.config.enable_rendguard {
        if let (Some(ref p), Some(ref hs)) = (&purpose, &hs_state) {
            if p == "HS_SERVICE_REND" && hs == "HSSR_CONNECTING" {
                // Get the rendezvous point (last hop in path)
                if let Some(rp_fp) = path.last() {
                    let valid = state
                        .vanguard_state
                        .rendguard
                        .valid_rend_use(rp_fp, &state.config.rendguard);
                    if !valid {
                        let usage_rate = state.vanguard_state.rendguard.usage_rate(rp_fp);
                        let expected = state.vanguard_state.rendguard.expected_weight(rp_fp);
                        plog(
                            LogLevel::Warn,
                            &format!(
                                "Possible rendezvous point overuse attack: {} used {:.2}% vs expected {:.2}%",
                                rp_fp, usage_rate, expected
                            ),
                        );
                    }
                }
            }
        }
    }

    // Bandguards
    if state.config.enable_bandguards {
        state.bandwidth_stats.circ_event(
            circ_id,
            &status,
            purpose.as_deref().unwrap_or("GENERAL"),
            hs_state.as_deref(),
            &path,
            reason.as_deref(),
            arrived_at,
        );
    }

    // CBT verify
    if state.config.enable_cbtverify {
        state.timeout_stats.circ_event(
            circ_id,
            &status,
            purpose.as_deref().unwrap_or("GENERAL"),
            hs_state.as_deref(),
            reason.as_deref(),
        );
    }

    // Log guard
    if state.config.enable_logguard {
        if let Some(ref mut lg) = state.logguard {
            lg.circ_event(circ_id, &status, reason.as_deref());
        }
    }

    // Path verify
    if state.config.enable_pathverify {
        if let Some(ref mut pv) = state.pathverify {
            pv.circ_event(
                circ_id,
                &status,
                purpose.as_deref().unwrap_or("GENERAL"),
                hs_state.as_deref(),
                &event.path,
            );
        }
    }
}

/// Handles a circuit bandwidth event.
fn handle_circbw_event(
    state: &mut AppState,
    event: &stem_rs::events::CircuitBandwidthEvent,
    arrived_at: f64,
) {
    if state.config.enable_bandguards {
        state.bandwidth_stats.circbw_event(
            &event.id.0,
            event.read,
            event.written,
            event.delivered_read.unwrap_or(0),
            event.delivered_written.unwrap_or(0),
            event.overhead_read.unwrap_or(0),
            event.overhead_written.unwrap_or(0),
            arrived_at,
        );
    }
}

/// Handles a circuit minor event.
#[allow(dead_code)]
fn handle_circ_minor_event(state: &mut AppState, event: &stem_rs::events::CircuitEvent) {
    let circ_id = &event.id.0;
    let purpose = event.purpose.as_ref().map(|p| format!("{:?}", p));
    let hs_state = event.hs_state.as_ref().map(|s| format!("{:?}", s));
    let path: Vec<String> = event.path.iter().map(|(fp, _)| fp.clone()).collect();

    // Bandguards
    if state.config.enable_bandguards {
        // For CIRC_MINOR, we need old_purpose and old_hs_state which aren't in the event
        // We'll pass None for now as the event doesn't provide these
        state.bandwidth_stats.circ_minor_event(
            circ_id,
            "PURPOSE_CHANGED",
            purpose.as_deref().unwrap_or("GENERAL"),
            hs_state.as_deref(),
            None, // old_purpose
            None, // old_hs_state
            &path,
        );
    }

    // Path verify
    if state.config.enable_pathverify {
        if let Some(ref mut pv) = state.pathverify {
            pv.circ_minor_event(
                circ_id,
                purpose.as_deref().unwrap_or("GENERAL"),
                None, // old_purpose
                &event.path,
            );
        }
    }
}

/// Handles a raw CIRC_MINOR event from Unknown variant.
///
/// CIRC_MINOR events indicate minor changes to circuits like purpose changes.
/// Format: CircuitID EVENT [Path] [PURPOSE=...] [HS_STATE=...] [OLD_PURPOSE=...] [OLD_HS_STATE=...]
fn handle_circ_minor_raw(state: &mut AppState, content: &str) {
    let parts: Vec<&str> = content.split_whitespace().collect();
    if parts.len() < 2 {
        return;
    }

    let circ_id = parts[0];
    let _event_type = parts[1]; // e.g., "PURPOSE_CHANGED"

    // Parse path and key-value pairs
    let mut path: Vec<(String, Option<String>)> = Vec::new();
    let mut purpose: Option<String> = None;
    let mut hs_state: Option<String> = None;
    let mut old_purpose: Option<String> = None;
    let mut old_hs_state: Option<String> = None;

    for part in parts.iter().skip(2) {
        if let Some((key, value)) = part.split_once('=') {
            match key {
                "PURPOSE" => purpose = Some(value.to_string()),
                "HS_STATE" => hs_state = Some(value.to_string()),
                "OLD_PURPOSE" => old_purpose = Some(value.to_string()),
                "OLD_HS_STATE" => old_hs_state = Some(value.to_string()),
                _ => {}
            }
        } else if part.starts_with('$') || part.contains('~') || part.contains(',') {
            // Parse path
            for hop in part.split(',') {
                let hop = hop.trim_start_matches('$');
                if let Some((fp, nick)) = hop.split_once('~') {
                    path.push((fp.to_string(), Some(nick.to_string())));
                } else if let Some((fp, nick)) = hop.split_once('=') {
                    path.push((fp.to_string(), Some(nick.to_string())));
                } else if !hop.is_empty() {
                    path.push((hop.to_string(), None));
                }
            }
        }
    }

    // Bandguards
    if state.config.enable_bandguards {
        let path_fps: Vec<String> = path.iter().map(|(fp, _)| fp.clone()).collect();
        state.bandwidth_stats.circ_minor_event(
            circ_id,
            _event_type,
            purpose.as_deref().unwrap_or("GENERAL"),
            hs_state.as_deref(),
            old_purpose.as_deref(),
            old_hs_state.as_deref(),
            &path_fps,
        );
    }

    // Path verify
    if state.config.enable_pathverify {
        if let Some(ref mut pv) = state.pathverify {
            pv.circ_minor_event(
                circ_id,
                purpose.as_deref().unwrap_or("GENERAL"),
                old_purpose.as_deref(),
                &path,
            );
        }
    }
}

/// Handles an OR connection event.
fn handle_orconn_event(
    state: &mut AppState,
    event: &stem_rs::events::OrConnEvent,
    arrived_at: f64,
) {
    let status = format!("{:?}", event.status);
    let reason = event.reason.as_ref().map(|r| format!("{:?}", r));
    let conn_id = event.id.as_deref().unwrap_or("");

    // Bandguards
    if state.config.enable_bandguards {
        state.bandwidth_stats.orconn_event(
            conn_id,
            &event.target,
            &status,
            reason.as_deref(),
            arrived_at,
        );
    }

    // Path verify
    if state.config.enable_pathverify {
        if let Some(ref mut pv) = state.pathverify {
            // Extract fingerprint from target (format: $fingerprint~nickname or $fingerprint=nickname)
            let guard_fp = if event.target.starts_with('$') {
                event.target[1..].split(['~', '=']).next().unwrap_or("")
            } else {
                &event.target
            };
            pv.orconn_event(guard_fp, &status);
        }
    }
}

/// Handles a bandwidth event (1x/sec heartbeat).
fn handle_bw_event(
    state: &mut AppState,
    _event: &stem_rs::events::BandwidthEvent,
    arrived_at: f64,
) {
    if state.config.enable_bandguards {
        state
            .bandwidth_stats
            .check_connectivity(arrived_at, &state.config.bandguards);
    }
}

/// Handles a network liveness event.
fn handle_network_liveness_event(
    state: &mut AppState,
    event: &stem_rs::events::NetworkLivenessEvent,
    arrived_at: f64,
) {
    if state.config.enable_bandguards {
        let status = format!("{:?}", event.status);
        state
            .bandwidth_stats
            .network_liveness_event(&status, arrived_at);
    }
}

/// Handles a build timeout set event.
fn handle_buildtimeout_set_event(
    state: &mut AppState,
    event: &stem_rs::events::BuildTimeoutSetEvent,
) {
    if state.config.enable_cbtverify {
        let set_type = format!("{:?}", event.set_type);
        state.timeout_stats.cbt_event(&set_type, event.timeout_rate);
    }
}

/// Handles a guard event.
fn handle_guard_event(state: &mut AppState, event: &stem_rs::events::GuardEvent) {
    if state.config.enable_pathverify {
        if let Some(ref mut pv) = state.pathverify {
            let status = format!("{:?}", event.status);
            // Use endpoint_fingerprint directly from the event
            pv.guard_event(&event.endpoint_fingerprint, &status);
        }
    }
}

/// Handles a configuration changed event.
fn handle_conf_changed_event(state: &mut AppState, event: &stem_rs::events::ConfChangedEvent) {
    if state.config.enable_pathverify {
        if let Some(ref mut pv) = state.pathverify {
            pv.conf_changed_event(&event.changed);
        }
    }
}

/// Handles a log event.
fn handle_log_event(state: &mut AppState, event: &stem_rs::events::LogEvent, arrived_at: f64) {
    if state.config.enable_logguard {
        if let Some(ref mut lg) = state.logguard {
            let runlevel = format!("{:?}", event.runlevel);
            lg.log_event_with_timestamp(&runlevel, &event.message, arrived_at);

            // Also handle warn events specially
            if matches!(event.runlevel, stem_rs::Runlevel::Warn) {
                lg.log_warn_event(&event.message);
            }
        }
    }
}

/// Handles a signal event.
async fn handle_signal_event(
    controller: &mut Controller,
    state: &mut AppState,
    event: &stem_rs::events::SignalEvent,
) -> Result<()> {
    let signal_name = format!("{:?}", event.signal);
    signal_event(
        controller,
        &state.vanguard_state,
        &state.config,
        &signal_name,
    )
    .await
}

/// Main control loop for event processing.
///
/// Connects to Tor, authenticates, initializes state, and processes events
/// in a continuous loop until the connection is lost or an error occurs.
///
/// # Flow
///
/// 1. Connect to Tor's control port (socket or TCP)
/// 2. Authenticate using available methods
/// 3. Get Tor version for feature detection
/// 4. Initialize vanguard state from consensus
/// 5. Initialize optional components (logguard, pathverify)
/// 6. Subscribe to configured event types
/// 7. Process events until connection closes
///
/// # Arguments
///
/// * `state` - The application state containing all protection components
///
/// # Returns
///
/// Returns a status string:
/// - `"closed"` - Connection was closed normally
/// - `"failed: <reason>"` - Connection or operation failed
///
/// # Event Processing
///
/// The loop dispatches events to appropriate handlers:
///
/// | Event Type | Handlers |
/// |------------|----------|
/// | CIRC | RendGuard, BandGuards, CBTVerify, PathVerify, LogGuard |
/// | CIRC_BW | BandGuards |
/// | CIRC_MINOR | BandGuards, PathVerify |
/// | ORCONN | BandGuards, PathVerify |
/// | BW | BandGuards (connectivity check) |
/// | NEWCONSENSUS | VanguardState update |
/// | SIGNAL | Configuration reload (SIGHUP) |
///
/// # Example
///
/// ```rust,no_run
/// use vanguards_rs::control::{AppState, control_loop};
/// use vanguards_rs::vanguards::VanguardState;
/// use vanguards_rs::config::Config;
///
/// # async fn example() {
/// let state = VanguardState::new("/tmp/vanguards.state");
/// let config = Config::default();
/// let mut app_state = AppState::new(state, config);
///
/// let result = control_loop(&mut app_state).await;
/// println!("Control loop exited: {}", result);
/// # }
/// ```
///
/// # See Also
///
/// - [`run_main`] - Higher-level entry point with reconnection support
/// - [`authenticate_any`] - Authentication implementation
/// - [`new_consensus_event`] - Consensus processing
pub async fn control_loop(state: &mut AppState) -> String {
    // Connect to Tor
    let mut controller = match connect_to_tor(&state.config).await {
        Ok(c) => c,
        Err(e) => return format!("failed: {}", e),
    };

    // Authenticate
    if let Err(e) = authenticate_any(&mut controller, state.config.control_pass.as_deref()).await {
        return format!("failed: {}", e);
    }

    // Get Tor version for feature detection
    let tor_version = match controller.get_version().await {
        Ok(v) => v,
        Err(e) => return format!("failed: {}", e),
    };

    // Initialize vanguard state from consensus
    if state.config.enable_vanguards || state.config.enable_rendguard {
        match new_consensus_event(&mut controller, &mut state.vanguard_state, &state.config).await {
            Ok(()) => {}
            Err(Error::DescriptorUnavailable(msg)) => {
                plog(
                    LogLevel::Notice,
                    &format!("Tor needs descriptors: {}. Trying again...", msg),
                );
                return format!("failed: {}", msg);
            }
            Err(e) => return format!("failed: {}", e),
        }
    }

    // Handle one-shot mode
    if state.config.one_shot_vanguards {
        // Note: SaveConf is not available in stem-rs Signal enum
        // We just exit after setting vanguards - user should save config manually if needed
        plog(
            LogLevel::Notice,
            "Updated vanguards. Exiting (one-shot mode).",
        );
        std::process::exit(0);
    }

    // Initialize logguard if enabled
    if state.config.enable_logguard {
        state.logguard = Some(LogGuard::new(&state.config.logguard));
    }

    // Initialize pathverify if enabled
    if state.config.enable_pathverify {
        state.pathverify = Some(PathVerify::new(
            state.config.enable_vanguards,
            state.config.vanguards.num_layer1_guards,
            state.config.vanguards.num_layer2_guards,
            state.config.vanguards.num_layer3_guards,
        ));

        // Send NEWNYM to get fresh circuits
        if let Err(e) = controller.signal(stem_rs::Signal::Newnym).await {
            plog(LogLevel::Warn, &format!("Failed to send NEWNYM: {}", e));
        }
    }

    // Subscribe to events
    let event_types = get_event_types(&state.config, &tor_version);
    if let Err(e) = controller.set_events(&event_types).await {
        return format!("failed: {}", e);
    }

    // Main event loop
    loop {
        match controller.recv_event().await {
            Ok(event) => {
                let arrived_at = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .map(|d| d.as_secs_f64())
                    .unwrap_or(0.0);

                match event {
                    ParsedEvent::Circuit(ref e) => {
                        handle_circ_event(state, e, arrived_at);
                    }
                    ParsedEvent::CircuitBandwidth(ref e) => {
                        handle_circbw_event(state, e, arrived_at);
                    }
                    ParsedEvent::OrConn(ref e) => {
                        handle_orconn_event(state, e, arrived_at);
                    }
                    ParsedEvent::Bandwidth(ref e) => {
                        handle_bw_event(state, e, arrived_at);
                    }
                    ParsedEvent::NetworkLiveness(ref e) => {
                        handle_network_liveness_event(state, e, arrived_at);
                    }
                    ParsedEvent::BuildTimeoutSet(ref e) => {
                        handle_buildtimeout_set_event(state, e);
                    }
                    ParsedEvent::Guard(ref e) => {
                        handle_guard_event(state, e);
                    }
                    ParsedEvent::ConfChanged(ref e) => {
                        handle_conf_changed_event(state, e);
                    }
                    ParsedEvent::Log(ref e) => {
                        handle_log_event(state, e, arrived_at);
                    }
                    ParsedEvent::Signal(ref e) => {
                        if let Err(err) = handle_signal_event(&mut controller, state, e).await {
                            plog(LogLevel::Warn, &format!("Signal event error: {}", err));
                        }
                    }
                    ParsedEvent::Unknown {
                        ref event_type,
                        ref content,
                    } => {
                        // Handle NEWCONSENSUS specially since it may not be in ParsedEvent
                        if event_type == "NEWCONSENSUS" {
                            if let Err(err) = new_consensus_event(
                                &mut controller,
                                &mut state.vanguard_state,
                                &state.config,
                            )
                            .await
                            {
                                plog(LogLevel::Warn, &format!("Consensus event error: {}", err));
                            }
                        } else if event_type == "CIRC_MINOR" {
                            // Parse CIRC_MINOR event manually
                            // Format: CircuitID EVENT [Path] [PURPOSE=...] [HS_STATE=...] [OLD_PURPOSE=...] [OLD_HS_STATE=...]
                            handle_circ_minor_raw(state, content);
                        }
                    }
                    _ => {
                        // Ignore other events
                    }
                }

                // Check circuit limits after bandwidth events
                if state.config.enable_bandguards {
                    let circs_to_check: Vec<String> =
                        state.bandwidth_stats.circs.keys().cloned().collect();
                    for circ_id in circs_to_check {
                        let limit_result = state
                            .bandwidth_stats
                            .check_circuit_limits(&circ_id, &state.config.bandguards);
                        match limit_result {
                            crate::bandguards::CircuitLimitResult::Ok => {}
                            crate::bandguards::CircuitLimitResult::TorBug {
                                bug_id,
                                dropped_cells,
                            } => {
                                plog(
                                    LogLevel::Info,
                                    &format!(
                                        "Tor bug {} (dropped {} cells): {}",
                                        bug_id, dropped_cells, circ_id
                                    ),
                                );
                            }
                            crate::bandguards::CircuitLimitResult::DroppedCells {
                                dropped_cells,
                            } => {
                                plog(
                                    LogLevel::Warn,
                                    &format!(
                                        "Dropped cells attack ({} cells): {}",
                                        dropped_cells, circ_id
                                    ),
                                );
                                try_close_circuit(
                                    &mut controller,
                                    &circ_id,
                                    state.logguard.as_mut(),
                                )
                                .await;
                            }
                            crate::bandguards::CircuitLimitResult::MaxBytesExceeded {
                                bytes,
                                limit,
                            } => {
                                plog(
                                    LogLevel::Warn,
                                    &format!(
                                        "Circuit {} exceeded max bytes ({} > {})",
                                        circ_id, bytes, limit
                                    ),
                                );
                                try_close_circuit(
                                    &mut controller,
                                    &circ_id,
                                    state.logguard.as_mut(),
                                )
                                .await;
                            }
                            crate::bandguards::CircuitLimitResult::HsdirBytesExceeded {
                                bytes,
                                limit,
                            } => {
                                plog(
                                    LogLevel::Warn,
                                    &format!(
                                        "HSDIR circuit {} exceeded max bytes ({} > {})",
                                        circ_id, bytes, limit
                                    ),
                                );
                                try_close_circuit(
                                    &mut controller,
                                    &circ_id,
                                    state.logguard.as_mut(),
                                )
                                .await;
                            }
                            crate::bandguards::CircuitLimitResult::ServIntroBytesExceeded {
                                bytes,
                                limit,
                            } => {
                                plog(
                                    LogLevel::Warn,
                                    &format!(
                                        "Service intro circuit {} exceeded max bytes ({} > {})",
                                        circ_id, bytes, limit
                                    ),
                                );
                                try_close_circuit(
                                    &mut controller,
                                    &circ_id,
                                    state.logguard.as_mut(),
                                )
                                .await;
                            }
                        }
                    }
                }
            }
            Err(e) => {
                // Connection closed or error
                plog(LogLevel::Debug, &format!("Event receive error: {}", e));
                return "closed".to_string();
            }
        }
    }
}

/// Runs the main application loop with reconnection support.
///
/// This is the primary entry point for the vanguards application. It manages
/// the complete lifecycle including connection, reconnection, and graceful shutdown.
///
/// # Lifecycle
///
/// ```text
/// ┌─────────────────────────────────────────────────────────────┐
/// │                      run_main()                             │
/// │                                                             │
/// │  1. Set up CTRL+C handler                                   │
/// │  2. Load/create vanguard state                              │
/// │  3. Enter reconnection loop:                                │
/// │     ┌─────────────────────────────────────────────────────┐ │
/// │     │  • Check shutdown flag                              │ │
/// │     │  • Check retry limit                                │ │
/// │     │  • Run control_loop()                               │ │
/// │     │  • Log disconnection                                │ │
/// │     │  • Wait 1 second                                    │ │
/// │     │  • Increment reconnect counter                      │ │
/// │     └─────────────────────────────────────────────────────┘ │
/// │  4. Exit when shutdown or retry limit reached               │
/// └─────────────────────────────────────────────────────────────┘
/// ```
///
/// # Arguments
///
/// * `config` - The application configuration
///
/// # Returns
///
/// Returns `Ok(())` on graceful shutdown, or an error if the application
/// fails to start or encounters an unrecoverable error.
///
/// # Errors
///
/// Returns [`Error::Config`] if:
/// - Failed to connect to Tor after all retry attempts
/// - Invalid configuration values
///
/// # Shutdown Behavior
///
/// The function handles graceful shutdown via:
/// - CTRL+C signal (sets shutdown flag)
/// - Retry limit reached (configurable via `config.retry_limit`)
///
/// # Example
///
/// ```rust,no_run
/// use vanguards_rs::config::Config;
/// use vanguards_rs::control::run_main;
///
/// #[tokio::main]
/// async fn main() -> Result<(), vanguards_rs::error::Error> {
///     // Load configuration from file or use defaults
///     let config = Config::default();
///     
///     // Run until shutdown signal or error
///     run_main(config).await
/// }
/// ```
///
/// # See Also
///
/// - [`control_loop`] - The inner event processing loop
/// - [`Config`] - Configuration options
/// - [`VanguardState`] - State persistence
pub async fn run_main(config: Config) -> Result<()> {
    // Set up CTRL+C handler
    let shutdown = Arc::new(AtomicBool::new(false));
    let shutdown_clone = shutdown.clone();

    tokio::spawn(async move {
        if let Ok(()) = tokio::signal::ctrl_c().await {
            plog(LogLevel::Notice, "Got CTRL+C. Exiting.");
            shutdown_clone.store(true, Ordering::SeqCst);
        }
    });

    // Set close circuits flag from config
    set_close_circuits(config.close_circuits);

    // Load or create vanguard state
    let state_path = &config.state_file;
    let vanguard_state = match VanguardState::read_from_file(state_path) {
        Ok(mut state) => {
            plog(
                LogLevel::Info,
                &format!("Current layer2 guards: {}", state.layer2_guardset()),
            );
            plog(
                LogLevel::Info,
                &format!("Current layer3 guards: {}", state.layer3_guardset()),
            );
            state.enable_vanguards = config.enable_vanguards;
            state
        }
        Err(_) => {
            plog(
                LogLevel::Notice,
                &format!(
                    "Creating new vanguard state file at: {}",
                    state_path.display()
                ),
            );
            let mut state = VanguardState::new(&state_path.to_string_lossy());
            state.enable_vanguards = config.enable_vanguards;
            state
        }
    };

    let mut app_state = AppState::new(vanguard_state, config.clone());

    let mut reconnects = 0u32;
    let mut last_connected_at: Option<f64> = None;
    let mut connected = false;

    loop {
        // Check for shutdown
        if shutdown.load(Ordering::SeqCst) {
            break;
        }

        // Check retry limit
        if let Some(limit) = config.retry_limit {
            if reconnects >= limit {
                break;
            }
        }

        let result = control_loop(&mut app_state).await;

        if last_connected_at.is_none() {
            last_connected_at = Some(
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .map(|d| d.as_secs_f64())
                    .unwrap_or(0.0),
            );
        }

        if result == "closed" {
            connected = true;
        }

        // Log reconnection attempts (every 10 seconds or on first close)
        if result == "closed" || reconnects.is_multiple_of(10) {
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_secs_f64())
                .unwrap_or(0.0);

            let disconnected_secs = now - last_connected_at.unwrap_or(now);
            let max_disconnected = config.bandguards.conn_max_disconnected_secs as f64;

            if disconnected_secs > max_disconnected {
                plog(
                    LogLevel::Warn,
                    &format!("Tor daemon connection {}. Trying again...", result),
                );
            } else {
                plog(
                    LogLevel::Notice,
                    &format!("Tor daemon connection {}. Trying again...", result),
                );
            }
        }

        reconnects += 1;

        // Wait before reconnecting
        tokio::time::sleep(Duration::from_secs(1)).await;
    }

    if !connected {
        return Err(Error::Config("Failed to connect to Tor".to_string()));
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_get_consensus_weights() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(
            file,
            "network-status-version 3 microdesc\n\
             bandwidth-weights Wbd=0 Wbe=0 Wbg=4194 Wbm=10000 Wdb=10000 Wed=10000 Wee=10000 Weg=10000 Wem=10000 Wgb=10000 Wgd=0 Wgg=5806 Wgm=5806 Wmb=10000 Wmd=0 Wme=0 Wmg=4194 Wmm=10000"
        )
        .unwrap();

        let weights = get_consensus_weights(file.path()).unwrap();

        assert_eq!(weights.get("Wmm"), Some(&10000));
        assert_eq!(weights.get("Wgg"), Some(&5806));
        assert_eq!(weights.get("Wbd"), Some(&0));
    }

    #[test]
    fn test_get_consensus_weights_missing() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "network-status-version 3 microdesc").unwrap();

        let result = get_consensus_weights(file.path());
        assert!(result.is_err());
    }

    #[test]
    fn test_base64_decode() {
        // Test standard base64 decoding
        let decoded = base64_decode("SGVsbG8=").unwrap();
        assert_eq!(decoded, b"Hello");

        // Test without padding
        let decoded = base64_decode("SGVsbG8").unwrap();
        assert_eq!(decoded, b"Hello");
    }

    #[test]
    fn test_decode_base64_fingerprint() {
        // A typical Tor fingerprint in base64 (27 chars without padding)
        // 20 bytes = 160 bits, which is 27 base64 chars (ceil(160/6) = 27)
        let b64 = "AAAAAAAAAAAAAAAAAAAAAAAAAAA";
        let hex = decode_base64_fingerprint(b64);
        // Should produce 40 hex characters (20 bytes)
        assert_eq!(hex.len(), 40);
        assert!(hex.chars().all(|c| c.is_ascii_hexdigit()));
    }

    #[test]
    fn test_parse_network_statuses() {
        let response = "\
r relay1 AAAAAAAAAAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBBBBBBBBBB 2024-01-01 00:00:00 192.168.1.1 9001 0
s Fast Guard Running Stable Valid
w Bandwidth=1000 Measured=900
r relay2 CCCCCCCCCCCCCCCCCCCCCCCCCCCC DDDDDDDDDDDDDDDDDDDDDDDDDDDD 2024-01-01 00:00:00 192.168.1.2 9002 0
s Fast Running Stable Valid Exit
w Bandwidth=2000";

        let routers = parse_network_statuses(response).unwrap();
        assert_eq!(routers.len(), 2);

        assert_eq!(routers[0].nickname, "relay1");
        assert!(routers[0].flags.contains(&"Guard".to_string()));
        assert_eq!(routers[0].bandwidth, Some(1000));
        assert_eq!(routers[0].measured, Some(900));

        assert_eq!(routers[1].nickname, "relay2");
        assert!(routers[1].flags.contains(&"Exit".to_string()));
        assert_eq!(routers[1].bandwidth, Some(2000));
        assert_eq!(routers[1].measured, None);
    }

    #[test]
    fn test_close_circuits_flag() {
        set_close_circuits(true);
        assert!(get_close_circuits());

        set_close_circuits(false);
        assert!(!get_close_circuits());

        // Reset to default
        set_close_circuits(true);
    }
}