-
Notifications
You must be signed in to change notification settings - Fork 36
/
loghandler.cpp
333 lines (278 loc) · 8.23 KB
/
loghandler.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "loghandler.h"
#include "mainwindow.h"
#include "worker.h"
#include <QMessageBox>
#include <QNetworkReply>
#include <QNetworkInterface>
#define MINIUPNP_STATICLIB
#include <miniupnpc.h>
#include <upnpcommands.h>
#ifdef WIN32
#include <winsock2.h>
#endif
LogHandler::LogHandler(MainWindow *main)
{
this->logPort = 0;
this->manager = NULL;
this->logsocket = NULL;
this->pExternalIP = NULL;
this->pInternalIP = NULL;
this->pLocalAddress = NULL;
this->createSocket();
this->pMain = main;
this->findLocalAddress();
}
LogHandler::~LogHandler()
{
if(this->logsocket)
{
this->logsocket->close();
delete this->logsocket;
}
if(this->pExternalIP)
delete this->pExternalIP;
if(this->pInternalIP)
delete this->pInternalIP;
if(this->pLocalAddress)
delete this->pLocalAddress;
if(this->manager)
this->manager->deleteLater();
}
/* Checks if ip is within the following ranges
*
* 10.0.0.0 – 10.255.255.255
* 172.16.0.0 – 172.31.255.255
* 192.168.0.0 – 192.168.255.255
*
*/
bool LogHandler::isPrivateIP(QHostAddress address)
{
static QHostAddress arryPrivate[] = {QHostAddress("10.0.0.0"), QHostAddress("10.255.255.255"), QHostAddress("172.16.0.0"), QHostAddress("172.31.255.255"), QHostAddress("192.168.0.0"), QHostAddress("192.168.255.255")};
quint32 iAddress = address.toIPv4Address();
for(int i = 0; i < (sizeof(arryPrivate)/sizeof(arryPrivate[0])); i+=2)
{
if(iAddress >= arryPrivate[i].toIPv4Address() && iAddress <= arryPrivate[i+1].toIPv4Address())
return true;
}
return false;
}
void LogHandler::findLocalAddress()
{
if(!this->pLocalAddress)
this->pLocalAddress = new QHostAddress();
this->pLocalAddress->clear();
foreach (const QHostAddress &address, QNetworkInterface::allAddresses())
{
if(address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost) && this->isPrivateIP(address))
{
this->pLocalAddress->setAddress(address.toString());
return;
}
}
}
void LogHandler::setExternalIP(QString szAddress)
{
if(!this->pExternalIP)
this->pExternalIP = new QHostAddress();
this->pExternalIP->setAddress(szAddress);
}
void LogHandler::setInternalIP(QString szAddress)
{
if(!this->pInternalIP)
this->pInternalIP = new QHostAddress();
this->pInternalIP->setAddress(szAddress);
}
void LogHandler::createSocket()
{
if(!this->logsocket)
{
this->logsocket = new QUdpSocket();
connect(this->logsocket, &QUdpSocket::readyRead, this, &LogHandler::socketReadyRead);
connect(this->logsocket, &QUdpSocket::disconnected, this, &LogHandler::socketDisconnected);
this->isBound = false;
}
}
void LogHandler::socketDisconnected()
{
this->isBound = false;
this->createBind(this->logPort);
}
void LogHandler::socketReadyRead()
{
QByteArray datagram(this->logsocket->pendingDatagramSize(), Qt::Uninitialized);
QHostAddress sender;
quint16 senderPort;
this->logsocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
ServerInfo *info = NULL;
for(int i = 0; i < this->logList.size(); i++)
{
if(this->logList.at(i)->host == sender && this->logList.at(i)->port == senderPort)
{
info = this->logList.at(i);
break;
}
}
int idx = datagram.indexOf(" ");
QString logLine = QString(datagram.remove(0, idx+1));
pMain->parseLogLine(logLine, info);
}
void LogHandler::addServer(ServerInfo *info)
{
for(int i = 0; i < this->logList.size(); i++)
{
if(this->logList.at(i)->isEqual(info))
{
return;
}
}
this->logList.append(info);
}
void LogHandler::removeServer(ServerInfo *info)
{
for(int i = 0; i < this->logList.size(); i++)
{
if(this->logList.at(i)->isEqual(info))
{
this->logList.removeAt(i);
return;
}
}
}
void LogHandler::createBind(quint16 port)
{
this->createSocket();
if(this->isBound)
{
this->logsocket->close();
}
bool newPort = (this->logPort != port);
this->logPort = port;
this->szPort = QString::number(port);
if(!this->logsocket->bind(QHostAddress::AnyIPv4, logPort))
{
QMessageBox::critical(pMain, "Log Handler Error", "Failed to bind to port:\n" + this->logsocket->errorString());
return;
}
this->isBound = true;
if(!workerThread.isRunning() && newPort)
{
Worker *worker = new Worker;
worker->moveToThread(&workerThread);
connect(&workerThread, &QThread::finished, worker, &Worker::deleteLater);
connect(this, &LogHandler::setupUPnP, worker, &Worker::setupUPnP);
connect(worker, &Worker::UPnPReady, this, &LogHandler::UPnPReady);
workerThread.start();
this->setupUPnP(this);
}
}
void LogHandler::UPnPReady()
{
if(this->pExternalIP && !this->pExternalIP->isNull())
{
if(this->manager)
{
this->manager->deleteLater();
this->manager = NULL;
}
if(pMain->showLoggingInfo)
{
QMessageBox::information(this->pMain, "Log Handler", QString("Listening on: %1:%2").arg(this->pExternalIP->toString(), this->szPort));
pMain->showLoggingInfo = false;
}
}
else if(!this->manager)
{
this->manager = new QNetworkAccessManager(this);
connect(this->manager, &QNetworkAccessManager::finished, this, &LogHandler::apiFinished);
this->manager->get(QNetworkRequest(QUrl("http://api.ipify.org/")));
}
else
{
this->manager->deleteLater();
this->manager = NULL;
QMessageBox::critical(this->pMain, "Log Handler Error", QString("Failed to retrieve external IP."));
}
}
void LogHandler::apiFinished(QNetworkReply *reply)
{
if(!reply->error())
{
QString external = reply->readAll();
this->setExternalIP(external);
}
reply->deleteLater();
emit UPnPReady();
}
//TODO : Clean this up
void Worker::setupUPnP(LogHandler *logger)
{
int nResult;
#ifdef WIN32
WSADATA wsaData;
nResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if(nResult != NO_ERROR)
{
WSACleanup();
emit UPnPReady();
this->currentThread()->quit();
return;
}
#endif
int error;
UPNPDev *devlist = upnpDiscover(2000, NULL, NULL, 0, 0, 2, &error);
if(!devlist)
{
#ifdef WIN32
WSACleanup();
#endif
emit UPnPReady();
this->currentThread()->quit();
return;
}
char lanaddress[64] = "";
UPNPUrls urls;
IGDdatas data;
UPNP_GetValidIGD(devlist, &urls, &data, lanaddress, sizeof(lanaddress));
logger->setInternalIP(QString(lanaddress));
char externalIPAddress[64] = "";
nResult = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
if(nResult != UPNPCOMMAND_SUCCESS)
{
freeUPNPDevlist(devlist);
#ifdef WIN32
WSACleanup();
#endif
emit UPnPReady();
this->currentThread()->quit();
return;
}
logger->setExternalIP(QString(externalIPAddress));
nResult = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype, logger->szPort.toLatin1().data(), logger->szPort.toLatin1().data(), lanaddress, "Source Admin Tool", "UDP", 0, "0");
if(nResult != UPNPCOMMAND_SUCCESS)
{
freeUPNPDevlist(devlist);
#ifdef WIN32
WSACleanup();
#endif
emit UPnPReady();
this->currentThread()->quit();
return;
}
freeUPNPDevlist(devlist);
#ifdef WIN32
WSACleanup();
#endif
emit UPnPReady();
this->currentThread()->quit();
}
// Returns internal ip (from either network interface look up or UPnP creation) or external depending on if server is LAN or not.
QHostAddress *LogHandler::getAddressToLogTo(QHostAddress serverAddress)
{
if(this->isPrivateIP(serverAddress) && ((this->pInternalIP && !this->pInternalIP->isNull()) || (this->pLocalAddress && !this->pLocalAddress->isNull())))
{
if(this->pInternalIP && !this->pInternalIP->isNull())
return this->pInternalIP;
return this->pLocalAddress;
}
return this->pExternalIP;
}