Skip to content

Commit 6808ba0

Browse files
committed
initial version of a command line invocation
1 parent d56be21 commit 6808ba0

File tree

4 files changed

+167
-14
lines changed

4 files changed

+167
-14
lines changed

js/Cargo.lock

Lines changed: 16 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/check.js

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

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
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)