| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 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 |
} |