File indexing completed on 2024-11-17 04:55:14
0001 /* This Source Code Form is subject to the terms of the Mozilla Public 0002 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 0003 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 0004 0005 const { AdBlockClient, FilterOptions } = require('..') 0006 const path = require('path') 0007 const fs = require('fs') 0008 const request = require('request') 0009 const braveUnbreakPath = './test/data/brave-unbreak.txt' 0010 const { getListBufferFromURL, getListFilterFunction } = require('../lib/util') 0011 const { adBlockLists } = require('..') 0012 0013 let totalExceptionFalsePositives = 0 0014 let totalNumFalsePositives = 0 0015 let totalTime = 0 0016 0017 const generateDataFileFromString = (filterRuleData, outputDATFilename) => { 0018 const client = new AdBlockClient() 0019 if (filterRuleData.constructor === Array) { 0020 filterRuleData.forEach(filterRuleDataItem => client.parse(filterRuleDataItem)) 0021 } else { 0022 client.parse(filterRuleData) 0023 } 0024 0025 console.log('Parsing stats:', client.getParsingStats()) 0026 client.enableBadFingerprintDetection() 0027 checkSiteList(client, top500URLList20k) 0028 client.generateBadFingerprintsHeader('bad_fingerprints.h') 0029 const serializedData = client.serialize() 0030 if (!fs.existsSync('out')) { 0031 fs.mkdirSync('./out') 0032 } 0033 fs.writeFileSync(path.join('out', outputDATFilename), serializedData) 0034 } 0035 0036 const generateDataFileFromURL = (listURL, outputDATFilename, filter) => { 0037 return new Promise((resolve, reject) => { 0038 console.log(`${listURL}...`) 0039 request.get(listURL, function (error, response, body) { 0040 if (error) { 0041 reject(new Error(`Request error: ${error}`)) 0042 return 0043 } 0044 if (response.statusCode !== 200) { 0045 reject(new Error(`Error status code ${response.statusCode} returned for URL: ${listURL}`)) 0046 return 0047 } 0048 const braveUnbreakBody = fs.readFileSync(braveUnbreakPath, 'utf8') 0049 if (filter) { 0050 body = filter(body) 0051 } 0052 generateDataFileFromString([body, braveUnbreakBody], outputDATFilename) 0053 resolve() 0054 }) 0055 }) 0056 } 0057 0058 const generateDataFilesForAllRegions = () => { 0059 let p = Promise.resolve() 0060 adBlockLists.regions.forEach((region) => { 0061 p = p.then(generateDataFileFromURL.bind(null, region.listURL, `${region.uuid}.dat`)) 0062 }) 0063 p = p.then(() => { 0064 console.log(`Total time: ${totalTime / 1000}s ${totalTime % 1000}ms`) 0065 console.log(`Num false positives: ${totalNumFalsePositives}`) 0066 console.log(`Num exception false positives: ${totalExceptionFalsePositives}`) 0067 }) 0068 return p 0069 } 0070 0071 const generateDataFilesForList = (lists, filename) => { 0072 let promises = [] 0073 lists.forEach((l) => { 0074 console.log(`${l.listURL}...`) 0075 const filterFn = getListFilterFunction(l.uuid) 0076 promises.push(getListBufferFromURL(l.listURL, filterFn)) 0077 }) 0078 let p = Promise.all(promises) 0079 p = p.then((listBuffers) => { 0080 generateDataFileFromString(listBuffers, filename) 0081 }) 0082 return p 0083 } 0084 0085 const generateDataFilesForMalware = generateDataFilesForList.bind(null, adBlockLists.malware, 'SafeBrowsingData.dat') 0086 const generateDataFilesForDefaultAdblock = generateDataFilesForList.bind(null, adBlockLists.default, 'ABPFilterParserData.dat') 0087 0088 const checkSiteList = (client, siteList) => { 0089 const start = new Date().getTime() 0090 siteList.forEach(site => { 0091 // console.log('matches: ', client.matches(site, FilterOptions.image, 'slashdot.org')) 0092 client.matches(site, FilterOptions.noFilterOption, 'slashdot.org') 0093 }) 0094 const stats = client.getMatchingStats() 0095 console.log('Matching stats:', stats) 0096 totalNumFalsePositives += stats.numFalsePositives 0097 totalExceptionFalsePositives += stats.numExceptionFalsePositives 0098 const end = new Date().getTime() 0099 const time = end - start 0100 totalTime += time 0101 console.log('done, time: ', time, 'ms') 0102 } 0103 0104 const top500URLList20k = fs.readFileSync('./test/data/sitelist.txt', 'utf8').split('\n') 0105 // const shortURLList = fs.readFileSync('./test/data/short-sitelist.txt', 'utf8').split('\n') 0106 0107 generateDataFilesForDefaultAdblock() 0108 .then(generateDataFilesForMalware) 0109 .then(generateDataFilesForAllRegions) 0110 .then(() => { 0111 console.log('Thank you for updating the data files, don\'t forget to upload them too!') 0112 }) 0113 .catch(() => { 0114 console.error('Something went wrong, aborting!') 0115 process.exit(1) 0116 }) 0117 0118 process.on('uncaughtException', (err) => { 0119 console.error('Caught exception:', err) 0120 process.exit(1) 0121 }) 0122 0123 process.on('unhandledRejection', (err) => { 0124 console.error('Unhandled rejection:', err) 0125 process.exit(1) 0126 })