alighieri 0.4.0

Alighieri — a lightweight, secure, asynchronous SOCKS5 proxy server with Dante-inspired configuration
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
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
//! The Alighieri configuration model and its Dante-inspired parser.
//!
//! The configuration language borrows Dante's `sockd.conf` look and feel:
//! line-oriented `keyword: value` settings plus brace-delimited
//! `client`/`socks` rule blocks. It is **not** a copy of Dante's grammar — it
//! is a clean, independent implementation of a familiar style.
//!
//! # Example
//!
//! ```text
//! # Listen for clients on all interfaces, port 1080.
//! internal: 0.0.0.0 port = 1080
//! # Make outbound connections from this address (0.0.0.0 = OS default).
//! external: 0.0.0.0
//!
//! # Offer "no auth" and username/password; require a userlist for the latter.
//! socksmethod: username none
//! userlist: /etc/alighieri/users
//! auth.cachettl: 300
//!
//! connecttimeout: 30
//! handshaketimeout: 10
//! iotimeout: 0
//! udptimeout: 60
//! maxconnections: 1024
//! logoutput: stdout
//! logformat: text
//! dns.prefer: system
//! dns.tryall: false
//! dns.cachettl: 0
//! dns.timeout: 5
//! # metrics.listen: 127.0.0.1:9090
//! # tls.certfile: /etc/alighieri/tls/server.crt
//! # tls.keyfile: /etc/alighieri/tls/server.key
//!
//! # Admission: accept connections from the RFC1918 LAN only.
//! client pass {
//!     from: 10.0.0.0/8 to: 0.0.0.0/0
//! }
//!
//! # Authorisation: allow TCP CONNECT and UDP anywhere except loopback.
//! socks block {
//!     from: 0.0.0.0/0 to: 127.0.0.0/8
//! }
//! socks pass {
//!     from: 0.0.0.0/0 to: 0.0.0.0/0
//!     protocol: tcp udp
//!     command: connect udpassociate
//! }
//! ```

use std::fs;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::{Component, Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;

use crate::errors::{Error, Result};
use crate::socks5::Method;

// These are part of the supported configuration vocabulary. Re-export them
// through `config` so `Config` and `Rule` never force consumers through the
// doc-hidden engine modules where the implementations live.
pub use crate::acl::{Rule, RuleSet, Scope, Verdict};
pub use crate::net::{AddrSpec, Cidr, HostPattern, PortRange};
pub use crate::socks5::Command;

/// An authentication kind usable both as a server-offered method and as a
/// per-rule selector.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AuthKind {
    /// No authentication (`socksmethod: none`).
    None,
    /// RFC 1929 username/password (`socksmethod: username`).
    Username,
}

impl AuthKind {
    /// Maps to the corresponding SOCKS5 method byte.
    pub(crate) fn to_method(self) -> Method {
        match self {
            AuthKind::None => Method::NoAuth,
            AuthKind::Username => Method::UserPass,
        }
    }
}

/// A transport protocol selector for `socks` rules.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
// Re-exported by the plugin SDK; `#[non_exhaustive]` so a future transport stays
// an additive change for plugins that match on it.
#[non_exhaustive]
pub enum Protocol {
    Tcp,
    Udp,
}

/// Where log lines are emitted.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum LogOutput {
    Stdout,
    Stderr,
    File,
}

/// Log record encoding.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum LogFormat {
    Text,
    Json,
}

/// Address-family ordering for DNS results.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum DnsPreference {
    System,
    Ipv4,
    Ipv6,
}

/// Destination IP categories that may be denied after DNS resolution.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum DnsDenyCategory {
    Private,
    LinkLocal,
    Loopback,
    Multicast,
    Unspecified,
    Documentation,
    Reserved,
}

/// DNS resolution policy used by TCP CONNECT and UDP ASSOCIATE.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct DnsPolicy {
    pub preference: DnsPreference,
    pub try_all: bool,
    pub deny: Vec<DnsDenyCategory>,
    pub cache_ttl: Option<Duration>,
    /// Deadline for resolving one destination name, as monotonic elapsed time
    /// (enforced with `tokio::time::timeout`). Bounds how long a CONNECT or a
    /// domain-target UDP datagram *waits* on a slow or wedged resolver, so it
    /// cannot pin a connection permit or stall UDP forwarding. It does not cancel
    /// the underlying system `getaddrinfo`, which runs on a blocking thread and
    /// keeps running until the OS resolver returns — this bounds the caller's
    /// wait, not the resolver thread's lifetime.
    pub timeout: Duration,
}

/// TLS listener settings. When present, accepted client TCP connections are
/// upgraded to TLS before the SOCKS5 greeting is read. The certificate is
/// either operator-provided files or obtained automatically over ACME.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum TlsConfig {
    /// Operator-provided certificate and private key (PEM).
    Files {
        cert_file: PathBuf,
        key_file: PathBuf,
    },
    /// Automatic certificates from an ACME provider (Let's Encrypt), answered
    /// with the TLS-ALPN-01 challenge on the TLS listener itself.
    Acme(AcmeConfig),
}

/// ACME (Let's Encrypt) settings for automatic TLS certificates. Validation uses
/// TLS-ALPN-01, so the listener must be reachable at each domain on port 443.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct AcmeConfig {
    /// Domains the certificate covers (the SANs).
    pub domains: Vec<String>,
    /// Optional account contact e-mail (e.g. for expiry notices).
    pub email: Option<String>,
    /// Directory persisting the ACME account and issued certificates across
    /// restarts, so they are not re-requested (which would hit rate limits).
    pub cache_dir: PathBuf,
    /// Use the provider's staging environment — for testing, with far looser
    /// rate limits but untrusted certificates.
    pub staging: bool,
}

/// A fixed-window rate limit.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct RateLimit {
    pub limit: u64,
    pub window: Duration,
}

/// Optional per-client abuse controls.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct RateLimits {
    pub connection_rate: Option<RateLimit>,
    pub auth_failure_rate: Option<RateLimit>,
    pub concurrent_connections: Option<usize>,
    pub byte_rate: Option<RateLimit>,
}

/// The fully validated runtime configuration.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Config {
    /// Address the proxy listens on for clients (Dante `internal`).
    pub internal: SocketAddr,
    /// Local address used as the source for outbound connections
    /// (Dante `external`). `0.0.0.0` lets the OS choose.
    pub external: IpAddr,
    /// Trusted upstream CIDRs permitted to send a PROXY protocol header. Empty
    /// disables PROXY protocol. When non-empty, a connection from a listed
    /// source must begin with a v1/v2 header (its advertised client address then
    /// drives rules, abuse limits, metrics, and logs), and connections from any
    /// other source are rejected.
    pub proxy_protocol: Vec<Cidr>,
    /// Authentication methods offered to clients, in preference order.
    pub socks_methods: Vec<AuthKind>,
    /// Maximum time allowed to establish an outbound connection.
    pub connect_timeout: Duration,
    /// Maximum time allowed for pre-relay SOCKS negotiation.
    pub handshake_timeout: Duration,
    /// Idle timeout for established relays. `Duration::ZERO` disables it.
    pub io_timeout: Duration,
    /// Idle timeout for UDP associations.
    pub udp_timeout: Duration,
    /// Inclusive UDP port range for the client-facing relay socket (the
    /// `BND.PORT` advertised in the ASSOCIATE reply, where clients send their
    /// datagrams). `None` lets the OS pick an ephemeral port. Useful for
    /// firewalling: open exactly this range to the proxy for inbound UDP.
    pub udp_port_range: Option<PortRange>,
    /// Whether a UDP ASSOCIATE reply must come from the exact remote `host:port`
    /// the client sent to, rather than merely the same host. Defaults on (`true`);
    /// set `udp.strictreply: false` to relax to host-only matching for servers
    /// that legitimately answer from a different port (e.g. TFTP).
    pub udp_strict_reply: bool,
    /// Public host advertised as the `BND.ADDR` in the UDP ASSOCIATE reply (the
    /// real bound relay port is kept), for a proxy reached via NAT or a different
    /// public address than it binds on locally. An IP is advertised directly; a
    /// hostname is resolved per UDP association through the async resolver (so
    /// config load does no DNS). `None` advertises the locally-bound relay address.
    pub udp_advertise: Option<UdpAdvertise>,
    /// How long shutdown waits for in-flight connections to finish before
    /// aborting the rest (`shutdown.draintimeout`, seconds). `0` cuts them
    /// immediately. Read at process start; a reload does not change it.
    pub shutdown_drain_timeout: Duration,
    /// Path to the username/password database (required if `username` is
    /// offered as a method).
    pub userlist: Option<PathBuf>,
    /// How long a successful credential verification may be reused without
    /// re-running the password hash. `None` disables the cache.
    pub auth_cache_ttl: Option<Duration>,
    /// External credential-verification command (the `auth.command` hook). When
    /// set, username/password verification runs this command instead of the
    /// userlist; the first element is the program and the rest are arguments.
    pub auth_command: Option<Vec<String>>,
    /// Maximum number of concurrent client connections.
    pub max_connections: usize,
    /// Active log sinks.
    pub log_outputs: Vec<LogOutput>,
    /// File path used when `logoutput` includes `file`.
    pub log_file: Option<PathBuf>,
    /// Log record encoding.
    pub log_format: LogFormat,
    /// Maximum active log file size before rotation.
    pub log_rotate_size: u64,
    /// Number of rotated log files to retain.
    pub log_rotate_keep: usize,
    /// DNS resolution policy.
    pub dns: DnsPolicy,
    /// Optional local metrics endpoint listen address.
    pub metrics_listen: Option<SocketAddr>,
    /// Whether a `metrics.listen` that is not loopback — including an unspecified
    /// address such as `0.0.0.0`/`[::]` — is allowed. The metrics endpoint is
    /// unauthenticated, so binding it off loopback is refused unless this is
    /// explicitly set (`metrics.allowpublic: true`).
    pub metrics_allow_public: bool,
    /// Optional TLS wrapper for the client listener.
    pub tls: Option<TlsConfig>,
    /// Optional per-client rate limits and abuse controls.
    pub rate_limits: RateLimits,
    /// The access-control rule set.
    pub rules: RuleSet,
}

impl Config {
    pub fn uses_file_logging(&self) -> bool {
        self.log_outputs.contains(&LogOutput::File)
    }

    /// True when the no-authentication (`none`) SOCKS method is offered on a
    /// non-loopback `internal` listener. `none` is offered by default, and also
    /// whenever `socksmethod` lists it (e.g. `username none`). Combined with
    /// permissive `socks`/`client` rules that is an open proxy, so the server
    /// warns about it at startup and on reload. An IPv4-mapped loopback address
    /// (`::ffff:127.0.0.1`) is canonicalised first so it is recognised as
    /// loopback.
    pub fn noauth_on_non_loopback_listener(&self) -> bool {
        self.socks_methods.contains(&AuthKind::None)
            && !self.internal.ip().to_canonical().is_loopback()
    }

    /// Validates settings that only take effect when the process starts (so this
    /// is run at startup and by `config check`, but not on reload, where these
    /// settings are not applied anyway).
    ///
    /// The metrics endpoint is unauthenticated and exposes operational counters
    /// and rule labels, so a non-loopback (or unspecified) `metrics.listen` is
    /// refused unless the operator explicitly opts in with `metrics.allowpublic`.
    /// An IPv4-mapped loopback address is canonicalised first.
    pub fn validate_startup(&self) -> Result<()> {
        if let Some(addr) = self.metrics_listen {
            let ip = addr.ip().to_canonical();
            if (ip.is_unspecified() || !ip.is_loopback()) && !self.metrics_allow_public {
                return Err(Error::Config(format!(
                    "metrics.listen {addr} is not loopback; the metrics endpoint is unauthenticated, \
                     so set 'metrics.allowpublic: true' to expose it or bind it to 127.0.0.1/[::1]"
                )));
            }
        }
        Ok(())
    }

    /// Loads and validates configuration from a file on disk.
    pub fn load(path: &Path) -> Result<Config> {
        let mut builder = Builder::default();
        let mut stack = Vec::new();
        let text = fs::read_to_string(path)
            .map_err(|e| Error::Config(format!("failed to read {}: {e}", path.display())))?;
        let canonical = fs::canonicalize(path)
            .map_err(|e| Error::Config(format!("failed to resolve {}: {e}", path.display())))?;
        parse_resolved_config_file(canonical, text, &mut builder, &mut stack)?;
        builder.build()
    }

    /// Parses and validates configuration from a string.
    pub fn parse(text: &str) -> Result<Config> {
        let mut builder = Builder::default();
        let source = ParseSource::memory();
        let mut stack = Vec::new();
        parse_config_text(text, &mut builder, &source, &mut stack)?;
        builder.build()
    }
}

#[derive(Debug)]
struct ParseSource {
    display: Option<String>,
    base_dir: Option<PathBuf>,
}

impl ParseSource {
    fn memory() -> Self {
        Self {
            display: None,
            base_dir: None,
        }
    }

    fn file(path: &Path) -> Self {
        Self {
            display: Some(path.display().to_string()),
            base_dir: path.parent().map(Path::to_path_buf),
        }
    }
}

fn parse_config_file(path: &Path, builder: &mut Builder, stack: &mut Vec<PathBuf>) -> Result<()> {
    let canonical = fs::canonicalize(path)
        .map_err(|e| Error::Config(format!("failed to resolve {}: {e}", path.display())))?;
    let text = fs::read_to_string(&canonical)
        .map_err(|e| Error::Config(format!("failed to read {}: {e}", canonical.display())))?;
    parse_resolved_config_file(canonical, text, builder, stack)
}

fn parse_resolved_config_file(
    canonical: PathBuf,
    text: String,
    builder: &mut Builder,
    stack: &mut Vec<PathBuf>,
) -> Result<()> {
    if stack.contains(&canonical) {
        let mut cycle = stack
            .iter()
            .map(|path| path.display().to_string())
            .collect::<Vec<_>>();
        cycle.push(canonical.display().to_string());
        return Err(Error::Config(format!(
            "include cycle detected: {}",
            cycle.join(" -> ")
        )));
    }

    let source = ParseSource::file(&canonical);

    stack.push(canonical);
    let result = parse_config_text(&text, builder, &source, stack);
    stack.pop();
    result
}

fn parse_config_text(
    text: &str,
    builder: &mut Builder,
    source: &ParseSource,
    stack: &mut Vec<PathBuf>,
) -> Result<()> {
    let lines: Vec<&str> = text.lines().collect();
    let mut i = 0;
    while i < lines.len() {
        let lineno = i + 1;
        let stripped = strip_comment(lines[i]);
        let trimmed = stripped.trim();
        if trimmed.is_empty() {
            i += 1;
            continue;
        }
        let head_tokens: Vec<&str> = trimmed.split_whitespace().collect();
        let head = head_tokens[0].trim_end_matches(':').to_ascii_lowercase();

        if (head == "client" || head == "socks")
            && head_tokens.len() >= 2
            && matches!(
                head_tokens[1].to_ascii_lowercase().as_str(),
                "pass" | "block"
            )
        {
            let (block, next) =
                gather_block(&lines, i).map_err(|e| with_source_context(e, source))?;
            let rule = parse_rule(&block, lineno).map_err(|e| with_source_context(e, source))?;
            builder.rules.push(rule);
            i = next;
        } else if head == "include" {
            let values: Vec<String> = head_tokens[1..].iter().map(|s| s.to_string()).collect();
            parse_include(&values, lineno, source, builder, stack)?;
            i += 1;
        } else {
            let values: Vec<String> = head_tokens[1..].iter().map(|s| s.to_string()).collect();
            parse_setting(builder, &head, &values, lineno)
                .map_err(|e| with_source_context(e, source))?;
            i += 1;
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Builder + defaults
// ---------------------------------------------------------------------------

const DEFAULT_CONNECT_TIMEOUT_SECS: u64 = 30;
const DEFAULT_HANDSHAKE_TIMEOUT_SECS: u64 = 10;
const DEFAULT_UDP_TIMEOUT_SECS: u64 = 60;
const DEFAULT_DNS_TIMEOUT_SECS: u64 = 5;
/// How long shutdown waits for in-flight connections to drain before aborting
/// the rest. Kept under typical service-manager stop windows (systemd's 90s) so
/// the process exits on its own first.
const DEFAULT_SHUTDOWN_DRAIN_TIMEOUT_SECS: u64 = 10;
/// Upper bound for `shutdown.draintimeout`. A drain should never take an hour,
/// and bounding it avoids an overflow panic when the timer deadline is computed.
const MAX_SHUTDOWN_DRAIN_TIMEOUT_SECS: u64 = 3600;
/// Upper bound for `dns.timeout`. A resolution should never take an hour, and
/// rejecting absurd values keeps the timer deadline (`Instant::now() + dur`)
/// well clear of overflow.
const MAX_DNS_TIMEOUT_SECS: u64 = 3600;
const DEFAULT_AUTH_CACHE_TTL_SECS: u64 = 300;
const DEFAULT_MAX_CONNECTIONS: usize = 1024;
/// Upper bound on `maxconnections`: the value is passed to `Semaphore::new`,
/// which panics above `Semaphore::MAX_PERMITS`, so reject an absurd value at
/// parse time rather than crashing at startup.
const MAX_CONNECTIONS_LIMIT: usize = tokio::sync::Semaphore::MAX_PERMITS;
pub const DEFAULT_LOG_ROTATE_SIZE_BYTES: u64 = 10 * 1024 * 1024;
pub const DEFAULT_LOG_ROTATE_KEEP: usize = 5;
/// Upper bound on `logrotate.keep`: it drives an O(n) rename loop on each
/// rotation, so an absurd value would stall logging. Far above any practical
/// retention.
const MAX_LOG_ROTATE_KEEP: usize = 10_000;

#[derive(Default)]
struct Builder {
    internal: Option<SocketAddr>,
    external: Option<IpAddr>,
    proxy_protocol: Option<Vec<Cidr>>,
    socks_methods: Option<Vec<AuthKind>>,
    connect_timeout: Option<Duration>,
    handshake_timeout: Option<Duration>,
    io_timeout: Option<Duration>,
    udp_timeout: Option<Duration>,
    udp_port_range: Option<PortRange>,
    udp_strict_reply: Option<bool>,
    udp_advertise: Option<UdpAdvertise>,
    shutdown_drain_timeout: Option<Duration>,
    userlist: Option<PathBuf>,
    auth_cache_ttl: Option<Option<Duration>>,
    auth_command: Option<Vec<String>>,
    max_connections: Option<usize>,
    log_outputs: Option<Vec<LogOutput>>,
    log_file: Option<PathBuf>,
    log_format: Option<LogFormat>,
    log_rotate_size: Option<u64>,
    log_rotate_keep: Option<usize>,
    dns_preference: Option<DnsPreference>,
    dns_try_all: Option<bool>,
    dns_deny: Option<Vec<DnsDenyCategory>>,
    dns_cache_ttl: Option<Option<Duration>>,
    dns_timeout: Option<Duration>,
    metrics_listen: Option<SocketAddr>,
    metrics_allow_public: Option<bool>,
    tls_cert_file: Option<PathBuf>,
    tls_key_file: Option<PathBuf>,
    tls_acme_domains: Vec<String>,
    tls_acme_email: Option<String>,
    tls_acme_cache: Option<PathBuf>,
    tls_acme_staging: Option<bool>,
    rate_connection: Option<RateLimit>,
    rate_auth_failure: Option<RateLimit>,
    rate_concurrent: Option<usize>,
    rate_bytes: Option<RateLimit>,
    rules: Vec<Rule>,
}

impl Builder {
    fn build(self) -> Result<Config> {
        let internal = self
            .internal
            .ok_or_else(|| Error::Config("missing required setting: internal".into()))?;
        let socks_methods = self.socks_methods.unwrap_or_else(|| vec![AuthKind::None]);

        if socks_methods.is_empty() {
            return Err(Error::Config(
                "socksmethod must list at least one method".into(),
            ));
        }
        if socks_methods.contains(&AuthKind::Username)
            && self.userlist.is_none()
            && self.auth_command.is_none()
        {
            return Err(Error::Config(
                "socksmethod 'username' requires a 'userlist' or 'auth.command' setting".into(),
            ));
        }

        let mut log_outputs = self.log_outputs.unwrap_or_else(|| vec![LogOutput::Stdout]);
        if self.log_file.is_some() && !log_outputs.contains(&LogOutput::File) {
            log_outputs.push(LogOutput::File);
        }
        if log_outputs.contains(&LogOutput::File) && self.log_file.is_none() {
            return Err(Error::Config(
                "logoutput 'file' requires a 'logfile' setting".into(),
            ));
        }

        let has_files = self.tls_cert_file.is_some() || self.tls_key_file.is_some();
        let has_acme = !self.tls_acme_domains.is_empty()
            || self.tls_acme_email.is_some()
            || self.tls_acme_cache.is_some()
            || self.tls_acme_staging.is_some();
        if has_files && has_acme {
            return Err(Error::Config(
                "TLS cannot use both 'tls.certfile'/'tls.keyfile' and 'tls.acme.*'; choose one"
                    .into(),
            ));
        }
        let tls = if has_acme {
            if self.tls_acme_domains.is_empty() {
                return Err(Error::Config(
                    "tls.acme requires at least one domain in 'tls.acme.domains'".into(),
                ));
            }
            let cache_dir = self.tls_acme_cache.ok_or_else(|| {
                Error::Config(
                    "tls.acme requires 'tls.acme.cache' (a directory to persist the account and \
                     certificates so they are not re-requested on restart)"
                        .into(),
                )
            })?;
            Some(TlsConfig::Acme(AcmeConfig {
                domains: self.tls_acme_domains,
                email: self.tls_acme_email,
                cache_dir,
                staging: self.tls_acme_staging.unwrap_or(false),
            }))
        } else {
            match (self.tls_cert_file, self.tls_key_file) {
                (Some(cert_file), Some(key_file)) => Some(TlsConfig::Files {
                    cert_file,
                    key_file,
                }),
                (Some(_), None) => {
                    return Err(Error::Config(
                        "tls.certfile requires a matching 'tls.keyfile' setting".into(),
                    ));
                }
                (None, Some(_)) => {
                    return Err(Error::Config(
                        "tls.keyfile requires a matching 'tls.certfile' setting".into(),
                    ));
                }
                (None, None) => None,
            }
        };

        Ok(Config {
            internal,
            external: self.external.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED)),
            proxy_protocol: self.proxy_protocol.unwrap_or_default(),
            socks_methods,
            connect_timeout: self
                .connect_timeout
                .unwrap_or(Duration::from_secs(DEFAULT_CONNECT_TIMEOUT_SECS)),
            handshake_timeout: self
                .handshake_timeout
                .unwrap_or(Duration::from_secs(DEFAULT_HANDSHAKE_TIMEOUT_SECS)),
            io_timeout: self.io_timeout.unwrap_or(Duration::ZERO),
            udp_timeout: self
                .udp_timeout
                .unwrap_or(Duration::from_secs(DEFAULT_UDP_TIMEOUT_SECS)),
            udp_port_range: self.udp_port_range,
            udp_strict_reply: self.udp_strict_reply.unwrap_or(true),
            udp_advertise: self.udp_advertise,
            shutdown_drain_timeout: self
                .shutdown_drain_timeout
                .unwrap_or(Duration::from_secs(DEFAULT_SHUTDOWN_DRAIN_TIMEOUT_SECS)),
            userlist: self.userlist,
            auth_cache_ttl: self
                .auth_cache_ttl
                .unwrap_or(Some(Duration::from_secs(DEFAULT_AUTH_CACHE_TTL_SECS))),
            auth_command: self.auth_command,
            max_connections: self.max_connections.unwrap_or(DEFAULT_MAX_CONNECTIONS),
            log_outputs,
            log_file: self.log_file,
            log_format: self.log_format.unwrap_or(LogFormat::Text),
            log_rotate_size: self
                .log_rotate_size
                .unwrap_or(DEFAULT_LOG_ROTATE_SIZE_BYTES),
            log_rotate_keep: self.log_rotate_keep.unwrap_or(DEFAULT_LOG_ROTATE_KEEP),
            dns: DnsPolicy {
                preference: self.dns_preference.unwrap_or(DnsPreference::System),
                try_all: self.dns_try_all.unwrap_or(false),
                deny: self.dns_deny.unwrap_or_default(),
                cache_ttl: self.dns_cache_ttl.unwrap_or(None),
                timeout: self
                    .dns_timeout
                    .unwrap_or(Duration::from_secs(DEFAULT_DNS_TIMEOUT_SECS)),
            },
            metrics_listen: self.metrics_listen,
            metrics_allow_public: self.metrics_allow_public.unwrap_or(false),
            tls,
            rate_limits: RateLimits {
                connection_rate: self.rate_connection,
                auth_failure_rate: self.rate_auth_failure,
                concurrent_connections: self.rate_concurrent,
                byte_rate: self.rate_bytes,
            },
            rules: RuleSet::new(self.rules),
        })
    }
}

// ---------------------------------------------------------------------------
// Setting parsing
// ---------------------------------------------------------------------------

fn parse_setting(b: &mut Builder, key: &str, vals: &[String], lineno: usize) -> Result<()> {
    match key {
        "internal" => b.internal = Some(parse_endpoint(vals, lineno)?),
        "external" => b.external = Some(parse_ip(vals, lineno)?),
        "proxyprotocol" | "proxy.protocol" => {
            if vals.is_empty() {
                return Err(cfg_err(
                    lineno,
                    "proxyprotocol requires at least one trusted upstream CIDR",
                ));
            }
            let mut cidrs = Vec::with_capacity(vals.len());
            for v in vals {
                let cidr: Cidr = v.parse().map_err(|e| {
                    cfg_err(lineno, &format!("invalid proxyprotocol CIDR '{v}': {e}"))
                })?;
                cidrs.push(cidr);
            }
            b.proxy_protocol = Some(cidrs);
        }
        "socksmethod" | "clientmethod" => {
            // clientmethod is accepted for Dante familiarity; Alighieri performs
            // authentication at the SOCKS layer, so both populate the offered
            // method list. The last one wins if both are present.
            b.socks_methods = Some(parse_methods(vals, lineno)?);
        }
        "connecttimeout" | "connect.timeout" => {
            b.connect_timeout = Some(Duration::from_secs(parse_nonzero_secs(
                vals,
                lineno,
                "connecttimeout",
            )?));
        }
        "handshaketimeout" | "handshake.timeout" => {
            b.handshake_timeout = Some(Duration::from_secs(parse_nonzero_secs(
                vals,
                lineno,
                "handshaketimeout",
            )?));
        }
        "iotimeout" | "io.timeout" => {
            b.io_timeout = Some(Duration::from_secs(parse_u64(vals, lineno)?));
        }
        "udptimeout" | "udp.timeout" => {
            b.udp_timeout = Some(Duration::from_secs(parse_u64(vals, lineno)?));
        }
        "udpportrange" | "udp.portrange" => {
            if vals.is_empty() {
                return Err(cfg_err(
                    lineno,
                    "udp.portrange requires a value (MIN-MAX or a single PORT)",
                ));
            }
            let spec = vals.join(" ");
            let range: PortRange = spec
                .parse()
                .map_err(|e| cfg_err(lineno, &format!("invalid udp.portrange '{spec}': {e}")))?;
            if range.min == 0 {
                return Err(cfg_err(lineno, "udp.portrange must not include port 0"));
            }
            b.udp_port_range = Some(range);
        }
        "udpstrictreply" | "udp.strictreply" => {
            b.udp_strict_reply = Some(parse_bool(vals, lineno)?);
        }
        "udpadvertise" | "udp.advertise" => {
            let spec = expect_single(vals, lineno, "udp.advertise host")?;
            b.udp_advertise = Some(match spec.parse::<IpAddr>() {
                Ok(ip) => UdpAdvertise::Ip(ip),
                Err(_) => {
                    // A non-IP value is a hostname resolved per association — reject
                    // a malformed one now so `--check` catches it instead of it
                    // silently failing to resolve and falling back at runtime.
                    crate::net::validate_hostname(spec).map_err(|e| {
                        // `{spec:?}` escapes the value: it is one validation rejects
                        // (e.g. a control char), so it must not land verbatim in the
                        // error/log output it would otherwise forge.
                        cfg_err(lineno, &format!("invalid udp.advertise host {spec:?}: {e}"))
                    })?;
                    UdpAdvertise::Host(spec.clone())
                }
            });
        }
        "shutdowndraintimeout" | "shutdown.draintimeout" => {
            let secs = parse_u64(vals, lineno)?;
            if secs > MAX_SHUTDOWN_DRAIN_TIMEOUT_SECS {
                return Err(cfg_err(
                    lineno,
                    &format!(
                        "shutdown.draintimeout cannot exceed {MAX_SHUTDOWN_DRAIN_TIMEOUT_SECS} seconds"
                    ),
                ));
            }
            b.shutdown_drain_timeout = Some(Duration::from_secs(secs));
        }
        "userlist" => {
            if vals.is_empty() {
                return Err(cfg_err(lineno, "userlist requires a path"));
            }
            b.userlist = Some(PathBuf::from(vals.join(" ")));
        }
        "maxconnections" | "max.connections" => {
            let value = parse_usize_positive(vals, lineno, "maxconnections")?;
            if value > MAX_CONNECTIONS_LIMIT {
                return Err(cfg_err(
                    lineno,
                    &format!("maxconnections must be at most {MAX_CONNECTIONS_LIMIT}"),
                ));
            }
            b.max_connections = Some(value);
        }
        "logoutput" => b.log_outputs = Some(parse_log_outputs(vals, lineno)?),
        "logfile" | "log.file" => {
            if vals.is_empty() {
                return Err(cfg_err(lineno, "logfile requires a path"));
            }
            b.log_file = Some(PathBuf::from(vals.join(" ")));
        }
        "logformat" | "log.format" => b.log_format = Some(parse_log_format(vals, lineno)?),
        "logrotatesize" | "logrotate.size" | "log.rotate.size" => {
            let size = parse_byte_size(vals, lineno)?;
            if size == 0 {
                return Err(cfg_err(lineno, "logrotate.size must be > 0"));
            }
            b.log_rotate_size = Some(size);
        }
        "logrotatekeep" | "logrotate.keep" | "log.rotate.keep" => {
            let value = parse_usize(vals, lineno, "logrotate.keep")?;
            if value > MAX_LOG_ROTATE_KEEP {
                return Err(cfg_err(
                    lineno,
                    &format!("logrotate.keep must be at most {MAX_LOG_ROTATE_KEEP}"),
                ));
            }
            b.log_rotate_keep = Some(value);
        }
        "dnsprefer" | "dns.prefer" => b.dns_preference = Some(parse_dns_preference(vals, lineno)?),
        "dnstryall" | "dns.tryall" | "dns.try_all" => {
            b.dns_try_all = Some(parse_bool(vals, lineno)?);
        }
        "dnsdeny" | "dns.deny" => b.dns_deny = Some(parse_dns_deny(vals, lineno)?),
        "dnscachettl" | "dns.cachettl" | "dns.cache.ttl" => {
            b.dns_cache_ttl = Some(parse_cache_ttl(vals, lineno, "dns.cachettl")?);
        }
        "dnstimeout" | "dns.timeout" => {
            let secs = parse_u64(vals, lineno)?;
            if secs == 0 {
                return Err(cfg_err(
                    lineno,
                    "dns.timeout must be at least 1 second (0 would fail every name resolution)",
                ));
            }
            if secs > MAX_DNS_TIMEOUT_SECS {
                return Err(cfg_err(
                    lineno,
                    &format!("dns.timeout cannot exceed {MAX_DNS_TIMEOUT_SECS} seconds"),
                ));
            }
            b.dns_timeout = Some(Duration::from_secs(secs));
        }
        "authcachettl" | "auth.cachettl" | "auth.cache.ttl" => {
            b.auth_cache_ttl = Some(parse_cache_ttl(vals, lineno, "auth.cachettl")?);
        }
        "authcommand" | "auth.command" => {
            if vals.is_empty() {
                return Err(cfg_err(lineno, "auth.command requires a program path"));
            }
            b.auth_command = Some(vals.to_vec());
        }
        "metricslisten" | "metrics.listen" => {
            b.metrics_listen = Some(parse_endpoint(vals, lineno)?)
        }
        "metricsallowpublic" | "metrics.allowpublic" => {
            b.metrics_allow_public = Some(parse_bool(vals, lineno)?)
        }
        "tlscertfile" | "tls.certfile" | "tls.cert" => {
            b.tls_cert_file = Some(parse_path(vals, lineno, "tls.certfile")?);
        }
        "tlskeyfile" | "tls.keyfile" | "tls.key" => {
            b.tls_key_file = Some(parse_path(vals, lineno, "tls.keyfile")?);
        }
        "tlsacmedomains" | "tls.acme.domains" | "tls.acme.domain" => {
            if vals.is_empty() {
                return Err(cfg_err(
                    lineno,
                    "tls.acme.domains requires at least one domain",
                ));
            }
            // Reject a name a public CA cannot issue for via TLS-ALPN-01 (IP
            // address, single-label/local name, wildcard, underscore, raw Unicode,
            // bad hyphen) at config load, rather than starting up and only failing
            // later in the ACME order with no usable certificate.
            for domain in vals {
                crate::net::validate_acme_domain(domain).map_err(|e| {
                    // `{domain:?}` escapes the value so a rejected control character
                    // cannot forge the error/log line it appears in.
                    cfg_err(
                        lineno,
                        &format!("invalid tls.acme.domains entry {domain:?}: {e}"),
                    )
                })?;
            }
            // Store the canonical form: a tolerated trailing dot (FQDN) is stripped
            // so the ACME stack receives a clean identifier.
            b.tls_acme_domains = vals
                .iter()
                .map(|d| d.strip_suffix('.').unwrap_or(d).to_string())
                .collect();
        }
        "tlsacmeemail" | "tls.acme.email" => match vals {
            [] => return Err(cfg_err(lineno, "tls.acme.email requires an address")),
            [email] => b.tls_acme_email = Some(email.clone()),
            _ => {
                return Err(cfg_err(
                    lineno,
                    "tls.acme.email accepts only a single address",
                ))
            }
        },
        "tlsacmecache" | "tls.acme.cache" | "tls.acme.cachedir" => {
            b.tls_acme_cache = Some(parse_path(vals, lineno, "tls.acme.cache")?);
        }
        "tlsacmestaging" | "tls.acme.staging" => {
            b.tls_acme_staging = Some(parse_bool(vals, lineno)?);
        }
        "ratelimit.connectionrate" | "ratelimit.connection.rate" | "ratelimit.connections" => {
            b.rate_connection = Some(parse_rate_limit(vals, lineno, "ratelimit.connectionrate")?);
        }
        "ratelimit.authfailurerate" | "ratelimit.auth.failure.rate" | "ratelimit.authfailures" => {
            b.rate_auth_failure =
                Some(parse_rate_limit(vals, lineno, "ratelimit.authfailurerate")?);
        }
        "ratelimit.concurrentconnections"
        | "ratelimit.concurrent.connections"
        | "ratelimit.concurrent" => {
            b.rate_concurrent = Some(parse_usize_positive(
                vals,
                lineno,
                "ratelimit.concurrentconnections",
            )?);
        }
        "ratelimit.byterate" | "ratelimit.byte.rate" | "ratelimit.bytes" => {
            b.rate_bytes = Some(parse_byte_rate_limit(vals, lineno, "ratelimit.byterate")?);
        }
        other => {
            return Err(cfg_err(lineno, &format!("unknown keyword '{other}'")));
        }
    }
    Ok(())
}

/// Parses an `IP [port = N]` or `IP:PORT` endpoint.
fn parse_endpoint(vals: &[String], lineno: usize) -> Result<SocketAddr> {
    if vals.is_empty() {
        return Err(cfg_err(lineno, "expected an address"));
    }
    if let Some(pos) = vals.iter().position(|v| v.eq_ignore_ascii_case("port")) {
        // The only valid form is `IP port = N`, so `port` must follow the IP
        // directly; anything between them is a typo to reject rather than ignore.
        if pos != 1 {
            return Err(cfg_err(
                lineno,
                "expected 'IP port = N' (unexpected tokens before 'port')",
            ));
        }
        // The keyword form is exactly `port = N` (the documented spelling), so
        // the tokens after `port` must be `=` then the port. This rejects a
        // missing value (`port =`), a `=`-less `port N`, and split/trailing
        // tokens (`port = 10 80`) that whitespace tokenization would otherwise
        // join into a single port string.
        if !matches!(&vals[pos + 1..], [eq, _] if eq == "=") {
            return Err(cfg_err(lineno, "expected 'IP port = N'"));
        }
        let ip: IpAddr = vals[0]
            .parse()
            .map_err(|_| cfg_err(lineno, &format!("invalid IP address '{}'", vals[0])))?;
        let port_str = join_value_after(&vals[pos + 1..]);
        let port: u16 = port_str
            .parse()
            .map_err(|_| cfg_err(lineno, &format!("invalid port '{port_str}'")))?;
        Ok(SocketAddr::new(ip, port))
    } else {
        // A bare `IP:PORT` is a single token; reject trailing tokens.
        let val = expect_single(vals, lineno, "'IP:PORT' or 'IP port = N' address")?;
        val.parse::<SocketAddr>().map_err(|_| {
            cfg_err(
                lineno,
                &format!("expected 'IP port = N' or 'IP:PORT', got '{val}'"),
            )
        })
    }
}

fn parse_path(vals: &[String], lineno: usize, setting: &str) -> Result<PathBuf> {
    if vals.is_empty() {
        return Err(cfg_err(lineno, &format!("{setting} requires a path")));
    }
    Ok(PathBuf::from(vals.join(" ")))
}

/// Parses a `usize` setting (zero allowed), rejecting a value too large for the
/// target's pointer width rather than silently truncating it — `as usize` would
/// wrap a `u64` above `usize::MAX` on a 32-bit target.
fn parse_usize(vals: &[String], lineno: usize, setting: &str) -> Result<usize> {
    let n = parse_u64(vals, lineno)?;
    usize::try_from(n).map_err(|_| cfg_err(lineno, &format!("{setting} is too large")))
}

fn parse_usize_positive(vals: &[String], lineno: usize, setting: &str) -> Result<usize> {
    let n = parse_usize(vals, lineno, setting)?;
    if n == 0 {
        return Err(cfg_err(lineno, &format!("{setting} must be > 0")));
    }
    Ok(n)
}

fn parse_rate_limit(vals: &[String], lineno: usize, setting: &str) -> Result<RateLimit> {
    if vals.is_empty() {
        return Err(cfg_err(
            lineno,
            &format!("{setting} requires LIMIT/WINDOW_SECONDS"),
        ));
    }
    let raw = join_value_after(vals);
    let Some((limit, window)) = raw.split_once('/') else {
        return Err(cfg_err(
            lineno,
            &format!("{setting} must use LIMIT/WINDOW_SECONDS"),
        ));
    };
    let limit = limit
        .replace('_', "")
        .parse::<u64>()
        .map_err(|_| cfg_err(lineno, &format!("invalid rate limit '{limit}'")))?;
    let window = window
        .replace('_', "")
        .parse::<u64>()
        .map_err(|_| cfg_err(lineno, &format!("invalid rate window '{window}'")))?;
    if limit == 0 || window == 0 {
        return Err(cfg_err(lineno, &format!("{setting} values must be > 0")));
    }
    Ok(RateLimit {
        limit,
        window: Duration::from_secs(window),
    })
}

fn parse_byte_rate_limit(vals: &[String], lineno: usize, setting: &str) -> Result<RateLimit> {
    if vals.is_empty() {
        return Err(cfg_err(
            lineno,
            &format!("{setting} requires BYTES/WINDOW_SECONDS"),
        ));
    }
    let raw = join_value_after(vals);
    let Some((bytes, window)) = raw.split_once('/') else {
        return Err(cfg_err(
            lineno,
            &format!("{setting} must use BYTES/WINDOW_SECONDS"),
        ));
    };
    let limit = parse_byte_size(&[bytes.to_string()], lineno)?;
    let window = window
        .replace('_', "")
        .parse::<u64>()
        .map_err(|_| cfg_err(lineno, &format!("invalid byte rate window '{window}'")))?;
    if limit == 0 || window == 0 {
        return Err(cfg_err(lineno, &format!("{setting} values must be > 0")));
    }
    Ok(RateLimit {
        limit,
        window: Duration::from_secs(window),
    })
}

fn parse_ip(vals: &[String], lineno: usize) -> Result<IpAddr> {
    let val = expect_single(vals, lineno, "IP address")?;
    val.parse()
        .map_err(|_| cfg_err(lineno, &format!("invalid IP address '{val}'")))
}

/// Public host advertised as the UDP ASSOCIATE reply `BND.ADDR` (host only; the
/// real relay port is kept). An IP is advertised directly; a hostname is resolved
/// at UDP-associate time through the async resolver — so config load does no DNS,
/// and a wedged resolver can neither hang the load nor leak abandoned threads. The
/// address matching the client's connection family is chosen at that point.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum UdpAdvertise {
    /// A literal IP address, advertised as-is (family-matched per association).
    Ip(IpAddr),
    /// A hostname, resolved per association.
    Host(String),
}

/// Returns the single value of a scalar setting, rejecting trailing tokens that
/// would otherwise be silently ignored (e.g. `dns.tryall: yes maybe` or
/// `connecttimeout: 5 oops`).
fn expect_single<'a>(vals: &'a [String], lineno: usize, what: &str) -> Result<&'a String> {
    match vals {
        [single] => Ok(single),
        [] => Err(cfg_err(lineno, &format!("expected a {what}"))),
        _ => Err(cfg_err(
            lineno,
            &format!("expected a single {what}, got {} values", vals.len()),
        )),
    }
}

fn parse_u64(vals: &[String], lineno: usize) -> Result<u64> {
    let val = expect_single(vals, lineno, "number")?;
    val.parse()
        .map_err(|_| cfg_err(lineno, &format!("invalid number '{val}'")))
}

/// Parses a positive (non-zero) number of seconds. For `connecttimeout` and
/// `handshaketimeout`, `0` is not "disabled" — it would make `tokio::time::timeout`
/// expire immediately and fail every connection — so it is rejected.
fn parse_nonzero_secs(vals: &[String], lineno: usize, name: &str) -> Result<u64> {
    let secs = parse_u64(vals, lineno)?;
    if secs == 0 {
        return Err(cfg_err(
            lineno,
            &format!(
                "{name} must be at least 1 second (0 would fail every connection immediately)"
            ),
        ));
    }
    Ok(secs)
}

fn parse_bool(vals: &[String], lineno: usize) -> Result<bool> {
    let val = expect_single(vals, lineno, "boolean")?;
    match val.to_ascii_lowercase().as_str() {
        "true" | "yes" | "on" | "1" => Ok(true),
        "false" | "no" | "off" | "0" => Ok(false),
        other => Err(cfg_err(
            lineno,
            &format!("invalid boolean '{other}' (expected yes/no or true/false)"),
        )),
    }
}

fn parse_methods(vals: &[String], lineno: usize) -> Result<Vec<AuthKind>> {
    if vals.is_empty() {
        return Err(cfg_err(lineno, "expected at least one method"));
    }
    let mut out = Vec::new();
    for v in vals {
        let kind = match v.to_ascii_lowercase().as_str() {
            "none" => AuthKind::None,
            "username" => AuthKind::Username,
            other => {
                return Err(cfg_err(
                    lineno,
                    &format!("unknown method '{other}' (expected 'none' or 'username')"),
                ))
            }
        };
        if !out.contains(&kind) {
            out.push(kind);
        }
    }
    Ok(out)
}

fn parse_log_outputs(vals: &[String], lineno: usize) -> Result<Vec<LogOutput>> {
    if vals.is_empty() {
        return Err(cfg_err(lineno, "expected at least one log output"));
    }
    let mut out = Vec::new();
    for v in vals {
        let sink = match v.to_ascii_lowercase().as_str() {
            "stdout" => LogOutput::Stdout,
            "stderr" => LogOutput::Stderr,
            "file" => LogOutput::File,
            other => {
                return Err(cfg_err(
                    lineno,
                    &format!(
                        "unsupported logoutput '{other}' (expected 'stdout', 'stderr' or 'file')"
                    ),
                ))
            }
        };
        if !out.contains(&sink) {
            out.push(sink);
        }
    }
    Ok(out)
}

fn parse_log_format(vals: &[String], lineno: usize) -> Result<LogFormat> {
    match expect_single(vals, lineno, "log format")?
        .to_ascii_lowercase()
        .as_str()
    {
        "text" => Ok(LogFormat::Text),
        "json" => Ok(LogFormat::Json),
        other => Err(cfg_err(
            lineno,
            &format!("unsupported logformat '{other}' (expected 'text' or 'json')"),
        )),
    }
}

fn parse_byte_size(vals: &[String], lineno: usize) -> Result<u64> {
    if vals.is_empty() {
        return Err(cfg_err(lineno, "expected a byte size"));
    }
    let raw = join_value_after(vals).to_ascii_lowercase();
    let (number, multiplier) = if let Some(number) = raw.strip_suffix("kib") {
        (number, 1024_u64)
    } else if let Some(number) = raw.strip_suffix("kb") {
        (number, 1000_u64)
    } else if let Some(number) = raw.strip_suffix('k') {
        (number, 1024_u64)
    } else if let Some(number) = raw.strip_suffix("mib") {
        (number, 1024_u64 * 1024)
    } else if let Some(number) = raw.strip_suffix("mb") {
        (number, 1000_u64 * 1000)
    } else if let Some(number) = raw.strip_suffix('m') {
        (number, 1024_u64 * 1024)
    } else if let Some(number) = raw.strip_suffix("gib") {
        (number, 1024_u64 * 1024 * 1024)
    } else if let Some(number) = raw.strip_suffix("gb") {
        (number, 1000_u64 * 1000 * 1000)
    } else if let Some(number) = raw.strip_suffix('g') {
        (number, 1024_u64 * 1024 * 1024)
    } else if let Some(number) = raw.strip_suffix('b') {
        (number, 1_u64)
    } else {
        (raw.as_str(), 1_u64)
    };
    let number = number.replace('_', "");
    let bytes = number.parse::<u64>().map_err(|_| {
        cfg_err(
            lineno,
            &format!(
                "invalid byte size '{}' (expected bytes or K/M/G suffix)",
                vals[0]
            ),
        )
    })?;
    bytes
        .checked_mul(multiplier)
        .ok_or_else(|| cfg_err(lineno, "byte size is too large"))
}

fn parse_dns_preference(vals: &[String], lineno: usize) -> Result<DnsPreference> {
    match expect_single(vals, lineno, "DNS preference")?
        .to_ascii_lowercase()
        .as_str()
    {
        "system" | "default" => Ok(DnsPreference::System),
        "ipv4" | "v4" => Ok(DnsPreference::Ipv4),
        "ipv6" | "v6" => Ok(DnsPreference::Ipv6),
        other => Err(cfg_err(
            lineno,
            &format!("unsupported dns.prefer '{other}' (expected 'system', 'ipv4' or 'ipv6')"),
        )),
    }
}

fn parse_cache_ttl(vals: &[String], lineno: usize, setting: &str) -> Result<Option<Duration>> {
    let what = format!("{setting} value (seconds, 0, off, none, or disabled)");
    match expect_single(vals, lineno, &what)?
        .to_ascii_lowercase()
        .as_str()
    {
        "off" | "none" | "disabled" => Ok(None),
        _ => {
            let secs = parse_u64(vals, lineno)?;
            Ok((secs > 0).then(|| Duration::from_secs(secs)))
        }
    }
}

fn parse_dns_deny(vals: &[String], lineno: usize) -> Result<Vec<DnsDenyCategory>> {
    if vals.is_empty() {
        return Err(cfg_err(lineno, "expected at least one DNS deny category"));
    }
    let mut out = Vec::new();
    for v in vals {
        let category = match v.to_ascii_lowercase().as_str() {
            "private" => DnsDenyCategory::Private,
            "linklocal" | "link-local" | "link_local" => DnsDenyCategory::LinkLocal,
            "loopback" => DnsDenyCategory::Loopback,
            "multicast" => DnsDenyCategory::Multicast,
            "unspecified" => DnsDenyCategory::Unspecified,
            "documentation" | "doc" => DnsDenyCategory::Documentation,
            "reserved" => DnsDenyCategory::Reserved,
            other => {
                return Err(cfg_err(
                    lineno,
                    &format!("unknown DNS deny category '{other}'"),
                ))
            }
        };
        if !out.contains(&category) {
            out.push(category);
        }
    }
    Ok(out)
}

// ---------------------------------------------------------------------------
// Rule-block parsing
// ---------------------------------------------------------------------------

/// Collects the tokens of a brace-delimited block starting at line `start`,
/// returning the tokens (header words included) and the index of the first
/// line after the closing brace.
fn gather_block(lines: &[&str], start: usize) -> Result<(Vec<String>, usize)> {
    let mut tokens: Vec<String> = Vec::new();
    let mut depth: i32 = 0;
    let mut opened = false;
    let mut i = start;
    while i < lines.len() {
        let stripped = strip_comment(lines[i]);
        let spaced = stripped.replace('{', " { ").replace('}', " } ");
        let line_tokens: Vec<&str> = spaced.split_whitespace().collect();
        for (j, tok) in line_tokens.iter().enumerate() {
            match *tok {
                "{" => {
                    depth += 1;
                    opened = true;
                }
                "}" => {
                    depth -= 1;
                    if depth < 0 {
                        return Err(cfg_err(i + 1, "unexpected '}'"));
                    }
                }
                _ => {}
            }
            tokens.push((*tok).to_string());
            // The brace that closes the block must be the last token on its
            // line. Anything after it — a selector typed outside the braces, or
            // a second rule crammed onto the line — would be silently dropped by
            // the body parser (which only sees tokens up to '}'), broadening the
            // rule. Reject it instead.
            if opened && depth == 0 && j + 1 < line_tokens.len() {
                return Err(cfg_err(i + 1, "unexpected tokens after '}'"));
            }
        }
        i += 1;
        if opened && depth == 0 {
            return Ok((tokens, i));
        }
    }
    Err(cfg_err(start + 1, "unterminated rule block (missing '}')"))
}

fn parse_rule(tokens: &[String], lineno: usize) -> Result<Rule> {
    let scope = match tokens[0].to_ascii_lowercase().as_str() {
        "client" => Scope::Client,
        "socks" => Scope::Socks,
        other => return Err(cfg_err(lineno, &format!("unknown rule scope '{other}'"))),
    };
    let verdict = match tokens[1].to_ascii_lowercase().as_str() {
        "pass" => Verdict::Pass,
        "block" => Verdict::Block,
        other => return Err(cfg_err(lineno, &format!("unknown verdict '{other}'"))),
    };

    let open = tokens
        .iter()
        .position(|t| t == "{")
        .ok_or_else(|| cfg_err(lineno, "rule is missing '{'"))?;
    let close = tokens
        .iter()
        .rposition(|t| t == "}")
        .ok_or_else(|| cfg_err(lineno, "rule is missing '}'"))?;
    let name = parse_rule_name(&tokens[2..open], lineno)?;
    let body = &tokens[open + 1..close];

    let mut from: Option<AddrSpec> = None;
    let mut to: Option<AddrSpec> = None;
    let mut protocols: Vec<Protocol> = Vec::new();
    let mut commands: Vec<Command> = Vec::new();
    let mut methods: Vec<AuthKind> = Vec::new();
    let mut bandwidth: Option<RateLimit> = None;

    let mut idx = 0;
    while idx < body.len() {
        let key = body[idx].trim_end_matches(':').to_ascii_lowercase();
        idx += 1;
        let mut vals: Vec<String> = Vec::new();
        while idx < body.len() && !is_known_body_key(&body[idx]) {
            vals.push(body[idx].clone());
            idx += 1;
        }
        match key.as_str() {
            "from" => from = Some(parse_addr_spec(&vals, lineno, false)?),
            "to" => to = Some(parse_addr_spec(&vals, lineno, scope == Scope::Socks)?),
            "protocol" => protocols = parse_protocols(&vals, lineno)?,
            "command" => commands = parse_commands(&vals, lineno)?,
            "method" => methods = parse_methods(&vals, lineno)?,
            "bandwidth" => {
                if scope != Scope::Socks {
                    return Err(cfg_err(lineno, "bandwidth is only valid in a socks rule"));
                }
                bandwidth = Some(parse_byte_rate_limit(&vals, lineno, "bandwidth")?);
            }
            "log" => { /* accepted for familiarity; per-rule logging is implicit */ }
            other => {
                return Err(cfg_err(
                    lineno,
                    &format!("unknown rule directive '{other}'"),
                ))
            }
        }
    }

    Ok(Rule {
        name,
        verdict,
        scope,
        from: from.unwrap_or_else(AddrSpec::any),
        to: to.unwrap_or_else(AddrSpec::any),
        commands,
        protocols,
        methods,
        bandwidth,
        source_line: lineno,
    })
}

fn parse_rule_name(tokens: &[String], lineno: usize) -> Result<Option<Arc<str>>> {
    match tokens {
        [] => Ok(None),
        [name] => {
            let name = unquote_rule_name(name, lineno)?;
            if name.is_empty() {
                return Err(cfg_err(lineno, "rule name cannot be empty"));
            }
            Ok(Some(Arc::from(name)))
        }
        _ => Err(cfg_err(
            lineno,
            "rule name must be a single token before '{'",
        )),
    }
}

fn unquote_rule_name(name: &str, lineno: usize) -> Result<String> {
    if let Some(stripped) = name.strip_prefix('"') {
        let Some(stripped) = stripped.strip_suffix('"') else {
            return Err(cfg_err(lineno, "quoted rule name is missing closing quote"));
        };
        return Ok(stripped.to_string());
    }
    if name.ends_with('"') {
        return Err(cfg_err(lineno, "quoted rule name is missing opening quote"));
    }
    Ok(name.to_string())
}

fn is_known_body_key(tok: &str) -> bool {
    matches!(
        tok.trim_end_matches(':').to_ascii_lowercase().as_str(),
        "from" | "to" | "protocol" | "command" | "method" | "bandwidth" | "log"
    )
}

fn parse_addr_spec(vals: &[String], lineno: usize, allow_hosts: bool) -> Result<AddrSpec> {
    let Some((addr, rest)) = vals.split_first() else {
        return Err(cfg_err(lineno, "address selector requires a value"));
    };

    // After the address, the only accepted continuation is `port = RANGE` (the
    // `=` is optional). Any other trailing tokens — a `ports`/typo, a word before
    // `port`, or garbage after the range — are rejected rather than silently
    // dropped: dropping the port spec would broaden the rule to match *all* ports
    // (e.g. `to: 0.0.0.0/0 ports = 443` would otherwise allow every port).
    let ports = match rest {
        [] => None,
        [kw, spec @ ..] if kw.eq_ignore_ascii_case("port") => {
            // The `=` is optional but, if present, appears exactly once right
            // after `port`. Strip that single leading `=`; any further `=` is a
            // stray token and rejected rather than silently dropped. The range
            // itself may still be split across tokens (`1024 - 2000`).
            let range_tokens = match spec {
                [eq, rest @ ..] if eq == "=" => rest,
                _ => spec,
            };
            if range_tokens.iter().any(|tok| tok == "=") {
                return Err(cfg_err(lineno, "unexpected '=' in port spec"));
            }
            let spec = range_tokens.concat();
            if spec.is_empty() {
                return Err(cfg_err(lineno, "'port' requires a range value"));
            }
            let range: PortRange = spec
                .parse()
                .map_err(|e| cfg_err(lineno, &format!("invalid port spec '{spec}': {e}")))?;
            Some(range)
        }
        _ => {
            return Err(cfg_err(
                lineno,
                &format!(
                    "expected 'ADDR' or 'ADDR port = RANGE', got unexpected tokens after '{addr}'"
                ),
            ));
        }
    };

    // The selector address is the first token: a network (CIDR or bare IP), or
    // — only for a `socks` rule `to:` — a hostname pattern such as
    // `.example.com` (domain and subdomains) or `example.com` (exact).
    match addr.parse::<Cidr>() {
        Ok(cidr) => Ok(AddrSpec::new(cidr, ports)),
        Err(cidr_err) if allow_hosts => match HostPattern::parse(addr) {
            Ok(pattern) => Ok(AddrSpec::host(pattern, ports)),
            // Neither a network nor a hostname: surface both errors so a
            // mistyped CIDR (e.g. `10.0.0.0/33`) is not mislabelled as a bad
            // hostname pattern.
            Err(host_err) => Err(cfg_err(
                lineno,
                &format!(
                    "invalid destination '{addr}': not a network ({cidr_err}) \
                     or hostname pattern ({host_err})"
                ),
            )),
        },
        Err(cidr_err) => Err(cfg_err(
            lineno,
            &format!(
                "invalid address '{addr}': {cidr_err} \
                 (hostname patterns are only allowed in a socks rule 'to:')"
            ),
        )),
    }
}

fn parse_protocols(vals: &[String], lineno: usize) -> Result<Vec<Protocol>> {
    if vals.is_empty() {
        // A present-but-empty `protocol:` would parse to an empty set, which the
        // matcher treats as "any" — silently broadening the rule. Omit the
        // directive entirely to mean "any protocol".
        return Err(cfg_err(lineno, "expected at least one protocol"));
    }
    let mut out = Vec::new();
    for v in vals {
        let p = match v.to_ascii_lowercase().as_str() {
            "tcp" => Protocol::Tcp,
            "udp" => Protocol::Udp,
            other => {
                return Err(cfg_err(
                    lineno,
                    &format!("unknown protocol '{other}' (expected 'tcp' or 'udp')"),
                ))
            }
        };
        if !out.contains(&p) {
            out.push(p);
        }
    }
    Ok(out)
}

fn parse_commands(vals: &[String], lineno: usize) -> Result<Vec<Command>> {
    if vals.is_empty() {
        // A present-but-empty `command:` would parse to an empty set, which the
        // matcher treats as "any" — silently broadening the rule. Omit the
        // directive entirely to mean "any command".
        return Err(cfg_err(lineno, "expected at least one command"));
    }
    let mut out = Vec::new();
    for v in vals {
        let c = match v.to_ascii_lowercase().as_str() {
            "connect" => Command::Connect,
            "bind" => Command::Bind,
            "udpassociate" | "udp" => Command::UdpAssociate,
            other => {
                return Err(cfg_err(
                    lineno,
                    &format!(
                        "unknown command '{other}' (expected 'connect', 'bind' or 'udpassociate')"
                    ),
                ))
            }
        };
        if !out.contains(&c) {
            out.push(c);
        }
    }
    Ok(out)
}

// ---------------------------------------------------------------------------
// Include expansion
// ---------------------------------------------------------------------------

fn parse_include(
    vals: &[String],
    lineno: usize,
    source: &ParseSource,
    builder: &mut Builder,
    stack: &mut Vec<PathBuf>,
) -> Result<()> {
    if vals.is_empty() {
        return Err(cfg_err_at(source, lineno, "include requires a path"));
    }
    if source.base_dir.is_none() {
        return Err(cfg_err_at(
            source,
            lineno,
            "include is only supported when loading configuration from a file",
        ));
    }

    let raw_pattern = vals.join(" ");
    let pattern = resolve_include_pattern(&raw_pattern, source).map_err(|e| {
        cfg_err_at(
            source,
            lineno,
            &format!(
                "include '{raw_pattern}' failed: {}",
                config_error_message(&e)
            ),
        )
    })?;
    let include_paths = expand_include_pattern(&pattern).map_err(|e| {
        cfg_err_at(
            source,
            lineno,
            &format!("include '{raw_pattern}' failed: {e}"),
        )
    })?;

    if include_paths.is_empty() {
        return Err(cfg_err_at(
            source,
            lineno,
            &format!("include '{raw_pattern}' failed: matched no files"),
        ));
    }

    for include_path in include_paths {
        parse_config_file(&include_path, builder, stack).map_err(|e| {
            cfg_err_at(
                source,
                lineno,
                &format!(
                    "include '{}' failed: {}",
                    include_path.display(),
                    config_error_message(&e)
                ),
            )
        })?;
    }

    Ok(())
}

fn resolve_include_pattern(raw_pattern: &str, source: &ParseSource) -> Result<PathBuf> {
    let pattern = PathBuf::from(raw_pattern);
    if pattern.is_absolute() {
        return Ok(pattern);
    }

    let base = source
        .base_dir
        .clone()
        .ok_or_else(|| Error::Config("include has no base directory".into()))?;
    Ok(base.join(pattern))
}

fn expand_include_pattern(pattern: &Path) -> std::io::Result<Vec<PathBuf>> {
    if !path_has_wildcards(pattern) {
        return Ok(vec![pattern.to_path_buf()]);
    }

    let Some(file_name) = pattern.file_name() else {
        return Ok(Vec::new());
    };
    let file_pattern = file_name.to_string_lossy();
    let parent = pattern.parent().unwrap_or_else(|| Path::new("."));
    if path_has_wildcards(parent) {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            "wildcards are only supported in the final path component",
        ));
    }

    let mut matches = Vec::new();
    for entry in fs::read_dir(parent)? {
        let entry = entry?;
        let name = entry.file_name();
        let name = name.to_string_lossy();
        if wildcard_match(&file_pattern, &name) && entry.path().metadata()?.is_file() {
            matches.push(parent.join(entry.file_name()));
        }
    }

    matches.sort();
    Ok(matches)
}

fn path_has_wildcards(path: &Path) -> bool {
    path.components().any(|component| {
        matches!(
            component,
            Component::Normal(value) if string_has_wildcards(&value.to_string_lossy())
        )
    })
}

fn string_has_wildcards(value: &str) -> bool {
    value.contains('*') || value.contains('?')
}

fn wildcard_match(pattern: &str, text: &str) -> bool {
    #[cfg(windows)]
    let (pattern, text) = (pattern.to_ascii_lowercase(), text.to_ascii_lowercase());
    #[cfg(not(windows))]
    let (pattern, text) = (pattern.to_string(), text.to_string());

    let pattern: Vec<char> = pattern.chars().collect();
    let text: Vec<char> = text.chars().collect();
    let mut dp = vec![vec![false; text.len() + 1]; pattern.len() + 1];
    dp[0][0] = true;

    for i in 1..=pattern.len() {
        if pattern[i - 1] == '*' {
            dp[i][0] = dp[i - 1][0];
        }
    }

    for i in 1..=pattern.len() {
        for j in 1..=text.len() {
            dp[i][j] = match pattern[i - 1] {
                '*' => dp[i - 1][j] || dp[i][j - 1],
                '?' => dp[i - 1][j - 1],
                ch => ch == text[j - 1] && dp[i - 1][j - 1],
            };
        }
    }

    dp[pattern.len()][text.len()]
}

// ---------------------------------------------------------------------------
// Small helpers
// ---------------------------------------------------------------------------

/// Strips a `#` comment from a line, respecting nothing fancy (no quoting).
fn strip_comment(line: &str) -> &str {
    match line.find('#') {
        Some(pos) => &line[..pos],
        None => line,
    }
}

/// Joins tokens (dropping a leading `=`) and removes interior whitespace so
/// that `["=", "1024", "-", "2000"]` becomes `"1024-2000"`.
fn join_value_after(vals: &[String]) -> String {
    vals.iter()
        .filter(|v| v.as_str() != "=")
        .map(|s| s.as_str())
        .collect::<Vec<_>>()
        .join("")
}

fn cfg_err(lineno: usize, msg: &str) -> Error {
    Error::Config(format!("line {lineno}: {msg}"))
}

fn cfg_err_at(source: &ParseSource, lineno: usize, msg: &str) -> Error {
    match &source.display {
        Some(display) => Error::Config(format!("{display}:line {lineno}: {msg}")),
        None => cfg_err(lineno, msg),
    }
}

fn with_source_context(err: Error, source: &ParseSource) -> Error {
    let Some(display) = &source.display else {
        return err;
    };

    match err {
        Error::Config(msg) if msg.starts_with("line ") => Error::Config(format!("{display}:{msg}")),
        other => other,
    }
}

fn config_error_message(err: &Error) -> String {
    match err {
        Error::Config(msg) => msg.clone(),
        other => other.to_string(),
    }
}

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

    fn sample() -> &'static str {
        r#"
# Alighieri sample
internal: 0.0.0.0 port = 1080
external: 0.0.0.0
socksmethod: none
connecttimeout: 15
handshaketimeout: 7
udptimeout: 90
maxconnections: 256
logoutput: stdout stderr
logformat: json
logrotate.size: 2MiB
logrotate.keep: 3
dns.prefer: ipv6
dns.tryall: yes
dns.deny: private linklocal loopback
dns.cachettl: 30
auth.cachettl: 120
metrics.listen: 127.0.0.1:9090
tls.certfile: /etc/alighieri/tls/server.crt
tls.keyfile: /etc/alighieri/tls/server.key
ratelimit.connectionrate: 60/60
ratelimit.authfailurerate: 5/300
ratelimit.concurrentconnections: 10
ratelimit.byterate: 10MiB/60

client pass {
    from: 10.0.0.0/8 to: 0.0.0.0/0
}

socks block {
    from: 0.0.0.0/0 to: 127.0.0.0/8
}

socks pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    protocol: tcp udp
    command: connect udpassociate
}
"#
    }

    #[test]
    fn parse_full_sample() {
        let cfg = Config::parse(sample()).unwrap();
        assert_eq!(cfg.internal, "0.0.0.0:1080".parse().unwrap());
        assert_eq!(cfg.external, "0.0.0.0".parse::<IpAddr>().unwrap());
        assert_eq!(cfg.socks_methods, vec![AuthKind::None]);
        assert_eq!(cfg.connect_timeout, Duration::from_secs(15));
        assert_eq!(cfg.handshake_timeout, Duration::from_secs(7));
        assert_eq!(cfg.udp_timeout, Duration::from_secs(90));
        assert_eq!(cfg.max_connections, 256);
        assert_eq!(cfg.log_outputs, vec![LogOutput::Stdout, LogOutput::Stderr]);
        assert_eq!(cfg.log_format, LogFormat::Json);
        assert_eq!(cfg.log_rotate_size, 2 * 1024 * 1024);
        assert_eq!(cfg.log_rotate_keep, 3);
        assert_eq!(cfg.dns.preference, DnsPreference::Ipv6);
        assert!(cfg.dns.try_all);
        assert_eq!(
            cfg.dns.deny,
            vec![
                DnsDenyCategory::Private,
                DnsDenyCategory::LinkLocal,
                DnsDenyCategory::Loopback
            ]
        );
        assert_eq!(cfg.dns.cache_ttl, Some(Duration::from_secs(30)));
        assert_eq!(cfg.auth_cache_ttl, Some(Duration::from_secs(120)));
        assert_eq!(cfg.metrics_listen, Some("127.0.0.1:9090".parse().unwrap()));
        assert_eq!(
            cfg.tls,
            Some(TlsConfig::Files {
                cert_file: PathBuf::from("/etc/alighieri/tls/server.crt"),
                key_file: PathBuf::from("/etc/alighieri/tls/server.key")
            })
        );
        assert_eq!(
            cfg.rate_limits.connection_rate,
            Some(RateLimit {
                limit: 60,
                window: Duration::from_secs(60)
            })
        );
        assert_eq!(
            cfg.rate_limits.auth_failure_rate,
            Some(RateLimit {
                limit: 5,
                window: Duration::from_secs(300)
            })
        );
        assert_eq!(cfg.rate_limits.concurrent_connections, Some(10));
        assert_eq!(
            cfg.rate_limits.byte_rate,
            Some(RateLimit {
                limit: 10 * 1024 * 1024,
                window: Duration::from_secs(60)
            })
        );
        assert_eq!(cfg.rules.rules.len(), 3);
    }

    #[test]
    fn defaults_applied() {
        let cfg = Config::parse("internal: 127.0.0.1 port = 1080").unwrap();
        assert_eq!(cfg.external, IpAddr::V4(Ipv4Addr::UNSPECIFIED));
        assert_eq!(cfg.socks_methods, vec![AuthKind::None]);
        assert_eq!(cfg.connect_timeout, Duration::from_secs(30));
        assert_eq!(cfg.handshake_timeout, Duration::from_secs(10));
        assert_eq!(cfg.io_timeout, Duration::ZERO);
        assert_eq!(cfg.udp_timeout, Duration::from_secs(60));
        assert_eq!(cfg.max_connections, 1024);
        assert_eq!(cfg.log_outputs, vec![LogOutput::Stdout]);
        assert_eq!(cfg.log_file, None);
        assert_eq!(cfg.log_format, LogFormat::Text);
        assert_eq!(cfg.log_rotate_size, DEFAULT_LOG_ROTATE_SIZE_BYTES);
        assert_eq!(cfg.log_rotate_keep, DEFAULT_LOG_ROTATE_KEEP);
        assert_eq!(cfg.dns.preference, DnsPreference::System);
        assert!(!cfg.dns.try_all);
        assert!(cfg.dns.deny.is_empty());
        assert_eq!(cfg.dns.cache_ttl, None);
        assert_eq!(
            cfg.auth_cache_ttl,
            Some(Duration::from_secs(DEFAULT_AUTH_CACHE_TTL_SECS))
        );
        assert_eq!(cfg.metrics_listen, None);
        assert_eq!(cfg.tls, None);
        assert_eq!(cfg.rate_limits, RateLimits::default());
    }

    #[test]
    fn tls_requires_cert_and_key() {
        let err = Config::parse("internal: 127.0.0.1:1080\ntls.certfile: server.crt").unwrap_err();
        assert!(err.to_string().contains("tls.certfile requires"));

        let err = Config::parse("internal: 127.0.0.1:1080\ntls.keyfile: server.key").unwrap_err();
        assert!(err.to_string().contains("tls.keyfile requires"));
    }

    #[test]
    fn acme_tls_parses() {
        let cfg = Config::parse(
            r#"
internal: 0.0.0.0:443
tls.acme.domains: a.example.com b.example.com
tls.acme.email: admin@example.com
tls.acme.cache: /var/lib/alighieri/acme
tls.acme.staging: on
"#,
        )
        .unwrap();
        assert_eq!(
            cfg.tls,
            Some(TlsConfig::Acme(AcmeConfig {
                domains: vec!["a.example.com".into(), "b.example.com".into()],
                email: Some("admin@example.com".into()),
                cache_dir: PathBuf::from("/var/lib/alighieri/acme"),
                staging: true,
            }))
        );
    }

    #[test]
    fn acme_domains_normalize_trailing_dot() {
        // A tolerated trailing dot (FQDN) is stripped when stored, so the ACME stack
        // receives a clean identifier.
        let cfg = Config::parse(
            "internal: 0.0.0.0:443\ntls.acme.domains: proxy.example.com.\ntls.acme.cache: /tmp/acme",
        )
        .unwrap();
        let Some(TlsConfig::Acme(acme)) = cfg.tls else {
            panic!("expected an ACME TLS config");
        };
        assert_eq!(acme.domains, vec!["proxy.example.com".to_string()]);
    }

    #[test]
    fn acme_email_rejects_multiple_addresses() {
        let err = Config::parse(
            "internal: 0.0.0.0:443\ntls.acme.domains: x.example.com\ntls.acme.cache: /tmp/acme\ntls.acme.email: a@example.com b@example.com",
        )
        .unwrap_err();
        assert!(err.to_string().contains("single address"), "got: {err}");
    }

    #[test]
    fn acme_and_certfile_are_mutually_exclusive() {
        let err = Config::parse(
            "internal: 0.0.0.0:443\ntls.acme.domains: x.example.com\ntls.acme.cache: /tmp/acme\ntls.certfile: server.crt\ntls.keyfile: server.key",
        )
        .unwrap_err();
        assert!(err.to_string().contains("cannot use both"), "got: {err}");
    }

    #[test]
    fn acme_requires_a_cache_dir() {
        let err =
            Config::parse("internal: 0.0.0.0:443\ntls.acme.domains: x.example.com").unwrap_err();
        assert!(err.to_string().contains("tls.acme.cache"), "got: {err}");
    }

    #[test]
    fn parses_rate_limit_aliases() {
        let cfg = Config::parse(
            r#"
internal: 127.0.0.1:1080
ratelimit.connection.rate: 2/10
ratelimit.auth.failure.rate: 3/20
ratelimit.concurrent: 4
ratelimit.bytes: 64KiB/30
"#,
        )
        .unwrap();

        assert_eq!(
            cfg.rate_limits.connection_rate,
            Some(RateLimit {
                limit: 2,
                window: Duration::from_secs(10)
            })
        );
        assert_eq!(
            cfg.rate_limits.auth_failure_rate,
            Some(RateLimit {
                limit: 3,
                window: Duration::from_secs(20)
            })
        );
        assert_eq!(cfg.rate_limits.concurrent_connections, Some(4));
        assert_eq!(
            cfg.rate_limits.byte_rate,
            Some(RateLimit {
                limit: 64 * 1024,
                window: Duration::from_secs(30)
            })
        );
    }

    #[test]
    fn rejects_invalid_rate_limits() {
        let err =
            Config::parse("internal: 127.0.0.1:1080\nratelimit.connectionrate: 1").unwrap_err();
        assert!(err.to_string().contains("LIMIT/WINDOW_SECONDS"));

        let err = Config::parse("internal: 127.0.0.1:1080\nratelimit.byterate: 0/60").unwrap_err();
        assert!(err.to_string().contains("values must be > 0"));

        let err = Config::parse("internal: 127.0.0.1:1080\nratelimit.concurrentconnections: 0")
            .unwrap_err();
        assert!(err.to_string().contains("must be > 0"));
    }

    #[test]
    fn logfile_implies_file_output() {
        let cfg =
            Config::parse("internal: 127.0.0.1:1080\nlogfile: /var/log/alighieri/alighieri.log")
                .unwrap();
        assert_eq!(cfg.log_outputs, vec![LogOutput::Stdout, LogOutput::File]);
        assert_eq!(
            cfg.log_file,
            Some(PathBuf::from("/var/log/alighieri/alighieri.log"))
        );
    }

    #[test]
    fn file_output_requires_logfile() {
        let err = Config::parse("internal: 127.0.0.1:1080\nlogoutput: file").unwrap_err();
        assert!(err.to_string().contains("requires a 'logfile'"));
    }

    #[test]
    fn parses_log_rotation_size_suffixes() {
        let cfg =
            Config::parse("internal: 127.0.0.1:1080\nlogrotate.size: 10MiB\nlogrotate.keep: 0")
                .unwrap();
        assert_eq!(cfg.log_rotate_size, 10 * 1024 * 1024);
        assert_eq!(cfg.log_rotate_keep, 0);
    }

    #[test]
    fn parses_dns_policy_aliases() {
        let cfg = Config::parse(
            "internal: 127.0.0.1:1080\ndnsprefer: v4\ndnstryall: on\ndnsdeny: reserved doc\ndnscachettl: 45",
        )
        .unwrap();
        assert_eq!(cfg.dns.preference, DnsPreference::Ipv4);
        assert!(cfg.dns.try_all);
        assert_eq!(
            cfg.dns.deny,
            vec![DnsDenyCategory::Reserved, DnsDenyCategory::Documentation]
        );
        assert_eq!(cfg.dns.cache_ttl, Some(Duration::from_secs(45)));
    }

    #[test]
    fn parses_disabled_dns_cache_ttl() {
        let cfg = Config::parse("internal: 127.0.0.1:1080\ndns.cache.ttl: off").unwrap();
        assert_eq!(cfg.dns.cache_ttl, None);

        let cfg = Config::parse("internal: 127.0.0.1:1080\ndns.cachettl: 0").unwrap();
        assert_eq!(cfg.dns.cache_ttl, None);
    }

    #[test]
    fn parses_dns_timeout_and_rejects_zero() {
        let cfg = Config::parse("internal: 127.0.0.1:1080\ndns.timeout: 3").unwrap();
        assert_eq!(cfg.dns.timeout, Duration::from_secs(3));

        // Unset falls back to the default.
        let cfg = Config::parse("internal: 127.0.0.1:1080").unwrap();
        assert_eq!(cfg.dns.timeout, Duration::from_secs(5));

        // Zero is rejected — it would time out every name resolution.
        let err = Config::parse("internal: 127.0.0.1:1080\ndns.timeout: 0").unwrap_err();
        assert!(format!("{err}").contains("dns.timeout"), "{err}");

        // An absurd value is rejected so it cannot overflow the timer deadline.
        let err = Config::parse("internal: 127.0.0.1:1080\ndns.timeout: 100000").unwrap_err();
        assert!(format!("{err}").contains("dns.timeout"), "{err}");
        // The upper bound itself is accepted.
        let cfg = Config::parse("internal: 127.0.0.1:1080\ndns.timeout: 3600").unwrap();
        assert_eq!(cfg.dns.timeout, Duration::from_secs(3600));
    }

    #[test]
    fn rejects_zero_connect_and_handshake_timeout() {
        // Zero would make tokio::time::timeout expire immediately and fail every
        // connection, so it is rejected rather than silently accepted.
        let err = Config::parse("internal: 127.0.0.1:1080\nconnecttimeout: 0").unwrap_err();
        assert!(format!("{err}").contains("connecttimeout"), "{err}");
        let err = Config::parse("internal: 127.0.0.1:1080\nhandshaketimeout: 0").unwrap_err();
        assert!(format!("{err}").contains("handshaketimeout"), "{err}");

        // Positive values parse, and `iotimeout: 0` (a genuine "disabled" idle
        // timeout) is still accepted.
        let cfg = Config::parse(
            "internal: 127.0.0.1:1080\nconnecttimeout: 5\nhandshaketimeout: 3\niotimeout: 0",
        )
        .unwrap();
        assert_eq!(cfg.connect_timeout, Duration::from_secs(5));
        assert_eq!(cfg.handshake_timeout, Duration::from_secs(3));
        assert_eq!(cfg.io_timeout, Duration::ZERO);
    }

    #[test]
    fn rejects_trailing_tokens_on_scalar_settings() {
        // A trailing token on a scalar setting is a typo, not silently ignored.
        assert!(Config::parse("internal: 127.0.0.1:1080\nmaxconnections: 100 oops").is_err());
        assert!(Config::parse("internal: 127.0.0.1:1080\ndns.tryall: yes maybe").is_err());
        // Endpoint: tokens before `port`, or after a bare `IP:PORT`.
        assert!(Config::parse("internal: 127.0.0.1 oops port = 1080").is_err());
        assert!(Config::parse("internal: 127.0.0.1:1080 oops").is_err());
        // Endpoint: split or trailing tokens after `port` must not be joined into
        // a single port (e.g. `port = 10 80` -> 1080); a lone `port =` (no value)
        // is rejected directly rather than as an empty port string.
        assert!(Config::parse("internal: 127.0.0.1 port = 10 80").is_err());
        assert!(Config::parse("internal: 127.0.0.1 port = 1080 =").is_err());
        assert!(Config::parse("internal: 127.0.0.1 port =").is_err());
        // The keyword form is exactly `port = N`; a `=`-less `port N` is rejected.
        assert!(Config::parse("internal: 127.0.0.1 port 1080").is_err());
        // The documented `IP port = N` and bare `IP:PORT` forms still parse.
        assert!(Config::parse("internal: 127.0.0.1 port = 1080").is_ok());
        assert!(Config::parse("internal: 127.0.0.1:1080").is_ok());
        assert!(
            Config::parse("internal: 127.0.0.1:1080\nmaxconnections: 100\ndns.tryall: yes").is_ok()
        );
    }

    #[test]
    fn rejects_trailing_tokens_on_value_settings() {
        // Scalar value parsers (IP, enum keywords, the cache-ttl keyword, usize)
        // reject extra tokens instead of silently using only the first.
        assert!(Config::parse("internal: 127.0.0.1:1080\nexternal: 0.0.0.0 oops").is_err());
        assert!(Config::parse("internal: 127.0.0.1:1080\nlogformat: json text").is_err());
        assert!(Config::parse("internal: 127.0.0.1:1080\ndns.prefer: ipv4 oops").is_err());
        assert!(Config::parse("internal: 127.0.0.1:1080\ndns.cachettl: off oops").is_err());
        assert!(Config::parse("internal: 127.0.0.1:1080\nauth.cachettl: none oops").is_err());
        assert!(Config::parse("internal: 127.0.0.1:1080\nlogrotate.keep: 5 oops").is_err());
        // The valid single-value forms still parse.
        assert!(Config::parse("internal: 127.0.0.1:1080\nexternal: 0.0.0.0").is_ok());
        assert!(Config::parse("internal: 127.0.0.1:1080\nlogformat: json").is_ok());
        assert!(Config::parse("internal: 127.0.0.1:1080\ndns.prefer: ipv4").is_ok());
        assert!(Config::parse("internal: 127.0.0.1:1080\ndns.cachettl: off").is_ok());
        assert!(Config::parse("internal: 127.0.0.1:1080\ndns.cachettl: 60").is_ok());
        assert!(Config::parse("internal: 127.0.0.1:1080\nlogrotate.keep: 5").is_ok());
    }

    #[test]
    fn rejects_out_of_range_numeric_limits() {
        // maxconnections above the connection-limiter maximum is rejected — it
        // would otherwise panic `Semaphore::new` at startup.
        assert!(
            Config::parse("internal: 127.0.0.1:1080\nmaxconnections: 18446744073709551615")
                .is_err()
        );
        // logrotate.keep above the practical maximum is rejected — it drives an
        // O(n) rename loop on each rotation.
        assert!(Config::parse("internal: 127.0.0.1:1080\nlogrotate.keep: 10001").is_err());
        // Generous-but-bounded values still parse.
        assert!(Config::parse("internal: 127.0.0.1:1080\nmaxconnections: 1000000").is_ok());
        assert!(Config::parse("internal: 127.0.0.1:1080\nlogrotate.keep: 10000").is_ok());
    }

    #[test]
    fn parses_disabled_auth_cache_ttl() {
        let cfg = Config::parse("internal: 127.0.0.1:1080\nauth.cachettl: off").unwrap();
        assert_eq!(cfg.auth_cache_ttl, None);

        let cfg = Config::parse("internal: 127.0.0.1:1080\nauth.cache.ttl: 0").unwrap();
        assert_eq!(cfg.auth_cache_ttl, None);
    }

    #[test]
    fn missing_dns_cache_ttl_mentions_disabled_values() {
        let err = Config::parse("internal: 127.0.0.1:1080\ndns.cachettl:").unwrap_err();
        let message = err.to_string();

        assert!(message.contains("0"));
        assert!(message.contains("off"));
        assert!(message.contains("none"));
        assert!(message.contains("disabled"));
    }

    #[test]
    fn rejects_unknown_dns_preference() {
        let err = Config::parse("internal: 127.0.0.1:1080\ndns.prefer: ipv10").unwrap_err();
        assert!(err.to_string().contains("unsupported dns.prefer"));
    }

    #[test]
    fn internal_required() {
        let err = Config::parse("external: 0.0.0.0").unwrap_err();
        assert!(err
            .to_string()
            .contains("missing required setting: internal"));
    }

    #[test]
    fn internal_ip_port_colon_form() {
        let cfg = Config::parse("internal: 0.0.0.0:1080").unwrap();
        assert_eq!(cfg.internal, "0.0.0.0:1080".parse().unwrap());
    }

    #[test]
    fn username_requires_userlist() {
        let err =
            Config::parse("internal: 0.0.0.0 port = 1080\nsocksmethod: username").unwrap_err();
        assert!(err.to_string().contains("requires a 'userlist'"));
    }

    #[test]
    fn username_with_userlist_ok() {
        let cfg = Config::parse(
            "internal: 0.0.0.0 port = 1080\nsocksmethod: username none\nuserlist: /tmp/users",
        )
        .unwrap();
        assert_eq!(cfg.socks_methods, vec![AuthKind::Username, AuthKind::None]);
        assert_eq!(cfg.userlist, Some(PathBuf::from("/tmp/users")));
    }

    #[test]
    fn metrics_public_bind_requires_allowpublic() {
        let head = "internal: 127.0.0.1 port = 1080\n";

        // The rule is a startup-only validation, not a parse error (so a reload
        // that touches the non-reloadable metrics settings does not fail).
        // Loopback — including IPv4-mapped — is always allowed.
        for addr in ["127.0.0.1:9090", "[::1]:9090", "[::ffff:127.0.0.1]:9090"] {
            let cfg = Config::parse(&format!("{head}metrics.listen: {addr}")).unwrap();
            assert!(!cfg.metrics_allow_public);
            cfg.validate_startup()
                .unwrap_or_else(|e| panic!("{addr} should be allowed: {e}"));
        }

        // A non-loopback or unspecified bind parses but is refused at startup
        // without the opt-in.
        for addr in ["0.0.0.0:9090", "192.168.1.5:9090", "[::]:9090"] {
            let cfg = Config::parse(&format!("{head}metrics.listen: {addr}")).unwrap();
            let err = cfg.validate_startup().unwrap_err();
            let msg = err.to_string();
            assert!(msg.contains("metrics.allowpublic"), "{addr}: {err}");
            // The message uses a `\` line continuation, which strips the next
            // line's indentation — guard against it leaking a run of spaces.
            assert!(!msg.contains("  "), "message has stray spacing: {msg}");
        }

        // With the explicit opt-in it passes startup validation.
        let cfg = Config::parse(&format!(
            "{head}metrics.listen: 0.0.0.0:9090\nmetrics.allowpublic: true"
        ))
        .unwrap();
        assert!(cfg.metrics_allow_public);
        assert_eq!(cfg.metrics_listen, Some("0.0.0.0:9090".parse().unwrap()));
        cfg.validate_startup().unwrap();

        // The concatenated alias parses too (set to the non-default value).
        let cfg = Config::parse(&format!(
            "{head}metrics.listen: 127.0.0.1:9090\nmetricsallowpublic: true"
        ))
        .unwrap();
        assert!(cfg.metrics_allow_public);
    }

    #[test]
    fn flags_noauth_on_a_non_loopback_listener() {
        let flagged = |cfg: &str| {
            Config::parse(cfg)
                .unwrap()
                .noauth_on_non_loopback_listener()
        };

        // No-auth (the default) on a non-loopback listener is flagged.
        assert!(flagged("internal: 0.0.0.0 port = 1080"));
        assert!(flagged("internal: 192.168.1.5 port = 1080"));
        // Loopback listeners — including IPv6 and IPv4-mapped — are not flagged.
        assert!(!flagged("internal: 127.0.0.1 port = 1080"));
        assert!(!flagged("internal: ::1 port = 1080"));
        assert!(!flagged("internal: ::ffff:127.0.0.1 port = 1080"));
        // Username-only auth on a non-loopback listener is not flagged.
        assert!(!flagged(
            "internal: 0.0.0.0 port = 1080\nsocksmethod: username\nuserlist: /tmp/users"
        ));
        // Offering 'none' alongside 'username' still flags it: a client can pick none.
        assert!(flagged(
            "internal: 0.0.0.0 port = 1080\nsocksmethod: username none\nuserlist: /tmp/users"
        ));
    }

    #[test]
    fn rule_with_port_range() {
        let cfg = Config::parse(
            r#"internal: 0.0.0.0 port = 1080
socks pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0 port = 1024 - 2000
    command: connect
}"#,
        )
        .unwrap();
        let rule = &cfg.rules.rules[0];
        assert_eq!(
            rule.to.ports,
            Some(PortRange {
                min: 1024,
                max: 2000
            })
        );
        assert_eq!(rule.commands, vec![Command::Connect]);
    }

    #[test]
    fn socks_rule_bandwidth_parses() {
        let cfg = Config::parse(
            r#"internal: 0.0.0.0 port = 1080
socks pass {
    to: 0.0.0.0/0
    command: connect
    bandwidth: 5MiB/2
}"#,
        )
        .unwrap();
        assert_eq!(
            cfg.rules.rules[0].bandwidth,
            Some(RateLimit {
                limit: 5 * 1024 * 1024,
                window: Duration::from_secs(2),
            })
        );
    }

    #[test]
    fn bandwidth_on_client_rule_is_rejected() {
        let err = Config::parse(
            "internal: 0.0.0.0 port = 1080\nclient pass { from: 0.0.0.0/0 bandwidth: 1MiB/1 }",
        )
        .unwrap_err();
        assert!(
            err.to_string()
                .contains("bandwidth is only valid in a socks rule"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn rule_single_port() {
        let cfg = Config::parse(
            r#"internal: 0.0.0.0 port = 1080
socks pass {
    to: 0.0.0.0/0 port = 443
}"#,
        )
        .unwrap();
        assert_eq!(
            cfg.rules.rules[0].to.ports,
            Some(PortRange { min: 443, max: 443 })
        );
    }

    #[test]
    fn socks_to_accepts_hostname_patterns() {
        let cfg = Config::parse("internal: 0.0.0.0 port = 1080\nsocks pass { to: .example.com }")
            .unwrap();
        let to = &cfg.rules.rules[0].to;
        assert_eq!(to.hosts, vec![HostPattern::Suffix("example.com".into())]);
        assert!(to.cidrs.is_empty());

        let cfg = Config::parse(
            "internal: 0.0.0.0 port = 1080\nsocks pass { to: api.example.com port 443 }",
        )
        .unwrap();
        let to = &cfg.rules.rules[0].to;
        assert_eq!(to.hosts, vec![HostPattern::Exact("api.example.com".into())]);
        assert_eq!(to.ports, Some(PortRange { min: 443, max: 443 }));
    }

    #[test]
    fn address_selectors_reject_stray_tokens() {
        let base = "internal: 0.0.0.0 port = 1080\n";
        let parse = |rule: &str| Config::parse(&format!("{base}{rule}"));

        // A `port` typo or stray token must not be silently dropped: dropping the
        // port spec would broaden the rule to match *all* ports.
        assert!(parse("socks pass { to: 0.0.0.0/0 ports = 443 }").is_err());
        assert!(parse("socks pass { to: 0.0.0.0/0 oops port = 443 }").is_err());
        assert!(parse("socks pass { to: 0.0.0.0/0 port = 443 oops }").is_err());
        assert!(parse("socks pass { to: 0.0.0.0/0 oops }").is_err());
        assert!(parse("socks pass { from: 10.0.0.0/8 oops }").is_err());
        // A stray `=` is a token too: it is not silently filtered out.
        assert!(parse("socks pass { to: 0.0.0.0/0 port = 443 = }").is_err());
        assert!(parse("socks pass { to: 0.0.0.0/0 port = = 443 }").is_err());
        // The documented forms still parse (the `=` is optional; ranges allowed).
        assert!(parse("socks pass { to: 0.0.0.0/0 port = 443 }").is_ok());
        assert!(parse("socks pass { to: 0.0.0.0/0 port 443 }").is_ok());
        assert!(parse("socks pass { to: 0.0.0.0/0 port = 1024 - 2000 }").is_ok());
        assert!(parse("socks pass { to: 0.0.0.0/0 }").is_ok());
    }

    #[test]
    fn hostname_patterns_rejected_outside_socks_to() {
        // `from:` is IP-only (source-hostname matching is unsupported).
        assert!(Config::parse(
            "internal: 0.0.0.0 port = 1080\nsocks pass { from: .example.com to: 0.0.0.0/0 }"
        )
        .is_err());
        // A `client` rule `to:` is the proxy's own address — an IP, not a host.
        assert!(Config::parse(
            "internal: 0.0.0.0 port = 1080\nclient pass { from: 0.0.0.0/0 to: .example.com }"
        )
        .is_err());
    }

    #[test]
    fn socks_to_mistyped_cidr_reports_network_error() {
        // A bad CIDR in a socks `to:` must not be mislabelled as a bad hostname;
        // the error should mention the network failure too.
        let err = Config::parse("internal: 0.0.0.0 port = 1080\nsocks pass { to: 10.0.0.0/33 }")
            .unwrap_err();
        assert!(err.to_string().contains("not a network"), "got: {err}");
    }

    #[test]
    fn rule_defaults_to_any() {
        let cfg = Config::parse(
            r#"internal: 0.0.0.0 port = 1080
socks pass { command: connect }"#,
        )
        .unwrap();
        let rule = &cfg.rules.rules[0];
        assert!(rule.from.matches("8.8.8.8".parse().unwrap(), 443));
        assert!(rule.from.matches("2001:db8::1".parse().unwrap(), 443));
        assert!(rule.to.matches("8.8.8.8".parse().unwrap(), 443));
        assert!(rule.to.matches("2001:db8::1".parse().unwrap(), 443));
    }

    #[test]
    fn parses_named_rules() {
        let cfg = Config::parse(
            r#"internal: 0.0.0.0 port = 1080
client pass "lan-clients" { from: 10.0.0.0/8 }
socks block blocked-loopback { to: 127.0.0.0/8 }"#,
        )
        .unwrap();

        assert_eq!(cfg.rules.rules[0].name.as_deref(), Some("lan-clients"));
        assert_eq!(cfg.rules.rules[1].name.as_deref(), Some("blocked-loopback"));
    }

    #[test]
    fn rejects_invalid_rule_names() {
        let err = Config::parse(
            r#"internal: 0.0.0.0 port = 1080
socks pass "allow web" { command: connect }"#,
        )
        .unwrap_err();
        assert!(err.to_string().contains("single token"));

        let err = Config::parse(
            r#"internal: 0.0.0.0 port = 1080
socks pass "" { command: connect }"#,
        )
        .unwrap_err();
        assert!(err.to_string().contains("cannot be empty"));
    }

    #[test]
    fn unknown_keyword_rejected() {
        let err = Config::parse("internal: 0.0.0.0 port = 1080\nbogus: 1").unwrap_err();
        assert!(err.to_string().contains("unknown keyword 'bogus'"));
    }

    #[test]
    fn udp_port_range_parses_with_default_none() {
        let cfg = Config::parse("internal: 0.0.0.0 port = 1080").unwrap();
        assert_eq!(cfg.udp_port_range, None);

        let cfg =
            Config::parse("internal: 0.0.0.0 port = 1080\nudp.portrange: 20000-21000").unwrap();
        assert_eq!(
            cfg.udp_port_range,
            Some(PortRange {
                min: 20000,
                max: 21000
            })
        );

        // The concatenated alias and the single-port form both work.
        let cfg = Config::parse("internal: 0.0.0.0 port = 1080\nudpportrange: 30000").unwrap();
        assert_eq!(
            cfg.udp_port_range,
            Some(PortRange {
                min: 30000,
                max: 30000
            })
        );
    }

    #[test]
    fn udp_strict_reply_parses_and_defaults_on() {
        let cfg = Config::parse("internal: 0.0.0.0 port = 1080").unwrap();
        assert!(
            cfg.udp_strict_reply,
            "defaults to strict host:port matching"
        );

        // Set `false` (not the default) so the assertion actually proves the key
        // was parsed rather than falling back to the default.
        let cfg = Config::parse("internal: 0.0.0.0 port = 1080\nudp.strictreply: false").unwrap();
        assert!(!cfg.udp_strict_reply);

        // The concatenated alias works too — again with the non-default value.
        let cfg = Config::parse("internal: 0.0.0.0 port = 1080\nudpstrictreply: false").unwrap();
        assert!(!cfg.udp_strict_reply);

        // A non-boolean value is rejected like other booleans.
        assert!(Config::parse("internal: 0.0.0.0 port = 1080\nudp.strictreply: maybe").is_err());
    }

    #[test]
    fn udp_advertise_parses_an_ip() {
        let cfg =
            Config::parse("internal: 0.0.0.0 port = 1080\nudp.advertise: 203.0.113.5").unwrap();
        assert_eq!(
            cfg.udp_advertise,
            Some(UdpAdvertise::Ip("203.0.113.5".parse().unwrap()))
        );

        let cfg =
            Config::parse("internal: 0.0.0.0 port = 1080\nudp.advertise: 2001:db8::1").unwrap();
        assert_eq!(
            cfg.udp_advertise,
            Some(UdpAdvertise::Ip("2001:db8::1".parse().unwrap()))
        );

        // Unset by default.
        assert!(Config::parse("internal: 0.0.0.0 port = 1080")
            .unwrap()
            .udp_advertise
            .is_none());
    }

    #[test]
    fn udp_advertise_parses_a_hostname_without_resolving() {
        // A non-IP value is kept as a hostname and resolved per association, not
        // at config load — so a bad or wedged resolver never affects parsing.
        let cfg = Config::parse(
            "internal: 0.0.0.0 port = 1080\nudp.advertise: nonexistent.invalid.example",
        )
        .unwrap();
        assert_eq!(
            cfg.udp_advertise,
            Some(UdpAdvertise::Host("nonexistent.invalid.example".into()))
        );
    }

    #[test]
    fn udp_advertise_rejects_empty_and_trailing() {
        assert!(Config::parse("internal: 0.0.0.0 port = 1080\nudp.advertise:").is_err());
        // Trailing token is rejected at parse time.
        assert!(
            Config::parse("internal: 0.0.0.0 port = 1080\nudp.advertise: 203.0.113.5 extra")
                .is_err()
        );
    }

    #[test]
    fn udp_advertise_rejects_malformed_hostname() {
        // A non-IP advertise value is a hostname; a structurally malformed one
        // must be caught at config load, not silently fall back at runtime. (Each
        // is a single token reaching the validator; a space-separated value is
        // instead rejected earlier as multiple tokens.)
        let oversize = format!("{}.com", "a".repeat(64));
        for bad in ["foo..bar", ".", oversize.as_str()] {
            let src = format!("internal: 0.0.0.0 port = 1080\nudp.advertise: {bad}");
            let err = Config::parse(&src).unwrap_err().to_string();
            assert!(
                err.contains("invalid udp.advertise host"),
                "expected a host-validation error for {bad:?}, got: {err}"
            );
        }
        // A normal hostname (and an IP) are still accepted.
        assert!(
            Config::parse("internal: 0.0.0.0 port = 1080\nudp.advertise: relay.example.com")
                .is_ok()
        );
        assert!(Config::parse("internal: 0.0.0.0 port = 1080\nudp.advertise: 203.0.113.5").is_ok());
    }

    #[test]
    fn acme_domains_reject_malformed_entries() {
        // Every tls.acme.domains entry is validated during parsing, so a name that
        // is malformed (foo..bar/.) or that a CA cannot issue for via TLS-ALPN-01
        // (wildcard, underscore, single-label, IP, special-use TLD) fails --check
        // rather than reaching the ACME stack.
        for bad in [
            "foo..bar",
            ".",
            "*.example.com",
            "_svc.example.com",
            "localhost",
            "127.0.0.1",
            "router.local",
        ] {
            let src = format!("internal: 0.0.0.0 port = 1080\ntls.acme.domains: {bad}");
            let err = Config::parse(&src).unwrap_err().to_string();
            assert!(
                err.contains("invalid tls.acme.domains entry"),
                "expected a host-validation error for {bad:?}, got: {err}"
            );
        }
        // (Acceptance of a valid issuable name is covered by net::validate_acme_domain
        // tests; a full ACME config needs more fields than this unit asserts.)
    }

    #[test]
    fn shutdown_drain_timeout_parses_and_defaults() {
        let cfg = Config::parse("internal: 0.0.0.0 port = 1080").unwrap();
        assert_eq!(cfg.shutdown_drain_timeout, Duration::from_secs(10));

        let cfg =
            Config::parse("internal: 0.0.0.0 port = 1080\nshutdown.draintimeout: 30").unwrap();
        assert_eq!(cfg.shutdown_drain_timeout, Duration::from_secs(30));

        // `0` is allowed (cut in-flight connections immediately); alias parses.
        let cfg = Config::parse("internal: 0.0.0.0 port = 1080\nshutdowndraintimeout: 0").unwrap();
        assert_eq!(cfg.shutdown_drain_timeout, Duration::ZERO);

        // A non-numeric value is rejected.
        assert!(
            Config::parse("internal: 0.0.0.0 port = 1080\nshutdown.draintimeout: soon").is_err()
        );

        // A value over the cap is rejected (it would overflow the timer deadline).
        let err = Config::parse("internal: 0.0.0.0 port = 1080\nshutdown.draintimeout: 3601")
            .unwrap_err();
        assert!(err.to_string().contains("cannot exceed"), "{err}");
    }

    #[test]
    fn udp_port_range_rejects_invalid() {
        for bad in [
            "udp.portrange:",             // missing value
            "udp.portrange: 0-100",       // includes the ephemeral sentinel 0
            "udp.portrange: 21000-20000", // min > max
            "udp.portrange: nope",        // not a number
        ] {
            let src = format!("internal: 0.0.0.0 port = 1080\n{bad}");
            assert!(Config::parse(&src).is_err(), "expected error for: {bad}");
        }
    }

    #[test]
    fn proxyprotocol_parses_trusted_cidrs() {
        let cfg = Config::parse(
            "internal: 0.0.0.0 port = 1080\nproxyprotocol: 10.0.0.0/8 192.168.0.0/16",
        )
        .unwrap();
        assert_eq!(cfg.proxy_protocol.len(), 2);
        assert!(cfg.proxy_protocol[0].contains("10.1.2.3".parse().unwrap()));
        assert!(cfg.proxy_protocol[1].contains("192.168.5.5".parse().unwrap()));

        // Disabled by default.
        let cfg = Config::parse("internal: 0.0.0.0 port = 1080").unwrap();
        assert!(cfg.proxy_protocol.is_empty());
    }

    #[test]
    fn proxyprotocol_rejects_empty_or_invalid() {
        assert!(Config::parse("internal: 0.0.0.0 port = 1080\nproxyprotocol:").is_err());
        assert!(Config::parse("internal: 0.0.0.0 port = 1080\nproxyprotocol: not-a-cidr").is_err());
    }

    #[test]
    fn auth_command_parses_program_and_args() {
        let cfg = Config::parse(
            "internal: 0.0.0.0 port = 1080\nauth.command: /usr/local/bin/verify --ldap",
        )
        .unwrap();
        assert_eq!(
            cfg.auth_command.as_deref(),
            Some(["/usr/local/bin/verify".to_string(), "--ldap".to_string()].as_slice())
        );

        // Disabled by default.
        assert!(Config::parse("internal: 0.0.0.0 port = 1080")
            .unwrap()
            .auth_command
            .is_none());

        // A program path is required.
        assert!(Config::parse("internal: 0.0.0.0 port = 1080\nauth.command:").is_err());

        // The `username` method is satisfied by auth.command without a userlist.
        assert!(Config::parse(
            "internal: 0.0.0.0 port = 1080\nsocksmethod: username\nauth.command: /bin/true"
        )
        .is_ok());
    }

    #[test]
    fn unterminated_block_rejected() {
        let err = Config::parse("internal: 0.0.0.0 port = 1080\nsocks pass {\n from: 0.0.0.0/0")
            .unwrap_err();
        assert!(err.to_string().contains("unterminated"));
    }

    #[test]
    fn rejects_tokens_after_closing_brace() {
        let head = "internal: 0.0.0.0 port = 1080\n";

        // A selector typed outside the braces is dropped by the body parser
        // (which only sees tokens up to '}'), silently widening the rule — so it
        // must be a parse error.
        let err = Config::parse(&format!(
            "{head}socks pass {{ command: connect }} protocol: tcp"
        ))
        .unwrap_err();
        assert!(
            err.to_string().contains("unexpected tokens after '}'"),
            "{err}"
        );

        // A second rule crammed onto the same line as the first's '}' is also
        // rejected rather than silently mangled.
        let err = Config::parse(&format!(
            "{head}socks pass {{ command: connect }} socks block {{ }}"
        ))
        .unwrap_err();
        assert!(
            err.to_string().contains("unexpected tokens after '}'"),
            "{err}"
        );
    }

    #[test]
    fn comment_after_closing_brace_is_ok() {
        // A comment is stripped before tokenizing, so it is not mistaken for
        // stray tokens after the closing brace.
        let cfg = Config::parse(
            "internal: 0.0.0.0 port = 1080\nsocks pass { command: connect } # trailing comment",
        )
        .expect("a comment after '}' should parse");
        assert_eq!(cfg.rules.rules.len(), 1);
    }

    #[test]
    fn brace_on_same_line_inline_rule() {
        let cfg = Config::parse(
            "internal: 0.0.0.0 port = 1080\nsocks pass { from: 0.0.0.0/0 to: 0.0.0.0/0 }",
        )
        .unwrap();
        assert_eq!(cfg.rules.rules.len(), 1);
    }

    #[test]
    fn comments_are_stripped() {
        let cfg = Config::parse(
            "internal: 0.0.0.0 port = 1080 # listen here\n# full line comment\nsocksmethod: none",
        )
        .unwrap();
        assert_eq!(cfg.internal, "0.0.0.0:1080".parse().unwrap());
    }

    #[test]
    fn load_expands_relative_include_glob() {
        let dir = tempfile::tempdir().unwrap();
        let conf_dir = dir.path().join("conf.d");
        fs::create_dir(&conf_dir).unwrap();
        fs::write(
            dir.path().join("alighieri.conf"),
            "internal: 127.0.0.1:1080\ninclude: conf.d/*.conf\n",
        )
        .unwrap();
        fs::write(
            conf_dir.join("10-policy.conf"),
            "socks block { to: 127.0.0.0/8 }\n",
        )
        .unwrap();
        fs::write(
            conf_dir.join("20-policy.conf"),
            "socks pass { to: 0.0.0.0/0 command: connect }\n",
        )
        .unwrap();

        let cfg = Config::load(&dir.path().join("alighieri.conf")).unwrap();

        assert_eq!(cfg.internal, "127.0.0.1:1080".parse().unwrap());
        assert_eq!(cfg.rules.rules.len(), 2);
        assert_eq!(cfg.rules.rules[0].verdict, Verdict::Block);
        assert_eq!(cfg.rules.rules[1].verdict, Verdict::Pass);
    }

    #[test]
    fn missing_entrypoint_reports_read_error() {
        let dir = tempfile::tempdir().unwrap();
        let err = Config::load(&dir.path().join("missing.conf")).unwrap_err();
        let message = err.to_string();

        assert!(message.contains("failed to read"));
        assert!(!message.contains("failed to resolve"));
    }

    #[test]
    fn included_parse_error_reports_file_and_line() {
        let dir = tempfile::tempdir().unwrap();
        let included = dir.path().join("bad.conf");
        fs::write(
            dir.path().join("alighieri.conf"),
            "internal: 127.0.0.1:1080\ninclude: bad.conf\n",
        )
        .unwrap();
        fs::write(&included, "bogus: true\n").unwrap();

        let err = Config::load(&dir.path().join("alighieri.conf")).unwrap_err();
        let message = err.to_string();

        assert!(message.contains("alighieri.conf:line 2"));
        assert!(message.contains("bad.conf"));
        assert!(message.contains("line 1"));
        assert!(message.contains("unknown keyword 'bogus'"));
    }

    #[test]
    fn include_cycle_is_rejected() {
        let dir = tempfile::tempdir().unwrap();
        let main = dir.path().join("alighieri.conf");
        let included = dir.path().join("loop.conf");
        fs::write(&main, "internal: 127.0.0.1:1080\ninclude: loop.conf\n").unwrap();
        fs::write(&included, "include: alighieri.conf\n").unwrap();

        let err = Config::load(&main).unwrap_err();

        assert!(err.to_string().contains("include cycle detected"));
    }

    #[test]
    fn unmatched_include_glob_is_rejected() {
        let dir = tempfile::tempdir().unwrap();
        let main = dir.path().join("alighieri.conf");
        let conf_dir = dir.path().join("conf.d");
        fs::create_dir(&conf_dir).unwrap();
        fs::write(&main, "internal: 127.0.0.1:1080\ninclude: conf.d/*.conf\n").unwrap();

        let err = Config::load(&main).unwrap_err();

        assert!(err
            .to_string()
            .contains("include 'conf.d/*.conf' failed: matched no files"));
    }

    #[test]
    fn include_glob_ignores_matching_directories() {
        let dir = tempfile::tempdir().unwrap();
        let conf_dir = dir.path().join("conf.d");
        fs::create_dir(&conf_dir).unwrap();
        fs::create_dir(conf_dir.join("10-dir.conf")).unwrap();
        fs::write(
            dir.path().join("alighieri.conf"),
            "internal: 127.0.0.1:1080\ninclude: conf.d/*.conf\n",
        )
        .unwrap();
        fs::write(
            conf_dir.join("20-policy.conf"),
            "socks pass { command: connect }\n",
        )
        .unwrap();

        let cfg = Config::load(&dir.path().join("alighieri.conf")).unwrap();

        assert_eq!(cfg.rules.rules.len(), 1);
    }

    #[cfg(unix)]
    #[test]
    fn include_glob_includes_symlinked_files() {
        let dir = tempfile::tempdir().unwrap();
        let conf_dir = dir.path().join("conf.d");
        fs::create_dir(&conf_dir).unwrap();
        fs::write(
            dir.path().join("alighieri.conf"),
            "internal: 127.0.0.1:1080\ninclude: conf.d/*.conf\n",
        )
        .unwrap();
        fs::write(
            dir.path().join("policy-target.conf"),
            "socks pass { command: connect }\n",
        )
        .unwrap();
        std::os::unix::fs::symlink(
            dir.path().join("policy-target.conf"),
            conf_dir.join("10-policy.conf"),
        )
        .unwrap();

        let cfg = Config::load(&dir.path().join("alighieri.conf")).unwrap();

        assert_eq!(cfg.rules.rules.len(), 1);
    }

    #[test]
    fn parse_rejects_include_without_file_context() {
        let err = Config::parse("internal: 127.0.0.1:1080\ninclude: conf.d/*.conf\n").unwrap_err();

        assert!(err
            .to_string()
            .contains("include is only supported when loading configuration from a file"));
    }

    #[test]
    fn missing_direct_include_reports_resolution_error_with_line() {
        let dir = tempfile::tempdir().unwrap();
        let main = dir.path().join("alighieri.conf");
        fs::write(&main, "internal: 127.0.0.1:1080\ninclude: missing.conf\n").unwrap();

        let err = Config::load(&main).unwrap_err();
        let message = err.to_string();

        assert!(message.contains("alighieri.conf:line 2"));
        assert!(message.contains("include"));
        assert!(message.contains("failed to resolve"));
        assert!(!message.contains("failed to read"));
    }

    #[test]
    fn bad_protocol_rejected() {
        let err = Config::parse("internal: 0.0.0.0 port = 1080\nsocks pass { protocol: sctp }")
            .unwrap_err();
        assert!(err.to_string().contains("unknown protocol"));
    }

    #[test]
    fn empty_selector_directives_rejected() {
        // A present-but-empty `protocol:`/`command:`/`method:` parses to an empty
        // set, which the matcher treats as "any" — silently broadening the rule.
        // The directive must carry at least one value; omit it to mean "any".
        let head = "internal: 0.0.0.0 port = 1080\n";

        let err = Config::parse(&format!("{head}socks pass {{ protocol: }}")).unwrap_err();
        assert!(
            err.to_string().contains("expected at least one protocol"),
            "{err}"
        );

        let err = Config::parse(&format!("{head}socks pass {{ command: }}")).unwrap_err();
        assert!(
            err.to_string().contains("expected at least one command"),
            "{err}"
        );

        let err = Config::parse(&format!("{head}socks pass {{ method: }}")).unwrap_err();
        assert!(
            err.to_string().contains("expected at least one method"),
            "{err}"
        );

        // The sneaky case: an empty `protocol:` whose value is swallowed by the
        // next selector keyword must still be rejected, not read as "any".
        let err = Config::parse(&format!(
            "{head}socks pass {{ protocol: command: connect }}"
        ))
        .unwrap_err();
        assert!(
            err.to_string().contains("expected at least one protocol"),
            "{err}"
        );
    }

    #[test]
    fn omitted_selector_directives_are_wildcard() {
        // Omitting a selector entirely keeps the rule a wildcard for that axis
        // (an empty set the matcher reads as "any"); only the present-but-empty
        // form is an error.
        let cfg = Config::parse("internal: 0.0.0.0 port = 1080\nsocks pass { command: connect }")
            .expect("a rule without protocol:/method: should parse (wildcard)");
        let rule = &cfg.rules.rules[0];
        assert!(
            rule.protocols.is_empty(),
            "omitted protocol: should stay wildcard"
        );
        assert!(
            rule.methods.is_empty(),
            "omitted method: should stay wildcard"
        );
        // The selector that *was* present is restricted — so the empty ones above
        // are genuinely omitted, not a vacuous pass.
        assert_eq!(rule.commands, vec![Command::Connect]);
    }

    #[test]
    fn auth_kind_to_method() {
        assert_eq!(AuthKind::None.to_method(), Method::NoAuth);
        assert_eq!(AuthKind::Username.to_method(), Method::UserPass);
    }
}