Skip to content

Commit 8c08842

Browse files
author
mrpond
committed
0.46 improve code quality
1 parent 58ad9b8 commit 8c08842

File tree

7 files changed

+82
-104
lines changed

7 files changed

+82
-104
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<h4 align="center">A multi-purpose adblocker and skip bypass for the <strong>Windows</strong> Spotify Desktop Application.</h4>
44
<h5 align="center">Please support Spotify by purchasing premium</h5>
55
<p align="center">
6-
<strong>Current Version:</strong> 0.45 <br>
7-
<strong>Last updated:</strong> 14 February 2020<br>
8-
<strong>Last tested version:</strong> 1.1.25.559.g85cf5e4c
6+
<strong>Current Version:</strong> 0.46 <br>
7+
<strong>Last updated:</strong> 20 February 2020<br>
8+
<strong>Last tested version:</strong> 1.1.26.501.gbe11e53b
99
</p>
1010
<h4 align="center">Important Notice(s)</h4>
1111
<p align="center">

chrome_elf.zip

-371 Bytes
Binary file not shown.

src/Adblock.cpp

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,66 @@
11
#include "stdafx.h"
22

33
extern bool g_Log;
4+
extern bool g_Skip_wpad;
5+
46
std::ofstream Log_DNS;
57

6-
Adblock g_Adsblock;
8+
Adsblock g_Adsblock;
79

8-
Adblock::Adblock () {
9-
SecureZeroMemory (dns_ipaddr, sizeof (dns_ipaddr));
10-
pSrvList = nullptr;
11-
enable = false;
10+
Adsblock::Adsblock () {
11+
isActive = false;
12+
pSrvList = (PIP4_ARRAY)LocalAlloc (LPTR, sizeof (IP4_ARRAY));
1213
}
1314

14-
void Adblock::activate () {
15-
enable = true;
16-
}
17-
void Adblock::deactivate () {
18-
enable = false;
19-
}
15+
void Adsblock::setDNSIP (const char* ip) {
2016

21-
bool Adblock::init (const char* configFile) {
22-
if (!isEnable ()) {
23-
return false;
24-
}
25-
pSrvList = (PIP4_ARRAY)LocalAlloc (LPTR, sizeof (IP4_ARRAY));
2617
if (nullptr != pSrvList) {
27-
GetPrivateProfileString ("Config",
28-
"AdGuardDNS_IP",
29-
"176.103.130.134",
30-
dns_ipaddr,
31-
INET_ADDRSTRLEN,
32-
configFile);
18+
3319
// https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-inetptonw
3420
if (1 == InetPton (AF_INET,
35-
dns_ipaddr, // dns server ip
21+
ip, // dns server ip
3622
&pSrvList->AddrArray[0])) {
3723
pSrvList->AddrCount = 1;
3824
if (g_Log)
39-
Log_DNS << "AdGuard DNS Server - " << dns_ipaddr << std::endl;
40-
return true;
25+
Log_DNS << "AdGuard DNS Server - " << ip << std::endl;
26+
isActive = true;
4127
}
4228
else {
4329
if (g_Log)
4430
Log_DNS << "AdGuard DNS Disable - InetPton "
45-
<< dns_ipaddr << " failed!" << std::endl;
31+
<< ip << " failed!" << std::endl;
4632
}
4733
}
4834
else {
4935
if (g_Log)
50-
Log_DNS << "AdGuard DNS Disable - "
51-
<< "pSrvList LocalAlloc failed!" << std::endl;
36+
Log_DNS << __FUNCTION__
37+
<< " pSrvList LocalAlloc failed!" << std::endl;
5238
}
53-
return false;
5439
}
5540

56-
bool Adblock::isblock (const char* nodename) {
41+
bool Adsblock::isblock (const char* nodename) {
42+
43+
// Web Proxy Auto-Discovery (WPAD)
44+
if (0 == _stricmp (nodename, "wpad"))
45+
return g_Skip_wpad ? true : false;
46+
47+
if (nullptr != strstr (nodename, "google"))
48+
return true;
49+
50+
if (nullptr != strstr (nodename, "doubleclick."))
51+
return true;
5752

5853
// AdGuard DNS
59-
if (isEnable ()) {
54+
if (isActive) {
6055
for (auto allow : whitelist) {
6156
if (0 == _stricmp (allow.c_str (), nodename))
6257
return false;
6358
}
64-
6559
for (auto block : blacklist) {
6660
if (0 == _stricmp (block.c_str (), nodename))
6761
return true;
6862
}
69-
70-
int result = adguardlookup (nodename);
63+
int result = lookup (nodename);
7164
if (1 == result) { // return 1 block
7265
blacklist.push_back (nodename); // add to blacklist
7366
return true;
@@ -80,7 +73,7 @@ bool Adblock::isblock (const char* nodename) {
8073
return false;
8174
}
8275

83-
int Adblock::adguardlookup (const char* nodename) {
76+
int Adsblock::lookup (const char* nodename) {
8477

8578
bool isBlock = false;
8679
DNS_STATUS dnsStatus = 0;
@@ -89,7 +82,7 @@ int Adblock::adguardlookup (const char* nodename) {
8982
dnsStatus = DnsQuery (nodename,
9083
DNS_TYPE_A,
9184
DNS_QUERY_WIRE_ONLY,
92-
pSrvList,
85+
Adsblock::pSrvList,
9386
&QueryResult,
9487
NULL); // Reserved
9588

@@ -106,7 +99,7 @@ int Adblock::adguardlookup (const char* nodename) {
10699
}
107100
else {
108101
if (g_Log)
109-
Log_DNS << "AdGuard DNS Error: " << dnsStatus
102+
Log_DNS << __FUNCTION__ << " host:" << nodename << " status:" << dnsStatus
110103
<< " GLE: " << GetLastError () << std::endl;
111104
return -1;
112105
}
@@ -117,10 +110,12 @@ int Adblock::adguardlookup (const char* nodename) {
117110
return 0;
118111
}
119112

120-
void Adblock::destroy () {
121-
if (nullptr != pSrvList)
113+
void Adsblock::destroy () {
114+
if (nullptr != pSrvList) {
122115
LocalFree (pSrvList);
116+
pSrvList = nullptr;
117+
}
123118
}
124119

125-
Adblock::~Adblock () {
120+
Adsblock::~Adsblock () {
126121
}

src/Adblock.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,18 @@
55
// blogs.msdn.microsoft.com/winsdk/2014/12/17/
66
// dnsquery-sample-to-loop-through-multiple-ip-addresses/
77

8-
class Adblock {
8+
class Adsblock {
99
public:
10-
Adblock::Adblock ();
11-
Adblock::~Adblock ();
12-
bool init (const char* configFile);
10+
Adsblock::Adsblock ();
11+
Adsblock::~Adsblock ();
12+
void setDNSIP (const char* ip);
1313
bool isblock (const char* nodename);
14-
bool isEnable () { return enable; };
1514
void destroy ();
16-
void activate ();
17-
void deactivate ();
1815

1916
private:
20-
bool enable;
2117
PIP4_ARRAY pSrvList;
22-
char dns_ipaddr[INET_ADDRSTRLEN];
18+
bool isActive;
2319
std::vector<std::string> blacklist;
2420
std::vector<std::string> whitelist;
25-
int adguardlookup (const char* nodename);
21+
int lookup (const char* nodename);
2622
};

src/Resource.rc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
7979

8080
VS_VERSION_INFO VERSIONINFO
8181
FILEVERSION 1,0,0,1
82-
PRODUCTVERSION 0,44,0,0
82+
PRODUCTVERSION 0,46,0,0
8383
FILEFLAGSMASK 0x3fL
8484
#ifdef _DEBUG
8585
FILEFLAGS 0x1L
@@ -100,7 +100,7 @@ BEGIN
100100
VALUE "LegalCopyright", "Copyright (C) 2019"
101101
VALUE "OriginalFilename", "BlockTheSpot.dll"
102102
VALUE "ProductName", "BlockTheSpot"
103-
VALUE "ProductVersion", "0.44.0.0"
103+
VALUE "ProductVersion", "0.46.0.0"
104104
END
105105
END
106106
BLOCK "VarFileInfo"

src/dllmain.cpp

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,39 @@ extern bool g_Skip_wpad;
66
extern bool g_WinHttpReadDataFix;
77

88
extern std::ofstream Log_DNS;
9-
extern std::ofstream Log_GetAddr;
109
extern std::ofstream Log_WinHttp;
11-
extern Adblock g_Adsblock;
10+
extern Adsblock g_Adsblock;
1211

13-
const char* configFile = "./config.ini";
12+
void init_log () {
13+
Log_DNS.open ("log_dnsquery.txt",
14+
std::ios::out | std::ios::app);
15+
Log_WinHttp.open ("log_winhttp.txt",
16+
std::ios::out | std::ios::app);
17+
}
1418

1519
void init_config () {
16-
if (0 == GetPrivateProfileInt ("Config", "AdGuardDNS", 1, configFile))
17-
g_Adsblock.deactivate ();
18-
else
19-
g_Adsblock.activate ();
20+
const char* configFile = "./config.ini";
21+
char IP[INET_ADDRSTRLEN];
2022

21-
if (1 == GetPrivateProfileInt ("Config", "Log", 0, configFile))
23+
if (1 == GetPrivateProfileInt ("Config", "Log", 0, configFile)) {
2224
g_Log = true;
25+
init_log ();
26+
}
2327
if (1 == GetPrivateProfileInt ("Config", "Skip_wpad", 0, configFile))
2428
g_Skip_wpad = true;
2529
if (1 == GetPrivateProfileInt ("Config", "WinHttpReadDataFix", 0, configFile))
2630
g_WinHttpReadDataFix = true;
27-
}
2831

29-
void init_log () {
30-
if (g_Log) {
31-
Log_DNS.open ("log_dnsquery.txt",
32-
std::ios::out | std::ios::app);
33-
Log_GetAddr.open ("log_getaddrinfo.txt",
34-
std::ios::out | std::ios::app);
35-
Log_WinHttp.open ("log_winhttp.txt",
36-
std::ios::out | std::ios::app);
32+
if (1 == GetPrivateProfileInt ("Config", "AdGuardDNS", 1, configFile)) {
33+
GetPrivateProfileString ("Config",
34+
"AdGuardDNS_IP",
35+
"176.103.130.134",
36+
IP,
37+
INET_ADDRSTRLEN,
38+
configFile);
39+
g_Adsblock.setDNSIP (IP);
3740
}
41+
3842
}
3943

4044
BOOL APIENTRY DllMain (HMODULE hModule,
@@ -50,12 +54,6 @@ BOOL APIENTRY DllMain (HMODULE hModule,
5054
{
5155
case DLL_PROCESS_ATTACH:
5256
init_config ();
53-
init_log ();
54-
55-
if (false == g_Adsblock.init (configFile)) {
56-
if (g_Log) Log_DNS << "AdGuard DNS Disable!" << std::endl;
57-
g_Adsblock.deactivate ();
58-
}
5957

6058
// block ads banner by hostname.
6159
InstallHookApi ("ws2_32.dll", "getaddrinfo", getaddrinfohook);
@@ -65,7 +63,6 @@ BOOL APIENTRY DllMain (HMODULE hModule,
6563
case DLL_PROCESS_DETACH:
6664
if (g_Log) {
6765
Log_DNS.close ();
68-
Log_GetAddr.close ();
6966
Log_WinHttp.close ();
7067
}
7168
g_Adsblock.destroy ();

src/hosts.cpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ bool g_Log = false;
44
bool g_Skip_wpad = false;
55
bool g_WinHttpReadDataFix = false;
66

7-
std::ofstream Log_GetAddr;
87
std::ofstream Log_WinHttp;
8+
extern std::ofstream Log_DNS;
99

10-
extern Adblock g_Adsblock;
10+
extern Adsblock g_Adsblock;
1111

1212
int WINAPI getaddrinfohook (DWORD RetAddr,
1313
pfngetaddrinfo fngetaddrinfo,
@@ -16,34 +16,23 @@ int WINAPI getaddrinfohook (DWORD RetAddr,
1616
const struct addrinfo* hints,
1717
struct addrinfo** res)
1818
{
19-
auto adguardlookup = std::async (std::launch::async, &Adblock::isblock, g_Adsblock, nodename);
20-
19+
auto lookup = std::async (std::launch::async, &Adsblock::isblock, g_Adsblock, nodename);
20+
// future/async
2121
auto result = fngetaddrinfo (nodename,
2222
servname,
2323
hints,
2424
res);
2525

26-
bool isBlock = adguardlookup.get ();
27-
28-
if (0 == result) { // GetAddrInfo return 0 on success
29-
// Web Proxy Auto-Discovery (WPAD)
30-
if (g_Skip_wpad && 0 == _stricmp (nodename, "wpad"))
31-
return WSAHOST_NOT_FOUND;
32-
33-
if (nullptr != strstr (nodename, "google"))
34-
return true;
26+
bool isBlock = lookup.get ();
27+
if (0 == result && isBlock) { // GetAddrInfo return 0 on success
3528

36-
if (nullptr != strstr (nodename, "doubleclick."))
37-
return true;
38-
39-
if (isBlock) {
40-
for (auto ptr = *res; nullptr != ptr; ptr = ptr->ai_next) {
41-
auto ipv4 = (struct sockaddr_in*)ptr->ai_addr;
42-
ipv4->sin_addr.S_un.S_addr = INADDR_ANY;
43-
}
44-
if (g_Log) {
45-
Log_GetAddr << nodename << " blocked" << std::endl;
46-
}
29+
for (auto ptr = *res; nullptr != ptr; ptr = ptr->ai_next) {
30+
auto ipv4 = (struct sockaddr_in*)ptr->ai_addr;
31+
//memset (&ipv4->sin_addr.S_un.S_addr, 0x0, sizeof ULONG);
32+
ipv4->sin_addr.S_un.S_addr = INADDR_ANY;
33+
}
34+
if (g_Log) {
35+
Log_DNS << nodename << " blocked" << std::endl;
4736
}
4837
}
4938
return result;
@@ -79,7 +68,8 @@ int WINAPI winhttpreaddatahook (DWORD RetAddr,
7968
Log_WinHttp << data << std::endl;
8069
}
8170
if (g_WinHttpReadDataFix) return false;
82-
memset (lpBuffer, 0x20, dwNumberOfBytesToRead);
71+
memset (lpBuffer, 0x0, dwNumberOfBytesToRead);
72+
//SecureZeroMemory (lpBuffer, dwNumberOfBytesToRead);
8373
return true;
8474
}
8575

0 commit comments

Comments
 (0)