diff --git a/src/Config.h b/src/Config.h index 60805faa..68485c50 100644 --- a/src/Config.h +++ b/src/Config.h @@ -1,15 +1,17 @@ #pragma once -#include +#include #include +#include class Config { public: Config () { m_filename = "./config.ini"; - m_data["Log"] = read ("Config", "Log"); - m_data["Skip_wpad"] = read ("Config", "Skip_wpad"); - m_data["Block_BannerOnly"] = read ("Config", "Block_BannerOnly"); + for (auto &conf : config_data) { + m_data[conf] = read ("Config", conf); + } + } bool getConfig (std::string_view key) { @@ -22,9 +24,9 @@ class Config { private: std::string_view m_filename; std::map m_data; - - bool read (const char* app, const char* key, int def_value = 0) { - if (1 == GetPrivateProfileInt (app, key, def_value, m_filename.data())) { + static constexpr std::array config_data = { "Log", "Skip_wpad", "Block_BannerOnly" }; + bool read (std::string_view app, std::string_view key, int def_value = 0) { + if (1 == GetPrivateProfileInt (app.data(), key.data(), def_value, m_filename.data())) { return true; } return false; diff --git a/src/Logger.h b/src/Logger.h index 707e0b7c..61d43059 100644 --- a/src/Logger.h +++ b/src/Logger.h @@ -1,5 +1,4 @@ #pragma once -#include "stdafx.h" #include #include #include "Config.h" @@ -12,7 +11,7 @@ class Logger { m_active = config->getConfig ("Log"); if (m_active) { m_log.open ("blockthespot_log.txt", std::ios::out | std::ios::app); - m_log << "BlockTheSpot - Build date: " << __TIMESTAMP__ << std::endl; + //m_log << "BlockTheSpot - Build date: " << __TIMESTAMP__ << std::endl; } } diff --git a/src/dllmain.cpp b/src/dllmain.cpp index c3c28bfd..b1ee245c 100644 --- a/src/dllmain.cpp +++ b/src/dllmain.cpp @@ -17,7 +17,7 @@ BOOL APIENTRY DllMain (HMODULE hModule, switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: - auto* config = new Config(); + auto *config = new Config(); if (std::string_view::npos == procname.find ("--type=") && false == config->getConfig("Block_BannerOnly")) { // block ads request - main process CreateThread (NULL, NULL, KillBanner, config, 0, NULL); @@ -26,7 +26,7 @@ BOOL APIENTRY DllMain (HMODULE hModule, else if (std::string_view::npos != procname.find ("--type=utility")) { // block ads banner by hostname - utility process //g_Logger.setLogfile ("utility_log.txt"); - auto* log = new Logger (config); + auto *log = new Logger (config); getaddrinfo_orig = getaddrinfo; if (getaddrinfo_orig) { Mhook_SetHook ((PVOID*)&getaddrinfo_orig, getaddrinfo_hook); diff --git a/src/hosts.cpp b/src/hosts.cpp index 5ea55c81..efe61ceb 100644 --- a/src/hosts.cpp +++ b/src/hosts.cpp @@ -1,3 +1,5 @@ +#include +#include #include "hosts.h" #include "Config.h" #include "Logger.h" @@ -7,15 +9,21 @@ _getaddrinfo getaddrinfo_orig; +auto *config = new Config (); +auto *logger = new Logger (config); +static constexpr std::array dnscheck = { "dns.google", "cloudflare" }; +static constexpr std::array blockList = { "google", "doubleclick", "qualaroo.com", "fbsbx.com" }; +static const bool wpad = config->getConfig ("Skip_wpad"); + // check if ads hostname -bool is_blockhost (std::string_view nodename, Config* config) { - static bool wpad = config->getConfig ("Skip_wpad"); - static const std::vector blockList = { "google", "doubleclick", "qualaroo.com", "fbsbx.com", /*"cloudflare"*/ }; +bool is_blockhost (std::string_view nodename) { + //static bool wpad = config->getConfig ("Skip_wpad"); + if (0 == nodename.compare ("wpad")) return wpad ? true : false; - for (auto &i : blockList) { - if (std::string_view::npos != nodename.find (i)) + for (auto &hostname : blockList) { + if (std::string_view::npos != nodename.find (hostname)) return true; } return false; @@ -27,16 +35,13 @@ int WSAAPI getaddrinfo_hook ( _In_opt_ const ADDRINFOA* hints, _Out_ PADDRINFOA* res) { - static auto* config = new Config (); - static auto* logger = new Logger (config); - static const std::vector dnscheck = { "dns.google", "cloudflare" }; if (nodename == nullptr) return getaddrinfo_orig (nodename, servname, hints, res); std::string nnodename (nodename); - auto isblock = std::async (std::launch::async, is_blockhost, nnodename, config); + auto isblock = std::async (std::launch::async, is_blockhost, nnodename); auto result = getaddrinfo_orig (nodename, servname, hints, res); if (0 == result) { if (isblock.get ()) { @@ -51,10 +56,10 @@ int WSAAPI getaddrinfo_hook ( } } if (true == logger->is_active() && - true == config->getConfig ("Skip_wpad")) + true == wpad) { - for (auto& i : dnscheck) { - if (std::string_view::npos != nnodename.find (i)) + for (auto &hostname : dnscheck) { + if (std::string_view::npos != nnodename.find (hostname)) logger->Log ("custom dns currently in use - " + nnodename + " turn on Skip_wpad in config.ini or switch to adguard dns"); } diff --git a/src/stdafx.h b/src/stdafx.h index 648a2f57..566c6f79 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -14,8 +14,6 @@ // TODO: reference additional headers your program requires here #include -#include -#include #include "mhook-lib/mhook.h"