diff --git a/README.md b/README.md index 41bbb84..6d5b40a 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,9 @@

A multi-purpose adblocker and skip bypass for the Windows Spotify Desktop Application.

Please support Spotify by purchasing premium

- Current Version: 0.38
- Last updated: 11 December 2019
- Last tested version: 1.1.21.1654.g282a2807 + Current Version: 0.39
+ Last updated: 27 December 2019
+ Last tested version: 1.1.22.633.g1bab253a

Important Notice(s)

diff --git a/chrome_elf.zip b/chrome_elf.zip index 875b47e..fb91870 100644 Binary files a/chrome_elf.zip and b/chrome_elf.zip differ diff --git a/src/BlockTheSpot.vcxproj b/src/BlockTheSpot.vcxproj index 39dd5b8..fa828a2 100644 --- a/src/BlockTheSpot.vcxproj +++ b/src/BlockTheSpot.vcxproj @@ -29,7 +29,7 @@ false v142 true - Unicode + MultiByte @@ -82,6 +82,7 @@ Speed AnySuitable Sync + true Windows @@ -92,6 +93,8 @@ UseLinkTimeCodeGeneration + true + false diff --git a/src/dllmain.cpp b/src/dllmain.cpp index 7db76e6..8efdda3 100644 --- a/src/dllmain.cpp +++ b/src/dllmain.cpp @@ -10,39 +10,63 @@ extern std::ofstream Log_DNS; extern std::ofstream Log_GetAddr; extern std::ofstream Log_WinHttp; +PIP4_ARRAY pSrvList = NULL; + +void Init_config () { + if (0 == GetPrivateProfileInt ("Config", "AdGuardDNS", 1, "./config.ini")) + g_UseAdGuard = false; + if (0 < GetPrivateProfileInt ("Config", "Log", 0, "./config.ini")) + g_Log = true; + if (0 < GetPrivateProfileInt ("Config", "Skip_wpad", 0, "./config.ini")) + g_Skip_wpad = true; + if (0 < GetPrivateProfileInt ("Config", "WinHttpReadDataFix", 0, "./config.ini")) + g_WinHttpReadDataFix = true; +} + +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); + + if (!g_UseAdGuard) + Log_DNS << "AdGuard DNS Disable!\n"; +} + +void Init_DNS () { + pSrvList = (PIP4_ARRAY)LocalAlloc (LPTR, sizeof (IP4_ARRAY)); + if (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; +} + 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: - DisableThreadLibraryCalls (hModule); - if (0 == GetPrivateProfileIntA ("Config", "AdGuardDNS", 1, "./config.ini")) - g_UseAdGuard = false; - if (0 < GetPrivateProfileIntA ("Config", "Log", 0, "./config.ini")) - g_Log = true; - if (0 < GetPrivateProfileIntA ("Config", "Skip_wpad", 0, "./config.ini")) - g_Skip_wpad = true; - if (0 < GetPrivateProfileIntA ("Config", "WinHttpReadDataFix", 0, "./config.ini")) - g_WinHttpReadDataFix = true; - - 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 (!g_UseAdGuard) - Log_DNS << "AdGuard DNS Disable!\n"; + if (strstr (GetCommandLine (), "--type=utility") || !strstr (GetCommandLine (), "--type=")) { + Init_config (); + if (g_UseAdGuard) Init_DNS (); + if (g_Log) Init_log (); + // block ads banner by hostname. + InstallHookApi ("ws2_32.dll", "getaddrinfo", getaddrinfohook); + // block ads by manipulate json response. + InstallHookApi ("Winhttp.dll", "WinHttpReadData", winhttpreaddatahook); } - - // 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) { @@ -50,9 +74,10 @@ BOOL APIENTRY DllMain (HMODULE hModule, Log_GetAddr.close (); Log_WinHttp.close (); } + if (pSrvList) + LocalFree (pSrvList); break; } - return TRUE; } diff --git a/src/hosts.cpp b/src/hosts.cpp index 36d0e73..62cc195 100644 --- a/src/hosts.cpp +++ b/src/hosts.cpp @@ -8,6 +8,10 @@ bool g_WinHttpReadDataFix = false; std::ofstream Log_DNS; std::ofstream Log_GetAddr; std::ofstream Log_WinHttp; +extern PIP4_ARRAY pSrvList; + +std::vector blacklist; +std::vector whitelist; // support.microsoft.com/en-us/help/831226/ // how-to-use-the-dnsquery-function-to-resolve-host-names-and-host-addres @@ -17,48 +21,55 @@ std::ofstream Log_WinHttp; bool adguard_dnsblock (const char* nodename) { DNS_STATUS dnsStatus; PDNS_RECORD QueryResult; - PIP4_ARRAY pSrvList = NULL; bool isBlock = false; - char resolvedIP[INET6_ADDRSTRLEN]{}; + 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'; + } + g_UseAdGuard = false; + return false; + } - pSrvList = (PIP4_ARRAY)LocalAlloc (LPTR, sizeof (IP4_ARRAY)); - - if (pSrvList) { - // https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-inetptonw - if (1 == InetPtonA (AF_INET, - "176.103.130.134", // dns server ip - &pSrvList->AddrArray[0])) { - // "Family protection" - // adguard.com/en/adguard-dns/overview.html - pSrvList->AddrCount = 1; - - dnsStatus = DnsQuery_A (nodename, - DNS_TYPE_A, - DNS_QUERY_WIRE_ONLY, - pSrvList, - &QueryResult, - NULL); // Reserved - if (0 == dnsStatus) { - if (QueryResult) { - for (auto p = QueryResult; p; p = p->pNext) { - // 0.0.0.0 - inet_ntop (AF_INET, - &p->Data.A.IpAddress, - resolvedIP, - sizeof (resolvedIP)); - if (_stricmp (resolvedIP, "0.0.0.0") == 0) - isBlock = true; // AdGuard Block - } - DnsRecordListFree (QueryResult, DnsFreeRecordList); - } // QueryResult - } // dnsStatus - } // inet_pton - LocalFree (pSrvList); - } // pSrvList + 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, + pSrvList, + &QueryResult, + NULL); // Reserved + + if (0 == dnsStatus) { + if (QueryResult) { + 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 + } // QueryResult + } else { // dnsStatus + fail_count++; + } if (g_Log && isBlock) { Log_DNS << nodename << " blocked" << '\n'; } + return isBlock; } @@ -119,7 +130,6 @@ int WINAPI winhttpreaddatahook (DWORD RetAddr, } if (g_Log) { std::string data ((char*)lpBuffer, dwNumberOfBytesToRead); - Log_WinHttp << "Byte count: " << dwNumberOfBytesToRead << '\n'; Log_WinHttp << data << '\n'; } if (g_WinHttpReadDataFix) return false; diff --git a/src/stdafx.h b/src/stdafx.h index d9668cc..5022d40 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -18,6 +18,7 @@ #include #include #include +#include #include "HookApi.h" #include "hosts.h"