Skip to content

Commit

Permalink
v0.40 code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
mrpond committed Jan 5, 2020
1 parent 57d35a0 commit 59eac0e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 57 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<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.39 <br>
<strong>Last updated:</strong> 27 December 2019 <br>
<strong>Current Version:</strong> 0.40 <br>
<strong>Last updated:</strong> 5 January 2020<br>
<strong>Last tested version:</strong> 1.1.22.633.g1bab253a
</p>
<h4 align="center">Important Notice(s)</h4>
Expand Down
Binary file modified chrome_elf.zip
Binary file not shown.
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,39,0,0
PRODUCTVERSION 0,40,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.39.0.0"
VALUE "ProductVersion", "0.40.0.0"
END
END
BLOCK "VarFileInfo"
Expand Down
58 changes: 32 additions & 26 deletions src/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ extern bool g_WinHttpReadDataFix;
extern std::ofstream Log_DNS;
extern std::ofstream Log_GetAddr;
extern std::ofstream Log_WinHttp;
extern std::vector<std::string> blacklist;

PIP4_ARRAY pSrvList = NULL;
PIP4_ARRAY pSrvList = nullptr;

void Init_config () {
void init_config () {
if (0 == GetPrivateProfileInt ("Config", "AdGuardDNS", 1, "./config.ini"))
g_UseAdGuard = false;
if (0 < GetPrivateProfileInt ("Config", "Log", 0, "./config.ini"))
Expand All @@ -23,7 +24,7 @@ void Init_config () {
g_WinHttpReadDataFix = true;
}

void Init_log () {
void init_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);
Expand All @@ -32,51 +33,56 @@ void Init_log () {
Log_DNS << "AdGuard DNS Disable!\n";
}

void Init_DNS () {
void init_DNS () {
pSrvList = (PIP4_ARRAY)LocalAlloc (LPTR, sizeof (IP4_ARRAY));
if (pSrvList) {
if (nullptr != pSrvList) {
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-inetptonw
if (1 == InetPton (AF_INET,
"176.103.130.134", // dns server ip
&pSrvList->AddrArray[0])) {
// "Family protection"
// adguard.com/en/adguard-dns/overview.html
pSrvList->AddrCount = 1;
return;
}
}
//
g_UseAdGuard = false;
else {
if (g_Log)
Log_DNS << "AdGuard DNS Disable - pSrvList LocalAlloc failed!\n";
g_UseAdGuard = false;
}
}

BOOL APIENTRY DllMain (HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
// only utility process and main process
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
if (strstr (GetCommandLine (), "--type=utility") || !strstr (GetCommandLine (), "--type=")) {
Init_config ();
if (g_UseAdGuard) Init_DNS ();
if (g_Log) Init_log ();
if (strstr (GetCommandLine (), "--type=utility") ||
!strstr (GetCommandLine (), "--type=")) {

switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
init_config ();
if (g_Log) init_log ();
if (g_UseAdGuard) init_DNS ();
// Web Proxy Auto-Discovery (WPAD)
if (g_Skip_wpad) blacklist.push_back ("wpad");
// block ads banner by hostname.
InstallHookApi ("ws2_32.dll", "getaddrinfo", getaddrinfohook);
// block ads by manipulate json response.
InstallHookApi ("Winhttp.dll", "WinHttpReadData", winhttpreaddatahook);
break;
case DLL_PROCESS_DETACH:
if (g_Log) {
Log_DNS.close ();
Log_GetAddr.close ();
Log_WinHttp.close ();
}
if (nullptr != pSrvList)
LocalFree (pSrvList);
break;
}
break;
case DLL_PROCESS_DETACH:
if (g_Log) {
Log_DNS.close ();
Log_GetAddr.close ();
Log_WinHttp.close ();
}
if (pSrvList)
LocalFree (pSrvList);
break;
}
return TRUE;
}
Expand Down
53 changes: 26 additions & 27 deletions src/hosts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bool adguard_dnsblock (const char* nodename) {
bool isBlock = false;
static int fail_count = 0;
if (!g_UseAdGuard) return false;

if (fail_count > 5) {
if (g_Log) {
Log_DNS << "AdGuard DNS lookup disable! fail resolve > 5 times" << '\n';
Expand All @@ -33,15 +33,6 @@ bool adguard_dnsblock (const char* nodename) {
return false;
}

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

dnsStatus = DnsQuery (nodename,
DNS_TYPE_A,
DNS_QUERY_WIRE_ONLY,
Expand All @@ -54,16 +45,13 @@ bool adguard_dnsblock (const char* nodename) {
for (auto p = QueryResult; p; p = p->pNext) {
if (0 == p->Data.A.IpAddress) {
isBlock = true; // AdGuard Block
blacklist.push_back (nodename); // add to blacklist
break; // no more processing
}
}
DnsRecordListFree (QueryResult, DnsFreeRecordList);

if (!isBlock)
whitelist.push_back (nodename); // add to whitelist
DnsRecordListFree (QueryResult, DnsFreeRecordList);
} // QueryResult
} else { // dnsStatus
}
else { // dnsStatus
fail_count++;
}
if (g_Log && isBlock) {
Expand All @@ -80,25 +68,36 @@ int WINAPI getaddrinfohook (DWORD RetAddr,
const struct addrinfo* hints,
struct addrinfo** res)
{

auto result = fngetaddrinfo (nodename,
servname,
hints,
res);

if (0 == result) { // GetAddrInfo return 0 on success
if (NULL != strstr (nodename, "google"))

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

// Web Proxy Auto-Discovery (WPAD)
if (0 == _stricmp (nodename, "wpad"))
return g_Skip_wpad ? WSANO_RECOVERY : result;
for (auto block : blacklist) {
if (0 == _stricmp (block.c_str (), nodename))
return WSANO_RECOVERY;
}

for (auto allow : whitelist) {
if (0 == _stricmp (allow.c_str (), nodename))
return result;
}

// AdGuard DNS
if (adguard_dnsblock (nodename))
if (adguard_dnsblock (nodename)) {
blacklist.push_back (nodename); // add to blacklist
return WSANO_RECOVERY;

if (g_Log) {
Log_GetAddr << nodename << '\n';
}
else {
whitelist.push_back (nodename); // add to whitelist
if (g_Log) {
Log_GetAddr << nodename << '\n';
}
}
}
return result;
Expand All @@ -120,12 +119,12 @@ int WINAPI winhttpreaddatahook (DWORD RetAddr,
}

char* pdest = strstr ((LPSTR)lpBuffer, "{\"login_url");
if (pdest != NULL) {
if (nullptr != pdest) {
return true;
}

pdest = strstr ((LPSTR)lpBuffer, "{\"credentials");
if (pdest != NULL) {
if (nullptr != pdest) {
return true;
}
if (g_Log) {
Expand Down

0 comments on commit 59eac0e

Please sign in to comment.