@@ -18,20 +18,10 @@ std::vector<std::string> whitelist;
18
18
// blogs.msdn.microsoft.com/winsdk/2014/12/17/
19
19
// dnsquery-sample-to-loop-through-multiple-ip-addresses/
20
20
21
- bool adguard_dnsblock (const char * nodename) {
21
+ int adguard_dnsblock (const char * nodename) {
22
22
DNS_STATUS dnsStatus;
23
23
PDNS_RECORD QueryResult;
24
24
bool isBlock = false ;
25
- static int fail_count = 0 ;
26
- if (!g_UseAdGuard) return false ;
27
-
28
- if (fail_count > 5 ) {
29
- if (g_Log) {
30
- Log_DNS << " AdGuard DNS lookup disable! fail resolve > 5 times" << std::endl;
31
- }
32
- g_UseAdGuard = false ;
33
- return false ;
34
- }
35
25
36
26
dnsStatus = DnsQuery (nodename,
37
27
DNS_TYPE_A,
@@ -40,70 +30,77 @@ bool adguard_dnsblock (const char* nodename) {
40
30
&QueryResult,
41
31
NULL ); // Reserved
42
32
33
+ if (0 != dnsStatus) return -1 ;
34
+
43
35
if (0 == dnsStatus) {
44
36
if (QueryResult) {
45
37
for (auto p = QueryResult; p; p = p->pNext ) {
46
- if (0 == p->Data .A .IpAddress ) {
38
+ if (INADDR_ANY == p->Data .A .IpAddress ) {
47
39
isBlock = true ; // AdGuard Block
48
40
break ; // no more processing
49
41
}
50
42
}
51
43
DnsRecordListFree (QueryResult, DnsFreeRecordList);
52
44
} // QueryResult
53
45
}
54
- else { // dnsStatus
55
- fail_count++;
56
- }
57
- if (g_Log && isBlock) {
58
- Log_DNS << nodename << " blocked" << std::endl;
59
- }
46
+
60
47
if (isBlock) {
61
- blacklist.push_back (nodename); // add to blacklist
62
- }
63
- else {
64
- whitelist.push_back (nodename); // add to whitelist
48
+ return 1 ;
65
49
}
66
- return isBlock ;
50
+ return 0 ;
67
51
}
68
52
69
53
bool checkBlock (const char * nodename) {
70
54
71
55
if (nullptr != strstr (nodename, " google" ))
72
56
return true ;
73
57
74
- for (auto allow : whitelist) {
75
- if (0 == _stricmp (allow.c_str (), nodename))
76
- return false ;
77
- }
58
+ // AdGuard DNS
59
+ if (g_UseAdGuard) {
60
+ for (auto allow : whitelist) {
61
+ if (0 == _stricmp (allow.c_str (), nodename))
62
+ return false ;
63
+ }
78
64
79
- for (auto block : blacklist) {
80
- if (0 == _stricmp (block.c_str (), nodename))
81
- return true ;
82
- }
65
+ for (auto block : blacklist) {
66
+ if (0 == _stricmp (block.c_str (), nodename))
67
+ return true ;
68
+ }
83
69
84
- // AdGuard DNS
85
- if (adguard_dnsblock (nodename)) {
86
- return true ;
70
+ int result = adguard_dnsblock (nodename);
71
+ if (1 == result) {
72
+ blacklist.push_back (nodename); // add to blacklist
73
+ return true ;
74
+ }
75
+ else if (0 == result) {
76
+ whitelist.push_back (nodename); // add to whitelist
77
+ }
87
78
}
88
-
89
79
return false ;
90
80
}
81
+
91
82
int WINAPI getaddrinfohook (DWORD RetAddr,
92
83
pfngetaddrinfo fngetaddrinfo,
93
84
const char * nodename,
94
85
const char * servname,
95
86
const struct addrinfo * hints,
96
87
struct addrinfo ** res)
97
88
{
89
+
98
90
auto result = fngetaddrinfo (nodename,
99
91
servname,
100
92
hints,
101
93
res);
102
- addrinfo* p = *res;
103
- if (0 == result && nullptr != p) { // GetAddrInfo return 0 on success
104
- if (p->ai_family == AF_INET && checkBlock (nodename)) {
105
- struct sockaddr_in * ipv4 = (struct sockaddr_in *)p->ai_addr ;
106
- InetPton (AF_INET, " 0.0.0.0" , &(ipv4->sin_addr ));
94
+
95
+ if (0 == result) { // GetAddrInfo return 0 on success
96
+ // Web Proxy Auto-Discovery (WPAD)
97
+ if (g_Skip_wpad && 0 == _stricmp (nodename, " wpad" ))
98
+ return WSAHOST_NOT_FOUND;
99
+ if (checkBlock (nodename)) {
100
+ for (auto ptr = *res; nullptr != ptr; ptr = ptr->ai_next ) {
101
+ auto ipv4 = (struct sockaddr_in *)ptr->ai_addr ;
102
+ ipv4->sin_addr .S_un .S_addr = INADDR_ANY;
103
+ }
107
104
if (g_Log) {
108
105
Log_GetAddr << nodename << " blocked" << std::endl;
109
106
}
0 commit comments