Skip to content

Commit

Permalink
try lower dns lookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrpond committed Dec 27, 2019
1 parent e2a25e0 commit fe601fa
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 66 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.38 <br>
<strong>Last updated:</strong> 11 December 2019 <br>
<strong>Last tested version:</strong> 1.1.21.1654.g282a2807
<strong>Current Version:</strong> 0.39 <br>
<strong>Last updated:</strong> 27 December 2019 <br>
<strong>Last tested version:</strong> 1.1.22.633.g1bab253a
</p>
<h4 align="center">Important Notice(s)</h4>
<p align="center">
Expand Down
Binary file modified chrome_elf.zip
Binary file not shown.
5 changes: 4 additions & 1 deletion src/BlockTheSpot.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down Expand Up @@ -82,6 +82,7 @@
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<ExceptionHandling>Sync</ExceptionHandling>
<EnableParallelCodeGeneration>true</EnableParallelCodeGeneration>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -92,6 +93,8 @@
<IgnoreAllDefaultLibraries>
</IgnoreAllDefaultLibraries>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
<RandomizedBaseAddress>true</RandomizedBaseAddress>
<ImageHasSafeExceptionHandlers>false</ImageHasSafeExceptionHandlers>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
75 changes: 50 additions & 25 deletions src/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,74 @@ 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) {
Log_DNS.close ();
Log_GetAddr.close ();
Log_WinHttp.close ();
}
if (pSrvList)
LocalFree (pSrvList);
break;
}

return TRUE;
}

84 changes: 47 additions & 37 deletions src/hosts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> blacklist;
std::vector<std::string> whitelist;

// support.microsoft.com/en-us/help/831226/
// how-to-use-the-dnsquery-function-to-resolve-host-names-and-host-addres
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <windns.h>
#include <winhttp.h>
#include <fstream>
#include <vector>
#include "HookApi.h"
#include "hosts.h"

Expand Down

0 comments on commit fe601fa

Please sign in to comment.