@adguard/hostlist-compiler 1.0.15 → 1.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -1
- package/package.json +1 -1
- package/src/cli.js +78 -7
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@adguard/hostlist-compiler/)
|
|
4
4
|
|
|
5
|
-
This is a simple tool that makes it easier to compile a [hosts blocklist](https://
|
|
5
|
+
This is a simple tool that makes it easier to compile a [hosts blocklist](https://adguard-dns.io/kb/general/dns-filtering-syntax/) compatible with AdGuard Home or any other AdGuard product with **DNS filtering**.
|
|
6
6
|
|
|
7
7
|
- [Usage](#usage)
|
|
8
8
|
- [Configuration](#configuration)
|
|
@@ -28,6 +28,18 @@ First of all, install the `hostlist-compiler`:
|
|
|
28
28
|
npm i -g @adguard/hostlist-compiler
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
+
After that you have two options.
|
|
32
|
+
|
|
33
|
+
**Quick hosts conversion**
|
|
34
|
+
|
|
35
|
+
Convert and compress a `/etc/hosts`-syntax blocklist to [AdGuard syntax](https://adguard-dns.io/kb/general/dns-filtering-syntax/).
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
hostlist-compiler -i hosts.txt -i hosts2.txt -o output.txt
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**Build a configurable blocklist from multiple sources**
|
|
42
|
+
|
|
31
43
|
Prepare the list configuration (read more about that below) and run the compiler:
|
|
32
44
|
|
|
33
45
|
```bash
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -8,13 +8,32 @@ const packageJson = require('../package.json');
|
|
|
8
8
|
// eslint-disable-next-line import/order
|
|
9
9
|
const { argv } = require('yargs')
|
|
10
10
|
.usage('Usage: $0 [options]')
|
|
11
|
-
.example(
|
|
11
|
+
.example(
|
|
12
|
+
'$0 -c config.json -o output.txt',
|
|
13
|
+
'compile a blocklist and write the output to output.txt',
|
|
14
|
+
)
|
|
15
|
+
.example(
|
|
16
|
+
'$0 -i https://example.org/hosts.txt -o output.txt',
|
|
17
|
+
'compile a blocklist from the URL and write the output to output.txt',
|
|
18
|
+
)
|
|
12
19
|
.option('config', {
|
|
13
20
|
alias: 'c',
|
|
14
21
|
type: 'string',
|
|
15
22
|
description: 'Path to the compiler configuration file',
|
|
16
23
|
nargs: 1,
|
|
17
24
|
})
|
|
25
|
+
.option('input', {
|
|
26
|
+
alias: 'i',
|
|
27
|
+
array: true,
|
|
28
|
+
type: 'string',
|
|
29
|
+
description: 'URL (or path to a file) to convert to an AdGuard-syntax blocklist. Can be specified multiple times.',
|
|
30
|
+
})
|
|
31
|
+
.option('input-type', {
|
|
32
|
+
alias: 't',
|
|
33
|
+
type: 'string',
|
|
34
|
+
description: 'Type of the input file (/etc/hosts, adguard)',
|
|
35
|
+
nargs: 1,
|
|
36
|
+
})
|
|
18
37
|
.option('output', {
|
|
19
38
|
alias: 'o',
|
|
20
39
|
type: 'string',
|
|
@@ -26,25 +45,77 @@ const { argv } = require('yargs')
|
|
|
26
45
|
type: 'boolean',
|
|
27
46
|
description: 'Run with verbose logging',
|
|
28
47
|
})
|
|
29
|
-
.demandOption(['
|
|
48
|
+
.demandOption(['o'])
|
|
30
49
|
.version()
|
|
31
50
|
.help('h')
|
|
32
51
|
.alias('h', 'help');
|
|
33
52
|
|
|
34
53
|
if (argv.verbose) {
|
|
35
|
-
// trace level
|
|
54
|
+
// trace level.
|
|
36
55
|
consola.level = 5;
|
|
37
56
|
}
|
|
38
57
|
|
|
39
58
|
consola.info(`Starting ${packageJson.name} v${packageJson.version}`);
|
|
40
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Reads the configuration file and returns the parsed JSON.
|
|
62
|
+
*
|
|
63
|
+
* @returns {Promise<Object> | Promise<Error>} configuration file as JSON.
|
|
64
|
+
*/
|
|
65
|
+
async function readConfig() {
|
|
66
|
+
consola.debug(`Reading configuration from ${argv.config}`);
|
|
67
|
+
|
|
68
|
+
// Check if the configuration file exists.
|
|
69
|
+
await fs.access(argv.config);
|
|
70
|
+
const configStr = (await fs.readFile(argv.config)).toString();
|
|
71
|
+
|
|
72
|
+
return JSON.parse(configStr);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Creates a configuration object for the specified blocklist source.
|
|
77
|
+
* We assume that the input is a /etc/hosts file.
|
|
78
|
+
*
|
|
79
|
+
* @returns {Object} configuration object.
|
|
80
|
+
*/
|
|
81
|
+
function createConfig() {
|
|
82
|
+
const inputType = argv['input-type'] || 'hosts';
|
|
83
|
+
|
|
84
|
+
consola.debug(`Creating configuration for input ${argv.input} of type ${inputType}`);
|
|
85
|
+
|
|
86
|
+
const config = {
|
|
87
|
+
name: 'Blocklist',
|
|
88
|
+
sources: [],
|
|
89
|
+
transformations: [
|
|
90
|
+
'RemoveComments',
|
|
91
|
+
'Deduplicate',
|
|
92
|
+
'Compress',
|
|
93
|
+
'Validate',
|
|
94
|
+
'TrimLines',
|
|
95
|
+
'InsertFinalNewLine',
|
|
96
|
+
],
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
argv.input.forEach((input) => {
|
|
100
|
+
config.sources.push({
|
|
101
|
+
source: input,
|
|
102
|
+
type: inputType,
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
return config;
|
|
107
|
+
}
|
|
108
|
+
|
|
41
109
|
async function main() {
|
|
42
110
|
try {
|
|
43
|
-
|
|
44
|
-
|
|
111
|
+
if (!argv.input && !argv.config) {
|
|
112
|
+
throw new Error('Either --input or --config must be specified');
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const config = argv.input ? createConfig() : await readConfig();
|
|
116
|
+
|
|
117
|
+
consola.debug(`Configuration: ${JSON.stringify(config, 0, 4)}`);
|
|
45
118
|
|
|
46
|
-
const configStr = (await fs.readFile(argv.config)).toString();
|
|
47
|
-
const config = JSON.parse(configStr);
|
|
48
119
|
const lines = await compile(config);
|
|
49
120
|
|
|
50
121
|
consola.info(`Writing output to ${argv.output}`);
|