forked from quasemago/SourceQueryCache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qcache_mm.cpp
399 lines (311 loc) · 10.3 KB
/
qcache_mm.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#ifdef WIN32
#include <winsock2.h>
#define _FPREFIX __stdcall
#else
#include <sys/mman.h>
#include <unistd.h>
#include <link.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#define _FPREFIX
#define SOCKET int
#endif
#include <string>
#include <map>
#include <ctime>
#include "qcache_mm.h"
#include "Detour.h"
#define A2S_INFO_REQUEST "\xFF\xFF\xFF\xFF\x54"
#define A2S_INFO_REPLY "\xFF\xFF\xFF\xFF\x49"
QCachePlugin g_QCachePlugin;
QCacheListener g_QCacheListener;
PLUGIN_EXPOSE(QCachePlugin, g_QCachePlugin);
// -- Detours --
cDetour recvfrom_detour;
cDetour sendto_detour;
typedef int(_FPREFIX *sendto_f)(SOCKET, const char *, int, int, const struct sockaddr *, int);
typedef int(_FPREFIX *recvfrom_f)(SOCKET, char *, int, int, struct sockaddr *, int *);
int _FPREFIX sendto_hook(SOCKET s, const char *buf, int len, int flags, const struct sockaddr *to, int tolen);
int _FPREFIX recvfrom_hook(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen);
int _FPREFIX recvfrom_hook_RM(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen);
// -- / Detours --
// -- CVArs --
double CacheDelay = 5.0;
void qc_time_Changed(IConVar *var, const char *pOldValue, float flOldValue);
ConVar qc_time("qc_time", "5", FCVAR_PROTECTED, "A2S_INFO requests timeout in seconds.", true, 0, true, 3600, qc_time_Changed);
void qc_time_Changed(IConVar *var, const char *pOldValue, float flOldValue)
{
CacheDelay = qc_time.GetFloat();
}
bool bRequestMapEnabled = true;
void qc_requestmap_enabled_Changed(IConVar *var, const char *pOldValue, float flOldValue);
ConVar qc_requestmap_enabled("qc_requestmap_enabled", "1", FCVAR_PROTECTED, "Enables or disables the RequestMap", true, 0, true, 1, qc_requestmap_enabled_Changed);
void qc_requestmap_enabled_Changed(IConVar *var, const char *pOldValue, float flOldValue)
{
bool bValue = (qc_requestmap_enabled.GetInt() != 0);
if (bValue == bRequestMapEnabled)
return;
bool bOkay = true;
bOkay &= recvfrom_detour.UnHookFunction();
META_CONPRINTF("[QCache] UnHooking recvfrom - %s.\n", bOkay ? "SUCCESS" : "FAILED");
if (!bValue)
{
bOkay &= recvfrom_detour.HookFunction((void*)recvfrom, (void*)recvfrom_hook);
META_CONPRINTF("[QCache] Hooking recvfrom - %s.\n", bOkay ? "SUCCESS" : "FAILED");
META_CONPRINTF("[QCache] RequestMap disabled.\n");
}
else
{
bOkay &= recvfrom_detour.HookFunction((void*)recvfrom, (void*)recvfrom_hook_RM);
META_CONPRINTF("[QCache] Hooking recvfrom - %s.\n", bOkay ? "SUCCESS" : "FAILED");
META_CONPRINTF("[QCache] RequestMap enabled.\n");
}
bRequestMapEnabled = bValue;
}
unsigned int MaxRequests = 3;
void qc_maxrequests_Changed(IConVar *var, const char *pOldValue, float flOldValue);
ConVar qc_maxrequests("qc_maxrequests", "3", FCVAR_PROTECTED, "Maximum ammount of A2S_INFO requests per second.", true, 1, true, 30, qc_maxrequests_Changed);
void qc_maxrequests_Changed(IConVar *var, const char *pOldValue, float flOldValue)
{
MaxRequests = qc_maxrequests.GetInt();
}
// -- / CVArs --
// -- ConCmds --
void qc_requestmap_Called(const CCommand &command);
ConCommand qc_requestmap("qc_requestmap", qc_requestmap_Called, "Prints the RequestMap.");
void qc_requestmap_clear_Called(const CCommand &command);
ConCommand qc_requestmap_clear("qc_requestmap_clear", qc_requestmap_clear_Called, "Clears the RequestMap.");
// -- / ConCmds --
// -- RequestMap --
struct RMElement
{
time_t LastRequest;
unsigned int Requests;
unsigned int OverLimits;
RMElement(time_t _LastRequest, unsigned int _Requests, unsigned int _OverLimits) : LastRequest(_LastRequest), Requests(_Requests), OverLimits(_OverLimits) {}
};
std::map<std::string, RMElement> RequestMap;
void qc_requestmap_Called(const CCommand &command)
{
if (!bRequestMapEnabled)
{
META_CONPRINTF("[QCache] RequestMap is disabled.\n");
return;
}
if (RequestMap.empty())
{
META_CONPRINTF("[QCache] RequestMap is empty.\n");
return;
}
time_t Now = time(NULL);
unsigned int tRequests = 0;
unsigned int tOverLimits = 0;
unsigned int timeDiff;
META_CONPRINTF("[QCache] RequestMap contains :\n");
for (std::map<std::string, RMElement>::iterator it = RequestMap.begin(); it != RequestMap.end(); ++it)
{
timeDiff = difftime(Now, it->second.LastRequest);
META_CONPRINTF("[QCache] IP : % 15s ; LastReq : %u seconds ago ; Requests : %u ; OverLimits : %u\n", it->first.c_str(), timeDiff, it->second.Requests, it->second.OverLimits);
tRequests += (it->second.Requests * !timeDiff);
tOverLimits += (it->second.OverLimits * !timeDiff);
}
META_CONPRINTF("[QCache] Active traffic : Requests : %u ; Overlimits %u\n", tRequests, tOverLimits);
}
void qc_requestmap_clear_Called(const CCommand &command)
{
if (!bRequestMapEnabled)
{
META_CONPRINTF("[QCache] RequestMap is disabled.\n");
return;
}
RequestMap.clear();
META_CONPRINTF("[QCache] RequestMap is now empty.\n");
}
// -- / RequestMap --
// -- A2S_INFO Cache --
char *LastBuff;
size_t LastBuffLen;
time_t LastUpdate;
// -- / A2S_INFO Cache --
int _FPREFIX sendto_hook(SOCKET s, const char *buf, int len, int flags, const struct sockaddr *to, int tolen)
{
sendto_f sendto_Original = (sendto_f)sendto_detour.OriginalPointer();
if (len > 5)
{
if (!memcmp(buf, A2S_INFO_REPLY, 5))
{
time_t Now = time(NULL);
if (difftime(Now, LastUpdate) > CacheDelay)
{
//META_CONPRINTF("[QCache] Cached A2S_INFO_REPLY.\n");
LastUpdate = Now;
delete[] LastBuff;
LastBuff = new char[len];
memcpy(LastBuff, buf, len);
LastBuffLen = len;
}
//META_CONPRINTF("[QCache] Sending A2S_INFO_REPLY.\n");
}
}
int Result = sendto_Original(s, buf, len, flags, to, tolen);
return Result;
}
int _FPREFIX recvfrom_hook(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
{
recvfrom_f recvfrom_Original = (recvfrom_f)recvfrom_detour.OriginalPointer();
sendto_f sendto_Original = (sendto_f)sendto_detour.OriginalPointer();
int Result = recvfrom_Original(s, buf, len, flags, from, fromlen);
if (Result > 5)
{
if (!memcmp(buf, A2S_INFO_REQUEST, 5))
{
time_t Now = time(NULL);
//META_CONPRINTF("[QCache] Received A2S_INFO_REQUEST.\n");
if (difftime(Now, LastUpdate) < CacheDelay)
{
//META_CONPRINTF("[QCache] Sending cached A2S_INFO_REPLY.\n");
sendto_Original(s, LastBuff, LastBuffLen, 0, from, *fromlen);
return -1;
}
}
}
return Result;
}
int _FPREFIX recvfrom_hook_RM(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
{
recvfrom_f recvfrom_Original = (recvfrom_f)recvfrom_detour.OriginalPointer();
sendto_f sendto_Original = (sendto_f)sendto_detour.OriginalPointer();
int Result = recvfrom_Original(s, buf, len, flags, from, fromlen);
if (Result > 5)
{
if (!memcmp(buf, A2S_INFO_REQUEST, 5))
{
time_t Now = time(NULL);
//META_CONPRINTF("[QCache] Received A2S_INFO_REQUEST.\n");
std::string IP(inet_ntoa(((sockaddr_in*)from)->sin_addr));
std::map<std::string, RMElement>::iterator it = RequestMap.find(IP);
if (it == RequestMap.end())
RequestMap.insert(std::pair<std::string, RMElement>(IP, RMElement(Now, 1, 0)));
else
{
if (it->second.LastRequest == Now)
it->second.Requests++;
else
{
it->second.LastRequest = Now;
it->second.Requests = 1;
}
if (it->second.Requests >= MaxRequests)
{
//META_CONPRINTF("[QCache] A2S_INFO request limit per second hit by %s \n",IP.c_str());
it->second.OverLimits++;
return -1;
}
}
if (difftime(Now, LastUpdate) < CacheDelay)
{
//META_CONPRINTF("[QCache] Sending cached A2S_INFO_REPLY.\n");
sendto_Original(s, LastBuff, LastBuffLen, 0, from, *fromlen);
return -1;
}
}
}
return Result;
}
IServerGameDLL *Server = NULL;
ICvar *iCVar = NULL;
class BaseAccessor : public IConCommandBaseAccessor
{
public:
bool RegisterConCommandBase(ConCommandBase *pCommandBase)
{
return META_REGCVAR(pCommandBase);
}
} s_BaseAccessor;
bool QCachePlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
{
PLUGIN_SAVEVARS();
/* Make sure we build on MM:S 1.4 */
#if defined METAMOD_PLAPI_VERSION
GET_V_IFACE_ANY(GetServerFactory, Server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
#else
GET_V_IFACE_ANY(serverFactory, Server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
#endif
#if SOURCE_ENGINE >= SE_ORANGEBOX
GET_V_IFACE_CURRENT(GetEngineFactory, iCVar, ICvar, CVAR_INTERFACE_VERSION);
g_pCVar = iCVar;
ConVar_Register(0, &s_BaseAccessor);
#else
ConCommandBaseMgr::OneTimeInit(&s_BaseAccessor);
#endif
ismm->AddListener(g_PLAPI, &g_QCacheListener);
bool bOkay = true;
bOkay &= recvfrom_detour.HookFunction((void*)recvfrom, (void*)recvfrom_hook_RM);
META_CONPRINTF("[QCache] Hooking recvfrom - %s.\n", bOkay ? "SUCCESS" : "FAILED");
bOkay &= sendto_detour.HookFunction((void*)sendto, (void*)sendto_hook);
META_CONPRINTF("[QCache] Hooking sendto - %s.\n", bOkay ? "SUCCESS" : "FAILED");
RequestMap.clear();
return bOkay;
}
void QCacheListener::OnLevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
{
RequestMap.clear();
}
bool QCachePlugin::Unload(char *error, size_t maxlen)
{
bool bOkay = true;
bOkay &=recvfrom_detour.UnHookFunction();
META_CONPRINTF("[QCache] UnHooking recvfrom - %s.\n", bOkay ? "SUCCESS" : "FAILED");
bOkay &=sendto_detour.UnHookFunction();
META_CONPRINTF("[QCache] UnHooking sendto - %s.\n", bOkay ? "SUCCESS" : "FAILED");
RequestMap.clear();
return bOkay;
}
void QCachePlugin::AllPluginsLoaded()
{
/* This is where we'd do stuff that relies on the mod or other plugins
* being initialized (for example, cvars added and events registered).
*/
}
bool QCachePlugin::Pause(char *error, size_t maxlen)
{
return true;
}
bool QCachePlugin::Unpause(char *error, size_t maxlen)
{
return true;
}
const char *QCachePlugin::GetLicense()
{
return "Public Domain";
}
const char *QCachePlugin::GetVersion()
{
return "1.0.0.2";
}
const char *QCachePlugin::GetDate()
{
return __DATE__;
}
const char *QCachePlugin::GetLogTag()
{
return "QCACHE";
}
const char *QCachePlugin::GetAuthor()
{
return "Hashira";
}
const char *QCachePlugin::GetDescription()
{
return "Cache system for A2S_INFO replies. Original author recon0,ivailosp.";
}
const char *QCachePlugin::GetName()
{
return "Query Cache 2";
}
const char *QCachePlugin::GetURL()
{
return "http://www.sourcemm.net/";
}