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

Adds Node wrappers for handling redirect resources

Updates README to demonstrate redirect handling, includes logging to stderr for some of the worse failure cases
  • Loading branch information
AndriusA committed Jun 6, 2019
commit d6530ebd4e3352ff65b6a1f38db85543b037cf62
@@ -41,10 +41,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
@@ -55,12 +59,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

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

@@ -109,6 +109,18 @@ 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())
}
}
}

@@ -117,6 +117,9 @@ impl Blocker {
Some(data_url.trim().to_owned())
} else {
// TOOD: handle error - throw?

This comment has been minimized.

Copy link
@pes10k

pes10k Jun 6, 2019

Collaborator

Could this be logged somehow, even if its just mirroring the "didn't understand filter: X" stuff the current lib does? Would be a nice, noisy reminder if there is some new filter format we don't support, something like that

if self.debug {
eprintln!("Matched rule with redirect option but did not find corresponding resource to send");
}
None
}
} else {
@@ -51,7 +51,7 @@ impl Engine {
let gz = GzDecoder::new(serialized);
let blocker = bincode::deserialize_from(gz)
.or_else(|e| {
println!("Error deserializing: {:?}", e);
eprintln!("Error deserializing: {:?}", e);
Err(BlockerError::DeserializationError)
})?;
self.blocker = blocker;
@@ -65,6 +65,7 @@ impl Engine {
self.blocker.check(&request)
})
.unwrap_or_else(|_e| {
eprintln!("Error parsing request, returning no match");
BlockerResult {
matched: false,
explicit_cancel: false,
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.