Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ A read-only REST API and an MCP server, both scoped by keys that never see more
**Traffic you can trust**

- Crawlers, AI agents, monitoring probes and automated browsers filtered
- Referrer spam and unknown hostnames dropped
- Referrer spam (a list of over 2,000 domains, plus your own) and unknown hostnames dropped
- Exclusion rules for one site or all of them, with notes and pause
- A count of what was kept out, on the dashboard

Expand Down
2 changes: 1 addition & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ tests/ one file for each domain; tests/data holds recorded answers
## Following a hit

1. `r.js` sends a hit to `POST /e` (`src/domains/ingestion`).
2. The site is found by its key. The hostname, the user agent, known spam and the exclusion rules decide whether the hit counts. A dropped hit becomes a counter.
2. The site is found by its key. The hostname, the user agent, known referrer spam (`ingestion/referrer-spam.txt` and the install's own file) and the exclusion rules decide whether the hit counts. A dropped hit becomes a counter.
3. The hit is enriched: source and channel, browser and device, place, the daily visitor id and the visit.
4. It is queued in SQLite and the answer is 202, whether it was counted or not, so bots learn nothing.
5. The writer sends batches to ClickHouse every second, with a token, so a retry is not counted twice.
Expand Down
2 changes: 2 additions & 0 deletions docs/self-hosting.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Open `https://your-hostname`. The first account becomes the owner. After that, s
| `RYVR_SMTP_URL` | | `smtp://user:password@host:587` (STARTTLS) or `smtps://` (TLS). Invitations, reset links and security notices then go by email; without it, links are shown in the app. A password is only sent over TLS |
| `RYVR_MAIL_FROM` | | The sender, such as `RYVR <analytics@example.com>`. Needed with `RYVR_SMTP_URL` |
| `MAIL_MAILER` | `log` | `resend` or `ses` to send email through those services instead of SMTP |
| `RYVR_REFERRER_SPAM_FILE` | | A file of more referrer spam domains, one a line, added to the list RYVR ships. It is read again when it changes |
| `RYVR_REFERRER_SPAM_URL` | | An `https://` address to download that file from every week. The old file stays if a download fails or lists no domains |
| `RYVR_RETENTION_MONTHS` | keep all | Delete events older than this many whole months, once a day |
| `RYVR_SESSION_TIMEOUT` | `30` | Minutes without a hit that end a visit |
| `RYVR_INGEST_RATE_LIMIT` | `300` | Hits a minute from one network for one site |
Expand Down
6 changes: 6 additions & 0 deletions src/config/ryvr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ pub struct Ryvr {
pub geoip_database: Option<String>,
#[env("RYVR_GEOIP_AUTO_UPDATE", default = false)]
pub geoip_auto_update: bool,
/// A file of more referrer spam domains, one a line, read again when it changes.
#[env("RYVR_REFERRER_SPAM_FILE")]
pub referrer_spam_file: Option<String>,
/// Where to download that file from every week.
#[env("RYVR_REFERRER_SPAM_URL")]
pub referrer_spam_url: Option<String>,
#[env("RYVR_RETENTION_MONTHS", default = 0)]
pub retention_months: u32,
#[env("RYVR_TRACKER_URL")]
Expand Down
3 changes: 2 additions & 1 deletion src/console/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,9 @@ async fn traffic(
rows.clear();
}
}
// Inside the dashboard's five live minutes, so each one counts.
for _ in 0..demo.live {
let start = now - Duration::seconds(r.random_range(30..1500));
let start = now - Duration::seconds(r.random_range(30..240));
visit(&mut rows, r, site, organization, start, now);
}
events.insert("events", &rows, &token()).await?;
Expand Down
21 changes: 0 additions & 21 deletions src/domains/ingestion/bots.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::source::matches_host;
use crate::domains::lowercase;
use crate::domains::text::decoded_text;

Expand Down Expand Up @@ -223,22 +222,6 @@ const GROUPS: &[(&str, &[&str])] = &[
),
];

const SPAM_REFERRERS: &[&str] = &[
"semalt.com",
"buttons-for-website.com",
"darodar.com",
"ilovevitaly.com",
"priceg.com",
"hulfingtonpost.com",
"best-seo-offer.com",
"free-social-buttons.com",
"get-free-traffic-now.com",
"social-buttons.com",
"trafficmonetize.com",
"event-tracking.com",
"floating-share-buttons.com",
];

pub fn classify_agent(user_agent: impl AsRef<[u8]>) -> &'static str {
let ua = lowercase(decoded_text(user_agent.as_ref()).trim());
if ua.is_empty() {
Expand All @@ -254,7 +237,3 @@ pub fn classify_agent(user_agent: impl AsRef<[u8]>) -> &'static str {
}
""
}

pub fn is_spam_referrer(domain: &str) -> bool {
matches_host(&lowercase(domain), SPAM_REFERRERS)
}
17 changes: 16 additions & 1 deletion src/domains/ingestion/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use super::hit_url::{page_path, parse_query, split_url, truncate};
use super::limits::{Capacity, Limiter, locked};
use super::rules::Rules;
use super::source::resolve_source;
use super::spam::Spam;
use super::store;
use super::useragent::parse_user_agent;
use super::visitor::visitor_id;
Expand Down Expand Up @@ -63,6 +64,7 @@ pub struct Collector {
pub(super) config: Ryvr,
organization: String,
geo: Geo,
spam: Spam,
rules: Rules,
salt: Mutex<Option<DaySalt>>,
capacity: Capacity,
Expand All @@ -81,12 +83,22 @@ impl Collector {
);
// A location database is optional, including while it is being installed.
let _ = geo.reload().await;
let spam = Spam::new(
config
.referrer_spam_file
.as_deref()
.filter(|p| !p.is_empty())
.map(Into::into),
);
// A broken extra list must not stop collection: the one that ships still applies.
let _ = spam.reload().await;
let rules = Rules::default();
rules.reload().await?;
Ok(Self {
config,
organization: store::organization().await?,
geo,
spam,
rules,
salt: Mutex::default(),
capacity: Capacity::default(),
Expand All @@ -97,6 +109,9 @@ impl Collector {

pub(super) async fn refresh(&self) -> Result<()> {
self.rules.reload().await?;
if let Err(error) = self.spam.reload().await {
laravel::report(error);
}
self.geo.reload().await
}

Expand Down Expand Up @@ -142,7 +157,7 @@ impl Collector {
REASON_AUTOMATION
} else if !agent.is_empty() {
agent
} else if is_spam_referrer(&src.referrer_domain) {
} else if self.spam.is_spam(&src.referrer_domain) {
REASON_SPAM
} else if self.rules.excludes(
&hit.site.id,
Expand Down
19 changes: 19 additions & 0 deletions src/domains/ingestion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod limits;
mod queue;
mod rules;
pub mod source;
pub mod spam;
mod store;
pub mod useragent;
pub mod visitor;
Expand Down Expand Up @@ -55,6 +56,11 @@ pub fn ticks(ticks: &mut laravel::task::Ticks) {
.immediately()
.name("ingestion.locations")
.when(|host| host.is_server());
ticks
.every(1.days(), referrer_spam)
.immediately()
.name("ingestion.referrer_spam")
.when(|host| host.is_server());
ticks
.every(1.hours(), purge)
.immediately()
Expand All @@ -73,6 +79,19 @@ async fn locations(config: Section<Ryvr>) -> laravel::Result<()> {
}
}

/// The install's own referrer spam list is downloaded again when the install names where from.
async fn referrer_spam(config: Section<Ryvr>) -> laravel::Result<()> {
let url = config.referrer_spam_url.as_deref().filter(|url| !url.is_empty());
let path = config.referrer_spam_file.as_deref().filter(|path| !path.is_empty());
match (url, path) {
(Some(url), Some(path)) => spam::keep_fresh(url, Path::new(path), collector::now()).await,
(Some(_), None) => Err(laravel::Error::msg(
"RYVR_REFERRER_SPAM_URL needs RYVR_REFERRER_SPAM_FILE to say where to keep the list.",
)),
_ => Ok(()),
}
}

async fn purge() -> laravel::Result<()> {
store::purge(collector::now()).await
}
Expand Down
Loading