mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-10 12:23:58 -07:00
Turn Card, Deck_List, Protocol, RNG, Network (Client, Server), Settings and Utility into libraries and remove cockatrice_common. (#6212)
--------- Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de> Co-authored-by: ebbit1q <ebbit1q@gmail.com>
This commit is contained in:
parent
be1403c920
commit
1ef07309d6
605 changed files with 3812 additions and 3408 deletions
|
|
@ -0,0 +1,24 @@
|
|||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
|
||||
set(HEADERS remote_client.h)
|
||||
|
||||
set(SOURCES remote_client.cpp)
|
||||
|
||||
if(Qt6_FOUND)
|
||||
qt6_wrap_cpp(MOC_SOURCES ${HEADERS})
|
||||
elseif(Qt5_FOUND)
|
||||
qt5_wrap_cpp(MOC_SOURCES ${HEADERS})
|
||||
endif()
|
||||
|
||||
add_library(libcockatrice_network_client_remote STATIC ${MOC_SOURCES} ${SOURCES})
|
||||
|
||||
add_dependencies(libcockatrice_network_client_remote libcockatrice_network_client_abstract libcockatrice_protocol)
|
||||
|
||||
target_include_directories(libcockatrice_network_client_remote PUBLIC .)
|
||||
|
||||
target_link_libraries(
|
||||
libcockatrice_network_client_remote PUBLIC ${COCKATRICE_QT_MODULES} libcockatrice_network_client_abstract
|
||||
libcockatrice_settings libcockatrice_utility libcockatrice_protocol
|
||||
)
|
||||
|
|
@ -0,0 +1,730 @@
|
|||
#include "remote_client.h"
|
||||
|
||||
#include "../../../../cockatrice/src/main.h"
|
||||
#include "version_string.h"
|
||||
|
||||
#include <QCryptographicHash>
|
||||
#include <QDebug>
|
||||
#include <QHostAddress>
|
||||
#include <QHostInfo>
|
||||
#include <QList>
|
||||
#include <QThread>
|
||||
#include <QTimer>
|
||||
#include <QWebSocket>
|
||||
#include <libcockatrice/protocol/debug_pb_message.h>
|
||||
#include <libcockatrice/protocol/pb/event_server_identification.pb.h>
|
||||
#include <libcockatrice/protocol/pb/response_activate.pb.h>
|
||||
#include <libcockatrice/protocol/pb/response_forgotpasswordrequest.pb.h>
|
||||
#include <libcockatrice/protocol/pb/response_login.pb.h>
|
||||
#include <libcockatrice/protocol/pb/response_password_salt.pb.h>
|
||||
#include <libcockatrice/protocol/pb/response_register.pb.h>
|
||||
#include <libcockatrice/protocol/pb/server_message.pb.h>
|
||||
#include <libcockatrice/protocol/pb/session_commands.pb.h>
|
||||
#include <libcockatrice/protocol/pending_command.h>
|
||||
#include <libcockatrice/settings/cache_settings.h>
|
||||
#include <libcockatrice/utility/passwordhasher.h>
|
||||
|
||||
static const unsigned int protocolVersion = 14;
|
||||
|
||||
RemoteClient::RemoteClient(QObject *parent)
|
||||
: AbstractClient(parent), timeRunning(0), lastDataReceived(0), messageInProgress(false), handshakeStarted(false),
|
||||
usingWebSocket(false), messageLength(0), hashedPassword()
|
||||
{
|
||||
|
||||
clearNewClientFeatures();
|
||||
maxTimeout = SettingsCache::instance().getTimeOut();
|
||||
int keepalive = SettingsCache::instance().getKeepAlive();
|
||||
timer = new QTimer(this);
|
||||
timer->setInterval(keepalive * 1000);
|
||||
connect(timer, &QTimer::timeout, this, &RemoteClient::ping);
|
||||
|
||||
socket = new QTcpSocket(this);
|
||||
socket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
|
||||
connect(socket, &QTcpSocket::connected, this, &RemoteClient::slotConnected);
|
||||
connect(socket, &QTcpSocket::readyRead, this, &RemoteClient::readData);
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
|
||||
connect(socket, &QTcpSocket::errorOccurred, this, &RemoteClient::slotSocketError);
|
||||
#else
|
||||
connect(socket, qOverload<QAbstractSocket::SocketError>(&QTcpSocket::error), this, &RemoteClient::slotSocketError);
|
||||
#endif
|
||||
|
||||
websocket = new QWebSocket(QString(), QWebSocketProtocol::VersionLatest, this);
|
||||
connect(websocket, &QWebSocket::binaryMessageReceived, this, &RemoteClient::websocketMessageReceived);
|
||||
connect(websocket, &QWebSocket::connected, this, &RemoteClient::slotConnected);
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
|
||||
connect(websocket, &QWebSocket::errorOccurred, this, &RemoteClient::slotWebSocketError);
|
||||
#else
|
||||
connect(websocket, qOverload<QAbstractSocket::SocketError>(&QWebSocket::error), this,
|
||||
&RemoteClient::slotWebSocketError);
|
||||
#endif
|
||||
|
||||
connect(this, &RemoteClient::serverIdentificationEventReceived, this,
|
||||
&RemoteClient::processServerIdentificationEvent);
|
||||
connect(this, &RemoteClient::connectionClosedEventReceived, this, &RemoteClient::processConnectionClosedEvent);
|
||||
connect(this, &RemoteClient::sigConnectToServer, this, &RemoteClient::doConnectToServer);
|
||||
connect(this, &RemoteClient::sigDisconnectFromServer, this, &RemoteClient::doDisconnectFromServer);
|
||||
connect(this, &RemoteClient::sigRegisterToServer, this, &RemoteClient::doRegisterToServer);
|
||||
connect(this, &RemoteClient::sigActivateToServer, this, &RemoteClient::doActivateToServer);
|
||||
connect(this, &RemoteClient::sigRequestForgotPasswordToServer, this,
|
||||
&RemoteClient::doRequestForgotPasswordToServer);
|
||||
connect(this, &RemoteClient::sigSubmitForgotPasswordResetToServer, this,
|
||||
&RemoteClient::doSubmitForgotPasswordResetToServer);
|
||||
connect(this, &RemoteClient::sigSubmitForgotPasswordChallengeToServer, this,
|
||||
&RemoteClient::doSubmitForgotPasswordChallengeToServer);
|
||||
}
|
||||
|
||||
RemoteClient::~RemoteClient()
|
||||
{
|
||||
doDisconnectFromServer();
|
||||
thread()->quit();
|
||||
}
|
||||
|
||||
void RemoteClient::slotSocketError(QAbstractSocket::SocketError /*error*/)
|
||||
{
|
||||
QString errorString = socket->errorString();
|
||||
doDisconnectFromServer();
|
||||
emit socketError(errorString);
|
||||
}
|
||||
|
||||
void RemoteClient::slotWebSocketError(QAbstractSocket::SocketError /*error*/)
|
||||
{
|
||||
|
||||
QString errorString = websocket->errorString();
|
||||
if (getStatus() != ClientStatus::StatusDisconnected) {
|
||||
doDisconnectFromServer();
|
||||
emit socketError(errorString);
|
||||
}
|
||||
}
|
||||
|
||||
void RemoteClient::slotConnected()
|
||||
{
|
||||
timeRunning = lastDataReceived = 0;
|
||||
timer->start();
|
||||
|
||||
if (!usingWebSocket) {
|
||||
// dirty hack to be compatible with v14 server
|
||||
sendCommandContainer(CommandContainer());
|
||||
getNewCmdId();
|
||||
// end of hack
|
||||
}
|
||||
}
|
||||
|
||||
void RemoteClient::processServerIdentificationEvent(const Event_ServerIdentification &event)
|
||||
{
|
||||
if (event.protocol_version() != protocolVersion) {
|
||||
emit protocolVersionMismatch(protocolVersion, event.protocol_version());
|
||||
setStatus(StatusDisconnecting);
|
||||
return;
|
||||
}
|
||||
serverSupportsPasswordHash = event.server_options() & Event_ServerIdentification::SupportsPasswordHash;
|
||||
|
||||
if (getStatus() == StatusRequestingForgotPassword) {
|
||||
Command_ForgotPasswordRequest cmdForgotPasswordRequest;
|
||||
cmdForgotPasswordRequest.set_user_name(userName.toStdString());
|
||||
cmdForgotPasswordRequest.set_clientid(getSrvClientID(lastHostname).toStdString());
|
||||
PendingCommand *pend = prepareSessionCommand(cmdForgotPasswordRequest);
|
||||
connect(pend, &PendingCommand::finished, this, &RemoteClient::requestForgotPasswordResponse);
|
||||
sendCommand(pend);
|
||||
return;
|
||||
}
|
||||
|
||||
if (getStatus() == StatusSubmitForgotPasswordReset) {
|
||||
Command_ForgotPasswordReset cmdForgotPasswordReset;
|
||||
cmdForgotPasswordReset.set_user_name(userName.toStdString());
|
||||
cmdForgotPasswordReset.set_clientid(getSrvClientID(lastHostname).toStdString());
|
||||
cmdForgotPasswordReset.set_token(token.toStdString());
|
||||
if (!password.isEmpty() && serverSupportsPasswordHash) {
|
||||
auto passwordSalt = PasswordHasher::generateRandomSalt();
|
||||
hashedPassword = PasswordHasher::computeHash(password, passwordSalt);
|
||||
cmdForgotPasswordReset.set_hashed_new_password(hashedPassword.toStdString());
|
||||
} else if (!password.isEmpty()) {
|
||||
qCWarning(RemoteClientLog) << "using plain text password to reset password";
|
||||
cmdForgotPasswordReset.set_new_password(password.toStdString());
|
||||
}
|
||||
PendingCommand *pend = prepareSessionCommand(cmdForgotPasswordReset);
|
||||
connect(pend, &PendingCommand::finished, this, &RemoteClient::submitForgotPasswordResetResponse);
|
||||
sendCommand(pend);
|
||||
return;
|
||||
}
|
||||
|
||||
if (getStatus() == StatusSubmitForgotPasswordChallenge) {
|
||||
Command_ForgotPasswordChallenge cmdForgotPasswordChallenge;
|
||||
cmdForgotPasswordChallenge.set_user_name(userName.toStdString());
|
||||
cmdForgotPasswordChallenge.set_clientid(getSrvClientID(lastHostname).toStdString());
|
||||
cmdForgotPasswordChallenge.set_email(email.toStdString());
|
||||
PendingCommand *pend = prepareSessionCommand(cmdForgotPasswordChallenge);
|
||||
connect(pend, &PendingCommand::finished, this, &RemoteClient::submitForgotPasswordChallengeResponse);
|
||||
sendCommand(pend);
|
||||
return;
|
||||
}
|
||||
|
||||
if (getStatus() == StatusRegistering) {
|
||||
Command_Register cmdRegister;
|
||||
cmdRegister.set_user_name(userName.toStdString());
|
||||
if (!password.isEmpty() && serverSupportsPasswordHash) {
|
||||
auto passwordSalt = PasswordHasher::generateRandomSalt();
|
||||
hashedPassword = PasswordHasher::computeHash(password, passwordSalt);
|
||||
cmdRegister.set_hashed_password(hashedPassword.toStdString());
|
||||
} else if (!password.isEmpty()) {
|
||||
qCWarning(RemoteClientLog) << "using plain text password to register";
|
||||
cmdRegister.set_password(password.toStdString());
|
||||
}
|
||||
cmdRegister.set_email(email.toStdString());
|
||||
cmdRegister.set_country(country.toStdString());
|
||||
cmdRegister.set_real_name(realName.toStdString());
|
||||
cmdRegister.set_clientid(getSrvClientID(lastHostname).toStdString());
|
||||
PendingCommand *pend = prepareSessionCommand(cmdRegister);
|
||||
connect(pend, &PendingCommand::finished, this, &RemoteClient::registerResponse);
|
||||
sendCommand(pend);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (getStatus() == StatusActivating) {
|
||||
Command_Activate cmdActivate;
|
||||
cmdActivate.set_user_name(userName.toStdString());
|
||||
cmdActivate.set_token(token.toStdString());
|
||||
cmdActivate.set_clientid(getSrvClientID(lastHostname).toStdString());
|
||||
|
||||
PendingCommand *pend = prepareSessionCommand(cmdActivate);
|
||||
connect(pend, &PendingCommand::finished, this, &RemoteClient::activateResponse);
|
||||
sendCommand(pend);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
doLogin();
|
||||
}
|
||||
|
||||
void RemoteClient::doRequestPasswordSalt()
|
||||
{
|
||||
setStatus(StatusGettingPasswordSalt);
|
||||
Command_RequestPasswordSalt cmdRqSalt;
|
||||
cmdRqSalt.set_user_name(userName.toStdString());
|
||||
|
||||
PendingCommand *pend = prepareSessionCommand(cmdRqSalt);
|
||||
connect(pend, &PendingCommand::finished, this, &RemoteClient::passwordSaltResponse);
|
||||
sendCommand(pend);
|
||||
}
|
||||
|
||||
Command_Login RemoteClient::generateCommandLogin()
|
||||
{
|
||||
Command_Login cmdLogin;
|
||||
cmdLogin.set_user_name(userName.toStdString());
|
||||
cmdLogin.set_clientid(getSrvClientID(lastHostname).toStdString());
|
||||
cmdLogin.set_clientver(VERSION_STRING);
|
||||
|
||||
if (!clientFeatures.isEmpty()) {
|
||||
QMap<QString, bool>::iterator i;
|
||||
for (i = clientFeatures.begin(); i != clientFeatures.end(); ++i)
|
||||
cmdLogin.add_clientfeatures(i.key().toStdString().c_str());
|
||||
}
|
||||
|
||||
return cmdLogin;
|
||||
}
|
||||
|
||||
void RemoteClient::doLogin()
|
||||
{
|
||||
if (!password.isEmpty() && serverSupportsPasswordHash) {
|
||||
// TODO store and log in using stored hashed password
|
||||
if (hashedPassword.isEmpty()) {
|
||||
doRequestPasswordSalt(); // ask salt to create hashedPassword, then log in
|
||||
} else {
|
||||
doHashedLogin(); // log in using hashed password instead
|
||||
}
|
||||
} else {
|
||||
// TODO add setting for client to reject unhashed logins
|
||||
setStatus(StatusLoggingIn);
|
||||
Command_Login cmdLogin = generateCommandLogin();
|
||||
if (!password.isEmpty()) {
|
||||
qCWarning(RemoteClientLog) << "using plain text password to log in";
|
||||
cmdLogin.set_password(password.toStdString());
|
||||
}
|
||||
|
||||
PendingCommand *pend = prepareSessionCommand(cmdLogin);
|
||||
connect(pend, &PendingCommand::finished, this, &RemoteClient::loginResponse);
|
||||
sendCommand(pend);
|
||||
}
|
||||
}
|
||||
|
||||
void RemoteClient::doHashedLogin()
|
||||
{
|
||||
setStatus(StatusLoggingIn);
|
||||
Command_Login cmdLogin = generateCommandLogin();
|
||||
|
||||
cmdLogin.set_hashed_password(hashedPassword.toStdString());
|
||||
|
||||
PendingCommand *pend = prepareSessionCommand(cmdLogin);
|
||||
connect(pend, &PendingCommand::finished, this, &RemoteClient::loginResponse);
|
||||
sendCommand(pend);
|
||||
}
|
||||
|
||||
void RemoteClient::processConnectionClosedEvent(const Event_ConnectionClosed & /*event*/)
|
||||
{
|
||||
doDisconnectFromServer();
|
||||
}
|
||||
|
||||
void RemoteClient::passwordSaltResponse(const Response &response)
|
||||
{
|
||||
if (response.response_code() == Response::RespOk) {
|
||||
const Response_PasswordSalt &resp = response.GetExtension(Response_PasswordSalt::ext);
|
||||
auto passwordSalt = QString::fromStdString(resp.password_salt());
|
||||
if (passwordSalt.isEmpty()) { // the server does not recognize the user but allows them to enter unregistered
|
||||
password.clear(); // the password will not be used
|
||||
doLogin();
|
||||
} else {
|
||||
hashedPassword = PasswordHasher::computeHash(password, passwordSalt);
|
||||
doHashedLogin();
|
||||
}
|
||||
} else if (response.response_code() != Response::RespNotConnected) {
|
||||
emit loginError(response.response_code(), {}, 0, {});
|
||||
}
|
||||
}
|
||||
|
||||
void RemoteClient::loginResponse(const Response &response)
|
||||
{
|
||||
const Response_Login &resp = response.GetExtension(Response_Login::ext);
|
||||
|
||||
QString possibleMissingFeatures;
|
||||
if (resp.missing_features_size() > 0) {
|
||||
for (int i = 0; i < resp.missing_features_size(); ++i)
|
||||
possibleMissingFeatures.append("," + QString::fromStdString(resp.missing_features(i)));
|
||||
}
|
||||
|
||||
if (response.response_code() == Response::RespOk) {
|
||||
setStatus(StatusLoggedIn);
|
||||
emit userInfoChanged(resp.user_info());
|
||||
|
||||
QList<ServerInfo_User> buddyList;
|
||||
for (int i = resp.buddy_list_size() - 1; i >= 0; --i)
|
||||
buddyList.append(resp.buddy_list(i));
|
||||
emit buddyListReceived(buddyList);
|
||||
|
||||
QList<ServerInfo_User> ignoreList;
|
||||
for (int i = resp.ignore_list_size() - 1; i >= 0; --i)
|
||||
ignoreList.append(resp.ignore_list(i));
|
||||
emit ignoreListReceived(ignoreList);
|
||||
|
||||
if (newMissingFeatureFound(possibleMissingFeatures) && resp.missing_features_size() > 0 &&
|
||||
SettingsCache::instance().getNotifyAboutUpdates()) {
|
||||
SettingsCache::instance().setKnownMissingFeatures(possibleMissingFeatures);
|
||||
emit notifyUserAboutUpdate();
|
||||
}
|
||||
|
||||
} else if (response.response_code() != Response::RespNotConnected) {
|
||||
QList<QString> missingFeatures;
|
||||
if (resp.missing_features_size() > 0) {
|
||||
for (int i = 0; i < resp.missing_features_size(); ++i)
|
||||
missingFeatures << QString::fromStdString(resp.missing_features(i));
|
||||
}
|
||||
emit loginError(response.response_code(), QString::fromStdString(resp.denied_reason_str()),
|
||||
static_cast<quint32>(resp.denied_end_time()), missingFeatures);
|
||||
setStatus(StatusDisconnecting);
|
||||
}
|
||||
}
|
||||
|
||||
void RemoteClient::registerResponse(const Response &response)
|
||||
{
|
||||
const Response_Register &resp = response.GetExtension(Response_Register::ext);
|
||||
switch (response.response_code()) {
|
||||
case Response::RespRegistrationAccepted:
|
||||
emit registerAccepted();
|
||||
doLogin();
|
||||
break;
|
||||
case Response::RespRegistrationAcceptedNeedsActivation:
|
||||
emit registerAcceptedNeedsActivate();
|
||||
doLogin();
|
||||
break;
|
||||
case Response::RespNotConnected:
|
||||
// this response is created by the client from doDisconnectFromServer, do not call it again!
|
||||
emit registerError(response.response_code(), QString::fromStdString(resp.denied_reason_str()),
|
||||
static_cast<quint32>(resp.denied_end_time()));
|
||||
break;
|
||||
default:
|
||||
emit registerError(response.response_code(), QString::fromStdString(resp.denied_reason_str()),
|
||||
static_cast<quint32>(resp.denied_end_time()));
|
||||
setStatus(StatusDisconnecting);
|
||||
doDisconnectFromServer();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void RemoteClient::activateResponse(const Response &response)
|
||||
{
|
||||
if (response.response_code() == Response::RespActivationAccepted) {
|
||||
emit activateAccepted();
|
||||
|
||||
doLogin();
|
||||
} else {
|
||||
emit activateError();
|
||||
}
|
||||
}
|
||||
|
||||
void RemoteClient::readData()
|
||||
{
|
||||
lastDataReceived = timeRunning;
|
||||
QByteArray data = socket->readAll();
|
||||
|
||||
inputBuffer.append(data);
|
||||
|
||||
do {
|
||||
if (!messageInProgress) {
|
||||
if (inputBuffer.size() >= 4) {
|
||||
// dirty hack to be compatible with v14 server that sends 60 bytes of garbage at the beginning
|
||||
if (!handshakeStarted) {
|
||||
handshakeStarted = true;
|
||||
if (inputBuffer.startsWith("<?xm")) {
|
||||
messageInProgress = true;
|
||||
messageLength = 60;
|
||||
}
|
||||
} else {
|
||||
// end of hack
|
||||
messageLength = (((quint32)(unsigned char)inputBuffer[0]) << 24) +
|
||||
(((quint32)(unsigned char)inputBuffer[1]) << 16) +
|
||||
(((quint32)(unsigned char)inputBuffer[2]) << 8) +
|
||||
((quint32)(unsigned char)inputBuffer[3]);
|
||||
inputBuffer.remove(0, 4);
|
||||
messageInProgress = true;
|
||||
}
|
||||
} else
|
||||
return;
|
||||
}
|
||||
if (inputBuffer.size() < messageLength)
|
||||
return;
|
||||
|
||||
ServerMessage newServerMessage;
|
||||
newServerMessage.ParseFromArray(inputBuffer.data(), messageLength);
|
||||
|
||||
qCDebug(RemoteClientLog).noquote() << "IN" << getSafeDebugString(newServerMessage);
|
||||
|
||||
inputBuffer.remove(0, messageLength);
|
||||
messageInProgress = false;
|
||||
|
||||
processProtocolItem(newServerMessage);
|
||||
|
||||
if (getStatus() == StatusDisconnecting) // use thread-safe getter
|
||||
doDisconnectFromServer();
|
||||
} while (!inputBuffer.isEmpty());
|
||||
}
|
||||
|
||||
void RemoteClient::websocketMessageReceived(const QByteArray &message)
|
||||
{
|
||||
lastDataReceived = timeRunning;
|
||||
ServerMessage newServerMessage;
|
||||
newServerMessage.ParseFromArray(message.data(), message.length());
|
||||
|
||||
qCDebug(RemoteClientLog).noquote() << "IN" << getSafeDebugString(newServerMessage);
|
||||
|
||||
processProtocolItem(newServerMessage);
|
||||
}
|
||||
|
||||
void RemoteClient::sendCommandContainer(const CommandContainer &cont)
|
||||
{
|
||||
#if GOOGLE_PROTOBUF_VERSION > 3001000
|
||||
auto size = static_cast<unsigned int>(cont.ByteSizeLong());
|
||||
#else
|
||||
auto size = static_cast<unsigned int>(cont.ByteSize());
|
||||
#endif
|
||||
|
||||
qCDebug(RemoteClientLog).noquote() << "OUT" << getSafeDebugString(cont);
|
||||
|
||||
QByteArray buf;
|
||||
if (usingWebSocket) {
|
||||
buf.resize(size);
|
||||
cont.SerializeToArray(buf.data(), size);
|
||||
websocket->sendBinaryMessage(buf);
|
||||
} else {
|
||||
buf.resize(size + 4);
|
||||
cont.SerializeToArray(buf.data() + 4, size);
|
||||
buf.data()[3] = (unsigned char)size;
|
||||
buf.data()[2] = (unsigned char)(size >> 8);
|
||||
buf.data()[1] = (unsigned char)(size >> 16);
|
||||
buf.data()[0] = (unsigned char)(size >> 24);
|
||||
|
||||
socket->write(buf);
|
||||
}
|
||||
}
|
||||
|
||||
void RemoteClient::connectToHost(const QString &hostname, unsigned int port)
|
||||
{
|
||||
usingWebSocket = port == 443 || port == 80 || port == 4748 || port == 8080;
|
||||
if (usingWebSocket) {
|
||||
QUrl url(QString("%1://%2:%3/servatrice").arg(port == 443 ? "wss" : "ws").arg(hostname).arg(port));
|
||||
websocket->open(url);
|
||||
} else {
|
||||
socket->connectToHost(hostname, static_cast<quint16>(port));
|
||||
}
|
||||
}
|
||||
|
||||
void RemoteClient::doConnectToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_password)
|
||||
{
|
||||
doDisconnectFromServer();
|
||||
|
||||
userName = _userName;
|
||||
password = _password;
|
||||
lastHostname = hostname;
|
||||
lastPort = port;
|
||||
hashedPassword.clear();
|
||||
|
||||
connectToHost(hostname, port);
|
||||
setStatus(StatusConnecting);
|
||||
}
|
||||
|
||||
void RemoteClient::doRegisterToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_password,
|
||||
const QString &_email,
|
||||
const QString &_country,
|
||||
const QString &_realname)
|
||||
{
|
||||
doDisconnectFromServer();
|
||||
|
||||
userName = _userName;
|
||||
password = _password;
|
||||
email = _email;
|
||||
country = _country;
|
||||
realName = _realname;
|
||||
lastHostname = hostname;
|
||||
lastPort = port;
|
||||
hashedPassword.clear();
|
||||
|
||||
connectToHost(hostname, port);
|
||||
setStatus(StatusRegistering);
|
||||
}
|
||||
|
||||
void RemoteClient::doActivateToServer(const QString &_token)
|
||||
{
|
||||
doDisconnectFromServer();
|
||||
|
||||
token = _token.trimmed();
|
||||
|
||||
connectToHost(lastHostname, lastPort);
|
||||
setStatus(StatusActivating);
|
||||
}
|
||||
|
||||
void RemoteClient::doDisconnectFromServer()
|
||||
{
|
||||
timer->stop();
|
||||
|
||||
messageInProgress = false;
|
||||
handshakeStarted = false;
|
||||
messageLength = 0;
|
||||
|
||||
QList<PendingCommand *> pc = pendingCommands.values();
|
||||
for (const auto &i : pc) {
|
||||
Response response;
|
||||
response.set_response_code(Response::RespNotConnected);
|
||||
response.set_cmd_id(i->getCommandContainer().cmd_id());
|
||||
i->processResponse(response);
|
||||
|
||||
delete i;
|
||||
}
|
||||
pendingCommands.clear();
|
||||
|
||||
setStatus(StatusDisconnected);
|
||||
if (websocket->isValid())
|
||||
websocket->close();
|
||||
socket->close();
|
||||
}
|
||||
|
||||
void RemoteClient::ping()
|
||||
{
|
||||
QMutableMapIterator<int, PendingCommand *> i(pendingCommands);
|
||||
while (i.hasNext()) {
|
||||
PendingCommand *pend = i.next().value();
|
||||
if (pend->tick() > maxTimeout) {
|
||||
i.remove();
|
||||
pend->deleteLater();
|
||||
}
|
||||
}
|
||||
|
||||
int maxTime = timeRunning - lastDataReceived;
|
||||
emit maxPingTime(maxTime, maxTimeout);
|
||||
if (maxTime >= maxTimeout) {
|
||||
disconnectFromServer();
|
||||
emit serverTimeout();
|
||||
} else {
|
||||
sendCommand(prepareSessionCommand(Command_Ping()));
|
||||
++timeRunning;
|
||||
}
|
||||
}
|
||||
|
||||
void RemoteClient::connectToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_password)
|
||||
{
|
||||
emit sigConnectToServer(hostname, port, _userName, _password);
|
||||
}
|
||||
|
||||
void RemoteClient::registerToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_password,
|
||||
const QString &_email,
|
||||
const QString &_country,
|
||||
const QString &_realname)
|
||||
{
|
||||
emit sigRegisterToServer(hostname, port, _userName, _password, _email, _country, _realname);
|
||||
}
|
||||
|
||||
void RemoteClient::activateToServer(const QString &_token)
|
||||
{
|
||||
emit sigActivateToServer(_token.trimmed());
|
||||
}
|
||||
|
||||
void RemoteClient::disconnectFromServer()
|
||||
{
|
||||
emit sigDisconnectFromServer();
|
||||
}
|
||||
|
||||
QString RemoteClient::getSrvClientID(const QString &_hostname)
|
||||
{
|
||||
QString srvClientID = SettingsCache::instance().getClientID();
|
||||
QHostInfo hostInfo = QHostInfo::fromName(_hostname);
|
||||
if (!hostInfo.error()) {
|
||||
QHostAddress hostAddress = hostInfo.addresses().first();
|
||||
srvClientID += hostAddress.toString();
|
||||
} else {
|
||||
qCWarning(RemoteClientLog) << "ClientID generation host lookup failure [" << hostInfo.errorString() << "]";
|
||||
srvClientID += _hostname;
|
||||
}
|
||||
QString uniqueServerClientID =
|
||||
QCryptographicHash::hash(srvClientID.toUtf8(), QCryptographicHash::Sha1).toHex().right(15);
|
||||
return uniqueServerClientID;
|
||||
}
|
||||
|
||||
bool RemoteClient::newMissingFeatureFound(const QString &_serversMissingFeatures)
|
||||
{
|
||||
bool newMissingFeature = false;
|
||||
QStringList serversMissingFeaturesList = _serversMissingFeatures.split(",");
|
||||
for (const QString &feature : serversMissingFeaturesList) {
|
||||
if (!feature.isEmpty()) {
|
||||
if (!SettingsCache::instance().getKnownMissingFeatures().contains(feature))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return newMissingFeature;
|
||||
}
|
||||
|
||||
void RemoteClient::clearNewClientFeatures()
|
||||
{
|
||||
QString newKnownMissingFeatures;
|
||||
QStringList existingKnownMissingFeatures = SettingsCache::instance().getKnownMissingFeatures().split(",");
|
||||
for (const QString &existingKnownFeature : existingKnownMissingFeatures) {
|
||||
if (!existingKnownFeature.isEmpty()) {
|
||||
if (!clientFeatures.contains(existingKnownFeature))
|
||||
newKnownMissingFeatures.append("," + existingKnownFeature);
|
||||
}
|
||||
}
|
||||
SettingsCache::instance().setKnownMissingFeatures(newKnownMissingFeatures);
|
||||
}
|
||||
|
||||
void RemoteClient::requestForgotPasswordToServer(const QString &hostname, unsigned int port, const QString &_userName)
|
||||
{
|
||||
emit sigRequestForgotPasswordToServer(hostname, port, _userName);
|
||||
}
|
||||
|
||||
void RemoteClient::submitForgotPasswordResetToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_token,
|
||||
const QString &_newpassword)
|
||||
{
|
||||
emit sigSubmitForgotPasswordResetToServer(hostname, port, _userName, _token.trimmed(), _newpassword);
|
||||
}
|
||||
|
||||
void RemoteClient::doRequestForgotPasswordToServer(const QString &hostname, unsigned int port, const QString &_userName)
|
||||
{
|
||||
doDisconnectFromServer();
|
||||
|
||||
userName = _userName;
|
||||
lastHostname = hostname;
|
||||
lastPort = port;
|
||||
|
||||
connectToHost(lastHostname, lastPort);
|
||||
setStatus(StatusRequestingForgotPassword);
|
||||
}
|
||||
|
||||
void RemoteClient::requestForgotPasswordResponse(const Response &response)
|
||||
{
|
||||
const Response_ForgotPasswordRequest &resp = response.GetExtension(Response_ForgotPasswordRequest::ext);
|
||||
if (response.response_code() == Response::RespOk) {
|
||||
if (resp.challenge_email()) {
|
||||
emit sigPromptForForgotPasswordChallenge();
|
||||
} else
|
||||
emit sigPromptForForgotPasswordReset();
|
||||
} else
|
||||
emit sigForgotPasswordError();
|
||||
|
||||
doDisconnectFromServer();
|
||||
}
|
||||
|
||||
void RemoteClient::doSubmitForgotPasswordResetToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_token,
|
||||
const QString &_newpassword)
|
||||
{
|
||||
doDisconnectFromServer();
|
||||
|
||||
userName = _userName;
|
||||
lastHostname = hostname;
|
||||
lastPort = port;
|
||||
token = _token.trimmed();
|
||||
password = _newpassword;
|
||||
hashedPassword.clear();
|
||||
|
||||
connectToHost(lastHostname, lastPort);
|
||||
setStatus(StatusSubmitForgotPasswordReset);
|
||||
}
|
||||
|
||||
void RemoteClient::submitForgotPasswordResetResponse(const Response &response)
|
||||
{
|
||||
if (response.response_code() == Response::RespOk) {
|
||||
emit sigForgotPasswordSuccess();
|
||||
} else
|
||||
emit sigForgotPasswordError();
|
||||
|
||||
doDisconnectFromServer();
|
||||
}
|
||||
|
||||
void RemoteClient::submitForgotPasswordChallengeToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_email)
|
||||
{
|
||||
emit sigSubmitForgotPasswordChallengeToServer(hostname, port, _userName, _email);
|
||||
}
|
||||
|
||||
void RemoteClient::doSubmitForgotPasswordChallengeToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_email)
|
||||
{
|
||||
doDisconnectFromServer();
|
||||
|
||||
userName = _userName;
|
||||
lastHostname = hostname;
|
||||
lastPort = port;
|
||||
email = _email;
|
||||
|
||||
connectToHost(lastHostname, lastPort);
|
||||
setStatus(StatusSubmitForgotPasswordChallenge);
|
||||
}
|
||||
|
||||
void RemoteClient::submitForgotPasswordChallengeResponse(const Response &response)
|
||||
{
|
||||
if (response.response_code() == Response::RespOk) {
|
||||
emit sigPromptForForgotPasswordReset();
|
||||
} else
|
||||
emit sigForgotPasswordError();
|
||||
|
||||
doDisconnectFromServer();
|
||||
}
|
||||
|
|
@ -0,0 +1,156 @@
|
|||
/**
|
||||
* @file remote_client.h
|
||||
* @ingroup Client
|
||||
* @brief TODO: Document this.
|
||||
*/
|
||||
|
||||
#ifndef REMOTECLIENT_H
|
||||
#define REMOTECLIENT_H
|
||||
|
||||
#include "../abstract/abstract_client.h"
|
||||
|
||||
#include <QLoggingCategory>
|
||||
#include <QTcpSocket>
|
||||
#include <QWebSocket>
|
||||
#include <libcockatrice/protocol/pb/commands.pb.h>
|
||||
|
||||
inline Q_LOGGING_CATEGORY(RemoteClientLog, "remote_client");
|
||||
|
||||
class QTimer;
|
||||
|
||||
class RemoteClient : public AbstractClient
|
||||
{
|
||||
Q_OBJECT
|
||||
signals:
|
||||
void serverTimeout();
|
||||
void loginError(Response::ResponseCode resp, QString reasonStr, quint32 endTime, QList<QString> missingFeatures);
|
||||
void registerError(Response::ResponseCode resp, QString reasonStr, quint32 endTime);
|
||||
void activateError();
|
||||
void socketError(const QString &errorString);
|
||||
void protocolVersionMismatch(int clientVersion, int serverVersion);
|
||||
void
|
||||
sigConnectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password);
|
||||
void sigRegisterToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_password,
|
||||
const QString &_email,
|
||||
const QString &_country,
|
||||
const QString &_realname);
|
||||
void sigActivateToServer(const QString &_token);
|
||||
void sigDisconnectFromServer();
|
||||
void notifyUserAboutUpdate();
|
||||
void sigRequestForgotPasswordToServer(const QString &hostname, unsigned int port, const QString &_userName);
|
||||
void sigForgotPasswordSuccess();
|
||||
void sigForgotPasswordError();
|
||||
void sigPromptForForgotPasswordReset();
|
||||
void sigSubmitForgotPasswordResetToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_token,
|
||||
const QString &_newpassword);
|
||||
void sigPromptForForgotPasswordChallenge();
|
||||
void sigSubmitForgotPasswordChallengeToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_email);
|
||||
private slots:
|
||||
void slotConnected();
|
||||
void readData();
|
||||
void websocketMessageReceived(const QByteArray &message);
|
||||
void slotSocketError(QAbstractSocket::SocketError error);
|
||||
void slotWebSocketError(QAbstractSocket::SocketError error);
|
||||
void ping();
|
||||
void processServerIdentificationEvent(const Event_ServerIdentification &event);
|
||||
void processConnectionClosedEvent(const Event_ConnectionClosed &event);
|
||||
void passwordSaltResponse(const Response &response);
|
||||
void loginResponse(const Response &response);
|
||||
void registerResponse(const Response &response);
|
||||
void activateResponse(const Response &response);
|
||||
void
|
||||
doConnectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password);
|
||||
void doRegisterToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_password,
|
||||
const QString &_email,
|
||||
const QString &_country,
|
||||
const QString &_realname);
|
||||
void doRequestPasswordSalt();
|
||||
void doLogin();
|
||||
void doHashedLogin();
|
||||
Command_Login generateCommandLogin();
|
||||
void doDisconnectFromServer();
|
||||
void doActivateToServer(const QString &_token);
|
||||
void doRequestForgotPasswordToServer(const QString &hostname, unsigned int port, const QString &_userName);
|
||||
void requestForgotPasswordResponse(const Response &response);
|
||||
void doSubmitForgotPasswordResetToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_token,
|
||||
const QString &_newpassword);
|
||||
void submitForgotPasswordResetResponse(const Response &response);
|
||||
void doSubmitForgotPasswordChallengeToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_email);
|
||||
void submitForgotPasswordChallengeResponse(const Response &response);
|
||||
|
||||
private:
|
||||
int maxTimeout;
|
||||
int timeRunning, lastDataReceived;
|
||||
QByteArray inputBuffer;
|
||||
bool messageInProgress;
|
||||
bool handshakeStarted;
|
||||
bool usingWebSocket;
|
||||
int messageLength;
|
||||
QTimer *timer;
|
||||
QTcpSocket *socket;
|
||||
QWebSocket *websocket;
|
||||
QString lastHostname;
|
||||
unsigned int lastPort;
|
||||
QString hashedPassword;
|
||||
|
||||
QString getSrvClientID(const QString &_hostname);
|
||||
bool newMissingFeatureFound(const QString &_serversMissingFeatures);
|
||||
void clearNewClientFeatures();
|
||||
void connectToHost(const QString &hostname, unsigned int port);
|
||||
|
||||
protected slots:
|
||||
void sendCommandContainer(const CommandContainer &cont) override;
|
||||
|
||||
public:
|
||||
explicit RemoteClient(QObject *parent = nullptr);
|
||||
~RemoteClient() override;
|
||||
QString peerName() const
|
||||
{
|
||||
if (usingWebSocket) {
|
||||
return websocket->peerName();
|
||||
} else {
|
||||
return socket->peerName();
|
||||
}
|
||||
}
|
||||
void
|
||||
connectToServer(const QString &hostname, unsigned int port, const QString &_userName, const QString &_password);
|
||||
void registerToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_password,
|
||||
const QString &_email,
|
||||
const QString &_country,
|
||||
const QString &_realname);
|
||||
void activateToServer(const QString &_token);
|
||||
void disconnectFromServer();
|
||||
void requestForgotPasswordToServer(const QString &hostname, unsigned int port, const QString &_userName);
|
||||
void submitForgotPasswordResetToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_token,
|
||||
const QString &_newpassword);
|
||||
void submitForgotPasswordChallengeToServer(const QString &hostname,
|
||||
unsigned int port,
|
||||
const QString &_userName,
|
||||
const QString &_email);
|
||||
};
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue