Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements uBO style polyfills for requests redirects #29

Merged
merged 9 commits into from Jun 18, 2019

Some generated files are not rendered by default. Learn more.

@@ -33,6 +33,8 @@ bincode = "1.1"
flate2 = { version = "1.0", features = ["rust_backend"], default-features = false }
seahash = "3"
twoway = "0.2"
base64 = "0.10"
rmp-serde = "0.13.7"

# [target.'cfg(any(unix, windows))'.dependencies]
# rayon = "1.0"
@@ -43,10 +43,14 @@ Note the Node.js module has overheads inherent to boundary crossing between JS a

```js
const AdBlockClient = require('adblock-rs');
let rules = fs.readFileSync('./data/easylist.to/easylist/easylist.txt', { encoding: 'utf-8' }).split('\n');
let el_rules = fs.readFileSync('./data/easylist.to/easylist/easylist.txt', { encoding: 'utf-8' }).split('\n');
let ubo_unbreak_rules = fs.readFileSync('./data/uBlockOrigin/unbreak.txt', { encoding: 'utf-8' }).split('\n');
let rules = el_rules.concat(ubo_unbreak_rules);
let resources = fs.readFileSync('./data/uBlockOrigin/resources.txt', { encoding: 'utf-8' });
// create client with debug = true
const client = new AdBlockClient.Engine(rules, true);
client.updateResources(resources);
const serializedArrayBuffer = client.serialize(); // Serialize the engine to an ArrayBuffer
@@ -57,12 +61,13 @@ console.log("Matching:", client.check("http://example.com/-advertisement-icon.",
console.log("Matching:", client.check("http://example.com/-advertisement-icon.", "http://example.com/helloworld", "image", true))
// No, but still with debugging info
console.log("Matching:", client.check("https://github.githubassets.com/assets/frameworks-64831a3d.js", "https://github.com/AndriusA", "script", true))
// Example that inlcludes a redirect response
console.log("Matching:", client.check("https://bbci.co.uk/test/analytics.js", "https://bbc.co.uk", "script", true))
```


## TODO

- [ ] Generate redirect addresses based on provided resources.txt (uBo style)
- [ ] Function for extracting CSP directives
- [ ] Generate string representation of a rule when debug mode is off (i.e. initial rule is not available)
- [ ] Cosmetic filters
@@ -21,7 +21,7 @@ struct TestRequest {
}

fn load_requests() -> Vec<TestRequest> {
let requests_str = adblock::utils::read_rules("data/requests.json");
let requests_str = adblock::utils::read_file_lines("data/requests.json");
let reqs: Vec<TestRequest> = requests_str.into_iter().map(|r| serde_json::from_str(&r)).filter_map(Result::ok).collect();
reqs
}
@@ -3,7 +3,7 @@ extern crate criterion;
use criterion::*;

use adblock;
use adblock::utils::{read_rules, rules_from_lists};
use adblock::utils::{read_file_lines, rules_from_lists};
use adblock::blocker::{Blocker, BlockerOptions};


@@ -15,7 +15,7 @@ fn default_lists() -> Vec<String> {

fn default_rules_lists() -> Vec<Vec<String>> {
vec![
read_rules("data/easylist.to/easylist/easylist.txt"),
read_file_lines("data/easylist.to/easylist/easylist.txt"),
]
}

@@ -18,7 +18,7 @@ struct TestRequest {
}

fn load_requests() -> Vec<TestRequest> {
let requests_str = adblock::utils::read_rules("data/requests.json");
let requests_str = adblock::utils::read_file_lines("data/requests.json");
let reqs: Vec<TestRequest> = requests_str
.into_iter()
.map(|r| serde_json::from_str(&r))

Some generated files are not rendered by default. Learn more.

@@ -110,13 +110,25 @@ declare_types! {
Ok(JsNull::new().upcast())
}

method updateResources(mut cx) {
let resources: String = cx.argument::<JsString>(0)?.value();

let mut this = cx.this();
let guard = cx.lock();
{
let mut engine = this.borrow_mut(&guard);
engine.with_resources(&resources);
}
Ok(JsNull::new().upcast())

}
method tagExists(mut cx) {
let tag: String = cx.argument::<JsString>(0)?.value();

let this = cx.this();
let result = {
let guard = cx.lock();
let mut engine = this.borrow(&guard);
let engine = this.borrow(&guard);
engine.tag_exists(&tag)
};
Ok(cx.boolean(result).upcast())
@@ -144,6 +156,33 @@ declare_types! {
}
Ok(JsNull::new().upcast())
}

method addResource(mut cx) {
let name: String = cx.argument::<JsString>(0)?.value();
let content_type: String = cx.argument::<JsString>(1)?.value();
let data: String = cx.argument::<JsString>(2)?.value();

let mut this = cx.this();
let guard = cx.lock();
{
let mut engine = this.borrow_mut(&guard);
engine.resource_add(&name, &content_type, &data);
}
Ok(JsNull::new().upcast())
}

method getResource(mut cx) {
let name: String = cx.argument::<JsString>(0)?.value();

let this = cx.this();
let result = {
let guard = cx.lock();
let engine = this.borrow(&guard);
engine.resource_get(&name)
};
let js_value = neon_serde::to_value(&mut cx, &result)?;
Ok(js_value)
}
}
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.