This repository has been archived by the owner on May 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DnsUpdater.cpp
234 lines (211 loc) · 5.61 KB
/
DnsUpdater.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// DnsUpdater.cpp
//
#include "stdafx.h"
using namespace std;
DnsUpdater::DnsUpdater()
{
evLog = 0;
waitArray[0] = CreateEvent(NULL, FALSE, FALSE, NULL);
if (waitArray[0] == NULL) {
throw _T("Could not create Event!");
}
waitObjects = 1;
timeout = 1000 * 60 * 60 * 1; // 1 hour
if (WSAStartup(MAKEWORD(2,2), &wsadata) != 0)
throw _T("Could not start winsock");
}
DnsUpdater::~DnsUpdater()
{
CloseHandle(waitArray[0]);
WSACleanup();
}
void DnsUpdater::Add(const DomainInfo& info)
{
domainInfo.push_back(info);
}
bool DnsUpdater::RunOnce()
{
bool rval = true;
// get host name
char hostname[256];
*hostname = 0;
const char *addr = "1.1.1.1";
if (gethostname(hostname, 256) == 0) {
struct hostent* host = gethostbyname(hostname);
addr = inet_ntoa(*(struct in_addr *)*host->h_addr_list);
}
// evLog->LogEvent(EVENTLOG_INFORMATION_TYPE, addr);
// go through and post any files that exist currently, and set up change notification
{
DomainInfoVectIter iter = domainInfo.begin();
while (iter != domainInfo.end()) {
iter->UpdateDNS(addr, evLog);
iter++;
}
}
// wait for change notifications
while (waitObjects > 0) {
DWORD result = MsgWaitForMultipleObjectsEx(waitObjects, waitArray, timeout, 0, 0);
if (result == WAIT_OBJECT_0) {
// event was signalled, time to quit
rval = false;
break; // exit loop
}
else if (result == WAIT_TIMEOUT) {
DomainInfoVectIter iter = domainInfo.begin();
while (iter != domainInfo.end()) {
iter->UpdateDNS(addr, evLog);
if (WaitForSingleObject(waitArray[0], 0) != WAIT_TIMEOUT)
break; // service stopping
iter++;
}
}
// else if (result > WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + waitObjects) {
}
return rval;
}
void DnsUpdater::Run()
{
while (RunOnce());
}
void DnsUpdater::Stop()
{
SetEvent(waitArray[0]);
}
BOOL DomainInfo::UpdateDNS(const TCHAR* addr, EventLog* plog)
{
CAtlHttpClient client;
BOOL rval = FALSE;
CAtlNavigateData navData;
navData.SetMethod(ATL_HTTP_METHOD_GET);
if (httpTimeout > 0)
navData.SetSocketTimeout(httpTimeout);
// set auth
CBasicAuthObject auth;
client.AddAuthObj(_T("basic"), &auth, this);
// proxy
if (proxy.length() > 0) {
client.SetProxy(proxy.c_str(), (short) proxyPort);
}
// setup headers
tstring headers;
if (navData.GetExtraHeaders() != NULL)
headers += navData.GetExtraHeaders();
headers += _T("Connection: close");
headers += _T("\r\n");
navData.SetExtraHeaders(headers.c_str());
// setup url
TCHAR item[256];
DWORD itemLen;
tstring cUrl = easydnsurl;
if (easydnsurl.length() == 0)
cUrl = _T("https://members.easydns.com/dyn/dyndns.php");
// hostname
cUrl += _T("?hostname=");
if (AtlEscapeUrl(hostname.c_str(), item, &itemLen, 256) == FALSE) {
plog->LogEvent(EVENTLOG_ERROR_TYPE, "hostname is too long");
return FALSE;
}
cUrl += item;
// tld
if (tld.length() > 0) {
cUrl += _T("&tld=");
if (AtlEscapeUrl(tld.c_str(), item, &itemLen, 256) == FALSE) {
plog->LogEvent(EVENTLOG_ERROR_TYPE, "tld is too long");
return FALSE;
}
cUrl += item;
}
// myip
cUrl += _T("&myip=");
tstring tmp = myip;
if (tmp.length() == 0)
tmp = addr;
if (AtlEscapeUrl(tmp.c_str(), item, &itemLen, 256) == FALSE) {
plog->LogEvent(EVENTLOG_ERROR_TYPE, "myip is too long");
return FALSE;
}
cUrl += item;
// mx
if (mx.length() > 0) {
cUrl += _T("&mx=");
if (AtlEscapeUrl(mx.c_str(), item, &itemLen, 256) == FALSE) {
plog->LogEvent(EVENTLOG_ERROR_TYPE, "mx is too long");
return FALSE;
}
cUrl += item;
}
// backmx
if (backmx.length() > 0) {
cUrl += _T("&backmx=");
if (AtlEscapeUrl(backmx.c_str(), item, &itemLen, 256) == FALSE) {
plog->LogEvent(EVENTLOG_ERROR_TYPE, "backmx is too long");
return FALSE;
}
cUrl += item;
}
// wildcard
if (wildcard.length() > 0) {
cUrl += _T("&wildcard=");
if (AtlEscapeUrl(wildcard.c_str(), item, &itemLen, 256) == FALSE) {
plog->LogEvent(EVENTLOG_ERROR_TYPE, "wildcard is too long");
return FALSE;
}
cUrl += item;
}
// issue request
tstring msg = _T("Result for ");
msg += hostname;
msg += _T(" - HTTP ");
TCHAR s[128];
if (client.Navigate(cUrl.c_str(), &navData) || true) {
const BYTE* body = client.GetBody();
wsprintf(s, _T("%d"), client.GetStatus());
msg += s;
msg += _T(" - ");
WORD et = EVENTLOG_WARNING_TYPE;
if (client.GetStatus() != 200)
et = EVENTLOG_ERROR_TYPE;
if (body != 0) {
if (_tcsstr((const char*) body, "NOERROR") != 0)
et = EVENTLOG_INFORMATION_TYPE;
if (sizeof(TCHAR) > 1) {
TCHAR* pData = new TCHAR[client.GetBodyLength() + 1];
if (pData) {
mbstowcs((wchar_t*) pData, (const char*) body, client.GetBodyLength());
pData[client.GetBodyLength()] = 0;
msg += pData;
delete[] pData;
}
}
else
msg += (const char *) body;
}
plog->LogEvent(et, msg.c_str());
if (client.GetStatus() == 200)
rval = TRUE;
}
else {
wsprintf(s, _T("%d"), client.GetStatus());
msg += s;
msg += _T(" - ");
msg += _T("Navigate failed");
plog->LogEvent(EVENTLOG_ERROR_TYPE, msg.c_str());
}
return rval;
}
HRESULT DomainInfo::GetPassword(LPTSTR szPwd, DWORD* dwBuffSize)
{
_tcsncpy(szPwd, password.c_str(), *dwBuffSize);
return S_OK;
}
HRESULT DomainInfo::GetUsername(LPTSTR szUid, DWORD* dwBuffSize)
{
_tcsncpy(szUid, username.c_str(), *dwBuffSize);
return S_OK;
}
HRESULT DomainInfo::GetDomain(LPTSTR szDomain, DWORD* dwBuffSize)
{
_tcsncpy(szDomain, domain.c_str(), *dwBuffSize);
return S_OK;
}