Skip to content

Commit

Permalink
0.46 improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
mrpond committed Feb 20, 2020
1 parent 58ad9b8 commit 8c08842
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 104 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<h4 align="center">A multi-purpose adblocker and skip bypass for the <strong>Windows</strong> Spotify Desktop Application.</h4>
<h5 align="center">Please support Spotify by purchasing premium</h5>
<p align="center">
<strong>Current Version:</strong> 0.45 <br>
<strong>Last updated:</strong> 14 February 2020<br>
<strong>Last tested version:</strong> 1.1.25.559.g85cf5e4c
<strong>Current Version:</strong> 0.46 <br>
<strong>Last updated:</strong> 20 February 2020<br>
<strong>Last tested version:</strong> 1.1.26.501.gbe11e53b
</p>
<h4 align="center">Important Notice(s)</h4>
<p align="center">
Expand Down
Binary file modified chrome_elf.zip
Binary file not shown.
75 changes: 35 additions & 40 deletions src/Adblock.cpp
Original file line number Diff line number Diff line change
@@ -1,73 +1,66 @@
#include "stdafx.h"

extern bool g_Log;
extern bool g_Skip_wpad;

std::ofstream Log_DNS;

Adblock g_Adsblock;
Adsblock g_Adsblock;

Adblock::Adblock () {
SecureZeroMemory (dns_ipaddr, sizeof (dns_ipaddr));
pSrvList = nullptr;
enable = false;
Adsblock::Adsblock () {
isActive = false;
pSrvList = (PIP4_ARRAY)LocalAlloc (LPTR, sizeof (IP4_ARRAY));
}

void Adblock::activate () {
enable = true;
}
void Adblock::deactivate () {
enable = false;
}
void Adsblock::setDNSIP (const char* ip) {

bool Adblock::init (const char* configFile) {
if (!isEnable ()) {
return false;
}
pSrvList = (PIP4_ARRAY)LocalAlloc (LPTR, sizeof (IP4_ARRAY));
if (nullptr != pSrvList) {
GetPrivateProfileString ("Config",
"AdGuardDNS_IP",
"176.103.130.134",
dns_ipaddr,
INET_ADDRSTRLEN,
configFile);

// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-inetptonw
if (1 == InetPton (AF_INET,
dns_ipaddr, // dns server ip
ip, // dns server ip
&pSrvList->AddrArray[0])) {
pSrvList->AddrCount = 1;
if (g_Log)
Log_DNS << "AdGuard DNS Server - " << dns_ipaddr << std::endl;
return true;
Log_DNS << "AdGuard DNS Server - " << ip << std::endl;
isActive = true;
}
else {
if (g_Log)
Log_DNS << "AdGuard DNS Disable - InetPton "
<< dns_ipaddr << " failed!" << std::endl;
<< ip << " failed!" << std::endl;
}
}
else {
if (g_Log)
Log_DNS << "AdGuard DNS Disable - "
<< "pSrvList LocalAlloc failed!" << std::endl;
Log_DNS << __FUNCTION__
<< " pSrvList LocalAlloc failed!" << std::endl;
}
return false;
}

bool Adblock::isblock (const char* nodename) {
bool Adsblock::isblock (const char* nodename) {

// Web Proxy Auto-Discovery (WPAD)
if (0 == _stricmp (nodename, "wpad"))
return g_Skip_wpad ? true : false;

if (nullptr != strstr (nodename, "google"))
return true;

if (nullptr != strstr (nodename, "doubleclick."))
return true;

// AdGuard DNS
if (isEnable ()) {
if (isActive) {
for (auto allow : whitelist) {
if (0 == _stricmp (allow.c_str (), nodename))
return false;
}

for (auto block : blacklist) {
if (0 == _stricmp (block.c_str (), nodename))
return true;
}

int result = adguardlookup (nodename);
int result = lookup (nodename);
if (1 == result) { // return 1 block
blacklist.push_back (nodename); // add to blacklist
return true;
Expand All @@ -80,7 +73,7 @@ bool Adblock::isblock (const char* nodename) {
return false;
}

int Adblock::adguardlookup (const char* nodename) {
int Adsblock::lookup (const char* nodename) {

bool isBlock = false;
DNS_STATUS dnsStatus = 0;
Expand All @@ -89,7 +82,7 @@ int Adblock::adguardlookup (const char* nodename) {
dnsStatus = DnsQuery (nodename,
DNS_TYPE_A,
DNS_QUERY_WIRE_ONLY,
pSrvList,
Adsblock::pSrvList,
&QueryResult,
NULL); // Reserved

Expand All @@ -106,7 +99,7 @@ int Adblock::adguardlookup (const char* nodename) {
}
else {
if (g_Log)
Log_DNS << "AdGuard DNS Error: " << dnsStatus
Log_DNS << __FUNCTION__ << " host:" << nodename << " status:" << dnsStatus
<< " GLE: " << GetLastError () << std::endl;
return -1;
}
Expand All @@ -117,10 +110,12 @@ int Adblock::adguardlookup (const char* nodename) {
return 0;
}

void Adblock::destroy () {
if (nullptr != pSrvList)
void Adsblock::destroy () {
if (nullptr != pSrvList) {
LocalFree (pSrvList);
pSrvList = nullptr;
}
}

Adblock::~Adblock () {
Adsblock::~Adsblock () {
}
16 changes: 6 additions & 10 deletions src/Adblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@
// blogs.msdn.microsoft.com/winsdk/2014/12/17/
// dnsquery-sample-to-loop-through-multiple-ip-addresses/

class Adblock {
class Adsblock {
public:
Adblock::Adblock ();
Adblock::~Adblock ();
bool init (const char* configFile);
Adsblock::Adsblock ();
Adsblock::~Adsblock ();
void setDNSIP (const char* ip);
bool isblock (const char* nodename);
bool isEnable () { return enable; };
void destroy ();
void activate ();
void deactivate ();

private:
bool enable;
PIP4_ARRAY pSrvList;
char dns_ipaddr[INET_ADDRSTRLEN];
bool isActive;
std::vector<std::string> blacklist;
std::vector<std::string> whitelist;
int adguardlookup (const char* nodename);
int lookup (const char* nodename);
};
4 changes: 2 additions & 2 deletions src/Resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US

VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,1
PRODUCTVERSION 0,44,0,0
PRODUCTVERSION 0,46,0,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -100,7 +100,7 @@ BEGIN
VALUE "LegalCopyright", "Copyright (C) 2019"
VALUE "OriginalFilename", "BlockTheSpot.dll"
VALUE "ProductName", "BlockTheSpot"
VALUE "ProductVersion", "0.44.0.0"
VALUE "ProductVersion", "0.46.0.0"
END
END
BLOCK "VarFileInfo"
Expand Down
45 changes: 21 additions & 24 deletions src/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,39 @@ extern bool g_Skip_wpad;
extern bool g_WinHttpReadDataFix;

extern std::ofstream Log_DNS;
extern std::ofstream Log_GetAddr;
extern std::ofstream Log_WinHttp;
extern Adblock g_Adsblock;
extern Adsblock g_Adsblock;

const char* configFile = "./config.ini";
void init_log () {
Log_DNS.open ("log_dnsquery.txt",
std::ios::out | std::ios::app);
Log_WinHttp.open ("log_winhttp.txt",
std::ios::out | std::ios::app);
}

void init_config () {
if (0 == GetPrivateProfileInt ("Config", "AdGuardDNS", 1, configFile))
g_Adsblock.deactivate ();
else
g_Adsblock.activate ();
const char* configFile = "./config.ini";
char IP[INET_ADDRSTRLEN];

if (1 == GetPrivateProfileInt ("Config", "Log", 0, configFile))
if (1 == GetPrivateProfileInt ("Config", "Log", 0, configFile)) {
g_Log = true;
init_log ();
}
if (1 == GetPrivateProfileInt ("Config", "Skip_wpad", 0, configFile))
g_Skip_wpad = true;
if (1 == GetPrivateProfileInt ("Config", "WinHttpReadDataFix", 0, configFile))
g_WinHttpReadDataFix = true;
}

void init_log () {
if (g_Log) {
Log_DNS.open ("log_dnsquery.txt",
std::ios::out | std::ios::app);
Log_GetAddr.open ("log_getaddrinfo.txt",
std::ios::out | std::ios::app);
Log_WinHttp.open ("log_winhttp.txt",
std::ios::out | std::ios::app);
if (1 == GetPrivateProfileInt ("Config", "AdGuardDNS", 1, configFile)) {
GetPrivateProfileString ("Config",
"AdGuardDNS_IP",
"176.103.130.134",
IP,
INET_ADDRSTRLEN,
configFile);
g_Adsblock.setDNSIP (IP);
}

}

BOOL APIENTRY DllMain (HMODULE hModule,
Expand All @@ -50,12 +54,6 @@ BOOL APIENTRY DllMain (HMODULE hModule,
{
case DLL_PROCESS_ATTACH:
init_config ();
init_log ();

if (false == g_Adsblock.init (configFile)) {
if (g_Log) Log_DNS << "AdGuard DNS Disable!" << std::endl;
g_Adsblock.deactivate ();
}

// block ads banner by hostname.
InstallHookApi ("ws2_32.dll", "getaddrinfo", getaddrinfohook);
Expand All @@ -65,7 +63,6 @@ BOOL APIENTRY DllMain (HMODULE hModule,
case DLL_PROCESS_DETACH:
if (g_Log) {
Log_DNS.close ();
Log_GetAddr.close ();
Log_WinHttp.close ();
}
g_Adsblock.destroy ();
Expand Down
40 changes: 15 additions & 25 deletions src/hosts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ bool g_Log = false;
bool g_Skip_wpad = false;
bool g_WinHttpReadDataFix = false;

std::ofstream Log_GetAddr;
std::ofstream Log_WinHttp;
extern std::ofstream Log_DNS;

extern Adblock g_Adsblock;
extern Adsblock g_Adsblock;

int WINAPI getaddrinfohook (DWORD RetAddr,
pfngetaddrinfo fngetaddrinfo,
Expand All @@ -16,34 +16,23 @@ int WINAPI getaddrinfohook (DWORD RetAddr,
const struct addrinfo* hints,
struct addrinfo** res)
{
auto adguardlookup = std::async (std::launch::async, &Adblock::isblock, g_Adsblock, nodename);

auto lookup = std::async (std::launch::async, &Adsblock::isblock, g_Adsblock, nodename);
// future/async
auto result = fngetaddrinfo (nodename,
servname,
hints,
res);

bool isBlock = adguardlookup.get ();

if (0 == result) { // GetAddrInfo return 0 on success
// Web Proxy Auto-Discovery (WPAD)
if (g_Skip_wpad && 0 == _stricmp (nodename, "wpad"))
return WSAHOST_NOT_FOUND;

if (nullptr != strstr (nodename, "google"))
return true;
bool isBlock = lookup.get ();
if (0 == result && isBlock) { // GetAddrInfo return 0 on success

if (nullptr != strstr (nodename, "doubleclick."))
return true;

if (isBlock) {
for (auto ptr = *res; nullptr != ptr; ptr = ptr->ai_next) {
auto ipv4 = (struct sockaddr_in*)ptr->ai_addr;
ipv4->sin_addr.S_un.S_addr = INADDR_ANY;
}
if (g_Log) {
Log_GetAddr << nodename << " blocked" << std::endl;
}
for (auto ptr = *res; nullptr != ptr; ptr = ptr->ai_next) {
auto ipv4 = (struct sockaddr_in*)ptr->ai_addr;
//memset (&ipv4->sin_addr.S_un.S_addr, 0x0, sizeof ULONG);
ipv4->sin_addr.S_un.S_addr = INADDR_ANY;
}
if (g_Log) {
Log_DNS << nodename << " blocked" << std::endl;
}
}
return result;
Expand Down Expand Up @@ -79,7 +68,8 @@ int WINAPI winhttpreaddatahook (DWORD RetAddr,
Log_WinHttp << data << std::endl;
}
if (g_WinHttpReadDataFix) return false;
memset (lpBuffer, 0x20, dwNumberOfBytesToRead);
memset (lpBuffer, 0x0, dwNumberOfBytesToRead);
//SecureZeroMemory (lpBuffer, dwNumberOfBytesToRead);
return true;
}

0 comments on commit 8c08842

Please sign in to comment.