Merge pull request #441 from woogerboy21/servatrice_trustedsources
Servatrice trustedsources
This commit is contained in:
commit
8b0d67ea5c
3 changed files with 631 additions and 613 deletions
|
@ -22,7 +22,7 @@ port=4747
|
|||
; long delays (lag), you may want to try increasing this value; default is 1.
|
||||
number_pools=1
|
||||
|
||||
; When database is enabled, servatrice writes the server status in the "update" database table; this
|
||||
; When database is enabled, servatrice writes the server status in the "update" database table; this
|
||||
; setting defines every how many milliseconds servatrice will update its status; default is 15000 (15 secs)
|
||||
statusupdate=15000
|
||||
|
||||
|
@ -125,6 +125,11 @@ max_game_inactivity_time=120
|
|||
; Maximum number of users that can connect from the same IP address; useful to avoid bots, default is 4
|
||||
max_users_per_address=4
|
||||
|
||||
; You may want to allow an unlimited number of users from a trusted source. This setting can contain a
|
||||
; comma-separed list of IP addresses which will allow an unlimited number of connections from each of the
|
||||
; IP addresses listed (ignoring the max_users_per_address). Default is "127.0.0.1,::1"; example: "192.73.233.244,81.4.100.74"
|
||||
trusted_sources="127.0.0.1,::1"
|
||||
|
||||
; Servatrice can avoid users from flooding rooms with large number messages in an interval of time.
|
||||
; This setting defines the length in seconds of the considered interval; default is 10
|
||||
message_counting_interval=10
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <QTimer>
|
||||
#include <QDateTime>
|
||||
#include <QDebug>
|
||||
#include <QString>
|
||||
#include <iostream>
|
||||
#include "servatrice.h"
|
||||
#include "servatrice_database_interface.h"
|
||||
|
@ -45,25 +46,25 @@ Servatrice_GameServer::Servatrice_GameServer(Servatrice *_server, int _numberPoo
|
|||
server->setThreaded(false);
|
||||
Servatrice_DatabaseInterface *newDatabaseInterface = new Servatrice_DatabaseInterface(0, server);
|
||||
Servatrice_ConnectionPool *newPool = new Servatrice_ConnectionPool(newDatabaseInterface);
|
||||
|
||||
|
||||
server->addDatabaseInterface(thread(), newDatabaseInterface);
|
||||
newDatabaseInterface->initDatabase(_sqlDatabase);
|
||||
|
||||
|
||||
connectionPools.append(newPool);
|
||||
} else
|
||||
for (int i = 0; i < _numberPools; ++i) {
|
||||
Servatrice_DatabaseInterface *newDatabaseInterface = new Servatrice_DatabaseInterface(i, server);
|
||||
Servatrice_ConnectionPool *newPool = new Servatrice_ConnectionPool(newDatabaseInterface);
|
||||
|
||||
|
||||
QThread *newThread = new QThread;
|
||||
newThread->setObjectName("pool_" + QString::number(i));
|
||||
newPool->moveToThread(newThread);
|
||||
newDatabaseInterface->moveToThread(newThread);
|
||||
server->addDatabaseInterface(newThread, newDatabaseInterface);
|
||||
|
||||
|
||||
newThread->start();
|
||||
QMetaObject::invokeMethod(newDatabaseInterface, "initDatabase", Qt::BlockingQueuedConnection, Q_ARG(QSqlDatabase, _sqlDatabase));
|
||||
|
||||
|
||||
connectionPools.append(newPool);
|
||||
}
|
||||
}
|
||||
|
@ -98,12 +99,12 @@ void Servatrice_GameServer::incomingConnection(qintptr socketDescriptor)
|
|||
}
|
||||
qDebug() << "Pool utilisation:" << debugStr;
|
||||
Servatrice_ConnectionPool *pool = connectionPools[poolIndex];
|
||||
|
||||
|
||||
ServerSocketInterface *ssi = new ServerSocketInterface(server, pool->getDatabaseInterface());
|
||||
ssi->moveToThread(pool->thread());
|
||||
pool->addClient();
|
||||
connect(ssi, SIGNAL(destroyed()), pool, SLOT(removeClient()));
|
||||
|
||||
|
||||
QMetaObject::invokeMethod(ssi, "initConnection", Qt::QueuedConnection, Q_ARG(int, socketDescriptor));
|
||||
}
|
||||
|
||||
|
@ -111,11 +112,11 @@ void Servatrice_IslServer::incomingConnection(int socketDescriptor)
|
|||
{
|
||||
QThread *thread = new QThread;
|
||||
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
|
||||
|
||||
|
||||
IslInterface *interface = new IslInterface(socketDescriptor, cert, privateKey, server);
|
||||
interface->moveToThread(thread);
|
||||
connect(interface, SIGNAL(destroyed()), thread, SLOT(quit()));
|
||||
|
||||
|
||||
thread->start();
|
||||
QMetaObject::invokeMethod(interface, "initServer", Qt::QueuedConnection);
|
||||
}
|
||||
|
@ -137,7 +138,7 @@ bool Servatrice::initServer()
|
|||
serverName = settingsCache->value("server/name", "My Cockatrice server").toString();
|
||||
serverId = settingsCache->value("server/id", 0).toInt();
|
||||
bool regServerOnly = settingsCache->value("authentication/regonly", 0).toBool();
|
||||
|
||||
|
||||
const QString authenticationMethodStr = settingsCache->value("authentication/method").toString();
|
||||
if (authenticationMethodStr == "sql") {
|
||||
qDebug() << "Authenticating method: sql";
|
||||
|
@ -148,22 +149,22 @@ bool Servatrice::initServer()
|
|||
} else {
|
||||
if (regServerOnly) {
|
||||
qDebug() << "Registration only server enabled but no authentication method defined: Error.";
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
qDebug() << "Authenticating method: none";
|
||||
authenticationMethod = AuthenticationNone;
|
||||
}
|
||||
|
||||
|
||||
QString dbTypeStr = settingsCache->value("database/type").toString();
|
||||
if (dbTypeStr == "mysql")
|
||||
databaseType = DatabaseMySql;
|
||||
else
|
||||
databaseType = DatabaseNone;
|
||||
|
||||
|
||||
servatriceDatabaseInterface = new Servatrice_DatabaseInterface(-1, this);
|
||||
setDatabaseInterface(servatriceDatabaseInterface);
|
||||
|
||||
|
||||
if (databaseType != DatabaseNone) {
|
||||
settingsCache->beginGroup("database");
|
||||
dbPrefix = settingsCache->value("prefix").toString();
|
||||
|
@ -173,13 +174,13 @@ bool Servatrice::initServer()
|
|||
settingsCache->value("user").toString(),
|
||||
settingsCache->value("password").toString());
|
||||
settingsCache->endGroup();
|
||||
|
||||
|
||||
updateServerList();
|
||||
|
||||
|
||||
qDebug() << "Clearing previous sessions...";
|
||||
servatriceDatabaseInterface->clearSessionTables();
|
||||
}
|
||||
|
||||
|
||||
const QString roomMethod = settingsCache->value("rooms/method").toString();
|
||||
if (roomMethod == "sql") {
|
||||
QSqlQuery query(servatriceDatabaseInterface->getDatabase());
|
||||
|
@ -193,7 +194,7 @@ bool Servatrice::initServer()
|
|||
QStringList gameTypes;
|
||||
while (query2.next())
|
||||
gameTypes.append(query2.value(0).toString());
|
||||
|
||||
|
||||
addRoom(new Server_Room(query.value(0).toInt(),
|
||||
query.value(1).toString(),
|
||||
query.value(2).toString(),
|
||||
|
@ -207,7 +208,7 @@ bool Servatrice::initServer()
|
|||
int size = settingsCache->beginReadArray("rooms/roomlist");
|
||||
for (int i = 0; i < size; ++i) {
|
||||
settingsCache->setArrayIndex(i);
|
||||
|
||||
|
||||
QStringList gameTypes;
|
||||
int size2 = settingsCache->beginReadArray("game_types");
|
||||
for (int j = 0; j < size2; ++j) {
|
||||
|
@ -215,7 +216,7 @@ bool Servatrice::initServer()
|
|||
gameTypes.append(settingsCache->value("name").toString());
|
||||
}
|
||||
settingsCache->endArray();
|
||||
|
||||
|
||||
Server_Room *newRoom = new Server_Room(
|
||||
i,
|
||||
settingsCache->value("name").toString(),
|
||||
|
@ -240,17 +241,17 @@ bool Servatrice::initServer()
|
|||
QStringList("Standard"),
|
||||
this
|
||||
);
|
||||
addRoom(newRoom);
|
||||
addRoom(newRoom);
|
||||
}
|
||||
|
||||
settingsCache->endArray();
|
||||
}
|
||||
|
||||
|
||||
updateLoginMessage();
|
||||
|
||||
|
||||
maxGameInactivityTime = settingsCache->value("game/max_game_inactivity_time", 120).toInt();
|
||||
maxPlayerInactivityTime = settingsCache->value("game/max_player_inactivity_time", 15).toInt();
|
||||
|
||||
|
||||
maxUsersPerAddress = settingsCache->value("security/max_users_per_address", 4).toInt();
|
||||
messageCountingInterval = settingsCache->value("security/message_counting_interval", 10).toInt();
|
||||
maxMessageCountPerInterval = settingsCache->value("security/max_message_count_per_interval", 10).toInt();
|
||||
|
@ -283,7 +284,7 @@ bool Servatrice::initServer()
|
|||
QSslKey key(&keyFile, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey);
|
||||
if (key.isNull())
|
||||
throw QString("Invalid private key.");
|
||||
|
||||
|
||||
QMutableListIterator<ServerProperties> serverIterator(serverList);
|
||||
while (serverIterator.hasNext()) {
|
||||
const ServerProperties &prop = serverIterator.next();
|
||||
|
@ -291,22 +292,22 @@ bool Servatrice::initServer()
|
|||
serverIterator.remove();
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
QThread *thread = new QThread;
|
||||
thread->setObjectName("isl_" + QString::number(prop.id));
|
||||
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
|
||||
|
||||
|
||||
IslInterface *interface = new IslInterface(prop.id, prop.hostname, prop.address.toString(), prop.controlPort, prop.cert, cert, key, this);
|
||||
interface->moveToThread(thread);
|
||||
connect(interface, SIGNAL(destroyed()), thread, SLOT(quit()));
|
||||
|
||||
|
||||
thread->start();
|
||||
QMetaObject::invokeMethod(interface, "initClient", Qt::BlockingQueuedConnection);
|
||||
}
|
||||
|
||||
|
||||
const int networkPort = settingsCache->value("servernetwork/port", 14747).toInt();
|
||||
qDebug() << "Starting ISL server on port" << networkPort;
|
||||
|
||||
|
||||
islServer = new Servatrice_IslServer(this, cert, key, this);
|
||||
if (islServer->listen(QHostAddress::Any, networkPort))
|
||||
qDebug() << "ISL server listening.";
|
||||
|
@ -316,11 +317,11 @@ bool Servatrice::initServer()
|
|||
qDebug() << "ERROR --" << error;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
pingClock = new QTimer(this);
|
||||
connect(pingClock, SIGNAL(timeout()), this, SIGNAL(pingClockTimeout()));
|
||||
pingClock->start(1000);
|
||||
|
||||
|
||||
int statusUpdateTime = settingsCache->value("server/statusupdate", 15000).toInt();
|
||||
statusUpdateClock = new QTimer(this);
|
||||
connect(statusUpdateClock, SIGNAL(timeout()), this, SLOT(statusUpdate()));
|
||||
|
@ -328,7 +329,7 @@ bool Servatrice::initServer()
|
|||
qDebug() << "Starting status update clock, interval " << statusUpdateTime << " ms";
|
||||
statusUpdateClock->start(statusUpdateTime);
|
||||
}
|
||||
|
||||
|
||||
const int numberPools = settingsCache->value("server/number_pools", 1).toInt();
|
||||
gameServer = new Servatrice_GameServer(this, numberPools, servatriceDatabaseInterface->getDatabase(), this);
|
||||
gameServer->setMaxPendingConnections(1000);
|
||||
|
@ -351,10 +352,10 @@ void Servatrice::addDatabaseInterface(QThread *thread, Servatrice_DatabaseInterf
|
|||
void Servatrice::updateServerList()
|
||||
{
|
||||
qDebug() << "Updating server list...";
|
||||
|
||||
|
||||
serverListMutex.lock();
|
||||
serverList.clear();
|
||||
|
||||
|
||||
QSqlQuery query(servatriceDatabaseInterface->getDatabase());
|
||||
query.prepare("select id, ssl_cert, hostname, address, game_port, control_port from " + dbPrefix + "_servers order by id asc");
|
||||
servatriceDatabaseInterface->execSqlQuery(query);
|
||||
|
@ -363,7 +364,7 @@ void Servatrice::updateServerList()
|
|||
serverList.append(prop);
|
||||
qDebug() << QString("#%1 CERT=%2 NAME=%3 IP=%4:%5 CPORT=%6").arg(prop.id).arg(QString(prop.cert.digest().toHex())).arg(prop.hostname).arg(prop.address.toString()).arg(prop.gamePort).arg(prop.controlPort);
|
||||
}
|
||||
|
||||
|
||||
serverListMutex.unlock();
|
||||
}
|
||||
|
||||
|
@ -372,7 +373,7 @@ QList<ServerProperties> Servatrice::getServerList() const
|
|||
serverListMutex.lock();
|
||||
QList<ServerProperties> result = serverList;
|
||||
serverListMutex.unlock();
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -381,8 +382,9 @@ int Servatrice::getUsersWithAddress(const QHostAddress &address) const
|
|||
int result = 0;
|
||||
QReadLocker locker(&clientsLock);
|
||||
for (int i = 0; i < clients.size(); ++i)
|
||||
if (static_cast<ServerSocketInterface *>(clients[i])->getPeerAddress() == address)
|
||||
++result;
|
||||
if (static_cast<ServerSocketInterface *>(clients[i])->getPeerAddress() == address)
|
||||
++result;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -400,18 +402,18 @@ void Servatrice::updateLoginMessage()
|
|||
{
|
||||
if (!servatriceDatabaseInterface->checkSql())
|
||||
return;
|
||||
|
||||
|
||||
QSqlQuery query(servatriceDatabaseInterface->getDatabase());
|
||||
query.prepare("select message from " + dbPrefix + "_servermessages where id_server = :id_server order by timest desc limit 1");
|
||||
query.bindValue(":id_server", serverId);
|
||||
if (servatriceDatabaseInterface->execSqlQuery(query))
|
||||
if (query.next()) {
|
||||
const QString newLoginMessage = query.value(0).toString();
|
||||
|
||||
|
||||
loginMessageMutex.lock();
|
||||
loginMessage = newLoginMessage;
|
||||
loginMessageMutex.unlock();
|
||||
|
||||
|
||||
Event_ServerMessage event;
|
||||
event.set_message(newLoginMessage.toStdString());
|
||||
SessionEvent *se = Server_ProtocolHandler::prepareSessionEvent(event);
|
||||
|
@ -426,12 +428,12 @@ void Servatrice::statusUpdate()
|
|||
{
|
||||
if (!servatriceDatabaseInterface->checkSql())
|
||||
return;
|
||||
|
||||
|
||||
const int uc = getUsersCount(); // for correct mutex locking order
|
||||
const int gc = getGamesCount();
|
||||
|
||||
|
||||
uptime += statusUpdateClock->interval() / 1000;
|
||||
|
||||
|
||||
txBytesMutex.lock();
|
||||
quint64 tx = txBytes;
|
||||
txBytes = 0;
|
||||
|
@ -440,7 +442,7 @@ void Servatrice::statusUpdate()
|
|||
quint64 rx = rxBytes;
|
||||
rxBytes = 0;
|
||||
rxBytesMutex.unlock();
|
||||
|
||||
|
||||
QSqlQuery query(servatriceDatabaseInterface->getDatabase());
|
||||
query.prepare("insert into " + dbPrefix + "_uptime (id_server, timest, uptime, users_count, games_count, tx_bytes, rx_bytes) values(:id, NOW(), :uptime, :users_count, :games_count, :tx, :rx)");
|
||||
query.bindValue(":id", serverId);
|
||||
|
@ -481,7 +483,7 @@ void Servatrice::incRxBytes(quint64 num)
|
|||
void Servatrice::shutdownTimeout()
|
||||
{
|
||||
--shutdownMinutes;
|
||||
|
||||
|
||||
SessionEvent *se;
|
||||
if (shutdownMinutes) {
|
||||
Event_ServerShutdown event;
|
||||
|
@ -493,13 +495,13 @@ void Servatrice::shutdownTimeout()
|
|||
event.set_reason(Event_ConnectionClosed::SERVER_SHUTDOWN);
|
||||
se = Server_ProtocolHandler::prepareSessionEvent(event);
|
||||
}
|
||||
|
||||
|
||||
clientsLock.lockForRead();
|
||||
for (int i = 0; i < clients.size(); ++i)
|
||||
clients[i]->sendProtocolItem(*se);
|
||||
clientsLock.unlock();
|
||||
delete se;
|
||||
|
||||
|
||||
if (!shutdownMinutes)
|
||||
deleteLater();
|
||||
}
|
||||
|
@ -507,14 +509,14 @@ void Servatrice::shutdownTimeout()
|
|||
bool Servatrice::islConnectionExists(int serverId) const
|
||||
{
|
||||
// Only call with islLock locked at least for reading
|
||||
|
||||
|
||||
return islInterfaces.contains(serverId);
|
||||
}
|
||||
|
||||
void Servatrice::addIslInterface(int serverId, IslInterface *interface)
|
||||
{
|
||||
// Only call with islLock locked for writing
|
||||
|
||||
|
||||
islInterfaces.insert(serverId, interface);
|
||||
connect(interface, SIGNAL(externalUserJoined(ServerInfo_User)), this, SLOT(externalUserJoined(ServerInfo_User)));
|
||||
connect(interface, SIGNAL(externalUserLeft(QString)), this, SLOT(externalUserLeft(QString)));
|
||||
|
@ -531,7 +533,7 @@ void Servatrice::addIslInterface(int serverId, IslInterface *interface)
|
|||
void Servatrice::removeIslInterface(int serverId)
|
||||
{
|
||||
// Only call with islLock locked for writing
|
||||
|
||||
|
||||
// XXX we probably need to delete everything that belonged to it...
|
||||
islInterfaces.remove(serverId);
|
||||
}
|
||||
|
@ -539,7 +541,7 @@ void Servatrice::removeIslInterface(int serverId)
|
|||
void Servatrice::doSendIslMessage(const IslMessage &msg, int serverId)
|
||||
{
|
||||
QReadLocker locker(&islLock);
|
||||
|
||||
|
||||
if (serverId == -1) {
|
||||
QMapIterator<int, IslInterface *> islIterator(islInterfaces);
|
||||
while (islIterator.hasNext())
|
||||
|
|
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue