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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
- uses: actions/checkout@v7
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
# Every one of the 38 301 fixture cases, one test each, read off disk. Twice: once against
# Every one of the 38 383 fixture cases, one test each, read off disk. Twice: once against
# the database as it ships, embedded, and once against the files, which is how it is run
# while working.
- run: cargo test --release --test corpus
Expand Down
4 changes: 2 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ agent needs to know before touching anything.

## The specification is the fixtures, and only the fixtures

`tests/matomo-device-detector/fixtures/*.yml` says what every one of 38 301 user agents means.
`tests/matomo-device-detector/fixtures/*.yml` says what every one of 38 383 user agents means.
That corpus, pinned to one revision of matomo-org/device-detector in `castor.php`, is the whole
specification. A change is right when the tests pass and wrong when they do not; there is no other
authority to appeal to.
Expand Down Expand Up @@ -136,7 +136,7 @@ feature does rebuild, so stay on one side of it while working.

`cargo bench` is criterion, so it keeps the last run and tells you what moved; `--save-baseline`
and `--baseline` name one to keep. It detects over a stride across the corpus rather than all
38 301 agents, because at a budget of zero a lookup costs about six milliseconds against half a
38 383 agents, because at a budget of zero a lookup costs about six milliseconds against half a
millisecond cached, and ten samples of the whole corpus at that rate is most of an hour. That
makes the default good for comparing two runs and not for quoting on its own; `DETECT_SAMPLE`
raises it when a number has to stand by itself. Criterion sizes a run by the measurement time it
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ assert_eq!(found.os.unwrap().version, "10");
```

This library is based on the [matomo device detector library](https://github.com/matomo-org/device-detector):
its test fixtures, 38 301 user agents and what each one means, are the specification this database
its test fixtures, 38 383 user agents and what each one means, are the specification this database
is written against. Matomo's own regexes are not vendored and none of them is copied here.

The main change is how the parsing is done, with a specific structure to ensure better speed with
Expand Down Expand Up @@ -52,6 +52,9 @@ Detection {
- `bot` is set for a crawler, and then **it is the whole answer**: the other five fields are
empty. `Detection::named()` hands that back as an enum, so a `match` cannot read a client where
there is none. `bot.category` is a `BotCategory`, with `is_search_bot()` on it.
- `is_headless()` says whether the client is a browser run by a program -- Headless Chrome,
Headless Edge, Lightpanda. Matomo reads them as browsers, and so does this library: the
detection stays the same, and this is for a caller who counts visitors to leave them out.
- The text fields are empty rather than absent when nothing was found.

## Client hints
Expand Down
2 changes: 1 addition & 1 deletion benches/detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! cargo bench
//! cargo bench -- detect/20000 # one group of it
//! DETECT_SAMPLE=38301 cargo bench # the whole corpus, and a long wait
//! DETECT_SAMPLE=38383 cargo bench # the whole corpus, and a long wait
//!
//! The memory side of the same question is `cargo run --release --example memory`. Criterion
//! cannot answer it: resident size is a property of the process rather than of an operation, and
Expand Down
2 changes: 1 addition & 1 deletion castor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* library answers. How matomo goes about answering it is its own business, and its regexes are
* under a licence we do not want to carry.
*/
const MATOMO_REVISION = 'e83ce7a97f05d8ee3e1eee7ea77a73b3bdfebd9a';
const MATOMO_REVISION = 'eaafe3725acea466d8ba39f38c2b56d4f5ffa2c0';

#[AsTask(description: 'Syncs the matomo device-detector test fixtures', name: 'sync', namespace: 'matomo')]
function sync()
Expand Down
2 changes: 1 addition & 1 deletion examples/all/fixtures.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The fixtures, read as they are rather than compiled into a test.
//!
//! `tests/matomo-device-detector/fixtures/*.yml` is the specification: 38 301 user agents and
//! `tests/matomo-device-detector/fixtures/*.yml` is the specification: 38 383 user agents and
//! what each one means. Generating a `#[test]` per case out of them cost forty megabytes of rust
//! and two minutes of build for every change to an entry; reading them at startup costs the
//! second the yaml takes to parse.
Expand Down
90 changes: 30 additions & 60 deletions examples/compare/injector.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! What redirection.io's log injector makes of a detection.
//!
//! It stores two columns per request, `user_agent_simplified` and `user_agent_type`, and both
//! come from a detection by rust-device-detector. Comparing this library against production
//! come from a detection. Comparing this library against production
//! means replaying that derivation, so it lives here rather than in either of the two examples
//! that need it, and in `tests/traffic.rs`, which reaches for it across the tree.

#![allow(dead_code)]

use ua_detector::{BotCategory, ClientKind, Detection, DeviceKind, MOBILE_ONLY_BROWSERS};
use ua_detector::{ClientKind, Detection, Named};

/// `DeviceType` of the log injector, whose numbers are what the column holds.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
Expand Down Expand Up @@ -41,70 +41,40 @@ impl Kind {

/// What the log injector writes into the two columns, given a detection.
pub fn classify(detection: &Detection, user_agent: &str) -> (Kind, String) {
if let Some(bot) = &detection.bot {
// Production tested `category` for the text "search bot", which over the categories
// this database holds is the two `is_search_bot` names.
let kind = if bot.category.as_ref().is_some_and(BotCategory::is_search_bot) {
Kind::Bot
} else {
Kind::Tool
};

return (kind, bot.name.clone());
}

let name = match &detection.client {
Some(client) => client.name.clone(),
None => user_agent.to_string(),
let name = match detection.named() {
Named::Bot(bot) => bot.name.clone(),
Named::Agent { client: Some(client), .. } => client.name.clone(),
Named::Agent { client: None, .. } => user_agent.to_string(),
};

if is_mobile(detection) || is_tablet(detection) {
(Kind::Mobile, name)
} else if is_desktop(detection) {
(Kind::Desktop, name)
} else {
(Kind::Tool, name)
}
(kind(detection), name)
}

fn is_tablet(detection: &Detection) -> bool {
detection.device.as_ref().is_some_and(|device| device.kind == Some(DeviceKind::Tablet))
}

fn is_desktop(detection: &Detection) -> bool {
detection.device.as_ref().is_some_and(|device| device.kind == Some(DeviceKind::Desktop))
}

/// rust-device-detector's `KnownDevice::is_mobile`, minus the client hints, which a dump of user
/// agents alone cannot carry.
fn is_mobile(detection: &Detection) -> bool {
if let Some(device) = &detection.device {
match device.kind {
Some(
DeviceKind::FeaturePhone
| DeviceKind::Smartphone
| DeviceKind::Tablet
| DeviceKind::Phablet
| DeviceKind::Camera
| DeviceKind::PortableMediaPlayer,
) => return true,
Some(DeviceKind::Tv | DeviceKind::SmartDisplay | DeviceKind::Console) => return false,
_ => {}
/// The log injector's `device_type`, from `server/log-injector/src/enrichment.rs`.
fn kind(detection: &Detection) -> Kind {
match detection.named() {
Named::Bot(bot) => match &bot.category {
Some(category) if category.is_search_bot() => Kind::Bot,
_ => Kind::Tool,
},
Named::Agent { os, client, device, .. } => {
let driven_by_a_person = client
.and_then(|client| client.kind)
.is_some_and(|kind| matches!(kind, ClientKind::Browser | ClientKind::MobileApp | ClientKind::Pim));
let device_kind = device.and_then(|device| device.kind);
let platform_named =
os.is_some_and(|os| !os.name.is_empty()) || device_kind.is_some_and(|kind| kind.is_mobile().is_some());

match device_kind {
_ if !driven_by_a_person || !platform_named => Kind::Tool,
// A television, a console or a smart display is neither carried nor a desktop.
Some(kind) if kind.is_mobile() == Some(false) => Kind::Tool,
_ if detection.is_mobile() => Kind::Mobile,
_ if detection.is_desktop() => Kind::Desktop,
_ => Kind::Tool,
}
}
}

if let Some(client) = &detection.client
&& client.kind == Some(ClientKind::Browser)
&& MOBILE_ONLY_BROWSERS.contains(&client.name.as_str())
{
return true;
}

if detection.os.is_none() {
return false;
}

!is_desktop(detection)
}

/// What the log injector would have written for a user agent, detection or no detection.
Expand Down
17 changes: 17 additions & 0 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ impl Detection {

self.uses_mobile_only_browser() || !self.is_desktop()
}

/// Whether the client is a browser driven by a program rather than a person: Puppeteer,
/// Playwright, a prerenderer, a test runner.
///
/// Matomo has no counterpart and reads these as browsers, on purpose: they render with a real
/// engine. This says nothing against that, it adds what the name already holds. Only a
/// browser that declares itself headless is caught; one driven with its usual user agent
/// cannot be told apart from a person, and a crawler naming itself is a bot instead.
pub fn is_headless(&self) -> bool {
self.client
.as_ref()
.is_some_and(|client| client.kind == Some(ClientKind::Browser) && HEADLESS_BROWSERS.contains(&client.name.as_str()))
}
}

#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Deserialize)]
Expand Down Expand Up @@ -602,6 +615,10 @@ pub const MOBILE_ONLY_BROWSERS: &[&str] = &[

/// Whether a client is a browser only a phone runs, which both [`Detection::is_mobile`] and the
/// desktop fallback below have to ask, one of a finished detection and one part way through it.
/// The browsers that run without a window, under a program: the ones matomo names, by the name
/// this library answers with.
pub const HEADLESS_BROWSERS: &[&str] = &["Headless Chrome", "Headless Edge", "Lightpanda"];

fn mobile_only(kind: Option<ClientKind>, name: &str) -> bool {
kind == Some(ClientKind::Browser) && MOBILE_ONLY_BROWSERS.contains(&name)
}
Expand Down
115 changes: 114 additions & 1 deletion src/devices/bots.yml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,30 @@
bot:
name: WATCrawler
category: Crawler
- regex: .*TuneIn-DirMon.*
bot:
name: TuneIn-DirMon
category: Service Agent
url: https://tunein.com/
producer:
name: TuneIn, Inc.
url: https://tunein.com/
- regex: .*IsonAI-User.*
bot:
name: IsonAI-User
category: AI Assistant
url: https://docs.isonai.com/en/bots/
producer:
name: PT Epithre Teknologi Indonesia
url: https://www.epithre.com/
- regex: .*IsonSearchBot.*
bot:
name: IsonSearchBot
category: Search bot
url: https://search.isonai.com/bot
producer:
name: PT Epithre Teknologi Indonesia
url: https://www.epithre.com/
- regex: .*MyCellar.*
bot:
name: MyCellar
Expand Down Expand Up @@ -6514,6 +6538,14 @@
producer:
name: Aboundex.com
url: http://www.aboundex.com
- regex: .*\b9bot/.*
bot:
name: 9bot
category: Crawler
url: https://9bot.com.br/en/9bot-docs
producer:
name: 9bot
url: https://9bot.com.br
- regex: '.*360Spider.*'
bot:
name: 360Spider
Expand Down Expand Up @@ -6727,7 +6759,8 @@
producer:
name: Google Inc.
url: https://www.google.com
- regex: '.*cert-manager(?:-challenges)?/v[\d.]+.*'
# A build cut from no tag says `canary` where a release says its version.
- regex: '.*cert-manager(?:-challenges)?/(?:v[\d.]+|canary\b).*'
bot:
name: cert-manager
category: Service Agent
Expand Down Expand Up @@ -7232,6 +7265,86 @@
producer:
name: Rajahinta
url: https://rajahinta.fi/
- regex: '.*\baikido-scan-agent/[\d.]+.*'
bot:
name: Aikido Security
category: Security Checker
url: https://help.aikido.dev/dast-surface-monitoring/miscellaneous/allowing-ip-addresses-for-dast-surface-monitoring
producer:
name: Aikido Security
url: https://www.aikido.dev/
- regex: '.*\bElevenlabsBot/[\d.]+.*'
bot:
name: ElevenlabsBot
category: AI Assistant
url: https://elevenlabs.io/docs/bots
producer:
name: ElevenLabs
url: https://elevenlabs.io/
- regex: '.*\bEplictaBot/[\d.]+.*'
bot:
name: EplictaBot
category: Crawler
url: https://eplicta.se/bot/
producer:
name: Eplicta
url: https://eplicta.se/
- regex: '.*\bTalarionSearchBot/[\d.]+.*'
bot:
name: TalarionSearchBot
category: AI Search Crawler
url: https://knownagents.com/agents/talarionsearchbot
producer:
name: Talarion
url: https://talarion.com/
- regex: '.*\bClueWeb-Crawler/[\d.]+.*'
bot:
name: ClueWeb Crawler
category: Crawler
url: https://boston.lti.cs.cmu.edu/CMU-ClueWeb-Crawler/
producer:
name: Carnegie Mellon University
url: https://www.cmu.edu/
- regex: '.*\btopicsearch-crawler/[\d.]+.*'
bot:
name: FatGrid TopicSearch
category: Crawler
url: https://fatgrid.com/crawler
producer:
name: FatGrid
url: https://fatgrid.com/
- regex: '.*centreon::plugins::.*'
bot:
name: Centreon
category: Site Monitor
url: https://github.com/centreon/centreon-plugins
producer:
name: Centreon
url: https://www.centreon.com/
- regex: '.*\bkinsta-bot\b.*'
bot:
name: Kinsta
category: Site Monitor
url: https://kinsta.com/docs/wordpress-hosting/wordpress-monitoring/uptime-monitoring/
producer:
name: Kinsta Inc.
url: https://kinsta.com/
- regex: '.*contentsquare static resource scraper.*'
bot:
name: Contentsquare Static Resource Scraper
category: Service Agent
url: https://docs.contentsquare.com/en/web/static-assets-scraping
producer:
name: Contentsquare
url: https://contentsquare.com/
- regex: '.*\bSvix-Webhooks/[\d.]+.*'
bot:
name: Svix
category: Service Agent
url: https://www.svix.com/http-sender/
producer:
name: Svix
url: https://www.svix.com/

# feedparser 6 names itself in lower case, which the `UniversalFeedParser` rule above does not read.
- regex: '.*\bfeedparser/[\d.]+.*'
Expand Down
Loading
Loading