Debian Codegraph @

resources/scripts/adblock/adblock-server.js

raw symbols SCIP
1 // Simple local HTTP server providing ad-blocking functionality via https://github.com/ghostery/adblocker
2 //
3 // How to install:
4 // npm i @ghostery/adblocker
5 //
6 // How to run:
7 // NODE_PATH="..." node ./adblock-server.js "<port>" "<filters-file-path>"
8 //
9 // How to use:
10 // curl -i -X POST --data '
11 // {
12 // "url": "http://gompoozu.net/cwqcwq/js.js",
13 // "fp_url": "http://bbc.com",
14 // "url_type": "main_frame",
15 // "filter": true,
16 // "cosmetic": true
17 // }' 'http://localhost:<port>'
18
19 const fs = require('fs');
20 const tldts = require('tldts-experimental');
21 const adblock = require('@ghostery/adblocker');
22 const http = require('http');
23 const cluster = require('cluster');
24
25 const numCPUs = require('os').cpus().length;
26 const port = process.argv[2];
27 const filtersFile = process.argv[3];
28 const engine = adblock.FiltersEngine.parse(fs.readFileSync(filtersFile, 'utf-8'));
29 const hostname = '127.0.0.1';
30
31 if (cluster.isPrimary) {
32 console.log(`Primary ${process.pid} is running`);
33
34 // Fork workers.
35 for (let i = 0; i < numCPUs; i++) {
36 cluster.fork();
37 }
38
39 cluster.on('exit', (worker, code, signal) => {
40 console.log(`worker ${worker.process.pid} died`);
41 });
42 }
43 else {
44 const server = http.createServer((req, res) => {
45 try {
46 console.log(new Date());
47
48 const chunks = [];
49 req.on('data', chunk => chunks.push(chunk));
50 req.on('end', () => {
51 console.log(new Date());
52
53 try {
54 const jsonData = Buffer.concat(chunks);
55 const jsonStruct = JSON.parse(jsonData.toString());
56
57 const askUrl = jsonStruct['url'];
58 const askFpUrl = jsonStruct['fp_url'];
59 const askFilter = jsonStruct['filter'];
60 const askCosmetic = jsonStruct['cosmetic'];
61 const askUrlType = jsonStruct['url_type'];
62 const fullUrl = new URL(askUrl);
63
64 resultJson = {};
65
66 if (askFilter) {
67 const adblockMatch = engine.match(adblock.Request.fromRawDetails({
68 type: askUrlType,
69 url: askUrl,
70 sourceUrl: askFpUrl
71 }));
72
73 resultJson["filter"] = adblockMatch;
74 console.log(`adblocker: Filter is:\n${JSON.stringify(adblockMatch)}.`)
75 }
76
77 if (askCosmetic) {
78 const adblockCosmetic = engine.getCosmeticsFilters({
79 url: askUrl,
80 hostname: fullUrl.hostname,
81 domain: tldts.getDomain(fullUrl.hostname)
82 });
83
84 resultJson["cosmetic"] = adblockCosmetic;
85 console.log(`adblocker: Cosmetic is:\n${JSON.stringify(adblockCosmetic)}.`)
86 }
87
88 res.statusCode = 200;
89 res.setHeader('Content-Type', 'application/json');
90 res.end(JSON.stringify(resultJson));
91
92 console.log(new Date());
93 }
94 catch (inner_error) {
95 console.error(`adblocker: ${inner_error}.`);
96
97 res.statusCode = 500;
98 res.setHeader('Content-Type', 'text/plain');
99 res.end(String(inner_error));
100 }
101 })
102 }
103 catch (error) {
104 console.error(`adblocker: ${inner_error}.`);
105
106 res.statusCode = 500;
107 res.setHeader('Content-Type', 'text/plain');
108 res.end(String(error));
109 }
110 });
111
112 server.listen(port, hostname, () => {
113 console.log(`adblocker: Server started at local port ${port}.`);
114 });
115 }