nitrify boob

This commit is contained in:
malloc 2018-07-05 16:32:32 -05:00
parent 44a465810b
commit 2c32c63abb
4 changed files with 50 additions and 11 deletions

View file

@ -72,8 +72,9 @@ private:
bool authed;
int auth_attempts;
const int MAX_AUTH_ATTEMPTS = 3;
std::string license;
int32_t server_id;
std::string license;
const db::QueryList* queries;
};

View file

@ -15,43 +15,54 @@ sosc::MasterIntraPool::MasterIntraPool() {
"WHERE `KEY_ID` = ? AND `SECRET` = ?"
));
#define QRY_LICENSE_LIMIT 1
#define QRY_LICENSE_VERIFY 1
this->queries.push_back(new db::Query(
"SELECT COUNT(*) FROM `SERVER_LICENSES` "
"WHERE `KEY_ID` = ?"
));
#define QRY_LICENSE_LIMIT 2
this->queries.push_back(new db::Query(
"SELECT `ALLOWANCE` FROM `SERVER_LICENSES` WHERE `KEY_ID` = ?"
));
#define QRY_LICENSE_ACTIVE_COUNT 2
#define QRY_LICENSE_ACTIVE_COUNT 3
this->queries.push_back(new db::Query(
"SELECT COUNT(*) FROM `SERVER_LIST` WHERE `LICENSE` = ?"
, DB_USE_MEMORY));
#define QRY_LICENSE_ADD 3
#define QRY_LICENSE_ADD 4
this->queries.push_back(new db::Query(
"INSERT OR IGNORE INTO `SERVER_LICENSES` "
"(`KEY_ID`, `SECRET`, `ALLOWANCE`) "
"VALUES (?, RANDOMBLOB(512), ?)"
));
#define QRY_LICENSE_REMOVE 4
#define QRY_LICENSE_REMOVE 5
this->queries.push_back(new db::Query(
"DELETE FROM `SERVER_LICENSES` "
"WHERE `KEY_ID` = ?"
));
#define QRY_LICENSE_MODIFY 5
#define QRY_LICENSE_MODIFY 6
this->queries.push_back(new db::Query(
"UPDATE `SERVER_LICENSES` "
"SET `ALLOWANCE` = ? WHERE `KEY_ID` = ?"
));
#define QRY_SERVER_LIST_ADD 6
#define QRY_SERVER_LIST_ADD 7
this->queries.push_back(new db::Query(
"INSERT INTO `SERVER_LIST` "
"(`NAME`, `LICENSE`, `IP_ADDR`, `PORT`) "
"VALUES (?, ?, ?, ?)"
, DB_USE_MEMORY));
#define QRY_SERVER_LIST_DELETE 7
#define QRY_SERVER_LIST_GET_ID 8
this->queries.push_back(new db::Query(
"SELECT MAX(`ID`) FROM `SERVER_LIST`"
, DB_USE_MEMORY));
#define QRY_SERVER_LIST_DELETE 9
this->queries.push_back(new db::Query(
"DELETE FROM `SERVER_LIST` WHERE `ID` = ?"
, DB_USE_MEMORY));
@ -106,8 +117,9 @@ bool sosc::MasterIntra::InitAttempt(sosc::Packet& pck) {
return this->Close(
Packet(kEncryptionError, { net::htonv<uint16_t>(0x101) }));
this->cipher = cgc::Cipher(this->key);
this->sock.Send(response);
this->sock.SetCipher(&this->cipher);
}
bool sosc::MasterIntra::Authentication(sosc::Packet& pck) {
@ -149,6 +161,10 @@ bool sosc::MasterIntra::Authentication(sosc::Packet& pck) {
query->BindInt32(net::ntohv<uint16_t>(pck[1]), 3);
query->NonQuery();
query = this->queries->at(QRY_SERVER_LIST_GET_ID);
query->Reset();
this->server_id = query->ScalarInt32();
_ctx.license_check_mtx.unlock();
this->sock.Send(Packet(kPositiveAck, { packetId }));
@ -187,6 +203,7 @@ bool sosc::MasterIntra::StatusUpdate(sosc::Packet &pck) {
return this->Close();
return true;
}

View file

@ -6,6 +6,7 @@
sosc::IntraClient::IntraClient() {
this->client_open = false;
this->cipher = nullptr;
}
bool sosc::IntraClient::Open(std::string host, uint16_t port) {
@ -21,6 +22,15 @@ void sosc::IntraClient::Open(TcpClient client) {
this->client_open = true;
}
bool sosc::IntraClient::IsCiphered() const {
return this->cipher != nullptr;
}
void sosc::IntraClient::SetCipher(cgc::Cipher *cipher) {
this->cipher = cipher;
cipher->Parse(&this->buffer);
}
int sosc::IntraClient::Receive(Packet* packet, bool block) {
if(!this->client_open)
return PCK_ERR;
@ -32,9 +42,12 @@ int sosc::IntraClient::Receive(Packet* packet, bool block) {
return PCK_ERR;
if(!block && !first_recv)
return PCK_MORE;
std::string::size_type offset = this->buffer.size();
status = this->client.Receive
(&this->buffer, SOSC_TCP_APPEND | (block ? SOSC_TCP_BLOCK : 0));
if(this->IsCiphered())
this->cipher->Parse(&this->buffer, offset);
if(status == -1)
return PCK_ERR;
@ -50,6 +63,9 @@ bool sosc::IntraClient::Send(const Packet& packet) {
std::string packet_raw;
packet.ToString(&packet_raw);
if(this->IsCiphered())
this->cipher->Parse(&packet_raw);
return this->client.Send(packet_raw) == 0;
}

View file

@ -3,13 +3,17 @@
#include "tcpsock.hpp"
#include "packet.hpp"
#include "../crypto/cipher.hpp"
namespace sosc {
class IntraClient {
public:
IntraClient();
bool Open(std::string host, uint16_t port);
bool IsCiphered() const;
void SetCipher(cgc::Cipher* cipher);
int Receive(Packet* packet, bool block = false);
bool Send(const Packet& packet);
@ -31,6 +35,7 @@ private:
bool client_open;
TcpClient client;
std::string buffer;
cgc::Cipher* cipher;
friend class IntraServer;
};