421 lines
8.3 KiB
C++
Raw Normal View History

2019-02-13 13:03:40 +03:00
#include "client.h"
2019-02-15 23:36:59 +03:00
#include <QRegularExpression>
2019-02-13 13:03:40 +03:00
#include <QTcpSocket>
2019-02-15 23:36:59 +03:00
#include <QVariantMap>
2019-03-01 07:56:15 +03:00
#include <QDateTime>
2019-03-01 15:26:45 +03:00
#include <quasarapp.h>
2019-06-13 12:53:35 +03:00
#include <qrsaencryption.h>
#include <pubkey.h>
2019-07-27 17:03:59 +03:00
#include <websocket.h>
2019-03-05 00:48:32 +03:00
#include "factorynetobjects.h"
2019-04-29 14:39:48 +03:00
#include "gamedata.h"
#include "getitem.h"
#include "login.h"
#include "updateplayerdata.h"
2019-07-27 17:03:59 +03:00
#include <QHash>
2019-08-06 22:01:36 +03:00
#include <player.h>
2019-02-13 13:03:40 +03:00
2019-06-22 15:52:07 +03:00
#define SOLT "SNAKE"
2019-02-13 13:03:40 +03:00
namespace ClientProtocol {
Command Client::checkCommand(int sig, Command reqCmd, Type type) {
#define idx static_cast<quint8>(sig)
auto expCmd = static_cast<Command>(
_requestsMap[idx].value("expected",
static_cast<quint8>(Command::Undefined)).toInt());
if (expCmd == Command::Undefined ||
expCmd != reqCmd ||
type != Type::Responke) {
return Command::Undefined;
}
_requestsMap[idx]["time"] = QDateTime::currentMSecsSinceEpoch();
return expCmd;
}
void Client::updateStatuses(Command extCmd, Command cmd, Type type, const QByteArray& obj)
{
setOnlineStatus(extCmd != Command::Undefined && type == Type::Responke);
if (extCmd == Command::Login
&& type == Type::Responke) {
UpdatePlayerData data;
data.fromBytes(obj);
bool validData = data.isValid();
if (validData) {
_token = data.getToken();
}
2019-08-07 22:15:26 +03:00
_currentUserId = data.id();
setLoginStatus(cmd == Command::UpdatePlayerData && validData);
}
}
2019-04-29 14:39:48 +03:00
bool Client::receiveData(const QByteArray &obj, Header hdr) {
2019-04-29 14:39:48 +03:00
auto command = static_cast<Command>(hdr.command);
auto requesCommand = static_cast<Command>(hdr.requestCommand);
2019-04-29 14:39:48 +03:00
auto type = static_cast<Type>(hdr.type);
2019-06-13 12:53:35 +03:00
if (command == Command::PubKey && !_rsaKey.size()) {
2019-06-14 10:54:37 +03:00
PubKey data;
2019-06-13 12:53:35 +03:00
data.fromBytes(obj);
return setRSAKey(data.getKey());;
}
2019-07-27 17:03:59 +03:00
if (type != Type::Stream) {
auto expectedCommand = checkCommand(hdr.sig, requesCommand, type);
if (expectedCommand == Command::Undefined) {
QuasarAppUtils::Params::verboseLog("wrong sig of package");
return false;
}
2019-03-05 00:48:32 +03:00
2019-07-27 17:03:59 +03:00
updateStatuses(expectedCommand, command, type, obj);
} else if (_subscribe.contains(hdr.command)) {
_subscribe[hdr.command] = true;
} else {
2019-03-01 15:26:45 +03:00
return false;
}
2019-04-29 14:39:48 +03:00
emit sigIncommingData(static_cast<Command>(hdr.command), obj);
2019-03-01 15:26:45 +03:00
return true;
2019-03-01 07:56:15 +03:00
}
2019-06-13 12:53:35 +03:00
bool Client::setRSAKey(const QByteArray& key) {
bool newStatus = QRSAEncryption::isValidRsaKey(key);
setOnlineStatus(newStatus);
if (newStatus) {
2019-06-13 12:53:35 +03:00
_rsaKey = key;
}
2019-06-13 12:53:35 +03:00
return newStatus;
}
void Client::setLoginStatus(bool newStatus) {
if (newStatus != _logined) {
_logined = newStatus;
emit loginChanged(_logined);
}
}
void Client::setOnlineStatus(bool newOnline) {
if (newOnline != _online) {
_online = newOnline;
emit onlineChanged(_online);
if (!_online) {
_rsaKey = "";
setLoginStatus(false);
}
}
}
2019-02-13 13:03:40 +03:00
void Client::incommingData() {
auto array = _destination->readAll();
if (_downloadPackage.hdr.isValid()) {
_downloadPackage.data.append(array);
} else {
memcpy(&_downloadPackage.hdr,
2019-02-14 14:09:17 +03:00
array.data(), sizeof(Header));
_downloadPackage.data.append(array.mid(sizeof(Header)));
2019-02-13 13:03:40 +03:00
}
if (_downloadPackage.isValid()) {
2019-06-14 10:54:37 +03:00
if (!receiveData(_downloadPackage.data, _downloadPackage.hdr)) {
2019-03-01 15:26:45 +03:00
// ban
2019-02-15 23:36:59 +03:00
}
2019-02-13 13:03:40 +03:00
_downloadPackage.reset();
return;
}
}
void Client::handleDisconnected() {
setOnlineStatus(false);
}
2019-05-26 13:52:16 +03:00
Client::Client(const QString &addrress, unsigned short port, QObject *ptr):
2019-02-13 13:03:40 +03:00
QObject (ptr) {
_destination = new QTcpSocket(this);
2019-05-26 13:52:16 +03:00
_destination->connectToHost(addrress, port);
2019-02-13 13:03:40 +03:00
connect(_destination, &QTcpSocket::readyRead,
this, &Client::incommingData);
connect(_destination, &QTcpSocket::disconnected,
this, &Client::handleDisconnected);
2019-02-13 13:03:40 +03:00
}
2019-03-02 13:29:21 +03:00
bool Client::sendPackage(Package &pkg) {
2019-02-13 13:03:40 +03:00
if (!pkg.isValid()) {
return false;
}
if (!_destination->isValid()) {
qCritical() << "destination server not valid!";
return false;
}
if (!_destination->waitForConnected()) {
qCritical() << "no connected to server! " << _destination->errorString();
return false;
}
2019-03-02 13:29:21 +03:00
auto index = nextIndex();
2019-03-05 00:48:32 +03:00
_requestsMap[index] = {{"expected", static_cast<const unsigned short>(pkg.hdr.command)}};
2019-03-02 13:29:21 +03:00
pkg.hdr.sig = index;
2019-02-13 13:03:40 +03:00
2019-04-29 14:39:48 +03:00
QByteArray bytes = pkg.toBytes();
2019-03-01 15:26:45 +03:00
bool sendet = bytes.size() == _destination->write(bytes);
return sendet;
2019-02-13 13:03:40 +03:00
}
2019-03-02 13:29:21 +03:00
unsigned char Client::nextIndex() {
return static_cast<unsigned char>((currentIndex++) % 256);
}
2019-03-05 00:48:32 +03:00
bool Client::ping() {
Package pcg;
2019-04-29 14:39:48 +03:00
if (!pcg.create(Command::Ping, Type::Request)) {
2019-03-05 00:48:32 +03:00
return false;
};
if (!sendPackage(pcg)) {
return false;
}
return true;
}
2019-06-22 15:52:07 +03:00
/// Do not change the order of this function,
/// as this may lead to the loss of user accounts.
QByteArray Client::generateHash(const QByteArray& pass) const {
auto passHash = QCryptographicHash::hash(pass, QCryptographicHash::Sha256);
return QCryptographicHash::hash(SOLT + passHash, QCryptographicHash::Sha256);
}
2019-02-15 23:36:59 +03:00
bool Client::login(const QString &gmail, const QByteArray &pass) {
if (!pass.size()) {
return false;
}
2019-02-13 14:28:42 +03:00
2019-02-15 23:36:59 +03:00
if (!gmail.size()) {
return false;
}
2019-06-17 11:51:31 +03:00
if (!isOnline()) {
return false;
}
2019-02-15 23:36:59 +03:00
Package pcg;
2019-03-05 00:48:32 +03:00
2019-04-29 14:39:48 +03:00
Login login;
2019-06-17 11:51:31 +03:00
2019-07-26 16:25:03 +03:00
login.setHashPass(QRSAEncryption::encode(generateHash(pass), _rsaKey, QRSAEncryption::RSA_256));
2019-04-29 14:39:48 +03:00
login.setGmail(gmail);
2019-04-29 17:28:01 +03:00
login.setId(0);
if (!login.isValid()) {
return false;
}
2019-04-29 14:39:48 +03:00
if (!pcg.create(&login, Type::Request)) {
return false;
};
if (!sendPackage(pcg)) {
return false;
}
return true;
}
2019-06-17 18:09:06 +03:00
void Client::loginOut() {
_token = "";
setLoginStatus(false);
}
bool Client::updateData() {
2019-06-14 10:54:37 +03:00
if (!isLogin()) {
return false;
}
Package pcg;
2019-03-05 00:48:32 +03:00
2019-04-29 14:39:48 +03:00
UpdatePlayerData rec;
rec.setToken(_token);
2019-04-29 17:28:01 +03:00
rec.setId(0);
if (!rec.isValid()) {
return false;
}
2019-04-29 14:39:48 +03:00
if (!pcg.create(&rec, Type::Request)) {
2019-02-15 23:36:59 +03:00
return false;
};
if (!sendPackage(pcg)) {
return false;
}
2019-02-13 14:28:42 +03:00
2019-02-15 23:36:59 +03:00
return true;
}
2019-02-13 14:28:42 +03:00
2019-03-05 00:48:32 +03:00
bool Client::savaData(const QList<int>& gameData) {
2019-06-14 10:54:37 +03:00
if (!isLogin()) {
return false;
}
Package pcg;
2019-04-29 14:39:48 +03:00
GameData rec;
rec.setToken(_token);
rec.setTimeClick(gameData);
2019-04-29 17:28:01 +03:00
rec.setId(0);
if (!rec.isValid()) {
return false;
}
2019-03-05 00:48:32 +03:00
2019-04-29 14:39:48 +03:00
if (!pcg.create(&rec, Type::Request)) {
return false;
};
if (!sendPackage(pcg)) {
return false;
}
return true;
2019-02-13 14:28:42 +03:00
}
bool Client::getItem(int id) {
2019-06-14 10:54:37 +03:00
if (!isLogin()) {
2019-02-15 23:36:59 +03:00
return false;
}
2019-08-06 22:01:36 +03:00
if (id <= 0) {
2019-02-15 23:36:59 +03:00
return false;
}
Package pcg;
2019-03-05 00:48:32 +03:00
2019-04-29 14:39:48 +03:00
GetItem rec;
rec.setToken(_token);
rec.setId(id);
2019-04-29 17:28:01 +03:00
if (!rec.isValid()) {
2019-08-06 22:01:36 +03:00
return false;
}
if (!pcg.create(&rec, Type::Request)) {
return false;
};
if (!sendPackage(pcg)) {
return false;
}
return true;
}
bool Client::getPlayer(int id){
if (!isLogin()) {
return false;
}
if (id <= 0) {
return false;
}
Package pcg;
UpdatePlayerData rec;
rec.setToken(_token);
rec.setId(id);
if (!rec.isValid()) {
2019-04-29 17:28:01 +03:00
return false;
}
2019-04-29 14:39:48 +03:00
if (!pcg.create(&rec, Type::Request)) {
2019-02-15 23:36:59 +03:00
return false;
};
if (!sendPackage(pcg)) {
return false;
}
2019-02-13 14:28:42 +03:00
2019-02-15 23:36:59 +03:00
return true;
2019-02-13 14:28:42 +03:00
}
2019-06-13 12:53:35 +03:00
const bool& Client::isOnline() const {
return _online;
}
2019-06-13 12:53:35 +03:00
const bool& Client::isLogin() const {
return _logined;
}
2019-08-07 22:15:26 +03:00
bool Client::setSubscribe(Command cmd, bool subscribe, int id) {
2019-07-27 17:03:59 +03:00
if (!isLogin()) {
return false;
}
Package pcg;
WebSocket rec;
rec.setId(0);
rec.setToken(_token);
rec.setCommand(cmd);
rec.setObjectId(id);
rec.setSubscribe(subscribe);
if (!rec.isValid()) {
return false;
}
if (!pcg.create(&rec, Type::Stream)) {
return false;
};
if (!sendPackage(pcg)) {
return false;
}
if (subscribe)
_subscribe[static_cast<quint8>(cmd)] = false;
else {
_subscribe.remove(static_cast<quint8>(cmd));
}
return true;
}
QHash<quint8, bool> Client::getSubscribe() const {
return _subscribe;
}
2019-02-13 13:03:40 +03:00
}