runlight 0.4.3 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +1 -1
- data/assets/build.json +4 -4
- data/assets/dashboard.js +12 -12
- data/assets/locales.json +5 -5
- data/assets/picker.js +1 -1
- data/assets/tracker.js +1 -1
- data/lib/runlight/accounts/accounts.rb +40 -10
- data/lib/runlight/accounts/crypto.rb +6 -3
- data/lib/runlight/accounts/lockout.rb +98 -0
- data/lib/runlight/accounts/throttle.rb +70 -16
- data/lib/runlight/accounts/web.rb +71 -28
- data/lib/runlight/body.rb +13 -0
- data/lib/runlight/core.rb +194 -28
- data/lib/runlight/data/sources.rb +1 -2
- data/lib/runlight/db/connect.rb +2 -2
- data/lib/runlight/db/db.rb +52 -32
- data/lib/runlight/geo.rb +2 -2
- data/lib/runlight/http/response.rb +22 -3
- data/lib/runlight/icon.rb +2 -1
- data/lib/runlight/importers/csv_visits.rb +13 -3
- data/lib/runlight/importers/dub.rb +2 -1
- data/lib/runlight/importers/index.rb +2 -1
- data/lib/runlight/importers/umami.rb +3 -0
- data/lib/runlight/importers/visits.rb +16 -5
- data/lib/runlight/importers/write.rb +50 -12
- data/lib/runlight/links.rb +10 -1
- data/lib/runlight/mcp.rb +10 -1
- data/lib/runlight/oauth.rb +34 -23
- data/lib/runlight/payload.rb +26 -7
- data/lib/runlight/rack_app.rb +7 -1
- data/lib/runlight/routes.rb +51 -21
- data/lib/runlight/server/cli.rb +9 -0
- data/lib/runlight/server/db_ip.rb +287 -45
- data/lib/runlight/server/standalone.rb +41 -5
- data/lib/runlight/sources.rb +3 -2
- data/lib/runlight/store/sql.rb +14 -0
- data/lib/runlight/store/sql_store.rb +88 -59
- data/lib/runlight/ua.rb +14 -4
- data/lib/runlight/version.rb +1 -1
- data/lib/runlight/weak_keys.rb +31 -0
- data/lib/runlight.rb +2 -0
- metadata +6 -4
data/lib/runlight/core.rb
CHANGED
|
@@ -63,6 +63,14 @@ module Runlight
|
|
|
63
63
|
|
|
64
64
|
# What passes for an email address: something@somewhere.tld, with no spaces, quotes, or angle brackets.
|
|
65
65
|
EMAIL = /\A[^#{Js::SPACE}@<>"]+@[^#{Js::SPACE}@<>"]+\.[^#{Js::SPACE}@<>"]+\z/
|
|
66
|
+
# The longest an address is read: no real one is longer, and the pattern is slow on a very long string.
|
|
67
|
+
EMAIL_MAX = 320
|
|
68
|
+
|
|
69
|
+
# Whether text passes for an email address, of EMAIL_MAX characters at most.
|
|
70
|
+
def self.email?(value)
|
|
71
|
+
text = Js.scrub(value)
|
|
72
|
+
Js.length(text) <= EMAIL_MAX && text.match?(EMAIL)
|
|
73
|
+
end
|
|
66
74
|
|
|
67
75
|
# Raised whenever what a rolled-up day holds changes. 2: the heatmap counts visits only. 3: a page counts the views that can report time.
|
|
68
76
|
ROLLUP_VERSION = 3
|
|
@@ -77,7 +85,18 @@ module Runlight
|
|
|
77
85
|
TRACKER_WAIT_MS = 10_000
|
|
78
86
|
# How often the scheduled check downloads the agents' address lists, when updateAgentRanges is on.
|
|
79
87
|
AGENT_RANGES_EVERY_MS = 86_400_000
|
|
80
|
-
|
|
88
|
+
# The setting that changes whenever a process changes the sites: one added, deleted, renamed, moved to another
|
|
89
|
+
# timezone or other domains, or connected. Every process keeps the sites in memory, and rereads them when the
|
|
90
|
+
# setting no longer holds what it last saw, looking at most this often.
|
|
91
|
+
SITES_VERSION = "sites-version"
|
|
92
|
+
SITES_FRESH_MS = 5_000
|
|
93
|
+
# How long the link domains are kept, and how long a failure to read them is, before the database is asked again.
|
|
94
|
+
LINK_DOMAINS_FRESH_MS = 30_000
|
|
95
|
+
LINK_DOMAINS_RETRY_MS = 5_000
|
|
96
|
+
# How often a process clears sign-in counts that have run out, on its own, without the scheduled check.
|
|
97
|
+
SWEEP_EVERY_MS = 3_600_000
|
|
98
|
+
private_constant :AGENT_RANGES_EVERY_MS, :ROLLUP_VERSION, :ROLLUP_BATCH, :REMOTE_MAX_BYTES, :METERED_ROLLUP_BATCH, :ROLLUP_DELAY_MS, :TRACKER_WAIT_MS,
|
|
99
|
+
:SITES_VERSION, :SITES_FRESH_MS, :LINK_DOMAINS_FRESH_MS, :LINK_DOMAINS_RETRY_MS, :SWEEP_EVERY_MS
|
|
81
100
|
|
|
82
101
|
BUSY = /timeout exceeded when trying to connect|connection timeout|no MySQL connection was free|SQLITE_BUSY|database is locked/i
|
|
83
102
|
private_constant :BUSY
|
|
@@ -140,13 +159,15 @@ module Runlight
|
|
|
140
159
|
@limit = if number.is_a?(Numeric) && number.positive?
|
|
141
160
|
RateLimit.new(number.infinite? ? (2**63) - 1 : number.floor, -> { now })
|
|
142
161
|
end
|
|
162
|
+
# AI agent fetches allowed per address per minute, ten times the tracker's limit, since a crawler reads fast.
|
|
163
|
+
@fetch_limit = @limit.nil? ? nil : RateLimit.new(number.infinite? ? (2**63) - 1 : (number * 10).floor, -> { now })
|
|
143
164
|
@clock = options["now"] || -> { Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond) }
|
|
144
165
|
@fetcher = options["fetcher"] || Http::NetFetcher.new
|
|
145
166
|
@links = Links.new(self)
|
|
146
167
|
@link_path = "/#{(options["linkPath"].nil? ? "/go" : options["linkPath"]).to_s.gsub(%r{\A/+|/+\z}, "")}"
|
|
147
168
|
mail = options["mail"]
|
|
148
169
|
if mail.is_a?(Hash) && mail["send"].respond_to?(:call)
|
|
149
|
-
unless Js.string(mail["from"].nil? ? "" : mail["from"])
|
|
170
|
+
unless Core.email?(Js.string(mail["from"].nil? ? "" : mail["from"]))
|
|
150
171
|
raise ArgumentError, "Runlight: the app's mailer needs a from address, like reports@example.com"
|
|
151
172
|
end
|
|
152
173
|
|
|
@@ -183,6 +204,12 @@ module Runlight
|
|
|
183
204
|
@remotes = {}
|
|
184
205
|
@remote_seen = {}
|
|
185
206
|
@overrides = {}
|
|
207
|
+
# What SITES_VERSION held when the sites were last read, when it was last looked at, and one look at a time.
|
|
208
|
+
@sites_version = ""
|
|
209
|
+
@sites_checked_at = 0
|
|
210
|
+
@sites_lock = Mutex.new
|
|
211
|
+
# When this process last cleared the sign-in counts that have run out.
|
|
212
|
+
@swept_at = nil
|
|
186
213
|
@ready = false
|
|
187
214
|
# init runs once however many threads ask at once; reentrant, as init may ask for itself.
|
|
188
215
|
@init_lock = Monitor.new
|
|
@@ -284,7 +311,7 @@ module Runlight
|
|
|
284
311
|
end
|
|
285
312
|
end
|
|
286
313
|
from = Js.trim(Js.string(input["from"].nil? ? "" : input["from"]))
|
|
287
|
-
unless
|
|
314
|
+
unless Core.email?(from)
|
|
288
315
|
raise Mail::MailError.new("Enter the address reports come from, like reports@example.com", "mail_from", {})
|
|
289
316
|
end
|
|
290
317
|
|
|
@@ -400,12 +427,18 @@ module Runlight
|
|
|
400
427
|
# A database that never had its statistics gathered gets them now, before any report is read, rather
|
|
401
428
|
# than at the first scheduled check, which an app may never run.
|
|
402
429
|
@store.optimize(true)
|
|
430
|
+
# Read before the sites, so a change made while they are read is seen as one at the next look.
|
|
431
|
+
@sites_version = @store.setting(SITES_VERSION) || ""
|
|
432
|
+
@sites_checked_at = now
|
|
403
433
|
if @managed_sites
|
|
404
434
|
@configured = @store.sites
|
|
405
435
|
load_remotes
|
|
406
436
|
end
|
|
407
437
|
@configured.each { |site| @store.upsert_site(site, now) }
|
|
408
438
|
@overrides = @store.site_overrides
|
|
439
|
+
# Earlier versions kept counts under a hash of each caller's address in the settings: sign-in tries, and
|
|
440
|
+
# app registrations. Nothing made from an address is kept there now, and what was is cleared.
|
|
441
|
+
Accounts::Throttle.forget_addresses(@store)
|
|
409
442
|
# A process starting with a timezone set in code is the newest word on it: if the code changed it,
|
|
410
443
|
# the days built in the old one are cleared here, once, and never by a process still running.
|
|
411
444
|
sites.each do |site|
|
|
@@ -436,12 +469,14 @@ module Runlight
|
|
|
436
469
|
|
|
437
470
|
# The sites, with any settings changed in the dashboard applied.
|
|
438
471
|
def sites
|
|
472
|
+
refresh_sites
|
|
439
473
|
@configured.map { |site| site.merge(@overrides[site["id"]] || {}) }
|
|
440
474
|
end
|
|
441
475
|
|
|
442
476
|
# The install a site is read from, when it is counted elsewhere:
|
|
443
477
|
# { "url", "token", "site", "hostnames", "scope" (optional) }, or nil.
|
|
444
478
|
def remote(id)
|
|
479
|
+
refresh_sites
|
|
445
480
|
@remotes[id]
|
|
446
481
|
end
|
|
447
482
|
|
|
@@ -527,6 +562,7 @@ module Runlight
|
|
|
527
562
|
site = { "id" => id, "name" => name, "hostnames" => hostnames, "timezone" => timezone }
|
|
528
563
|
@store.upsert_site(site, now)
|
|
529
564
|
@configured = by_name(@configured + [site])
|
|
565
|
+
sites_changed
|
|
530
566
|
site
|
|
531
567
|
end
|
|
532
568
|
|
|
@@ -552,6 +588,7 @@ module Runlight
|
|
|
552
588
|
end
|
|
553
589
|
@configured = @configured.reject { |site| site["id"] == id }
|
|
554
590
|
@overrides.delete(id)
|
|
591
|
+
sites_changed
|
|
555
592
|
nil
|
|
556
593
|
end
|
|
557
594
|
|
|
@@ -595,6 +632,7 @@ module Runlight
|
|
|
595
632
|
@store.set_site_overrides(id, nxt)
|
|
596
633
|
@overrides[id] = nxt
|
|
597
634
|
end
|
|
635
|
+
sites_changed
|
|
598
636
|
set_retention(id, patch["retentionMonths"]) if given?(patch, "retentionMonths")
|
|
599
637
|
site(id)
|
|
600
638
|
end
|
|
@@ -639,6 +677,7 @@ module Runlight
|
|
|
639
677
|
warn("Runlight: could not apply retention #{e.message}")
|
|
640
678
|
end
|
|
641
679
|
end
|
|
680
|
+
sweep_counts
|
|
642
681
|
end
|
|
643
682
|
|
|
644
683
|
# Adds up each site's finished days, so long ranges read a row a day instead
|
|
@@ -809,11 +848,20 @@ module Runlight
|
|
|
809
848
|
else
|
|
810
849
|
h.get(@trust_proxy.to_s)
|
|
811
850
|
end
|
|
812
|
-
return Js.trim(forwarded) if !forwarded.nil? && Js.trim(forwarded) != ""
|
|
851
|
+
return Core.bare_address(Js.trim(forwarded)) if !forwarded.nil? && Js.trim(forwarded) != ""
|
|
813
852
|
end
|
|
814
853
|
connection_ip(request, context).to_s
|
|
815
854
|
end
|
|
816
855
|
|
|
856
|
+
# A forwarded address without its port: "203.0.113.7:51234" (as Azure App Service sends) and
|
|
857
|
+
# "[2001:db8::1]:443" are the address alone.
|
|
858
|
+
def self.bare_address(value)
|
|
859
|
+
bracketed = value.match(/\A\[([^\]]*)\](?::\d+)?\z/)
|
|
860
|
+
return bracketed[1] unless bracketed.nil?
|
|
861
|
+
|
|
862
|
+
value.match?(/\A\d{1,3}(?:\.\d{1,3}){3}:\d+\z/) ? value[0, value.rindex(":")] : value
|
|
863
|
+
end
|
|
864
|
+
|
|
817
865
|
# Whether a request's forwarding headers count. Left at its default, trustProxy believes them only on a
|
|
818
866
|
# connection from this computer or a private network, or one whose address the app did not pass on, since a
|
|
819
867
|
# client reaching the server straight from the internet could write them itself.
|
|
@@ -833,8 +881,18 @@ module Runlight
|
|
|
833
881
|
end
|
|
834
882
|
private :trusts?
|
|
835
883
|
|
|
836
|
-
# Handles one tracker request.
|
|
884
|
+
# Handles one tracker request. Never raises: bad input is dropped quietly, and a hit the store could not
|
|
885
|
+
# take is logged.
|
|
837
886
|
def collect(request, context = {})
|
|
887
|
+
take(request, context)
|
|
888
|
+
nil
|
|
889
|
+
rescue StandardError => e
|
|
890
|
+
# Analytics must never break the app it counts for.
|
|
891
|
+
warn("Runlight: could not record an event #{e.full_message(highlight: false)}")
|
|
892
|
+
nil
|
|
893
|
+
end
|
|
894
|
+
|
|
895
|
+
private def take(request, context)
|
|
838
896
|
length = Js.number(request.headers.get("content-length") || 0)
|
|
839
897
|
return if length > Payload::MAX_BODY
|
|
840
898
|
|
|
@@ -933,7 +991,8 @@ module Runlight
|
|
|
933
991
|
|
|
934
992
|
site = site(link["site"]) || sites[0]
|
|
935
993
|
ua = request.headers.get("user-agent") || ""
|
|
936
|
-
|
|
994
|
+
# Past the tracker's limit for one address a click still redirects, and is not counted.
|
|
995
|
+
if !site.nil? && Ua.ai_agent(ua).nil? && !Ua.bot?(ua) && request.method == "GET" && (@limit.nil? || @limit.allow(client_ip(request, context)))
|
|
937
996
|
begin
|
|
938
997
|
at = now
|
|
939
998
|
first = (request.headers.get("accept-language") || "").split(",", -1).first.to_s.split(";", -1).first.to_s
|
|
@@ -975,10 +1034,13 @@ module Runlight
|
|
|
975
1034
|
# for tracker hits, or context "agentIp" when a log reader or plugin
|
|
976
1035
|
# reports it) is checked against the list the agent's company publishes
|
|
977
1036
|
# and then dropped: only "verified", "mismatch", or "" is kept with the fetch.
|
|
1037
|
+
# Context "userAgent" stands in for the request's header when given.
|
|
978
1038
|
def observe(request, at = nil, context = {})
|
|
979
1039
|
return false if request.method != "GET"
|
|
980
1040
|
|
|
981
|
-
|
|
1041
|
+
# A log reader gives the user agent as text, since a logged one may not be fit for a header.
|
|
1042
|
+
given = context.key?("userAgent") ? context["userAgent"] : context[:user_agent]
|
|
1043
|
+
agent = Ua.ai_agent(given.nil? ? request.headers.get("user-agent") || "" : given.to_s)
|
|
982
1044
|
return false if agent.nil?
|
|
983
1045
|
|
|
984
1046
|
url = Http::Url.new(request.url)
|
|
@@ -986,10 +1048,13 @@ module Runlight
|
|
|
986
1048
|
extension = url.pathname.match(/\.([a-z0-9]+)\z/i)
|
|
987
1049
|
return false if extension && !%w[html htm md txt php].include?(extension[1].downcase)
|
|
988
1050
|
|
|
989
|
-
host = forwarded_host(request) || request.headers.get("host") || url.hostname
|
|
1051
|
+
host = Js.trim((forwarded_host(request, context) || request.headers.get("host") || url.hostname).split(",", -1).first.to_s).split(":", -1).first.to_s
|
|
990
1052
|
init
|
|
991
|
-
site = site_for(host
|
|
1053
|
+
site = site_for(host)
|
|
992
1054
|
return false if site.nil?
|
|
1055
|
+
# A fetch seen as it happens is limited per address, as tracker hits are; a log reader's report is not.
|
|
1056
|
+
reported = context.key?("agentIp") || context.key?(:agent_ip)
|
|
1057
|
+
return false if !reported && !@fetch_limit.nil? && !@fetch_limit.allow(client_ip(request, context))
|
|
993
1058
|
|
|
994
1059
|
# A log reader sends when the page was served. Older than a week is dropped, so a first run over
|
|
995
1060
|
# an old log does not land as one spike on today; a time ahead of now counts as now.
|
|
@@ -1006,7 +1071,7 @@ module Runlight
|
|
|
1006
1071
|
"session" => "",
|
|
1007
1072
|
"pageview" => "",
|
|
1008
1073
|
"path" => Js.slice(url.pathname, 0, 1000),
|
|
1009
|
-
"hostname" => Sources.strip_www(
|
|
1074
|
+
"hostname" => Sources.strip_www(host),
|
|
1010
1075
|
"name" => agent["name"],
|
|
1011
1076
|
"props" => { "company" => agent["company"], "kind" => agent["kind"] },
|
|
1012
1077
|
# A fetch keeps the result of the address check in title: "verified", "mismatch", or "" when there was
|
|
@@ -1139,14 +1204,69 @@ module Runlight
|
|
|
1139
1204
|
hostnames
|
|
1140
1205
|
end
|
|
1141
1206
|
|
|
1207
|
+
# Read aside and put in place whole, so a request on another thread never finds half of them.
|
|
1142
1208
|
def load_remotes
|
|
1143
|
-
|
|
1209
|
+
remotes = {}
|
|
1144
1210
|
@store.settings_starting_with("remote:").each do |row|
|
|
1145
1211
|
opened = Mail::Secret.unseal(row["value"], @secret)
|
|
1146
|
-
|
|
1212
|
+
remotes[row["key"].delete_prefix("remote:")] = Json.decode(opened) unless opened.nil? || opened == ""
|
|
1213
|
+
end
|
|
1214
|
+
@remotes = remotes
|
|
1215
|
+
end
|
|
1216
|
+
|
|
1217
|
+
# Says the sites changed, so every other process sharing the database reads them again within
|
|
1218
|
+
# SITES_FRESH_MS. This one already holds the change, and reads them again at its next look too, which
|
|
1219
|
+
# also brings in a change another process made at the same moment.
|
|
1220
|
+
def sites_changed
|
|
1221
|
+
@store.set_setting(SITES_VERSION, Hashing.random_id(8))
|
|
1222
|
+
end
|
|
1223
|
+
|
|
1224
|
+
# Reads the sites, their dashboard settings, and connected installs again when another process changed
|
|
1225
|
+
# them. One read of a setting at most every SITES_FRESH_MS, and nothing more while it holds what this
|
|
1226
|
+
# process last saw. A database that cannot be asked leaves what is known in place until the next look.
|
|
1227
|
+
def refresh_sites
|
|
1228
|
+
return unless @ready
|
|
1229
|
+
|
|
1230
|
+
at = now
|
|
1231
|
+
return if at >= @sites_checked_at && at - @sites_checked_at < SITES_FRESH_MS
|
|
1232
|
+
# One thread looks; the others, and anything the look itself asks of the sites, go on with what is known.
|
|
1233
|
+
return unless @sites_lock.try_lock
|
|
1234
|
+
|
|
1235
|
+
begin
|
|
1236
|
+
@sites_checked_at = at
|
|
1237
|
+
version = @store.setting(SITES_VERSION) || ""
|
|
1238
|
+
return if version == @sites_version
|
|
1239
|
+
|
|
1240
|
+
if @managed_sites
|
|
1241
|
+
# In the order a site added here takes its place, so the list reads the same in every process.
|
|
1242
|
+
@configured = by_name(@store.sites)
|
|
1243
|
+
load_remotes
|
|
1244
|
+
end
|
|
1245
|
+
@overrides = @store.site_overrides
|
|
1246
|
+
# The version read before the sites: a change made while they were read shows at the next look.
|
|
1247
|
+
@sites_version = version
|
|
1248
|
+
rescue StandardError => e
|
|
1249
|
+
warn("Runlight: could not look for changed sites #{e.message}")
|
|
1250
|
+
ensure
|
|
1251
|
+
@sites_lock.unlock
|
|
1147
1252
|
end
|
|
1148
1253
|
end
|
|
1149
1254
|
|
|
1255
|
+
# Clears the sign-in counts that have run out and the hashing keys of days gone, at most once an hour, so
|
|
1256
|
+
# they go even where the scheduled check never runs. Run once an answer is sent.
|
|
1257
|
+
def sweep_counts
|
|
1258
|
+
return unless @ready
|
|
1259
|
+
|
|
1260
|
+
at = now
|
|
1261
|
+
return if !@swept_at.nil? && at >= @swept_at && at - @swept_at < SWEEP_EVERY_MS
|
|
1262
|
+
|
|
1263
|
+
@swept_at = at
|
|
1264
|
+
Accounts::Throttle.sweep(@store, at)
|
|
1265
|
+
Accounts::Lockout.sweep(@store, at)
|
|
1266
|
+
rescue StandardError => e
|
|
1267
|
+
warn("Runlight: could not clear old sign-in counts #{e.message}")
|
|
1268
|
+
end
|
|
1269
|
+
|
|
1150
1270
|
# A capped JSON body, or nil where TS's readJsonCapped(...).catch(() => null) gives null.
|
|
1151
1271
|
def json_or_nil(answer)
|
|
1152
1272
|
Body.read_json_capped(answer, REMOTE_MAX_BYTES)
|
|
@@ -1218,7 +1338,13 @@ module Runlight
|
|
|
1218
1338
|
end
|
|
1219
1339
|
there ||= listed[0]
|
|
1220
1340
|
field = ->(key) { there.is_a?(Hash) ? there[key] : nil }
|
|
1221
|
-
|
|
1341
|
+
# Checked before anything is saved: a site there has an id and a name, as text.
|
|
1342
|
+
there_id = field.call("id")
|
|
1343
|
+
if !there_id.is_a?(String) || there_id == "" || Js.length(there_id) > 64 || !field.call("name").is_a?(String)
|
|
1344
|
+
raise SettingsError.new("#{url} did not answer like a Runlight install", "connect_not_runlight", { "url" => url })
|
|
1345
|
+
end
|
|
1346
|
+
|
|
1347
|
+
there_hostnames = field.call("hostnames").is_a?(Array) ? field.call("hostnames").grep(String).reject(&:empty?) : []
|
|
1222
1348
|
# Connecting the same site again (to allow changes, or with a new token) updates it in place.
|
|
1223
1349
|
@remotes.each do |existing, known|
|
|
1224
1350
|
next unless known["url"] == url && known["site"] == field.call("id")
|
|
@@ -1228,10 +1354,12 @@ module Runlight
|
|
|
1228
1354
|
@store.set_setting("remote:#{existing}", Mail::Secret.seal(Json.encode(updated), @secret))
|
|
1229
1355
|
@remotes[existing.to_s] = updated
|
|
1230
1356
|
@remote_seen.delete(existing)
|
|
1357
|
+
sites_changed
|
|
1231
1358
|
return site(existing.to_s)
|
|
1232
1359
|
end
|
|
1233
1360
|
# As TS's replace without the u flag, a character outside the BMP is two units, so two dashes.
|
|
1234
|
-
host = Js.lower((there_hostnames[0] || Http::Url.new(url).host).to_s.gsub(/[^a-z0-9._-]/i) { |c| c.ord > 0xFFFF ? "--" : "-" })
|
|
1361
|
+
host = Js.lower((there_hostnames[0] || Http::Url.new(url).host).to_s.gsub(/[^a-z0-9._-]/i) { |c| c.ord > 0xFFFF ? "--" : "-" }.sub(/\A[^a-z0-9]+/i, ""))
|
|
1362
|
+
host = "site" if host == ""
|
|
1235
1363
|
id = Js.slice(host, 0, 56)
|
|
1236
1364
|
n = 2
|
|
1237
1365
|
while site?(id)
|
|
@@ -1239,7 +1367,8 @@ module Runlight
|
|
|
1239
1367
|
n += 1
|
|
1240
1368
|
end
|
|
1241
1369
|
name = Js.slice(Js.trim(Js.string(input["name"].nil? ? "" : input["name"])), 0, 80)
|
|
1242
|
-
name = Js.
|
|
1370
|
+
name = Js.slice(Js.trim(field.call("name")), 0, 80) if name == ""
|
|
1371
|
+
name = host if name == ""
|
|
1243
1372
|
# No hostnames: tracker hits never land on a site that is counted elsewhere.
|
|
1244
1373
|
timezone = field.call("timezone")
|
|
1245
1374
|
site = { "id" => id, "name" => name, "hostnames" => [],
|
|
@@ -1249,6 +1378,7 @@ module Runlight
|
|
|
1249
1378
|
@store.set_setting("remote:#{id}", Mail::Secret.seal(Json.encode(remote), @secret))
|
|
1250
1379
|
@remotes[id] = remote
|
|
1251
1380
|
@configured = by_name(@configured + [site])
|
|
1381
|
+
sites_changed
|
|
1252
1382
|
site
|
|
1253
1383
|
end
|
|
1254
1384
|
|
|
@@ -1363,6 +1493,7 @@ module Runlight
|
|
|
1363
1493
|
def forwarded_host(request, context = {})
|
|
1364
1494
|
trusts?(request, context) ? request.headers.get("x-forwarded-host") : nil
|
|
1365
1495
|
end
|
|
1496
|
+
public :forwarded_host
|
|
1366
1497
|
|
|
1367
1498
|
# The connection's own address: the context's "ip", or else the request's remote_address, or nil.
|
|
1368
1499
|
def connection_ip(request, context)
|
|
@@ -1475,13 +1606,24 @@ module Runlight
|
|
|
1475
1606
|
# standalone server asks, so this saves a query on each tracker hit; a
|
|
1476
1607
|
# change made here clears it at once, one made by another process within
|
|
1477
1608
|
# half a minute.
|
|
1609
|
+
#
|
|
1610
|
+
# Middleware asks on every request of the app behind it, so a database that cannot be read must never
|
|
1611
|
+
# fail or hold up those: the failure is said once, the domains known before stay (none, when there were
|
|
1612
|
+
# none), and the database is not asked again for LINK_DOMAINS_RETRY_MS.
|
|
1478
1613
|
def link_domain_set
|
|
1479
1614
|
at = now
|
|
1480
|
-
|
|
1615
|
+
cached = @link_domain_cache
|
|
1616
|
+
return cached["domains"] if !cached.nil? && at - cached["at"] < (cached["failed"] ? LINK_DOMAINS_RETRY_MS : LINK_DOMAINS_FRESH_MS)
|
|
1481
1617
|
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1618
|
+
begin
|
|
1619
|
+
init
|
|
1620
|
+
domains = @store.link_domains.to_h { |d| [d["domain"], true] }
|
|
1621
|
+
@link_domain_cache = { "at" => at, "domains" => domains }
|
|
1622
|
+
rescue StandardError => e
|
|
1623
|
+
warn("Runlight: could not read the link domains #{e.message}") unless cached && cached["failed"]
|
|
1624
|
+
domains = cached.nil? ? {} : cached["domains"]
|
|
1625
|
+
@link_domain_cache = { "at" => at, "domains" => domains, "failed" => true }
|
|
1626
|
+
end
|
|
1485
1627
|
domains
|
|
1486
1628
|
end
|
|
1487
1629
|
|
|
@@ -1532,21 +1674,31 @@ module Runlight
|
|
|
1532
1674
|
saved = saved_agent_ranges(@store.setting("agent-ranges"))
|
|
1533
1675
|
if @update_agent_ranges && (saved.nil? || now - saved["at"] >= AGENT_RANGES_EVERY_MS)
|
|
1534
1676
|
lists = saved.nil? ? {} : saved["lists"].dup
|
|
1677
|
+
fetched = saved.nil? ? {} : saved["fetched"].dup
|
|
1535
1678
|
AgentCheck.bundled["lists"].each_key do |url|
|
|
1536
1679
|
fresh = AgentCheck.download_range_list(url, @fetcher)
|
|
1537
|
-
|
|
1680
|
+
next if fresh.nil?
|
|
1681
|
+
|
|
1682
|
+
lists[url] = fresh
|
|
1683
|
+
fetched[url] = now
|
|
1538
1684
|
end
|
|
1539
|
-
saved = { "at" => now, "lists" => lists }
|
|
1685
|
+
saved = { "at" => now, "lists" => lists, "fetched" => fetched }
|
|
1540
1686
|
@store.set_setting("agent-ranges", Json.encode(saved))
|
|
1541
1687
|
end
|
|
1542
|
-
|
|
1688
|
+
# A downloaded list stands in for the one that came with this version only while it is the newer of the two.
|
|
1689
|
+
shipped = Importers::Client.parse_date(AgentCheck.bundled["fetchedAt"].to_s)
|
|
1690
|
+
shipped = 0 unless shipped.is_a?(Integer) || (shipped.is_a?(Float) && shipped.finite?)
|
|
1691
|
+
newer = saved.nil? ? {} : saved["lists"].select { |url, _| (saved["fetched"][url] || 0) >= shipped }
|
|
1692
|
+
@agent_ranges = AgentCheck.bundled["lists"].merge(newer)
|
|
1543
1693
|
rescue StandardError => e
|
|
1544
1694
|
warn("Runlight: could not update the AI agents' address lists #{e.message}")
|
|
1545
1695
|
end
|
|
1546
1696
|
private :refresh_agent_ranges
|
|
1547
1697
|
|
|
1548
|
-
# The "agent-ranges" setting, { "at" => ms, "lists" => { url => [prefix] }
|
|
1549
|
-
# download is, or nil when there is none or it is not JSON. A list that does not read
|
|
1698
|
+
# The "agent-ranges" setting, { "at" => ms, "lists" => { url => [prefix] }, "fetched" => { url => ms } } with
|
|
1699
|
+
# each list read as a download is, or nil when there is none or it is not JSON. A list that does not read
|
|
1700
|
+
# is left out. `fetched` is when each list was downloaded; a copy saved before it was kept has only `at`,
|
|
1701
|
+
# for all of them.
|
|
1550
1702
|
def saved_agent_ranges(stored)
|
|
1551
1703
|
return nil if stored.nil? || stored == ""
|
|
1552
1704
|
|
|
@@ -1562,14 +1714,23 @@ module Runlight
|
|
|
1562
1714
|
elsif given.is_a?(Array) then given.each_with_index.map { |list, i| [i.to_s, list] }
|
|
1563
1715
|
else []
|
|
1564
1716
|
end
|
|
1717
|
+
at = parsed.is_a?(Hash) ? Js.number(parsed["at"]) : 0
|
|
1718
|
+
at = 0 if at.is_a?(Float) && (at.nan? || at.zero?)
|
|
1719
|
+
times = parsed.is_a?(Hash) ? parsed["fetched"] : nil
|
|
1565
1720
|
lists = {}
|
|
1721
|
+
fetched = {}
|
|
1566
1722
|
entries.each do |url, list|
|
|
1567
1723
|
wrapped = list.is_a?(Array) ? list.map { |p| { (Js.string(p).include?(":") ? "ipv6Prefix" : "ipv4Prefix") => p } } : nil
|
|
1568
1724
|
prefixes = AgentCheck.read_range_list({ "prefixes" => wrapped })
|
|
1569
|
-
|
|
1725
|
+
next if prefixes.nil?
|
|
1726
|
+
|
|
1727
|
+
lists[url] = prefixes
|
|
1728
|
+
time = times.is_a?(Hash) ? Js.number(times[url].nil? ? UNDEFINED : times[url]) : Float::NAN
|
|
1729
|
+
fetched[url] = if time.is_a?(Float) && (time.nan? || time.zero?) then Js.truthy?(times) ? 0 : at
|
|
1730
|
+
else time
|
|
1731
|
+
end
|
|
1570
1732
|
end
|
|
1571
|
-
at
|
|
1572
|
-
{ "at" => at.is_a?(Float) && (at.nan? || at.zero?) ? 0 : at, "lists" => lists }
|
|
1733
|
+
{ "at" => at, "lists" => lists, "fetched" => fetched }
|
|
1573
1734
|
end
|
|
1574
1735
|
private :saved_agent_ranges
|
|
1575
1736
|
|
|
@@ -1577,18 +1738,23 @@ module Runlight
|
|
|
1577
1738
|
init
|
|
1578
1739
|
# Requests take a current schema version on trust; the scheduled check goes over every table and index.
|
|
1579
1740
|
@store.migrate(true)
|
|
1741
|
+
version = @store.setting(SITES_VERSION) || ""
|
|
1580
1742
|
if @managed_sites
|
|
1581
1743
|
@configured = @store.sites
|
|
1582
1744
|
load_remotes
|
|
1583
1745
|
end
|
|
1584
1746
|
# A name or timezone changed in the dashboard by another process reaches this one too.
|
|
1585
1747
|
@overrides = @store.site_overrides
|
|
1748
|
+
@sites_version = version
|
|
1749
|
+
@sites_checked_at = now
|
|
1586
1750
|
refresh_agent_ranges
|
|
1587
1751
|
@salts = {}
|
|
1588
1752
|
sites.map { |site| site["timezone"] }.uniq.each { |timezone| current_salts(now, timezone) }
|
|
1589
1753
|
drop_old_salts(now)
|
|
1590
|
-
# Sign-in counts that have run out,
|
|
1754
|
+
# Sign-in counts that have run out, and the hashing keys of days gone.
|
|
1591
1755
|
Accounts::Throttle.sweep(@store, now)
|
|
1756
|
+
Accounts::Lockout.sweep(@store, now)
|
|
1757
|
+
@swept_at = now
|
|
1592
1758
|
# Every site's retention covers any one site's that is still waiting.
|
|
1593
1759
|
@pruning = []
|
|
1594
1760
|
apply_retention
|
|
@@ -66,7 +66,7 @@ module Runlight
|
|
|
66
66
|
{ "name" => "Yahoo Mail", "kind" => "email", "hosts" => ["mail.yahoo.com"], "aliases" => ["yahoomail"] },
|
|
67
67
|
{ "name" => "Fastmail", "kind" => "email", "hosts" => ["app.fastmail.com", "fastmail.com"], "aliases" => ["fastmail"] },
|
|
68
68
|
{ "name" => "Proton Mail", "kind" => "email", "hosts" => ["mail.proton.me"], "aliases" => ["protonmail"] },
|
|
69
|
-
{ "name" => "Newsletter", "kind" => "email", "hosts" => [], "aliases" => ["newsletter", "email", "e-mail", "mailchimp", "
|
|
69
|
+
{ "name" => "Newsletter", "kind" => "email", "hosts" => [], "aliases" => ["newsletter", "email", "e-mail", "mailchimp", "beehiiv", "buttondown"] },
|
|
70
70
|
].freeze
|
|
71
71
|
|
|
72
72
|
# Hosts known by their shape rather than their name: email services' click
|
|
@@ -77,7 +77,6 @@ module Runlight
|
|
|
77
77
|
{ "pattern" => /(\A|\.)convertkit-mail\d*\.com\z|(\A|\.)kit-mail\d*\.com\z/, "name" => "Kit", "kind" => "email" },
|
|
78
78
|
{ "pattern" => /(\A|\.)(list-manage|mailchi)\.(com|mp)\z/, "name" => "Mailchimp", "kind" => "email" },
|
|
79
79
|
{ "pattern" => /(\A|\.)beehiiv\.com\z/, "name" => "beehiiv", "kind" => "email" },
|
|
80
|
-
{ "pattern" => /(\A|\.)substack\.com\z/, "name" => "Substack", "kind" => "email" },
|
|
81
80
|
{ "pattern" => /\A(web)?mail\d*\.|(\A|\.)webmail\./, "name" => nil, "kind" => "email" },
|
|
82
81
|
].freeze
|
|
83
82
|
end
|
data/lib/runlight/db/connect.rb
CHANGED
|
@@ -78,8 +78,8 @@ module Runlight
|
|
|
78
78
|
db = Database.new(Pools.make(config.compact), owned: true, statement_timeout: statement_timeout.to_i, dialect: "mysql")
|
|
79
79
|
version, mode = db.connection_class.connection_pool.with_connection { |c| c.select_rows("SELECT VERSION(), @@GLOBAL.sql_mode")[0] }
|
|
80
80
|
db.connection_class.connection_pool.disconnect!
|
|
81
|
-
# Less NO_BACKSLASH_ESCAPES,
|
|
82
|
-
#
|
|
81
|
+
# Less NO_BACKSLASH_ESCAPES, as mysql.ts drops it. Nothing depends on that: Database writes values to
|
|
82
|
+
# read the same with or without it.
|
|
83
83
|
variables = { sql_mode: ((mode.to_s.split(",") - ["NO_BACKSLASH_ESCAPES"]) | ["IGNORE_SPACE"]).join(",") }
|
|
84
84
|
if statement_timeout.to_i.positive?
|
|
85
85
|
variables.merge!(version.to_s.match?(/mariadb/i) ? { max_statement_time: statement_timeout.to_i / 1000.0 } : { max_execution_time: statement_timeout.to_i })
|