Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
iostream authored and iostream committed Mar 25, 2021
1 parent 93dcfee commit d1ded15
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
16 changes: 9 additions & 7 deletions src/Config.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#pragma once
#include <string>
#include <array>
#include <map>
#include <string>

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) {
Expand All @@ -22,9 +24,9 @@ class Config {
private:
std::string_view m_filename;
std::map<std::string_view, bool> 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<std::string_view, 3> 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;
Expand Down
3 changes: 1 addition & 2 deletions src/Logger.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include "stdafx.h"
#include <fstream>
#include <string>
#include "Config.h"
Expand All @@ -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;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
29 changes: 17 additions & 12 deletions src/hosts.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <array>
#include <future>
#include "hosts.h"
#include "Config.h"
#include "Logger.h"
Expand All @@ -7,15 +9,21 @@

_getaddrinfo getaddrinfo_orig;

auto *config = new Config ();
auto *logger = new Logger (config);
static constexpr std::array<std::string_view, 2> dnscheck = { "dns.google", "cloudflare" };
static constexpr std::array<std::string_view, 4> 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<std::string_view> 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;
Expand All @@ -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<std::string_view> 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 ()) {
Expand All @@ -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");
}
Expand Down
2 changes: 0 additions & 2 deletions src/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

// TODO: reference additional headers your program requires here
#include <Psapi.h>
#include <future>
#include <vector>

#include "mhook-lib/mhook.h"

Expand Down

0 comments on commit d1ded15

Please sign in to comment.