Skip to content

Commit 29f016e

Browse files
committed
initial version of a command line invocation
1 parent d56be21 commit 29f016e

File tree

4 files changed

+161
-14
lines changed

4 files changed

+161
-14
lines changed

js/Cargo.lock

+16-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/check.js

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#!/usr/bin/env node
2+
3+
const { text } = require('node:stream/consumers')
4+
5+
const { ArgumentDefaultsHelpFormatter, ArgumentParser, FileType } = require('argparse')
6+
7+
const adblockRust = require('./index.js')
8+
9+
// These are defined by different content filter projects (AdBlock Plus,
10+
// uBlockOrigin, AdGuard, etc.).
11+
// For example, https://github.com/gorhill/uBlock/wiki/Static-filter-syntax
12+
const filterListRequestTypes = [
13+
'beacon',
14+
'csp_report',
15+
'document',
16+
'font',
17+
'image',
18+
'media',
19+
'object',
20+
'ping',
21+
'script',
22+
'stylesheet',
23+
'sub_frame',
24+
'websocket',
25+
'xhr',
26+
'other',
27+
'speculative',
28+
'web_manifest',
29+
'xbl',
30+
'xml_dtd',
31+
'xslt'
32+
]
33+
34+
// These values are defined by Blink, in `Resource::ResourceTypeToString`.
35+
// See third_party/blink/renderer/platform/loader/fetch/resource.h.
36+
// The OTHER catch all case covers the additional types
37+
// defined in `blink::Resource::InitiatorTypeNameToString`.
38+
//
39+
// See https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/platform/loader/fetch/resource.cc
40+
const chromiumRequestTypeMapping = {
41+
'Attribution resource': 'other',
42+
'Audio': 'media',
43+
'CSS resource': 'stylesheet',
44+
'CSS stylesheet': 'stylesheet',
45+
'Dictionary': 'other',
46+
'Document': 'document',
47+
'Fetch': 'xhr',
48+
'Font': 'font',
49+
'Icon': 'other',
50+
'Image': 'image',
51+
'Internal resource': 'other',
52+
'Link element resource': 'other',
53+
'Link prefetch resource': 'speculative',
54+
'Manifest': 'web_manifest',
55+
'Mock': 'other',
56+
'Other resource': 'other',
57+
'Processing instruction': 'other',
58+
'Script': 'script',
59+
'SpeculationRule': 'speculative',
60+
'SVG document': 'media',
61+
'SVG Use element resource': 'media',
62+
'Text track': 'other',
63+
'Track': 'other',
64+
'User Agent CSS resource': 'stylesheet',
65+
'Video': 'media',
66+
'XML resource': 'document',
67+
'XMLHttpRequest': 'xhr',
68+
'XSL stylesheet': 'xslt'
69+
}
70+
71+
const parser = new ArgumentParser({
72+
add_help: true,
73+
formatter_class: ArgumentDefaultsHelpFormatter,
74+
description: 'Check whether a URL would be blocked by given filter list rules'
75+
})
76+
parser.add_argument('--url', {
77+
required: true,
78+
type: URL,
79+
help: 'The full URL to check against the provided filter lists.'
80+
})
81+
82+
const requestTypeGroup = parser.add_argument_group(true)
83+
requestTypeGroup.add_argument('--type', {
84+
help: 'The type of the request, using the types defined by ' +
85+
'filter list projects',
86+
choices: filterListRequestTypes
87+
})
88+
requestTypeGroup.add_argument('--chromium-type', {
89+
help: 'The type of the request, using the types defined by chromium',
90+
choices: Object.keys(chromiumRequestTypeMapping)
91+
})
92+
93+
parser.add_argument('--context-url', {
94+
required: true,
95+
type: URL,
96+
help: 'The security context the request occurred in, as a full URL'
97+
})
98+
parser.add_argument('--rule-files', {
99+
required: true,
100+
type: FileType('r'),
101+
nargs: '*',
102+
help: 'One or more paths to files of filter list rules to check the ' +
103+
'request against'
104+
})
105+
parser.add_argument('--verbose', {
106+
default: false,
107+
action: 'store_true',
108+
help: 'Print information about what rule(s) the request matched.'
109+
})
110+
111+
;(async () => {
112+
const args = parser.parse_args()
113+
114+
const filterSet = new adblockRust.FilterSet(true)
115+
for (const aRuleFile of args.rule_files) {
116+
const rulesText = await text(aRuleFile)
117+
filterSet.addFilters(rulesText.split('\n'))
118+
}
119+
120+
const engine = new adblockRust.Engine(filterSet, true)
121+
const result = engine.check(
122+
args.url.toString(),
123+
args.context_url.toString(),
124+
args.type || chromiumRequestTypeMapping[args.chromium_type],
125+
true
126+
)
127+
128+
if (args.verbose) {
129+
console.log(result)
130+
}
131+
process.exit(result.matched ? 0 : 1)
132+
})()

package-lock.json

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
"url": "git+https://github.com/brave/adblock-rust.git"
2929
},
3030
"dependencies": {
31+
"argparse": "^2.0.1",
3132
"cargo-cp-artifact": "^0.1"
3233
},
3334
"scripts": {
3435
"build": "cd js && cargo-cp-artifact -nc index.node -- cargo build --message-format=json-render-diagnostics",
3536
"build-debug": "npm run build --",
3637
"build-release": "npm run build -- --release",
38+
"check": "node ./js/check.js",
3739
"update-lists": "node data/update-lists.js",
3840
"postinstall": "npm run build-release",
3941
"test": "cargo test"

0 commit comments

Comments
 (0)