Ad Block engine in Rust
Native Rust module for Adblock Plus syntax (e.g. EasyList, EasyPrivacy) filter parsing and matching.
It uses a tokenisation approach for quickly reducing the potentially matching rule search space against a URL.
The algorithm is inspired by, and closely follows the algorithm of uBlock Origin and Cliqz.
Somewhat graphical explanation of the algorithm:
Demo
Demo use in Rust:
use Engine;
use ;
Node.js module demo
Note the Node.js module has overheads inherent to boundary crossing between JS and native code.
const AdBlockClient = require;
let el_rules = fs..;
let ubo_unbreak_rules = fs..;
let rules = el_rules.;
let resources = ;
const filterSet = ;
filterSet.;
const client = ;
client.;
const serializedArrayBuffer = client.; // Serialize the engine to an ArrayBuffer
console.log;
console.log
// Match with full debuging info
console.log
// No, but still with debugging info
console.log
// Example that inlcludes a redirect response
console.log
Optional features
CSS validation during rule parsing
When parsing cosmetic filter rules, it's possible to include a built-in implementation of CSS validation (through the selectors and cssparser crates) by enabling the css-validation
feature. This will cause adblock-rust
to reject cosmetic filter rules with invalid CSS syntax.
Content blocking format translation
Enabling the content-blocking
feature gives adblock-rust
support for conversion of standard ABP-style rules into Apple's content-blocking format, which can be exported for use on iOS and macOS platforms.
External domain resolution
By default, adblock-rust
ships with a built-in domain resolution implementation (through the addr crate) that will generally suffice for standalone use-cases. For more advanced use-cases, disabling the embedded-domain-resolver
feature will allow adblock-rust
to use an external domain resolution implementation instead. This is extremely useful to reduce binary bloat and improve consistency when embedding adblock-rust
within a browser.
Parsing resources from uBlock Origin's formats
adblock-rust
uses uBlock Origin-compatible resources for scriptlet injection and redirect rules.
The resource-assembler
feature allows adblock-rust
to parse these resources directly from the file formats used by the uBlock Origin repository.