File indexing completed on 2024-11-24 04:54:31

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 /**
0006  * Example invocations:
0007  * Basic checking a URL:
0008  *   node scripts/check.js  --host www.cnet.com --location https://s0.2mdn.net/instream/html5/ima3.js
0009  * Checking with a particular resource type:
0010  *   node scripts/check.js --host www.scrumpoker.online --location https://www.scrumpoker.online/js/angular-google-analytics.js -O script
0011  * Checking a URL with discovery:
0012  *   node scripts/check.js  --host www.cnet.com --location "https://slashdot.org?t=1&ad_box_=2" --discover
0013  * Checking a URL against a particular adblock list:
0014  *   node scripts/check.js  --uuid 03F91310-9244-40FA-BCF6-DA31B832F34D --host slashdot.org --location https://s.yimg.jp/images/ds/ult/toppage/rapidjp-1.0.0.js
0015  * Checking a URL from a loaded DAT file:
0016  *   node scripts/check.js --dat ./out/SafeBrowsingData.dat --host excellentmovies.net --location https://excellentmovies.net
0017  * Checking a URL from a list URL:
0018  *   node scripts/check.js --http https://easylist-downloads.adblockplus.org/easylist.txt --host excellentmovies.net --location http://simple-adblock.com/adblocktest/files/adbanner.gif
0019  * Checking a list of URLs:
0020  *   node scripts/check.js  --host www.cnet.com --list ./test/data/sitelist.txt
0021  * Checking a list of URLS with discovery:
0022  *  node scripts/check.js  --host www.cnet.com --list ./test/data/sitelist.txt --discover
0023  * Get stats for a particular adblock list:
0024  *   node scripts/check.js  --uuid 67F880F5-7602-4042-8A3D-01481FD7437A --stats
0025 */
0026 const commander = require('commander')
0027 const { makeAdBlockClientFromListUUID, makeAdBlockClientFromDATFile, makeAdBlockClientFromListURL, makeAdBlockClientFromString, makeAdBlockClientFromFilePath, readSiteList } = require('../lib/util')
0028 const { FilterOptions } = require('..')
0029 
0030 const filterStringToFilterOption = (val) => FilterOptions[val]
0031 
0032 commander
0033   .option('-u, --uuid [uuid]', 'UUID of the list to use')
0034   .option('-d, --dat [dat]', 'file path of the adblock .dat file')
0035   .option('-f, --filter [filter]', 'Brave filter rules')
0036   .option('-F, --filter-path [filterPath]', 'Brave filter rules file path')
0037   .option('-w, --http [http]', 'Web filter to use')
0038   .option('-h, --host [host]', 'host of the page that is being loaded')
0039   .option('-l, --location [location]', 'URL to use for the check')
0040   .option('-o, --output [output]', 'Optionally saves a DAT file')
0041   .option('-L, --list [list]', 'Filename for list of sites to check')
0042   .option('-D, --discover', 'If specified does filter discovery for matched filter')
0043   .option('-s, --stats', 'If specified outputs parsing stats')
0044   .option('-C, --cache', 'Optionally cache results and use cached results')
0045   .option('-O, --filter-option [filterOption]', 'Filter option to use', filterStringToFilterOption, FilterOptions.noFilterOption)
0046   .parse(process.argv)
0047 
0048 let p = Promise.reject(new Error('Usage: node check.js --location <location> --host <host> [--uuid <uuid>]'))
0049 
0050 const ruleDiscovery = commander.discover && !commander.dat
0051 const parseOptions = {
0052   keepRuleText: !!ruleDiscovery
0053 }
0054 
0055 if ((commander.host && (commander.location || commander.list)) || commander.stats) {
0056   p.catch(() => {})
0057   if (commander.uuid) {
0058     p = makeAdBlockClientFromListUUID(commander.uuid, parseOptions)
0059   } else if (commander.dat) {
0060     if (ruleDiscovery) {
0061       console.log('Note, rule discovery is not supported when reading from DAT files')
0062     }
0063     p = makeAdBlockClientFromDATFile(commander.dat)
0064   } else if (commander.http) {
0065     p = makeAdBlockClientFromListURL(commander.http, undefined, parseOptions)
0066   } else if (commander.filter) {
0067     p = makeAdBlockClientFromString(commander.filter, parseOptions)
0068   } else if (commander.filterPath) {
0069     p = makeAdBlockClientFromFilePath(commander.filterPath, parseOptions)
0070   } else {
0071     const defaultLists = require('../').adBlockLists.default
0072       .map((listObj) => listObj.listURL)
0073     p = makeAdBlockClientFromListURL(defaultLists, undefined, parseOptions)
0074   }
0075 }
0076 
0077 p.then((adBlockClient) => {
0078   if (commander.stats) {
0079     console.log('Parsing stats:', adBlockClient.getParsingStats())
0080     return
0081   }
0082   if (commander.location) {
0083     console.log('params:', commander.location, commander.filterOption, commander.host)
0084     if (commander.discover) {
0085       console.log(adBlockClient.findMatchingFilters(commander.location, commander.filterOption, commander.host))
0086     } else {
0087       console.log('Matches: ', adBlockClient.matches(commander.location, commander.filterOption, commander.host))
0088     }
0089     console.log(adBlockClient.getMatchingStats())
0090   } else {
0091     const siteList = readSiteList(commander.list)
0092     let matchCount = 0
0093     let skipCount = 0
0094     console.time('check')
0095     if (commander.discover) {
0096       const m = new Map()
0097       siteList.forEach((site) => {
0098         if (commander.cache && m.has(site)) {
0099           if (m.get(site)) {
0100             matchCount++
0101           } else {
0102             skipCount++
0103           }
0104           return
0105         }
0106         if (adBlockClient.findMatchingFilters(site, commander.filterOption, commander.host)) {
0107           matchCount++
0108           m.set(site, true)
0109         } else {
0110           skipCount++
0111           m.set(site, false)
0112         }
0113       })
0114     } else {
0115       siteList.forEach((site) => {
0116         if (adBlockClient.matches(site, commander.filterOption, commander.host)) {
0117           matchCount++
0118         } else {
0119           skipCount++
0120         }
0121       })
0122     }
0123     console.timeEnd('check')
0124     console.log(adBlockClient.getMatchingStats())
0125     console.log('Matching:', matchCount)
0126     console.log('Skipped:', skipCount)
0127   }
0128   if (commander.output) {
0129     require('fs').writeFileSync(commander.output, adBlockClient.serialize())
0130   }
0131 }).catch((e) => {
0132   console.log('Error:', e)
0133 })