runlight 0.1.1

Privacy friendly web analytics that lives inside your Rust app: mount a route, add a script tag, read your stats at /runlight
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
//! The Runlight: sites, the tracker's hits, short links, AI agent fetches,
//! and the scheduled upkeep (runlight.ts).

use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::{Arc, Mutex};

use crate::error::Error;
use crate::geo::{SharedGeo, locate};
use crate::hash::{new_id, random_salt, visitor_hash};
use crate::http::{FetchInit, Headers, Request, Response, SharedFetcher, Url, default_fetcher};
use crate::js::{self, Object, Value};
use crate::limit::{RateLimit, Turns};
use crate::payload::{MAX_BODY, Payload, parse_payload};
use crate::re::{js_re, test};
use crate::safefetch::{Lookup, public_address};
use crate::sources::{Page, attribute, decode_uri_component, parse_page, strip_www};
use crate::store::{EVENT_TAIL_MS, EventRow, SessionRow, SiteOverrides, SiteRow, SqlStore};
use crate::time::{add_days, is_timezone, local_date, start_of};
use crate::ua::{ClientHints, ai_agent, is_bot, parse_client};

/// A path on every link domain that answers when the domain reaches this Runlight.
pub const LINK_DOMAIN_CHECK: &str = "/.well-known/runlight-link-domain";
/// Raised whenever what a rolled-up day holds changes.
const ROLLUP_VERSION: &str = "3";
/// Days of rollups built per site in one scheduled check.
const ROLLUP_BATCH: usize = 10;
/// The most a connected install's list of sites may weigh; a real one is a few kilobytes.
pub(crate) const REMOTE_MAX_BYTES: usize = 2 * 1024 * 1024;
/// On a database that caps statements per request (Cloudflare D1), fewer days a check.
const METERED_ROLLUP_BATCH: usize = 4;
const ROLLUP_DELAY_MS: i64 = 2 * 3_600_000;
/// The choices for how long a site keeps its visits.
pub const RETENTION_MONTHS: [i64; 5] = [6, 12, 24, 36, 60];
/// Thirty minutes without a request ends a session.
pub const SESSION_IDLE_MS: i64 = 30 * 60 * 1000;

/// A site as code gives it.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SiteOptions {
    /// Stable id, stored with every row. Default "default".
    pub id: Option<String>,
    /// Its name.
    pub name: Option<String>,
    /// Hostnames that belong to this site. With one site, empty means any hostname.
    pub hostnames: Option<Vec<String>>,
    /// IANA timezone for reports. Default "UTC".
    pub timezone: Option<String>,
}

/// Where the client's address is read from.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum TrustProxy {
    /// The last X-Forwarded-For entry, then X-Real-IP, then CF-Connecting-IP (the default).
    #[default]
    On,
    /// Only the connection's address.
    Off,
    /// Only this header: `x-forwarded-for`, `x-real-ip`, or `cf-connecting-ip`.
    Header(String),
}

/// A clock in epoch milliseconds.
pub type Clock = Arc<dyn Fn() -> i64 + Send + Sync>;

/// How a Runlight is made.
#[derive(Clone)]
pub struct RunlightOptions {
    /// Where everything is kept.
    pub store: SqlStore,
    /// The site this install counts. Ignored when `sites` is given.
    pub site: Option<SiteOptions>,
    /// Several sites in one install, told apart by hostname.
    pub sites: Option<Vec<SiteOptions>>,
    /// Sites are added, changed, and deleted in the dashboard and kept in the database.
    pub managed_sites: bool,
    /// Looks up a location for an IP when the platform sends no location headers.
    pub geo: Option<SharedGeo>,
    /// Where the client's address is read from. `None` is the default, [`TrustProxy::On`], and warns
    /// once if requests then arrive straight from public addresses with no proxy header.
    pub trust_proxy: Option<TrustProxy>,
    /// Where short links on the app's own domain live. Default "/go".
    pub link_path: Option<String>,
    /// The mail service for email reports, in code, as the SDK's MailSettings object.
    pub mail: Option<Object>,
    /// Encrypts the keys kept in the database. Default RUNLIGHT_SECRET, then RUNLIGHT_TOKEN.
    pub secret: Option<String>,
    /// Tracker requests allowed per visitor address per minute. `None` is the default, 120; zero,
    /// a negative number, or NaN turns the limit off.
    pub rate_limit: Option<f64>,
    /// The clock, for tests.
    pub now: Option<Clock>,
    /// Everything that calls another server goes through this.
    pub fetcher: Option<SharedFetcher>,
    /// Lets a connected install be at http://localhost or http://127.0.0.1, for trying a hub and an app
    /// on one machine. Default false: otherwise anyone who can add a site could have this server ask
    /// services on its own machine, so other installs must be public https addresses.
    pub local_installs: bool,
    /// Stands in for DNS where an install's or a service's address is checked before it is fetched, for
    /// tests.
    pub lookup: Option<Lookup>,
}

impl RunlightOptions {
    /// Options with a store and the defaults.
    pub fn new(store: SqlStore) -> RunlightOptions {
        RunlightOptions {
            store,
            site: None,
            sites: None,
            managed_sites: false,
            geo: None,
            trust_proxy: None,
            link_path: None,
            mail: None,
            secret: None,
            rate_limit: None,
            now: None,
            fetcher: None,
            local_installs: false,
            lookup: None,
        }
    }
}

/// Another Runlight install a site is read from: its address, its token there, and its own id for the site.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Remote {
    /// Its address.
    pub url: String,
    /// The token it gave.
    pub token: String,
    /// Its own id for the site.
    pub site: String,
    /// The site's hostnames there.
    pub hostnames: Vec<String>,
    /// `manage` when the token may change the site's settings there.
    pub scope: Option<String>,
}

impl Remote {
    fn to_value(&self) -> Value {
        let mut o = Object::new();
        o.set("url", self.url.clone());
        o.set("token", self.token.clone());
        o.set("site", self.site.clone());
        o.set("hostnames", Value::Array(self.hostnames.iter().map(|h| Value::from(h.as_str())).collect()));
        if let Some(scope) = &self.scope {
            o.set("scope", scope.clone());
        }
        Value::Object(o)
    }

    fn from_value(v: &Value) -> Remote {
        Remote {
            url: js::str_or_empty(v.get("url")),
            token: js::str_or_empty(v.get("token")),
            site: js::str_or_empty(v.get("site")),
            hostnames: v
                .get("hostnames")
                .and_then(Value::as_array)
                .map(|a| a.iter().map(js::js_string).collect())
                .unwrap_or_default(),
            scope: v.get("scope").and_then(Value::as_str).map(str::to_string),
        }
    }
}

/// What a connected install last said about its site.
#[derive(Clone, Debug, PartialEq)]
pub struct RemoteInfo {
    /// Its last visit.
    pub last_seen: Option<f64>,
    /// How long it keeps visits: `None` while it cannot be reached (undefined), `Some(None)` for forever.
    pub retention_months: Option<Option<f64>>,
    /// `ok`, `refused`, or `unreachable`.
    pub connection: &'static str,
}

/// An environment variable, trimmed, or `None` when it is empty.
pub fn env_value(name: &str) -> Option<String> {
    std::env::var(name).ok().map(|v| js::trim(&v).to_string()).filter(|v| !v.is_empty())
}

/// What passes for an email address: something@somewhere.tld, with no spaces, quotes, or angle brackets.
pub fn is_email(text: &str) -> bool {
    crate::re::uni_re!(r#"^[^@<>"]+@[^@<>"]+\.[^@<>"]+$"#).is_match(text) && !text.chars().any(js::is_space)
}

fn site_row(options: &SiteOptions, index: usize) -> Result<SiteRow, String> {
    let timezone = options.timezone.clone().unwrap_or_else(|| "UTC".into());
    if !is_timezone(&timezone) {
        return Err(format!("Runlight: unknown timezone \"{timezone}\""));
    }
    let id = options.id.clone().unwrap_or_else(|| if index == 0 { "default".into() } else { String::new() });
    if id.is_empty() || !test(js_re!(r"(?i)^[a-z0-9][a-z0-9._-]{0,63}$"), &id) {
        return Err(format!("Runlight: site id \"{id}\" must be letters, digits, dots, dashes, or underscores"));
    }
    let hostnames = options.hostnames.clone().unwrap_or_default();
    Ok(SiteRow {
        id,
        name: options.name.clone().unwrap_or_else(|| hostnames.first().cloned().unwrap_or_else(|| "My site".into())),
        hostnames: hostnames.iter().map(|h| strip_www(h)).collect(),
        timezone,
    })
}

fn utc_day(ts: i64) -> String {
    js::head16(&js::iso_string(ts), 10)
}

/// The sort `a.name.localeCompare(b.name)` gives, near enough: case-insensitive first, then as written.
pub(crate) fn locale_compare(a: &str, b: &str) -> std::cmp::Ordering {
    a.to_lowercase().cmp(&b.to_lowercase()).then_with(|| b.cmp(a))
}

#[derive(Default)]
struct State {
    configured: Vec<SiteRow>,
    overrides: HashMap<String, SiteOverrides>,
    remotes: Vec<(String, Remote)>,
    remote_seen: HashMap<String, (i64, RemoteInfo)>,
    link_domains: Option<(i64, HashSet<String>)>,
    salts: HashMap<String, (String, String, Option<String>)>,
    optimized_at: i64,
}

pub(crate) struct Inner {
    pub(crate) store: SqlStore,
    pub(crate) managed_sites: bool,
    geo: Option<SharedGeo>,
    trust_proxy: TrustProxy,
    /// True until trust_proxy left at its default has been seen answering a public address directly, and warned about once.
    warn_direct: AtomicBool,
    direct_warnings: AtomicU32,
    limit: Option<RateLimit>,
    clock: Clock,
    pub(crate) link_path: String,
    pub(crate) mail_in_code: Option<Object>,
    pub(crate) secret: Option<String>,
    pub(crate) fetcher: SharedFetcher,
    local_installs: bool,
    lookup: Option<Lookup>,
    state: Mutex<State>,
    ready: tokio::sync::Mutex<bool>,
    checking: tokio::sync::Mutex<()>,
    turns: Turns,
    pruning: Mutex<Vec<tokio::task::JoinHandle<()>>>,
    prune_turn: Arc<tokio::sync::Mutex<()>>,
    pub(crate) route_bases: Mutex<Vec<String>>,
}

/// Privacy friendly web analytics that lives inside your app. A cheap handle to clone.
#[derive(Clone)]
pub struct Runlight(pub(crate) Arc<Inner>);

impl Runlight {
    /// A Runlight over a store. Refuses a site whose timezone or id is not one, several sites without
    /// hostnames, and two sites with one id.
    pub fn new(options: RunlightOptions) -> Result<Runlight, String> {
        let configured: Vec<SiteOptions> = if options.managed_sites {
            vec![]
        } else {
            match &options.sites {
                Some(sites) if !sites.is_empty() => sites.clone(),
                _ => vec![options.site.clone().unwrap_or_default()],
            }
        };
        let configured = configured.iter().enumerate().map(|(i, s)| site_row(s, i)).collect::<Result<Vec<_>, _>>()?;
        if configured.len() > 1 && configured.iter().any(|s| s.hostnames.is_empty()) {
            return Err("Runlight: with several sites, give each one its hostnames".into());
        }
        let ids: HashSet<&str> = configured.iter().map(|s| s.id.as_str()).collect();
        if ids.len() != configured.len() {
            return Err("Runlight: two sites share an id".into());
        }
        let per_minute = options.rate_limit.unwrap_or(120.0);
        // Zero, or anything that is not a positive number, means no limit, never a limit of nothing.
        let limit = (per_minute > 0.0).then(|| RateLimit::new(per_minute));
        let clock: Clock = options.now.clone().unwrap_or_else(|| {
            Arc::new(|| {
                std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_millis() as i64)
            })
        });
        let link_path = format!("/{}", options.link_path.as_deref().unwrap_or("/go").trim_matches('/'));
        let secret =
            options.secret.clone().or_else(|| env_value("RUNLIGHT_SECRET")).or_else(|| env_value("RUNLIGHT_TOKEN"));
        Ok(Runlight(Arc::new(Inner {
            store: options.store,
            managed_sites: options.managed_sites,
            geo: options.geo,
            warn_direct: AtomicBool::new(options.trust_proxy.is_none()),
            direct_warnings: AtomicU32::new(0),
            trust_proxy: options.trust_proxy.unwrap_or_default(),
            limit,
            clock,
            link_path,
            mail_in_code: options.mail,
            secret,
            fetcher: options.fetcher.unwrap_or_else(default_fetcher),
            local_installs: options.local_installs,
            lookup: options.lookup,
            state: Mutex::new(State { configured, ..State::default() }),
            ready: tokio::sync::Mutex::new(false),
            checking: tokio::sync::Mutex::new(()),
            turns: Turns::default(),
            pruning: Mutex::new(Vec::new()),
            prune_turn: Arc::new(tokio::sync::Mutex::new(())),
            route_bases: Mutex::new(Vec::new()),
        })))
    }

    fn state(&self) -> std::sync::MutexGuard<'_, State> {
        self.0.state.lock().unwrap_or_else(|e| e.into_inner())
    }

    /// The store.
    pub fn store(&self) -> &SqlStore {
        &self.0.store
    }

    /// The clock's time, in epoch milliseconds.
    pub fn now(&self) -> i64 {
        (self.0.clock)()
    }

    /// The fetcher everything that calls out goes through.
    pub fn fetcher(&self) -> &SharedFetcher {
        &self.0.fetcher
    }

    /// Whether a connected install may be on this machine, at http://localhost or http://127.0.0.1.
    pub fn local_installs(&self) -> bool {
        self.0.local_installs
    }

    /// What stands in for DNS, when the options gave one.
    pub(crate) fn lookup(&self) -> Option<&Lookup> {
        self.0.lookup.as_ref()
    }

    /// A request to another install, through the fetcher, only to a public address (or this machine,
    /// with `local_installs`), and with no redirect followed.
    pub(crate) async fn fetch_install(&self, url: &str, init: FetchInit) -> Result<Response, crate::http::FetchError> {
        crate::safefetch::install_fetch(&*self.0.fetcher, url, init, self.0.local_installs, self.lookup()).await
    }

    /// Whether sites are managed in the dashboard.
    pub fn managed_sites(&self) -> bool {
        self.0.managed_sites
    }

    /// The secret the keys in the database are sealed with, if any.
    pub fn secret(&self) -> Option<&str> {
        self.0.secret.as_deref()
    }

    /// Where links on the app's own domain are served, such as "/go".
    pub fn link_path(&self) -> &str {
        &self.0.link_path
    }

    /// Creates tables and records the configured sites. Runs once.
    pub async fn init(&self) -> Result<(), Error> {
        let mut ready = self.0.ready.lock().await;
        if *ready {
            return Ok(());
        }
        let store = &self.0.store;
        store.migrate().await?;
        // A database that never had its statistics gathered gets them now, before any report is read.
        store.optimize(true).await;
        if self.0.managed_sites {
            let sites = store.sites().await?;
            self.state().configured = sites;
            self.load_remotes().await?;
        }
        let configured = self.state().configured.clone();
        for site in &configured {
            store.upsert_site(site, self.now()).await?;
        }
        let overrides = store.site_overrides().await?;
        self.state().overrides = overrides;
        // A process starting with a timezone set in code is the newest word on it: if the code changed it,
        // the days built in the old one are cleared here, once, and never by a process still running.
        for site in self.sites() {
            if self.is_remote(&site.id) {
                continue;
            }
            let stored = store.setting(&format!("rollup-zone:{}", site.id)).await?;
            let zone = stored.and_then(|s| js::parse(&s).ok()).map(|v| js::str_or_empty(v.get("zone")));
            match zone {
                None => {
                    let value = obj_zone(&site.timezone, 0);
                    store.set_setting(&format!("rollup-zone:{}", site.id), Some(&value)).await?;
                }
                Some(zone) if zone != site.timezone => {
                    self.zone_changed(&site.id, &site.timezone).await?;
                }
                _ => {}
            }
        }
        *ready = true;
        Ok(())
    }

    /// The sites, with any settings changed in the dashboard applied.
    pub fn sites(&self) -> Vec<SiteRow> {
        let state = self.state();
        state
            .configured
            .iter()
            .map(|site| {
                let mut site = site.clone();
                if let Some(o) = state.overrides.get(&site.id) {
                    if let Some(Value::String(name)) = o.get("name") {
                        site.name = name.clone();
                    }
                    if let Some(Value::String(tz)) = o.get("timezone") {
                        site.timezone = tz.clone();
                    }
                }
                site
            })
            .collect()
    }

    /// The configured sites, without dashboard changes.
    pub(crate) fn configured(&self) -> Vec<SiteRow> {
        self.state().configured.clone()
    }

    /// Checks a list of hostnames for a managed site: at least one, each a domain, none taken.
    fn hostnames_for(&self, input: Option<&Value>, except: Option<&str>) -> Result<Vec<String>, Error> {
        let list: Vec<String> = match input {
            Some(Value::Array(a)) => a.iter().map(js::js_string).collect(),
            other => {
                let text = js::str_or_empty(other);
                crate::re::uni_re!(r"[\t\n\x{0B}\f\r \x{A0}\x{1680}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}\x{FEFF},]+")
                    .split(&text)
                    .map(str::to_string)
                    .collect()
            }
        };
        let mut hostnames: Vec<String> = Vec::new();
        for h in list {
            let h = js::trim(&h).to_string();
            let h = crate::re::replace_first(js_re!(r"^https?://"), &h, "");
            let h = crate::re::replace_first(js_re!(r"[/:][\s\S]*$"), &h, "");
            let h = strip_www(&h);
            if !h.is_empty() && !hostnames.contains(&h) {
                hostnames.push(h);
            }
        }
        if hostnames.is_empty() {
            return Err(Error::settings("Add the site's domain, like example.com", "site_domain_needed", &[]));
        }
        let configured = self.configured();
        for host in &hostnames {
            if !is_domain(host) && host != "localhost" {
                return Err(Error::settings(
                    format!("\"{host}\" is not a domain name"),
                    "site_domain_invalid",
                    &[("host", host)],
                ));
            }
            if let Some(owner) = configured.iter().find(|s| Some(s.id.as_str()) != except && s.hostnames.contains(host))
            {
                return Err(Error::settings(
                    format!("{host} already belongs to {}", owner.name),
                    "site_domain_taken",
                    &[("host", host), ("site", &owner.name)],
                ));
            }
        }
        Ok(hostnames)
    }

    async fn load_remotes(&self) -> Result<(), Error> {
        let mut remotes = Vec::new();
        for (key, value) in self.0.store.settings_starting_with("remote:").await? {
            if let Some(opened) = crate::mail::secret::unseal(&value, self.0.secret.as_deref())
                && let Ok(v) = js::parse(&opened)
            {
                remotes.push((key["remote:".len()..].to_string(), Remote::from_value(&v)));
            }
        }
        self.state().remotes = remotes;
        Ok(())
    }

    /// Whether a site is counted by another install.
    pub fn is_remote(&self, id: &str) -> bool {
        self.state().remotes.iter().any(|(k, _)| k == id)
    }

    /// The install a site is read from, when it is counted elsewhere.
    pub fn remote(&self, id: &str) -> Option<Remote> {
        self.state().remotes.iter().find(|(k, _)| k == id).map(|(_, r)| r.clone())
    }

    /// When a connected install's site last had a visit, asked at most once a minute.
    pub async fn remote_last_seen(&self, id: &str) -> Option<f64> {
        self.remote_info(id).await.and_then(|i| i.last_seen)
    }

    /// What a connected install says about its site, asked at most once a minute.
    pub async fn remote_info(&self, id: &str) -> Option<RemoteInfo> {
        let remote = self.remote(id)?;
        let cached = self.state().remote_seen.get(id).cloned();
        if let Some((at, info)) = &cached
            && self.now() - at < 60_000
        {
            return Some(info.clone());
        }
        let mut info = RemoteInfo {
            last_seen: cached.as_ref().and_then(|c| c.1.last_seen),
            retention_months: None,
            connection: "unreachable",
        };
        let init = FetchInit::default()
            .header("authorization", format!("Bearer {}", remote.token))
            .timeout(8000)
            .max_bytes(REMOTE_MAX_BYTES);
        if let Ok(answer) = self.fetch_install(&format!("{}/api/sites", remote.url), init).await {
            if answer.status == 401 || answer.status == 403 {
                info.connection = "refused";
            }
            if let Ok(body) = answer.json_body()
                && let Some(there) = body.get("sites").and_then(Value::as_array).and_then(|a| {
                    a.iter().find(|s| {
                        js::str_or_empty(s.get("id")) == remote.site && s.get("id").is_some_and(Value::is_string)
                    })
                })
            {
                info = RemoteInfo {
                    last_seen: there.get("lastSeen").and_then(Value::as_f64),
                    retention_months: Some(there.get("retentionMonths").and_then(Value::as_f64)),
                    connection: "ok",
                };
            }
        }
        self.state().remote_seen.insert(id.to_string(), (self.now(), info.clone()));
        Some(info)
    }

    /// Forgets what a connected install said, after a change made through it.
    pub fn forget_remote_info(&self, id: &str) {
        self.state().remote_seen.remove(id);
    }

    /// Asks a connected install to delete the token this server holds for it. A failure leaves it listed there.
    async fn revoke_remote_token(&self, remote: &Remote) {
        let init =
            FetchInit::method("DELETE").header("authorization", format!("Bearer {}", remote.token)).timeout(5000);
        let _ = self.fetch_install(&format!("{}/api/token", remote.url), init).await;
    }

    async fn seal(&self, value: &str) -> String {
        crate::mail::secret::seal(value, self.0.secret.as_deref())
    }

    /// Connects a site counted by another Runlight (an app's own install) so this server shows it too.
    async fn add_remote_site(&self, input: &Value, name: Option<&Value>) -> Result<SiteRow, Error> {
        let url = js::trim(&js::str_or_empty(input.get("url"))).trim_end_matches('/').to_string();
        if !crate::safefetch::install_address(&url, self.0.local_installs) {
            return Err(Error::settings(
                "Enter the install's address, like https://example.com/runlight",
                "connect_url",
                &[],
            ));
        }
        let token = js::trim(&js::str_or_empty(input.get("token"))).to_string();
        if token.is_empty() {
            return Err(Error::settings("Enter an API token from that install", "install_token", &[]));
        }
        let auth = format!("Bearer {token}");
        let answer = match self
            .fetch_install(
                &format!("{url}/api/sites"),
                FetchInit::default().header("authorization", &auth).timeout(10_000).max_bytes(REMOTE_MAX_BYTES),
            )
            .await
        {
            Ok(a) => a,
            Err(crate::http::FetchError::TooLong(_)) => Response::status(200),
            Err(_) => {
                let host = Url::parse(&url).map(|u| u.host()).unwrap_or_default();
                return Err(Error::settings(format!("Could not reach {url}"), "unreachable", &[("host", &host)]));
            }
        };
        if answer.status == 401 || answer.status == 403 {
            return Err(Error::settings("That install refused the token", "install_refused", &[]));
        }
        let body = answer.json_body().ok();
        let sites: Vec<Value> =
            body.as_ref().and_then(|b| b.get("sites")).and_then(Value::as_array).cloned().unwrap_or_default();
        if !answer.ok() || sites.is_empty() {
            return Err(Error::settings(
                format!("{url} did not answer like a Runlight install"),
                "connect_not_runlight",
                &[("url", &url)],
            ));
        }
        // What the token may do there; an install from before manage tokens has no /api/token and reads only.
        let mut scope = "read".to_string();
        let mut token_site = String::new();
        if let Ok(about) = self
            .fetch_install(
                &format!("{url}/api/token"),
                FetchInit::default().header("authorization", &auth).timeout(10_000).max_bytes(REMOTE_MAX_BYTES),
            )
            .await
            && about.ok()
            && let Ok(info) = about.json_body()
        {
            if info.get("scope").and_then(Value::as_str) == Some("manage") {
                scope = "manage".into();
            }
            token_site = js::str_or_empty(info.get("site"));
        }
        let wanted = if token_site.is_empty() { input.get("site").cloned() } else { Some(Value::String(token_site)) };
        let there = sites.iter().find(|s| match (&wanted, s.get("id")) {
            (Some(w), Some(id)) => w == id,
            (None, None) => true,
            _ => false,
        });
        let there = there.unwrap_or(&sites[0]).clone();
        // An install's answer is read as given: a site without a list of hostnames has none.
        let hostnames: Vec<String> = there
            .get("hostnames")
            .and_then(Value::as_array)
            .map(|a| a.iter().filter_map(|h| h.as_str().map(str::to_string)).collect())
            .unwrap_or_default();
        let there_id = js::str_or_empty(there.get("id"));
        // Connecting the same site again (to allow changes, or with a new token) updates it in place.
        let known: Vec<(String, Remote)> = self.state().remotes.clone();
        for (existing, k) in known {
            if k.url == url && k.site == there_id {
                let updated = Remote {
                    token: token.clone(),
                    scope: Some(scope.clone()),
                    hostnames: hostnames.clone(),
                    ..k.clone()
                };
                if k.token != token {
                    self.revoke_remote_token(&k).await;
                }
                let sealed = self.seal(&updated.to_value().to_json()).await;
                self.0.store.set_setting(&format!("remote:{existing}"), Some(&sealed)).await?;
                {
                    let mut state = self.state();
                    if let Some(slot) = state.remotes.iter_mut().find(|(id, _)| *id == existing) {
                        slot.1 = updated;
                    }
                    state.remote_seen.remove(&existing);
                }
                return self.site(Some(&existing)).ok_or_else(|| Error::Range("Unknown site".into()));
            }
        }
        let host_source =
            hostnames.first().cloned().unwrap_or_else(|| Url::parse(&url).map(|u| u.host()).unwrap_or_default());
        let host = crate::re::replace_all(js_re!(r"(?i)[^a-z0-9._-]"), &host_source, "-").to_lowercase();
        let stem = js::head16(&host, 56);
        let mut id = stem.clone();
        let mut n = 2;
        while self.configured().iter().any(|s| s.id == id) {
            id = format!("{stem}-{n}");
            n += 1;
        }
        let given = js::head16(js::trim(&js::str_or_empty(name)), 80);
        let site_name = if given.is_empty() { js::str_or_empty(there.get("name")) } else { given };
        let tz = js::str_or_empty(there.get("timezone"));
        // No hostnames: tracker hits never land on a site that is counted elsewhere.
        let site = SiteRow {
            id: id.clone(),
            name: site_name,
            hostnames: vec![],
            timezone: if is_timezone(&tz) { tz } else { "UTC".into() },
        };
        let remote = Remote { url, token, site: there_id, hostnames, scope: Some(scope) };
        self.0.store.upsert_site(&site, self.now()).await?;
        let sealed = self.seal(&remote.to_value().to_json()).await;
        self.0.store.set_setting(&format!("remote:{id}"), Some(&sealed)).await?;
        let mut state = self.state();
        state.remotes.push((id, remote));
        state.configured.push(site.clone());
        state.configured.sort_by(|a, b| locale_compare(&a.name, &b.name));
        Ok(site)
    }

    /// Adds a site, when sites are managed in the dashboard: one counted here, or one connected from another install.
    pub async fn add_site(&self, input: &Value) -> Result<SiteRow, Error> {
        self.init().await?;
        if !self.0.managed_sites {
            return Err(Error::settings("Sites are set in code", "sites_in_code", &[]));
        }
        if let Some(remote) = input.get("remote")
            && js::truthy(remote)
            && (remote.is_object() || remote.is_array())
        {
            return self.add_remote_site(remote, input.get("name")).await;
        }
        let hostnames = self.hostnames_for(input.get("hostnames"), None)?;
        let given = js::trim(&js::str_or_empty(input.get("name"))).to_string();
        let name = if given.is_empty() { hostnames[0].clone() } else { given };
        if js::len16(&name) > 80 {
            return Err(Error::settings("A site name is 1 to 80 characters", "site_name", &[]));
        }
        let timezone = input.get("timezone").filter(|v| !v.is_null()).map_or_else(|| "UTC".to_string(), js::js_string);
        if !is_timezone(&timezone) {
            return Err(Error::settings(
                format!("Unknown timezone \"{timezone}\""),
                "unknown_timezone",
                &[("timezone", &timezone)],
            ));
        }
        let stem = js::head16(&crate::re::replace_all(js_re!(r"[^a-z0-9._-]"), &hostnames[0], "-"), 56);
        let mut id = stem.clone();
        let mut n = 2;
        while self.configured().iter().any(|s| s.id == id) {
            id = format!("{stem}-{n}");
            n += 1;
        }
        let site = SiteRow { id, name, hostnames, timezone };
        self.0.store.upsert_site(&site, self.now()).await?;
        let mut state = self.state();
        state.configured.push(site.clone());
        state.configured.sort_by(|a, b| locale_compare(&a.name, &b.name));
        Ok(site)
    }

    /// Deletes a site and everything recorded for it, when sites are managed in the dashboard.
    pub async fn delete_site(&self, id: &str) -> Result<(), Error> {
        self.init().await?;
        if !self.0.managed_sites {
            return Err(Error::settings("Sites are set in code", "sites_in_code", &[]));
        }
        if !self.configured().iter().any(|s| s.id == id) {
            return Err(Error::settings("Unknown site", "unknown_site", &[]));
        }
        let store = &self.0.store;
        store.delete_site(id).await?;
        for key in ["retention", "observe-key", "rollup-zone", "orphans-swept"] {
            store.set_setting(&format!("{key}:{id}"), None).await?;
        }
        // A site made again with the same id starts its Umami import from the beginning.
        for (key, _) in store.settings_starting_with(&format!("import:umami-visits:{id}:")).await? {
            store.set_setting(&key, None).await?;
        }
        // A connected install keeps its own data; only the connection goes, and its token there with it.
        if let Some(remote) = self.remote(id) {
            self.revoke_remote_token(&remote).await;
            self.state().remotes.retain(|(k, _)| k != id);
            store.set_setting(&format!("remote:{id}"), None).await?;
        }
        let mut state = self.state();
        state.configured.retain(|s| s.id != id);
        state.overrides.remove(id);
        Ok(())
    }

    /// Changes a site's name, timezone, or (when managed) hostnames from the dashboard.
    pub async fn update_site(&self, id: &str, patch: &Value) -> Result<SiteRow, Error> {
        self.init().await?;
        let Some(current) = self.configured().into_iter().find(|s| s.id == id) else {
            return Err(Error::settings("Unknown site", "unknown_site", &[]));
        };
        let name_of = |v: &Value| -> Result<String, Error> {
            let name = js::trim(&js::js_string(v)).to_string();
            if name.is_empty() || js::len16(&name) > 80 {
                return Err(Error::settings("A site name is 1 to 80 characters", "site_name", &[]));
            }
            Ok(name)
        };
        let zone_of = |v: &Value| -> Result<String, Error> {
            let tz = js::js_string(v);
            if !is_timezone(&tz) {
                return Err(Error::settings(
                    format!("Unknown timezone \"{tz}\""),
                    "unknown_timezone",
                    &[("timezone", &tz)],
                ));
            }
            Ok(tz)
        };
        if self.0.managed_sites {
            let mut next = current.clone();
            if let Some(name) = patch.get("name") {
                next.name = name_of(name)?;
            }
            if let Some(tz) = patch.get("timezone") {
                next.timezone = zone_of(tz)?;
                if Some(next.timezone.clone()) != self.site(Some(id)).map(|s| s.timezone) {
                    self.zone_changed(id, &next.timezone).await?;
                }
            }
            if let Some(hostnames) = patch.get("hostnames")
                && !self.is_remote(id)
            {
                next.hostnames = self.hostnames_for(Some(hostnames), Some(id))?;
            }
            self.0.store.upsert_site(&next, self.now()).await?;
            let mut state = self.state();
            if let Some(slot) = state.configured.iter_mut().find(|s| s.id == id) {
                *slot = next;
            }
            drop(state);
            return self.site(Some(id)).ok_or_else(|| Error::Range("Unknown site".into()));
        }
        let mut next = self.state().overrides.get(id).cloned().unwrap_or_default();
        if let Some(name) = patch.get("name") {
            next.set("name", name_of(name)?);
        }
        if let Some(tz) = patch.get("timezone") {
            let tz = zone_of(tz)?;
            next.set("timezone", tz.clone());
            if Some(tz.clone()) != self.site(Some(id)).map(|s| s.timezone) {
                self.zone_changed(id, &tz).await?;
            }
        }
        self.0.store.set_site_overrides(id, &next).await?;
        self.state().overrides.insert(id.to_string(), next);
        self.site(Some(id)).ok_or_else(|| Error::Range("Unknown site".into()))
    }

    /// How many months of visits a site keeps, or `None` to keep everything (the default).
    pub async fn retention(&self, site: &str) -> Result<Option<i64>, Error> {
        let value = self.0.store.setting(&format!("retention:{site}")).await?;
        let n = value.map_or(0.0, |v| js::text_number(&v));
        Ok(RETENTION_MONTHS.iter().find(|m| **m as f64 == n).copied())
    }

    /// Sets how many months of visits a site keeps; the deleting runs after the answer.
    pub async fn set_retention(&self, site: &str, months: Option<i64>) -> Result<(), Error> {
        if self.site(Some(site)).is_none() || self.is_remote(site) {
            return Err(Error::settings("Unknown site", "unknown_site", &[]));
        }
        if let Some(m) = months
            && !RETENTION_MONTHS.contains(&m)
        {
            return Err(Error::settings(
                "Keep visits for 6, 12, 24, 36, 60 months, or forever",
                "retention_bad",
                &[("months", "6, 12, 24, 36, 60")],
            ));
        }
        self.0.store.set_setting(&format!("retention:{site}"), months.map(|m| m.to_string()).as_deref()).await?;
        // Deleting a long history takes a while, so it runs in pieces after the answer, with tracking going on between them.
        self.prune_later(Some(site.to_string()));
        Ok(())
    }

    fn prune_later(&self, only: Option<String>) {
        let rl = self.clone();
        let turn = self.0.prune_turn.clone();
        let handle = tokio::spawn(async move {
            let _turn = turn.lock().await;
            if let Err(error) = rl.apply_retention(only.as_deref()).await {
                eprintln!("Runlight: could not apply retention {error}");
            }
        });
        self.0.pruning.lock().unwrap_or_else(|e| e.into_inner()).push(handle);
    }

    /// Runs work after the answer, such as an email that must not slow a sign-in down. `idle()` waits for it.
    pub fn later<F>(&self, work: F)
    where
        F: std::future::Future<Output = ()> + Send + 'static,
    {
        let handle = tokio::spawn(work);
        self.0.pruning.lock().unwrap_or_else(|e| e.into_inner()).push(handle);
    }

    /// Waits for retention work still running; the scheduled check and tests wait for it.
    pub async fn idle(&self) {
        loop {
            let handles: Vec<_> = std::mem::take(&mut *self.0.pruning.lock().unwrap_or_else(|e| e.into_inner()));
            if handles.is_empty() {
                return;
            }
            for h in handles {
                let _ = h.await;
            }
        }
    }

    /// Days are the site's local days, so a new timezone clears the built ones.
    async fn zone_changed(&self, id: &str, timezone: &str) -> Result<i64, Error> {
        let since = self.now();
        self.0.store.clear_rollups(id, None, None).await?;
        self.0.store.set_setting(&format!("rollup-zone:{id}"), Some(&obj_zone(timezone, since))).await?;
        Ok(since)
    }

    /// Since when a site's days may be built: 0 for always, or when its timezone last changed. `None`
    /// when this process holds a different timezone than the one on record.
    async fn rollup_since(&self, site: &SiteRow) -> Result<Option<i64>, Error> {
        let key = format!("rollup-zone:{}", site.id);
        let Some(stored) = self.0.store.setting(&key).await? else {
            self.0.store.set_setting(&key, Some(&obj_zone(&site.timezone, 0))).await?;
            return Ok(Some(0));
        };
        let zone = js::parse(&stored).unwrap_or(Value::Null);
        Ok((js::str_or_empty(zone.get("zone")) == site.timezone).then(|| js::to_i64(js::opt_number(zone.get("since")))))
    }

    /// Adds up each site's finished days, so long ranges read a row a day instead of every visit.
    pub async fn build_rollups(&self) -> Result<usize, Error> {
        let store = &self.0.store;
        // Days rolled up by an earlier way of counting are cleared once, and built again below.
        if store.setting("rollup-version").await?.as_deref() != Some(ROLLUP_VERSION) {
            for site in self.sites() {
                store.clear_rollups(&site.id, None, None).await?;
            }
            store.set_setting("rollup-version", Some(ROLLUP_VERSION)).await?;
        }
        let mut built = 0;
        let now = self.now();
        for site in self.sites() {
            if self.is_remote(&site.id) {
                continue;
            }
            let Some(first) = store.first_seen(&site.id).await? else { continue };
            let cutoff = self.retention_cutoff(&site.id).await?.unwrap_or(0);
            let Some(since) = self.rollup_since(&site).await? else { continue };
            let done = store.rollup_days(&site.id).await?;
            let today = local_date(now, &site.timezone);
            let batch = if store.db().metered() { METERED_ROLLUP_BATCH } else { ROLLUP_BATCH };
            let oldest = local_date(first.max(cutoff), &site.timezone);
            let mut made = 0;
            // Newest first, so recent ranges speed up before a long history is done.
            let mut day = add_days(&today, -1);
            while day >= oldest && made < batch {
                if !done.contains(&day) {
                    let start = start_of(&day, &site.timezone, 0);
                    let end = start_of(&add_days(&day, 1), &site.timezone, 0);
                    if start < since {
                        break;
                    }
                    if !(now < end + ROLLUP_DELAY_MS || start < cutoff) {
                        match store.build_rollup_day(&site.id, &day, start, end).await {
                            Ok(()) => made += 1,
                            // Another process building the same day at once loses nothing: the day is there either way.
                            Err(error) => {
                                if !store.rollup_days(&site.id).await?.contains(&day) {
                                    eprintln!("Runlight: could not add up {day} for {} {error}", site.id);
                                }
                            }
                        }
                        tokio::task::yield_now().await;
                    }
                }
                day = add_days(&day, -1);
            }
            built += made;
        }
        Ok(built)
    }

    /// The oldest moment a site keeps visits from, or `None` when it keeps everything.
    pub async fn retention_cutoff(&self, site: &str) -> Result<Option<i64>, Error> {
        let Some(months) = self.retention(site).await? else { return Ok(None) };
        Ok(Some(minus_months(self.now(), months)))
    }

    /// Deletes visits older than each site's retention allows. Cheap when there is nothing to delete.
    async fn apply_retention(&self, only: Option<&str>) -> Result<(), Error> {
        let store = &self.0.store;
        for site in self.sites() {
            if only.is_some_and(|o| o != site.id) || self.is_remote(&site.id) {
                continue;
            }
            let Some(cutoff) = self.retention_cutoff(&site.id).await? else { continue };
            store.drop_before(&site.id, cutoff).await?;
            // Earlier versions let an event join its visit days late, so retention could leave such an event
            // behind once its visit was gone. They are swept once.
            let key = format!("orphans-swept:{}", site.id);
            if store.setting(&key).await?.is_none_or(|v| v.is_empty()) {
                store.drop_orphans(&site.id, cutoff, self.now()).await?;
                store.set_setting(&key, Some("1")).await?;
            }
        }
        Ok(())
    }

    /// A site by id, or the first site when no id is given.
    pub fn site(&self, id: Option<&str>) -> Option<SiteRow> {
        let sites = self.sites();
        match id.filter(|i| !i.is_empty()) {
            None => sites.into_iter().next(),
            Some(id) => sites.into_iter().find(|s| s.id == id),
        }
    }

    /// The site a page belongs to, or `None` if it belongs to none.
    pub fn site_for(&self, hostname: &str, id: Option<&str>) -> Option<SiteRow> {
        let host = strip_www(hostname);
        let id = id.filter(|i| !i.is_empty());
        // A site counted by another install never takes hits here.
        let remotes: Vec<String> = self.state().remotes.iter().map(|(k, _)| k.clone()).collect();
        let sites = self.sites();
        if !remotes.is_empty() {
            let local: Vec<SiteRow> = sites.into_iter().filter(|s| !remotes.contains(&s.id)).collect();
            if let Some(id) = id
                && remotes.iter().any(|r| r == id)
            {
                return None;
            }
            return site_among(&local, &host, id);
        }
        site_among(&sites, &host, id)
    }

    /// A test from a developer's own machine while a site is being set up.
    async fn setup_site(&self, hostname: &str, id: Option<&str>) -> Result<Option<SiteRow>, Error> {
        let lower = hostname.to_lowercase();
        let host = lower.trim_start_matches('[').trim_end_matches(']');
        let host = {
            // `replace(/^\[|\]$/g, "")`: one bracket at each end at most.
            let mut h = lower.as_str();
            if let Some(rest) = h.strip_prefix('[') {
                h = rest;
            }
            if let Some(rest) = h.strip_suffix(']') {
                h = rest;
            }
            let _ = host;
            h.to_string()
        };
        if !(host == "localhost"
            || host == "127.0.0.1"
            || host == "::1"
            || test(js_re!(r"\.(localhost|local|test)$"), &host))
        {
            return Ok(None);
        }
        let site = match id.filter(|i| !i.is_empty()) {
            Some(id) => self.site(Some(id)),
            None => {
                let sites = self.sites();
                if sites.len() == 1 { sites.into_iter().next() } else { None }
            }
        };
        let Some(site) = site else { return Ok(None) };
        if self.is_remote(&site.id) {
            return Ok(None);
        }
        Ok(self.0.store.last_seen(&site.id).await?.is_none().then_some(site))
    }

    /// The visitor's address, for the daily visitor hash and the rate limit.
    pub fn client_ip(&self, request: &Request) -> String {
        let h = &request.headers;
        let last = |name: &str| -> Option<String> {
            let value = h.get(name)?;
            value.split(',').map(|x| js::trim(x).to_string()).rfind(|x| !x.is_empty())
        };
        let forwarded = match &self.0.trust_proxy {
            TrustProxy::Off => None,
            TrustProxy::On => {
                last("x-forwarded-for").or_else(|| h.get("x-real-ip")).or_else(|| h.get("cf-connecting-ip"))
            }
            TrustProxy::Header(name) if name == "x-forwarded-for" => last("x-forwarded-for"),
            TrustProxy::Header(name) => h.get(name),
        };
        if let Some(f) = forwarded
            && !js::trim(&f).is_empty()
        {
            return js::trim(&f).to_string();
        }
        // A public address with no forwarding header means nothing sits in front, and then any client
        // could name its own address in one. Said once, only when trust_proxy was left at its default.
        if self.0.warn_direct.load(Ordering::Relaxed)
            && public_address(&request.remote_address)
            && self.0.warn_direct.swap(false, Ordering::Relaxed)
        {
            self.0.direct_warnings.fetch_add(1, Ordering::Relaxed);
            eprintln!(
                "Runlight: a request came straight from a public address with no proxy in front, but trustProxy is on by default, so a client could send X-Forwarded-For and choose its own address, getting round the rate limits. Set trust_proxy: Some(TrustProxy::Off) when nothing sits in front of this server, or put a proxy in front that sets the header."
            );
        }
        request.remote_address.clone()
    }

    /// How many times the warning about a default trust_proxy with nothing in front was given, for tests.
    #[doc(hidden)]
    pub fn direct_warnings(&self) -> u32 {
        self.0.direct_warnings.load(Ordering::Relaxed)
    }

    /// Today's salt in a site's timezone and, if it still exists, yesterday's.
    async fn current_salts(&self, now: i64, timezone: &str) -> Result<(String, Option<String>), Error> {
        let day = local_date(now, timezone);
        if let Some((d, today, yesterday)) = self.state().salts.get(timezone).cloned()
            && d == day
        {
            return Ok((today, yesterday));
        }
        let today = self.0.store.salt(&day, &random_salt()).await?;
        let yesterday = self.0.store.salt_if_exists(&add_days(&day, -1)).await?;
        self.drop_old_salts(now).await?;
        self.state().salts.insert(timezone.to_string(), (day, today.clone(), yesterday.clone()));
        Ok((today, yesterday))
    }

    /// Deletes salts whose day has ended everywhere.
    async fn drop_old_salts(&self, now: i64) -> Result<(), Error> {
        self.0.store.drop_salts_before(&utc_day(now - 2 * 86_400_000)).await?;
        Ok(())
    }

    /// The host a proxy says the request was for, read only when proxy headers are trusted.
    pub(crate) fn forwarded_host(&self, request: &Request) -> Option<String> {
        if self.0.trust_proxy == TrustProxy::Off { None } else { request.headers.get("x-forwarded-host") }
    }

    /// Handles one tracker request. Always succeeds unless the database fails; bad input is dropped quietly.
    pub async fn collect(&self, request: &Request) -> Result<(), Error> {
        let length = request.headers.get("content-length").map_or(0.0, |l| js::text_number(&l));
        if length > MAX_BODY as f64 {
            return Ok(());
        }
        // Read no more than a tracker hit can be, whatever the length header says.
        if request.body.len() > MAX_BODY {
            return Ok(());
        }
        let text = request.text();
        let Some(payload) = parse_payload(&text) else { return Ok(()) };
        let ua = request.headers.get("user-agent").unwrap_or_default();
        if ai_agent(&ua).is_some() || is_bot(&ua) {
            return Ok(());
        }
        if let Some(limit) = &self.0.limit
            && !limit.allow(&self.client_ip(request), self.now())
        {
            return Ok(());
        }
        // A database too busy to take the hit right now gets it a little later, at the time it arrived.
        let now = self.now();
        let mut attempt = 1;
        loop {
            match self.record(&payload, request, now).await {
                Err(error) if attempt < 3 && busy(&error) => {
                    tokio::time::sleep(std::time::Duration::from_millis(500 * attempt)).await;
                    attempt += 1;
                }
                other => return other,
            }
        }
    }

    async fn record(&self, payload: &Payload, request: &Request, now: i64) -> Result<(), Error> {
        // Managed sites load from the database in init(), so it must come first.
        self.init().await?;
        let hostname = payload.url.hostname();
        let site = match self.site_for(&hostname, Some(&payload.site)) {
            Some(s) => s,
            None => match self.setup_site(&hostname, Some(&payload.site)).await? {
                Some(s) => s,
                None => return Ok(()),
            },
        };
        if payload.kind == "engagement" {
            return self.engagement(&site, payload, now).await;
        }
        let store = &self.0.store;
        let page = parse_page(&payload.url);
        let mut session: Option<(String, String)> = None;
        let mut reopen = true;
        if payload.kind == "event"
            && !payload.pageview_id.is_empty()
            && let Some(pageview) = store.pageview(&site.id, &payload.pageview_id).await?
            && now - pageview.started_at < EVENT_TAIL_MS
        {
            // An event joins its page's visit unless that visit began longer ago than reports look for its rows.
            session = Some((pageview.session.clone(), pageview.visitor.clone()));
            // A visit idle past the 30 minutes stays ended: the event counts in it without reopening it.
            reopen = now - pageview.last_at <= SESSION_IDLE_MS;
            if now - pageview.started_at > 3_600_000 {
                store.touched_old_visit(&site.id, pageview.started_at, now - ROLLUP_DELAY_MS + 3_600_000).await?;
            }
        }
        let (id, visitor) = match session {
            Some(s) => s,
            None => {
                let screen = match (payload.screen_width, payload.screen_height) {
                    (Some(w), Some(h)) if w != 0 && h != 0 => format!("{w}x{h}"),
                    _ => String::new(),
                };
                self.session_for(
                    &site,
                    request,
                    &page,
                    &payload.referrer,
                    now,
                    payload.screen_width.map(|w| w as f64),
                    &screen,
                    &payload.language,
                )
                .await?
            }
        };
        store.touch_session(&id, now, payload.kind, &page.path, reopen).await?;
        store
            .insert_event(&EventRow {
                site: site.id.clone(),
                ts: now,
                kind: payload.kind.into(),
                visitor,
                session: id,
                pageview: payload.pageview_id.clone(),
                path: page.path.clone(),
                hostname: page.hostname.clone(),
                title: if payload.kind == "pageview" { payload.title.clone() } else { String::new() },
                name: if payload.kind == "event" { payload.name.clone() } else { String::new() },
                props: payload.props.clone(),
                engaged_ms: 0,
                scroll: None,
                link: String::new(),
            })
            .await?;
        Ok(())
    }

    /// The visitor's open session on a site, or a new one attributed to this request. Shared by
    /// tracker hits and short link clicks. Gives its id and visitor.
    #[allow(clippy::too_many_arguments)]
    async fn session_for(
        &self,
        site: &SiteRow,
        request: &Request,
        page: &Page,
        referrer: &str,
        now: i64,
        screen_width: Option<f64>,
        screen: &str,
        language: &str,
    ) -> Result<(String, String), Error> {
        let ua = request.headers.get("user-agent").unwrap_or_default();
        let ip = self.client_ip(request);
        let (today_salt, yesterday_salt) = self.current_salts(now, &site.timezone).await?;
        let today = visitor_hash(&today_salt, &site.id, &ip, &ua);
        let mut candidates = vec![today.clone()];
        if let Some(y) = yesterday_salt {
            candidates.push(visitor_hash(&y, &site.id, &ip, &ua));
        }
        // One visitor's requests often arrive together (a pageview and the event right after it). Taking
        // turns per visitor means only the first opens a session and the rest find it.
        let _turn = self.0.turns.take(&format!("{}:{today}", site.id)).await;
        async {
            let store = &self.0.store;
            if let Some(open) = store.open_session(&site.id, &candidates, now - SESSION_IDLE_MS).await? {
                return Ok(open);
            }
            let id = new_id();
            let attribution = attribute(page, referrer, &site.hostnames);
            let parsed = parse_client(
                &ua,
                &ClientHints {
                    brands: request.headers.get("sec-ch-ua"),
                    mobile: request.headers.get("sec-ch-ua-mobile"),
                    platform: request.headers.get("sec-ch-ua-platform"),
                },
                screen_width,
            );
            let location = locate(&request.headers, &ip, self.0.geo.as_ref()).await;
            store
                .insert_session(&SessionRow {
                    id: id.clone(),
                    site: site.id.clone(),
                    visitor: today.clone(),
                    started_at: now,
                    hostname: page.hostname.clone(),
                    referrer_host: attribution.referrer_host,
                    referrer_path: attribution.referrer_path,
                    source: attribution.source,
                    channel: attribution.channel.into(),
                    utm_source: page.utm.source.clone(),
                    utm_medium: page.utm.medium.clone(),
                    utm_campaign: page.utm.campaign.clone(),
                    utm_term: page.utm.term.clone(),
                    utm_content: page.utm.content.clone(),
                    country: location.country,
                    region: location.region,
                    city: location.city,
                    browser: parsed.browser,
                    browser_version: parsed.browser_version,
                    os: parsed.os,
                    os_version: parsed.os_version,
                    device: parsed.device.into(),
                    screen: screen.into(),
                    language: language.into(),
                })
                .await?;
            Ok::<(String, String), Error>((id, today.clone()))
        }
        .await
    }

    /// The link domains, read at most every 30 seconds.
    async fn link_domain_set(&self) -> Result<HashSet<String>, Error> {
        let now = self.now();
        if let Some((at, domains)) = &self.state().link_domains
            && now - at < 30_000
        {
            return Ok(domains.clone());
        }
        self.init().await?;
        let domains: HashSet<String> = self.0.store.link_domains().await?.into_iter().map(|(d, _)| d).collect();
        self.state().link_domains = Some((now, domains.clone()));
        Ok(domains)
    }

    /// Clears the cached link domains after one is added or removed.
    pub fn forget_link_domains(&self) {
        self.state().link_domains = None;
    }

    /// Handles `{link_path}/{slug}` on the app's own domain.
    pub async fn link_response(&self, request: &Request) -> Result<Response, Error> {
        let path = request.parsed_url().pathname();
        let prefix = format!("{}/", self.0.link_path);
        let slug = match path.strip_prefix(&prefix) {
            Some(rest) => decode_uri_component(rest).ok_or_else(|| Error::Other("URIError: URI malformed".into()))?,
            None => String::new(),
        };
        let found =
            if !slug.is_empty() && !slug.contains('/') { self.redirect(request, &slug, "").await? } else { None };
        Ok(found.unwrap_or_else(not_found))
    }

    /// For middleware: when a request arrives on a link domain added in Settings (such as
    /// t.example.com), answers `/{slug}` there with the redirect, and anything else with a 404. `None`
    /// for every other host, so the app carries on as normal, and for the dashboard's own paths.
    pub async fn link_domain_response(&self, request: &Request) -> Result<Option<Response>, Error> {
        let url = request.parsed_url();
        // A forwarded host only counts behind a proxy that sets it; otherwise any client could pick one.
        let given = self.forwarded_host(request).or_else(|| request.headers.get("host")).unwrap_or_else(|| url.host());
        let first = js::trim(given.split(',').next().unwrap_or("")).to_string();
        let host = strip_www(first.split(':').next().unwrap_or(""));
        if !self.link_domain_set().await?.contains(&host) {
            return Ok(None);
        }
        let pathname = url.pathname();
        // Lets the dashboard confirm that requests to this domain reach Runlight.
        if pathname == LINK_DOMAIN_CHECK {
            return Ok(Some(Response::new(
                crate::obj! { "runlight" => true, "domain" => host }.to_json(),
                200,
                Headers::new().with("content-type", "application/json").with("cache-control", "no-store"),
            )));
        }
        let bases = {
            let b = self.0.route_bases.lock().unwrap_or_else(|e| e.into_inner()).clone();
            if b.is_empty() { vec!["/runlight".to_string()] } else { b }
        };
        for base in bases {
            if base != "/" && (pathname == base || pathname.starts_with(&format!("{base}/"))) {
                return Ok(None);
            }
        }
        let slug = decode_uri_component(&pathname[1.min(pathname.len())..])
            .ok_or_else(|| Error::Other("URIError: URI malformed".into()))?;
        let found =
            if !slug.is_empty() && !slug.contains('/') { self.redirect(request, &slug, &host).await? } else { None };
        Ok(Some(found.unwrap_or_else(not_found)))
    }

    /// Answers a request for a short link: a redirect to its destination, with the click recorded like
    /// a visit but kept out of visitor and pageview counts. `None` when no link fits.
    pub async fn redirect(&self, request: &Request, slug: &str, domain: &str) -> Result<Option<Response>, Error> {
        self.init().await?;
        let url = request.parsed_url();
        let given = self.forwarded_host(request).or_else(|| request.headers.get("host")).unwrap_or_else(|| url.host());
        let host = strip_www(given.split(':').next().unwrap_or(""));
        let Some(link) = self.0.store.link_by_slug(slug).await? else { return Ok(None) };
        // The app's own link path answers for every link; a link domain answers only for its own links.
        if !domain.is_empty() && link.domain != domain {
            return Ok(None);
        }
        let site = self.site(Some(&link.site)).or_else(|| self.sites().into_iter().next());
        let ua = request.headers.get("user-agent").unwrap_or_default();
        if let Some(site) = site
            && ai_agent(&ua).is_none()
            && !is_bot(&ua)
            && request.method == "GET"
        {
            let recorded = async {
                let now = self.now();
                let accept = request.headers.get("accept-language").unwrap_or_default();
                let first = accept.split(',').next().unwrap_or("").split(';').next().unwrap_or("");
                let language = js::head16(js::trim(first), 35);
                let referer = request.headers.get("referer").unwrap_or_default();
                let (id, visitor) =
                    self.session_for(&site, request, &parse_page(&url), &referer, now, None, "", &language).await?;
                let pathname = url.pathname();
                self.0.store.touch_session(&id, now, "click", &pathname, true).await?;
                self.0
                    .store
                    .insert_event(&EventRow {
                        site: site.id.clone(),
                        ts: now,
                        kind: "click".into(),
                        visitor,
                        session: id,
                        pageview: String::new(),
                        path: js::head16(&pathname, 1000),
                        hostname: host.clone(),
                        title: String::new(),
                        name: link.slug.clone(),
                        props: None,
                        engaged_ms: 0,
                        scroll: None,
                        link: link.id.clone(),
                    })
                    .await?;
                Ok::<(), Error>(())
            }
            .await;
            // A failed count must never break the redirect.
            if let Err(error) = recorded {
                eprintln!("Runlight: could not record a link click {error}");
            }
        }
        Ok(Some(Response::new(
            Vec::new(),
            302,
            Headers::new()
                .with("location", &link.url)
                .with("cache-control", "no-store")
                .with("referrer-policy", "no-referrer-when-downgrade"),
        )))
    }

    async fn engagement(&self, site: &SiteRow, payload: &Payload, now: i64) -> Result<(), Error> {
        if payload.engaged_ms <= 0 {
            return Ok(());
        }
        let store = &self.0.store;
        let Some(pageview) = store.pageview(&site.id, &payload.pageview_id).await? else { return Ok(()) };
        // Reports look for a visit's rows only so long after it began, so later time on it is let go.
        if now - pageview.started_at >= EVENT_TAIL_MS {
            return Ok(());
        }
        store.add_engagement(&pageview.session, payload.engaged_ms).await?;
        // Only a visit that began more than an hour ago can belong to a day that is already added up.
        if now - pageview.started_at > 3_600_000 {
            store.touched_old_visit(&site.id, pageview.started_at, now - ROLLUP_DELAY_MS + 3_600_000).await?;
        }
        store
            .insert_event(&EventRow {
                site: site.id.clone(),
                ts: now,
                kind: "engagement".into(),
                visitor: pageview.visitor,
                session: pageview.session,
                pageview: payload.pageview_id.clone(),
                path: pageview.path,
                hostname: pageview.hostname,
                title: String::new(),
                name: String::new(),
                props: None,
                engaged_ms: payload.engaged_ms,
                scroll: payload.scroll,
                link: String::new(),
            })
            .await?;
        Ok(())
    }

    /// Records a request from a known AI agent. Call it from middleware for every page request; it
    /// ignores everything else and never fails. `at` is when a log reader saw the page served.
    pub async fn observe(&self, request: &Request, at: Option<f64>) -> bool {
        match self.observe_inner(request, at).await {
            Ok(done) => done,
            Err(error) => {
                // Analytics must never break the page it watches, but a failure should still be seen.
                eprintln!("Runlight: could not record an AI agent fetch {error}");
                false
            }
        }
    }

    async fn observe_inner(&self, request: &Request, at: Option<f64>) -> Result<bool, Error> {
        if request.method != "GET" {
            return Ok(false);
        }
        let Some(agent) = ai_agent(&request.headers.get("user-agent").unwrap_or_default()) else { return Ok(false) };
        let Some(url) = Url::parse(&request.url) else { return Err(Error::Other("TypeError: Invalid URL".into())) };
        // Pages, not their assets.
        let pathname = url.pathname();
        if let Some(ext) = crate::re::group(js_re!(r"(?i)\.([a-z0-9]+)$"), &pathname, 1)
            && !["html", "htm", "md", "txt", "php"].contains(&ext.to_lowercase().as_str())
        {
            return Ok(false);
        }
        let host =
            self.forwarded_host(request).or_else(|| request.headers.get("host")).unwrap_or_else(|| url.hostname());
        self.init().await?;
        let Some(site) = self.site_for(host.split(':').next().unwrap_or(&host), None) else { return Ok(false) };
        // A log reader sends when the page was served. Older than a week is dropped; ahead of now counts as now.
        let now = self.now();
        if let Some(at) = at
            && at.is_finite()
            && at < (now - 7 * 86_400_000) as f64
        {
            return Ok(false);
        }
        let ts = match at {
            Some(at) if at.is_finite() && at <= now as f64 => at.floor() as i64,
            _ => now,
        };
        let mut props = Object::new();
        props.set("company", agent.company);
        props.set("kind", agent.kind);
        self.0
            .store
            .insert_event(&EventRow {
                site: site.id,
                ts,
                kind: "fetch".into(),
                visitor: String::new(),
                session: String::new(),
                pageview: String::new(),
                path: js::head16(&pathname, 1000),
                hostname: strip_www(&url.hostname()),
                title: String::new(),
                name: agent.name.into(),
                props: Some(props),
                engaged_ms: 0,
                scroll: None,
                link: String::new(),
            })
            .await?;
        Ok(true)
    }

    /// Scheduled upkeep, safe to run every minute: salts, retention, rollups, and the email reports
    /// that are due. Also rereads sites and their dashboard settings, so a change made by another
    /// process sharing the database shows up here too.
    pub async fn check(&self) -> Result<Value, Error> {
        // A check still running when the next is due is waited for, never run twice at once.
        let _turn = self.0.checking.lock().await;
        self.init().await?;
        let store = &self.0.store;
        if self.0.managed_sites {
            let sites = store.sites().await?;
            self.state().configured = sites;
            self.load_remotes().await?;
        }
        // A name or timezone changed in the dashboard by another process reaches this one too.
        let overrides = store.site_overrides().await?;
        {
            let mut state = self.state();
            state.overrides = overrides;
            state.salts.clear();
        }
        let mut zones: Vec<String> = Vec::new();
        for site in self.sites() {
            if !zones.contains(&site.timezone) {
                zones.push(site.timezone);
            }
        }
        for zone in zones {
            self.current_salts(self.now(), &zone).await?;
        }
        self.drop_old_salts(self.now()).await?;
        self.idle().await;
        {
            let _turn = self.0.prune_turn.lock().await;
            if let Err(error) = self.apply_retention(None).await {
                eprintln!("Runlight: could not apply retention {error}");
            }
        }
        let due = {
            let mut state = self.state();
            let due = self.now() - state.optimized_at >= 86_400_000;
            if due {
                state.optimized_at = self.now();
            }
            due
        };
        if due {
            store.optimize(false).await;
        }
        self.build_rollups().await?;
        let reports = self.send_reports().await?;
        Ok(crate::obj! { "ok" => true, "reports" => crate::obj! { "sent" => reports.0, "failed" => reports.1 } })
    }
}

fn obj_zone(zone: &str, since: i64) -> String {
    crate::obj! { "zone" => zone, "since" => since }.to_json()
}

fn site_among(sites: &[SiteRow], host: &str, id: Option<&str>) -> Option<SiteRow> {
    if let Some(id) = id {
        let site = sites.iter().find(|s| s.id == id)?;
        return (site.hostnames.is_empty() || site.hostnames.iter().any(|h| h == host)).then(|| site.clone());
    }
    if sites.len() == 1 {
        let only = &sites[0];
        return (only.hostnames.is_empty() || only.hostnames.iter().any(|h| h == host)).then(|| only.clone());
    }
    sites.iter().find(|s| s.hostnames.iter().any(|h| h == host)).cloned()
}

/// A domain name, such as go.example.com.
pub fn is_domain(host: &str) -> bool {
    let len = host.chars().count();
    (1..=253).contains(&len) && test(js_re!(r"^([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$"), host)
}

/// `new Date(now)` moved `months` back with `setUTCMonth`, which lets the day overflow into the next month.
fn minus_months(now: i64, months: i64) -> i64 {
    let days = now.div_euclid(86_400_000);
    let rest = now.rem_euclid(86_400_000);
    let (y, m, d) = js::civil_from_days(days);
    js::date_utc(y, m - 1 - months, d, 0, 0, 0, 0) + rest
}

fn not_found() -> Response {
    Response::new("Not found", 404, Headers::new().with("content-type", "text/plain; charset=utf-8"))
}

/// True for a database that could not take a statement just now and may a moment later.
fn busy(error: &Error) -> bool {
    test(
        js_re!(
            r"(?i)timeout exceeded when trying to connect|connection timeout|no MySQL connection was free|SQLITE_BUSY|database is locked"
        ),
        error.message(),
    )
}