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 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
|
/*
===========================================================================
Copyright (C) 2023 the OpenMoHAA team
This file is part of OpenMoHAA source code.
OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "../qcommon/q_shared.h"
#include "../server/server.h"
#include "sv_gqueryreporting.h"
#include "sv_gamespy.h"
#include "gcdkey/gcdkeys.h"
#include "common/gsCommon.h"
#include "common/gsAvailable.h"
static char gamemode[128];
static qboolean gcdInitialized = qfalse;
static qboolean gcdValid = qfalse;
extern GSIACResult __GSIACResult;
static const char *SECRET_GS_KEYS[] =
{
"M5Fdwc",
"h2P1c9",
"y32FDc"
};
static const unsigned int GCD_GAME_IDS[] =
{
0,
641,
802,
};
static const char *GS_GAME_NAME[] =
{
"mohaa",
"mohaas",
"mohaab"
};
static const char *GS_GAME_NAME_DEMO[] =
{
"mohaa",
"mohaas",
"mohaabd"
};
static const char *GS_GAME_VERSION[] =
{
TARGET_GAME_VERSION_MOH,
TARGET_GAME_VERSION_MOHTA,
TARGET_GAME_VERSION_MOHTT,
};
static const char* GS_GAME_VERSION_DEMO[] =
{
TARGET_GAME_VERSION_MOH,
"d" TARGET_GAME_VERSION_MOHTA,
"d" TARGET_GAME_VERSION_MOHTT_DEMO,
};
static const unsigned int GAMESPY_DEFAULT_PORT = 12300;
void qr_send_statechanged(qr_t qrec);
void qr_shutdown(qr_t qrec);
void qr_process_queries(qr_t qrec);
int qr_init(
qr_t *qrec,
const char *ip,
int baseport,
const char *gamename,
const char *secret_key,
qr_querycallback_t qr_basic_callback,
qr_querycallback_t qr_info_callback,
qr_querycallback_t qr_rules_callback,
qr_querycallback_t qr_players_callback,
void *userdata
);
const char* GS_GetGameKey(unsigned int index) {
return SECRET_GS_KEYS[index];
}
const char* GS_GetCurrentGameKey() {
return GS_GetGameKey(com_target_game->integer);
}
unsigned int GS_GetGameID(unsigned int index) {
return GCD_GAME_IDS[index];
}
unsigned int GS_GetCurrentGameID() {
return GS_GetGameID(com_target_game->integer);
}
const char* GS_GetGameName(unsigned int index) {
if (!com_target_demo->integer) {
return GS_GAME_NAME[index];
} else {
return GS_GAME_NAME_DEMO[index];
}
}
const char* GS_GetCurrentGameName() {
return GS_GetGameName(com_target_game->integer);
}
const char* GS_GetGameVersion(unsigned int index) {
if (!com_target_demo->integer) {
return GS_GAME_VERSION[index];
} else {
return GS_GAME_VERSION_DEMO[index];
}
}
const char* GS_GetCurrentGameVersion() {
return GS_GetGameVersion(com_target_game->integer);
}
static const char *ConvertMapFilename(const char *mapname)
{
static char converted[1024];
const char *name = strstr(mapname, "/");
if (!name) {
return mapname;
}
strcpy(converted, name + 1);
return converted;
}
static void basic_callback(char *outbuf, int maxlen, void *userdata)
{
Info_SetValueForKey(outbuf, "gamename", GS_GetCurrentGameName());
Info_SetValueForKey(outbuf, "gamever", GS_GetCurrentGameVersion());
Info_SetValueForKey(outbuf, "location", va("%i", sv_location->integer));
if (sv_debug_gamespy->integer) {
Com_DPrintf("Basic callback, sent: %s\n\n", outbuf);
}
}
static void info_callback(char *outbuf, int maxlen, void *userdata)
{
char infostring[1024];
qboolean allowlean = qfalse;
infostring[0] = 0;
Info_SetValueForKey(infostring, "hostname", sv_hostname->string);
Info_SetValueForKey(infostring, "hostport", Cvar_Get("net_port", "12203", CVAR_LATCH)->string);
Info_SetValueForKey(infostring, "mapname", ConvertMapFilename(svs.mapName));
Info_SetValueForKey(infostring, "gametype", g_gametypestring->string);
Info_SetValueForKey(infostring, "numplayers", va("%i", SV_NumClients()));
Info_SetValueForKey(infostring, "maxplayers", va("%i", svs.iNumClients - sv_privateClients->integer));
Info_SetValueForKey(infostring, "gamemode", gamemode);
Info_SetValueForKey(infostring, "gametype_i", va("%i", g_gametype->integer));
Info_SetValueForKey(infostring, "dedicated", Cvar_Get("ui_dedicated", "0", 0)->string);
Info_SetValueForKey(infostring, "sprinton", Cvar_Get("sv_sprinton", "1", 0)->string);
Info_SetValueForKey(infostring, "realism", Cvar_Get("g_realismmode", "0", 0)->string);
Info_SetValueForKey(infostring, "pure", va("%i", sv_pure->integer));
if ((Cvar_VariableIntegerValue("dmflags") & DF_ALLOW_LEAN_MOVEMENT) != 0) {
allowlean = 1;
}
Info_SetValueForKey(infostring, "allowlean", va("%i", allowlean));
if (strlen(infostring) < maxlen) {
strcpy(outbuf, infostring);
}
if (sv_debug_gamespy->integer) {
Com_DPrintf("Info callback, sent: %s\n\n", outbuf);
}
}
static void rules_callback(char *outbuf, int maxlen, void *userdata)
{
char infostring[1024];
infostring[0] = 0;
Info_SetValueForKey(infostring, "timelimit", Cvar_VariableString("timelimit"));
Info_SetValueForKey(infostring, "fraglimit", Cvar_VariableString("fraglimit"));
Info_SetValueForKey(infostring, "rankedserver", Cvar_VariableString("g_rankedserver"));
if (strlen(infostring) < maxlen) {
strcpy(outbuf, infostring);
}
if (sv_debug_gamespy->integer) {
Com_DPrintf("Rules callback, sent: %s\n\n", outbuf);
}
}
static void players_callback(char *outbuf, int maxlen, void *userdata)
{
int i;
char infostring[128];
size_t infolen;
size_t currlen = 0;
if (!svs.clients) {
return;
}
for (i = 0; i < svs.iNumClients; i++) {
if (svs.clients[i].state == CS_FREE) {
// ignore inactive clients
continue;
}
playerState_t *ps = SV_GameClientNum(i);
Com_sprintf(
infostring,
128,
"\\player_%d\\%s\\frags_%d\\%d\\deaths_%d\\%d\\ping_%d\\%d",
i,
svs.clients[i].name,
i,
ps->stats[STAT_KILLS],
i,
ps->stats[STAT_DEATHS],
i,
svs.clients[i].ping
);
infolen = strlen(infostring);
if (currlen + infolen < maxlen) {
strcat(outbuf, infostring);
currlen += infolen;
}
}
}
void SV_GamespyHeartbeat()
{
if (g_gametype->integer == GT_SINGLE_PLAYER || !sv_gamespy->integer) {
return;
}
if (sv_debug_gamespy->integer) {
Com_DPrintf("GameSpy Heartbeat\n");
}
qr_send_statechanged(NULL);
}
void SV_ProcessGamespyQueries()
{
if (g_gametype->integer == GT_SINGLE_PLAYER || !sv_gamespy->integer) {
return;
}
qr_process_queries(NULL);
if (gcdValid) {
// in case the game supports gcd
gcd_think();
}
}
void SV_ShutdownGamespy()
{
if (g_gametype->integer == GT_SINGLE_PLAYER || !sv_gamespy->integer) {
return;
}
strcpy(gamemode, "exiting");
if (gcdInitialized) {
gcd_shutdown();
gcdInitialized = qfalse;
}
qr_send_statechanged(NULL);
qr_shutdown(NULL);
}
qboolean SV_InitGamespy()
{
cvar_t *net_ip;
cvar_t *net_gamespy_port;
char secret_key[9];
const char *secret_gs_key;
const char *gs_game_name;
int gcd_game_id;
if (com_target_game->integer > ARRAY_LEN(SECRET_GS_KEYS)) {
Com_Error(ERR_DROP, "Invalid target game %d for GameSpy", com_target_game->integer);
return qfalse;
}
secret_gs_key = GS_GetCurrentGameKey();
gcd_game_id = GS_GetCurrentGameID();
gs_game_name = GS_GetCurrentGameName();
sv_debug_gamespy = Cvar_Get("sv_debuggamespy", "0", 0);
sv_location = Cvar_Get("sv_location", "1", CVAR_ARCHIVE);
sv_gamespy = Cvar_Get("sv_gamespy", "1", CVAR_LATCH);
if (!sv_gamespy->integer || g_gametype->integer == GT_SINGLE_PLAYER) {
return qfalse;
}
strcpy(gamemode, "openplaying");
strcpy(secret_key, secret_gs_key);
net_ip = Cvar_Get("net_ip", "localhost", CVAR_LATCH);
net_gamespy_port = Cvar_Get("net_gamespy_port", va("%i", GAMESPY_DEFAULT_PORT), CVAR_LATCH);
if (qr_init(
NULL,
net_ip->string,
net_gamespy_port->integer,
gs_game_name,
secret_key,
basic_callback,
info_callback,
rules_callback,
players_callback,
NULL
)) {
Com_DPrintf("Error starting query sockets in SV_GamespyInit\n");
return qfalse;
}
if (!sv_gamespy->integer) {
strcpy(gamemode, "exiting");
qr_send_statechanged(NULL);
}
// this will set the GSI as available for cdkey authorization
__GSIACResult = GSIACAvailable;
if (!gcdInitialized) {
if (gcd_game_id) {
gcd_init(gcd_game_id);
gcdValid = qtrue;
}
gcdInitialized = qtrue;
}
return qtrue;
}
void SV_CreateGamespyChallenge(char* challenge)
{
int i;
for (i = 0; i < 8; i++) {
challenge[i] = rand() % ('Z' - 'A' + 1) + 'A'; // random letters between A and Z
}
challenge[i] = 0;
}
challenge_t* FindChallengeById(int gameid)
{
challenge_t* challenge;
int i;
for (i = 0; i < MAX_CHALLENGES; i++) {
challenge = &svs.challenges[i];
if (challenge->connected == gameid) {
return challenge;
}
}
return NULL;
}
void AuthenticateCallback(int gameid, int localid, int authenticated, char* errmsg, void* instance)
{
challenge_t* challenge;
qboolean valid = qfalse;
if (localid || !Q_stricmp(errmsg, "CD Key in use")) {
valid = qtrue;
}
challenge = FindChallengeById(gameid);
if (valid)
{
challenge->cdkeyState = 2;
challenge->pingTime = svs.time;
SV_NET_OutOfBandPrint(&svs.netprofile, challenge->adr, "challengeResponse %i", challenge->challenge);
}
else
{
char buf[32];
if (!challenge) {
return;
}
Com_sprintf(buf, sizeof(buf), "%d.%d.%d.%d", challenge->adr.ip[0], challenge->adr.ip[1], challenge->adr.ip[2], challenge->adr.ip[3]);
Com_Printf("%s failed cdkey authorization\n", buf);
challenge->cdkeyState = 3;
// tell the client about the reason
SV_NET_OutOfBandPrint(&svs.netprofile, challenge->adr, "droperror\nServer rejected connection:\n%s", errmsg);
}
}
void RefreshAuthCallback(int gameid, int localid, int hint, char* challenge, void* instance)
{
}
void SV_GamespyAuthorize(netadr_t from, const char* response)
{
char buf[64];
challenge_t* challenge = FindChallenge(from, qtrue);
if (!challenge) {
return;
}
switch (challenge->cdkeyState)
{
case CDKS_NONE:
challenge->cdkeyState = CDKS_AUTHENTICATING;
challenge->firstTime = svs.time;
gcd_authenticate_user(
GS_GetCurrentGameID(),
challenge->gamespyId,
LittleLong(*(unsigned int*)from.ip),
challenge->gsChallenge,
response,
AuthenticateCallback,
RefreshAuthCallback,
NULL
);
break;
case CDKS_AUTHENTICATING:
// the server can't reach the authentication server
// let the client connect
if (svs.time - challenge->firstTime > 5000)
{
Com_DPrintf("authorize server timed out\n");
challenge->cdkeyState = CDKS_AUTHENTICATED;
challenge->pingTime = svs.time;
SV_NET_OutOfBandPrint(&svs.netprofile, from, "challengeResponse %i", challenge->challenge);
}
break;
case CDKS_AUTHENTICATED:
SV_NET_OutOfBandPrint(&svs.netprofile, from, "challengeResponse %i", challenge->challenge);
break;
case CDKS_FAILED:
// authentication server told the cdkey was invalid
Com_sprintf(buf, sizeof(buf), "%d.%d.%d.%d", challenge->adr.ip[0], challenge->adr.ip[1], challenge->adr.ip[2], challenge->adr.ip[3]);
Com_Printf("%s failed cdkey authorization\n", buf);
// reject the client
SV_NET_OutOfBandPrint(&svs.netprofile, from, "droperror\nServer rejected connection:\nInvalid CD Key");
break;
default:
break;
}
}
|