File indexing completed on 2024-11-17 04:55:18

0001 /*
0002     SPDX-License-Identifier: MPL-2.0
0003 */
0004 
0005 /* Copyright (c) 2015 Brian R. Bondy. Distributed under the MPL2 license.
0006  * This Source Code Form is subject to the terms of the Mozilla Public
0007  * License, v. 2.0. If a copy of the MPL was not distributed with this
0008  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
0009 
0010 #include <time.h>
0011 #include <cerrno>
0012 #include <algorithm>
0013 #include <iostream>
0014 #include <fstream>
0015 #include <sstream>
0016 #include <string>
0017 #include <vector>
0018 #include <iterator>
0019 #include "./ad_block_client.h"
0020 #include "./bad_fingerprint.h"
0021 
0022 using std::string;
0023 using std::cout;
0024 using std::endl;
0025 
0026 string getFileContents(const char *filename) {
0027   std::ifstream in(filename, std::ios::in);
0028   if (in) {
0029     std::ostringstream contents;
0030     contents << in.rdbuf();
0031     in.close();
0032     return(contents.str());
0033   }
0034   throw(errno);
0035 }
0036 
0037 void doSiteList(AdBlockClient *pClient, bool outputPerf) {
0038   AdBlockClient &client = *pClient;
0039   std::string && siteList = getFileContents("./test/data/sitelist.txt");
0040   std::stringstream ss(siteList);
0041   std::istream_iterator<std::string> begin(ss);
0042   std::istream_iterator<std::string> end;
0043   std::vector<std::string> sites(begin, end);
0044 
0045   // This is the site who's URLs are being checked, not the domain of
0046   // the URL being checked.
0047   const char *currentPageDomain = "brianbondy.com";
0048 
0049   int numBlocks = 0;
0050   int numSkips = 0;
0051   const clock_t beginTime = clock();
0052   std::for_each(sites.begin(), sites.end(), [&client, currentPageDomain,
0053       &numBlocks, &numSkips](std::string const &urlToCheck) {
0054     if (client.matches(urlToCheck.c_str(), FONoFilterOption,
0055           currentPageDomain)) {
0056       ++numBlocks;
0057     } else {
0058       ++numSkips;
0059     }
0060   });
0061   if (outputPerf) {
0062     cout << "Time: " << float(clock() - beginTime)
0063       / CLOCKS_PER_SEC << "s" << endl;
0064     cout << "num blocks: " << numBlocks << ", num skips: " << numSkips << endl;
0065     cout << "False Positives: " << client.numFalsePositives
0066       << ", exception false positives: "
0067       << client.numExceptionFalsePositives << endl;
0068     cout << "Bloom filter saves: " << client.numBloomFilterSaves
0069       << ", exception bloom filter saves: "
0070       << client.numExceptionBloomFilterSaves << endl;
0071   }
0072 }
0073 
0074 int main(int argc, char**argv) {
0075   std::string && easyListTxt =
0076     getFileContents("./test/data/easylist.txt");
0077   std::string && easyPrivacyTxt =
0078     getFileContents("./test/data/easyprivacy.txt");
0079   std::string && braveUnblockTxt =
0080     getFileContents("./test/data/brave-unbreak.txt");
0081   std::string && ublockUnblockTxt =
0082     getFileContents("./test/data/ublock-unbreak.txt");
0083   std::string && spam404MainBlacklistTxt =
0084     getFileContents("./test/data/spam404-main-blacklist.txt");
0085   std::string && disconnectSimpleMalwareTxt =
0086     getFileContents("./test/data/disconnect-simple-malware.txt");
0087 
0088   cout << endl
0089     << "-------------\n"
0090     << "  AD BLOCK   \n"
0091     << "-------------\n"
0092     << endl;
0093 
0094   AdBlockClient adBlockClient;
0095   adBlockClient.parse(easyListTxt.c_str());
0096   adBlockClient.parse(easyPrivacyTxt.c_str());
0097   adBlockClient.parse(ublockUnblockTxt.c_str());
0098   adBlockClient.parse(braveUnblockTxt.c_str());
0099   int size;
0100   char* data = adBlockClient.serialize(&size);
0101   adBlockClient.deserialize(data);
0102   doSiteList(&adBlockClient, true);
0103 
0104   cout << endl
0105     << "-------------\n"
0106     << "SAFE BROWSING\n"
0107     << "-------------\n"
0108     << endl;
0109 
0110   AdBlockClient safeBrowsingClient;
0111   safeBrowsingClient.parse(spam404MainBlacklistTxt.c_str());
0112   safeBrowsingClient.parse(disconnectSimpleMalwareTxt.c_str());
0113   doSiteList(&safeBrowsingClient, true);
0114 
0115   cout << endl
0116     << "-------------\n"
0117     << "generating bad fingerprints list"
0118     << endl;
0119 
0120   AdBlockClient allClient;
0121   allClient.enableBadFingerprintDetection();
0122   allClient.parse(easyListTxt.c_str());
0123   allClient.parse(easyPrivacyTxt.c_str());
0124   allClient.parse(ublockUnblockTxt.c_str());
0125   allClient.parse(braveUnblockTxt.c_str());
0126   allClient.parse(spam404MainBlacklistTxt.c_str());
0127   allClient.parse(disconnectSimpleMalwareTxt.c_str());
0128   doSiteList(&allClient, false);
0129   allClient.badFingerprintsHashSet->generateHeader("bad_fingerprints.h");
0130 
0131   return 0;
0132 }