Repository navigation
Expand file tree
/
Copy pathserverinfo.cpp
More file actions
138 lines (123 loc) · 4.08 KB
/
Copy pathserverinfo.cpp
File metadata and controls
138 lines (123 loc) · 4.08 KB
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
#include "mainwindow.h"
#include "util.h"
#include <QFile>
#include <QMutex>
#include <maxminddb.h>
ServerInfo::ServerInfo(QString server, QueryState state, bool isIP)
{
this->appId = -1;
this->rconPassword = "";
this->saveRcon = false;
this->rcon = nullptr;
this->vac = 0;
this->version = "";
this->os = "";
this->tags = "";
this->haveInfo = false;
this->goldsrc = false;
this->queryState = state;
QStringList address = server.split(":");
if (address.count() == 1)
{
address.append("27015");
}
if(isIP)
{
this->host = QHostAddress(address.at(0));
this->GetCountryFlag();
}
else
{
this->hostname = address.at(0);
}
this->hostPort = server;
this->port = address.at(1).toInt();
}
bool ServerInfo::isEqual(ServerInfo *other) const
{
return (this->host == other->host && this->port == other->port);
}
void ServerInfo::cleanHashTable()
{
QList<QString> keys = this->logHashTable.keys();
for(int i = 0; i < keys.length(); i++)
{
PlayerLogInfo info = this->logHashTable.value(keys.at(i));
if(info.time+(1000*60*30) < QDateTime::currentMSecsSinceEpoch())//remove the key if its older than 30 minutes
{
this->logHashTable.remove(keys.at(i));
}
}
}
void ServerInfo::GetCountryFlag()
{
static QMutex mmdbMutex;
static MMDB_s mmdb;
static bool mmdbOpen = false;
static bool mmdbFailed = false;
if(this->host.toString().isEmpty())
return;
QMutexLocker locker(&mmdbMutex);
if(mmdbFailed)
return;
if(!mmdbOpen)
{
QByteArray path = BuildPath("GeoLite2-Country.mmdb").toUtf8();
int status = MMDB_open(path.data(), MMDB_MODE_MMAP, &mmdb);
if (status != MMDB_SUCCESS)
{
qDebug() << "Failed to open MaxMind db (" << MMDB_strerror(status) << ")";
mmdbFailed = true;
return;
}
mmdbOpen = true;
}
int gai_error, mmdb_error;
MMDB_lookup_result_s results = MMDB_lookup_string(&mmdb, this->host.toString().toLatin1().data(), &gai_error, &mmdb_error);
if (gai_error == 0 && mmdb_error == MMDB_SUCCESS && results.found_entry)
{
MMDB_entry_data_s entry_data;
int res = MMDB_get_value(&results.entry, &entry_data, "country", "iso_code", NULL);
if (res == MMDB_SUCCESS && entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_UTF8_STRING)
{
QString countryName = QString(QByteArray::fromRawData(entry_data.utf8_string, entry_data.data_size)).toLower();
QString flagPath = QString(":/icons/icons/countries/%1.png").arg(countryName);
if (QFile::exists(flagPath))
{
countryFlag.load(flagPath);
}
else
{
qDebug() << "Flag icon does not exist at " << flagPath << ".";
}
}
else
{
qDebug() << "Bad entry. MMDBerror " << MMDB_strerror(res) << ", HasData: " << entry_data.has_data << ", DataType: " << entry_data.utf8_string;
}
}
else
{
qDebug() << "Lookup failure. gai: " << gai_error << ", MMDBerror " << MMDB_strerror(mmdb_error) << " (" << mmdb_error << ")";
}
}
void HostQueryResult::HostInfoResolved(QHostInfo hostInfo)
{
for(const QHostAddress &addr : hostInfo.addresses())
{
if(!addr.isNull() && addr.protocol() == QAbstractSocket::IPv4Protocol)
{
this->info->host = addr;
this->info->queryState = QueryRunning;
this->mainWindow->CreateTableItemOrUpdate(id->row(), kBrowserColHostname, id->tableWidget(), this->info);
this->info->GetCountryFlag();
InfoQuery *infoQuery = new InfoQuery(this->mainWindow);
infoQuery->query(& this->info->host, this->info->port, this->id);
this->deleteLater();
return;
}
}
info->queryState = QueryResolveFailed;
this->mainWindow->CreateTableItemOrUpdate(id->row(), kBrowserColHostname, id->tableWidget(), this->info);
this->deleteLater();
}