straight up boobin

This commit is contained in:
malloc 2018-07-20 15:10:37 -05:00
parent de7672f266
commit 74bc6f1720
10 changed files with 71 additions and 58 deletions

View file

@ -26,7 +26,7 @@ private:
class MasterClientPool : public Pool<MasterClient> { class MasterClientPool : public Pool<MasterClient> {
protected: protected:
bool ProcessClient(MasterClient& client) override { bool ProcessClient(MasterClient& client, const Queries* queries) override {
// TODO implement // TODO implement
return true; return true;
} }
@ -79,14 +79,11 @@ private:
}; };
class MasterIntraPool : public Pool<MasterIntra> { class MasterIntraPool : public Pool<MasterIntra> {
public:
MasterIntraPool();
protected: protected:
void SetupQueries(Queries* queries) override;
bool ProcessClient(MasterIntra& client, const Queries* queries) override { bool ProcessClient(MasterIntra& client, const Queries* queries) override {
return client.Process(queries); return client.Process(queries);
} }
void Stop() override;
}; };
} }

View file

@ -8,82 +8,73 @@ static struct {
/** MASTERINTRAPOOL CODE **/ /** MASTERINTRAPOOL CODE **/
sosc::MasterIntraPool::MasterIntraPool() { void sosc::MasterIntraPool::SetupQueries(Queries* queries) {
#define QRY_LICENSE_CHECK 0 #define QRY_LICENSE_CHECK 0
this->queries.push_back(new db::Query( queries->push_back(new db::Query(
"SELECT COUNT(*) FROM `SERVER_LICENSES` " "SELECT COUNT(*) FROM `SERVER_LICENSES` "
"WHERE `KEY_ID` = ? AND `SECRET` = ?" "WHERE `KEY_ID` = ? AND `SECRET` = ?"
)); ));
#define QRY_LICENSE_VERIFY 1 #define QRY_LICENSE_VERIFY 1
this->queries.push_back(new db::Query( queries->push_back(new db::Query(
"SELECT COUNT(*) FROM `SERVER_LICENSES` " "SELECT COUNT(*) FROM `SERVER_LICENSES` "
"WHERE `KEY_ID` = ?" "WHERE `KEY_ID` = ?"
)); ));
#define QRY_LICENSE_LIMIT 2 #define QRY_LICENSE_LIMIT 2
this->queries.push_back(new db::Query( queries->push_back(new db::Query(
"SELECT `ALLOWANCE` FROM `SERVER_LICENSES` WHERE `KEY_ID` = ?" "SELECT `ALLOWANCE` FROM `SERVER_LICENSES` WHERE `KEY_ID` = ?"
)); ));
#define QRY_LICENSE_ACTIVE_COUNT 3 #define QRY_LICENSE_ACTIVE_COUNT 3
this->queries.push_back(new db::Query( queries->push_back(new db::Query(
"SELECT COUNT(*) FROM `SERVER_LIST` WHERE `LICENSE` = ?" "SELECT COUNT(*) FROM `SERVER_LIST` WHERE `LICENSE` = ?"
, DB_USE_MEMORY)); , DB_USE_MEMORY));
#define QRY_LICENSE_ADD 4 #define QRY_LICENSE_ADD 4
this->queries.push_back(new db::Query( queries->push_back(new db::Query(
"INSERT OR IGNORE INTO `SERVER_LICENSES` " "INSERT OR IGNORE INTO `SERVER_LICENSES` "
"(`KEY_ID`, `SECRET`, `ALLOWANCE`) " "(`KEY_ID`, `SECRET`, `ALLOWANCE`) "
"VALUES (?, RANDOMBLOB(512), ?)" "VALUES (?, RANDOMBLOB(512), ?)"
)); ));
#define QRY_LICENSE_REMOVE 5 #define QRY_LICENSE_REMOVE 5
this->queries.push_back(new db::Query( queries->push_back(new db::Query(
"DELETE FROM `SERVER_LICENSES` " "DELETE FROM `SERVER_LICENSES` "
"WHERE `KEY_ID` = ?" "WHERE `KEY_ID` = ?"
)); ));
#define QRY_LICENSE_MODIFY 6 #define QRY_LICENSE_MODIFY 6
this->queries.push_back(new db::Query( queries->push_back(new db::Query(
"UPDATE `SERVER_LICENSES` " "UPDATE `SERVER_LICENSES` "
"SET `ALLOWANCE` = ? WHERE `KEY_ID` = ?" "SET `ALLOWANCE` = ? WHERE `KEY_ID` = ?"
)); ));
#define QRY_SERVER_LIST_ADD 7 #define QRY_SERVER_LIST_ADD 7
this->queries.push_back(new db::Query( queries->push_back(new db::Query(
"INSERT INTO `SERVER_LIST` " "INSERT INTO `SERVER_LIST` "
"(`NAME`, `LICENSE`, `IP_ADDR`, `PORT`) " "(`NAME`, `LICENSE`, `IP_ADDR`, `PORT`) "
"VALUES (?, ?, ?, ?)" "VALUES (?, ?, ?, ?)"
, DB_USE_MEMORY)); , DB_USE_MEMORY));
#define QRY_SERVER_LIST_GET_ID 8 #define QRY_SERVER_LIST_GET_ID 8
this->queries.push_back(new db::Query( queries->push_back(new db::Query(
"SELECT MAX(`ID`) FROM `SERVER_LIST`" "SELECT MAX(`ID`) FROM `SERVER_LIST`"
, DB_USE_MEMORY)); , DB_USE_MEMORY));
#define QRY_SERVER_LIST_MODIFY 9 #define QRY_SERVER_LIST_MODIFY 9
this->queries.push_back(new db::Query( queries->push_back(new db::Query(
"UPDATE `SERVER_LIST` SET " "UPDATE `SERVER_LIST` SET "
"`USERS` = ?, `MAX_USERS` = ? " "`USERS` = ?, `MAX_USERS` = ? "
"WHERE ID = ?" "WHERE ID = ?"
, DB_USE_MEMORY)); , DB_USE_MEMORY));
#define QRY_SERVER_LIST_DELETE 10 #define QRY_SERVER_LIST_DELETE 10
this->queries.push_back(new db::Query( queries->push_back(new db::Query(
"DELETE FROM `SERVER_LIST` WHERE `ID` = ?" "DELETE FROM `SERVER_LIST` WHERE `ID` = ?"
, DB_USE_MEMORY)); , DB_USE_MEMORY));
} }
void sosc::MasterIntraPool::Stop() {
Pool<MasterIntra>::Stop();
for(auto& query : this->queries) {
query->Close();
delete query;
}
}
/** MASTERINTRA CODE **/ /** MASTERINTRA CODE **/
sosc::MasterIntra::MasterIntra(const IntraClient& client) { sosc::MasterIntra::MasterIntra(const IntraClient& client) {
@ -92,7 +83,7 @@ sosc::MasterIntra::MasterIntra(const IntraClient& client) {
this->auth_attempts = 0; this->auth_attempts = 0;
} }
bool sosc::MasterIntra::Process(const db::QueryList* queries) { bool sosc::MasterIntra::Process(const Pool::Queries* queries) {
Packet pck; Packet pck;
int status = this->sock.Receive(&pck); int status = this->sock.Receive(&pck);
if(status == PCK_ERR) if(status == PCK_ERR)
@ -136,7 +127,7 @@ bool sosc::MasterIntra::Authentication(sosc::Packet& pck) {
if(!pck.Check(4, PCK_ANY, 2, PCK_ANY, 512)) if(!pck.Check(4, PCK_ANY, 2, PCK_ANY, 512))
return this->Close(); return this->Close();
db::Query* query = &this->queries[QRY_LICENSE_CHECK]; db::Query* query = this->queries->at(QRY_LICENSE_CHECK);
query->Reset(); query->Reset();
query->BindText(pck[2], 0); query->BindText(pck[2], 0);
query->BindBlob(pck[3], 1); query->BindBlob(pck[3], 1);

View file

@ -9,7 +9,7 @@ sosc::IntraClient::IntraClient() {
this->cipher = nullptr; this->cipher = nullptr;
} }
bool sosc::IntraClient::Open(std::string host, uint16_t port) { bool sosc::IntraClient::Open(const std::string& host, uint16_t port) {
if(!this->client.Open(host, port)) if(!this->client.Open(host, port))
return false; return false;
@ -17,7 +17,7 @@ bool sosc::IntraClient::Open(std::string host, uint16_t port) {
return true; return true;
} }
void sosc::IntraClient::Open(TcpClient client) { void sosc::IntraClient::Open(const TcpClient& client) {
this->client = client; this->client = client;
this->client_open = true; this->client_open = true;
} }
@ -66,7 +66,7 @@ bool sosc::IntraClient::Send(const Packet& packet) {
if(this->IsCiphered()) if(this->IsCiphered())
this->cipher->Parse(&packet_raw); this->cipher->Parse(&packet_raw);
return this->client.Send(packet_raw) == 0; return this->client.Send(packet_raw);
} }
/****************************/ /****************************/

View file

@ -9,7 +9,7 @@ namespace sosc {
class IntraClient { class IntraClient {
public: public:
IntraClient(); IntraClient();
bool Open(std::string host, uint16_t port); bool Open(const std::string& host, uint16_t port);
bool IsCiphered() const; bool IsCiphered() const;
void SetCipher(cgc::Cipher* cipher); void SetCipher(cgc::Cipher* cipher);
@ -30,7 +30,7 @@ public:
this->client.Close(); this->client.Close();
} }
private: private:
void Open(TcpClient client); void Open(const TcpClient& client);
bool client_open; bool client_open;
TcpClient client; TcpClient client;

View file

@ -43,10 +43,10 @@ public:
virtual void Stop(); virtual void Stop();
typedef std::vector<db::Query> Queries; typedef std::vector<db::Query*> Queries;
protected: protected:
virtual void SetupQueries(Queries* queries) {}; virtual void SetupQueries(Queries* queries) {};
virtual bool ProcessClient(T& client, Queries* queries) = 0; virtual bool ProcessClient(T& client, const Queries* queries) = 0;
private: private:
bool IsStackFull(int stackCount) const; bool IsStackFull(int stackCount) const;
bool CanAddStack() const; bool CanAddStack() const;
@ -103,7 +103,7 @@ void Pool<T>::Start() {
if(this->is_open) if(this->is_open)
return; return;
this->queries = std::vector<db::Query>(); this->queries = std::vector<db::Query*>();
this->SetupQueries(&this->queries); this->SetupQueries(&this->queries);
for(int i = 0; i < this->info.initial_count; ++i) { for(int i = 0; i < this->info.initial_count; ++i) {
this->stacks.push_back(new Stack(this)); this->stacks.push_back(new Stack(this));
@ -181,8 +181,10 @@ void Pool<T>::Stop() {
delete stack; delete stack;
} }
for(auto& query : this->queries) for(auto& query : this->queries) {
query.Close(); query->Close();
delete query;
}
this->stacks.clear(); this->stacks.clear();
this->is_open = false; this->is_open = false;
@ -201,7 +203,7 @@ void Pool<T>::Stack::Start() {
return; return;
for(auto& query : this->pool->queries) for(auto& query : this->pool->queries)
this->queries.push_back(db::Query(query)); this->queries.push_back(new db::Query(*query));
this->is_open = true; this->is_open = true;
this->is_running = true; this->is_running = true;
@ -256,8 +258,10 @@ void Pool<T>::Stack::Stop() {
if(!this->is_open || !this->is_running) if(!this->is_open || !this->is_running)
return; return;
for(auto& query : this->queries) for(auto& query : this->queries) {
query.Close(); query->Close();
delete query;
}
this->is_running = false; this->is_running = false;
this->thread->join(); this->thread->join();

View file

@ -7,13 +7,22 @@
sosc::ScapeConnection::ScapeConnection() { sosc::ScapeConnection::ScapeConnection() {
this->client_open = false; this->client_open = false;
this->handshaked = false; this->handshaked = false;
this->cipher = nullptr;
} }
void sosc::ScapeConnection::Open(TcpClient client) { void sosc::ScapeConnection::Open(const TcpClient& client) {
this->client = client; this->client = client;
this->client_open = true; this->client_open = true;
} }
bool sosc::ScapeConnection::IsCiphered() const {
return this->cipher != nullptr;
}
void sosc::ScapeConnection::SetCipher(cgc::Cipher* cipher) {
this->cipher = cipher;
}
int sosc::ScapeConnection::Handshake() { int sosc::ScapeConnection::Handshake() {
if(this->handshaked) if(this->handshaked)
return SOSC_SHAKE_DONE; return SOSC_SHAKE_DONE;
@ -81,12 +90,16 @@ int sosc::ScapeConnection::Receive(Packet* packet, bool block) {
return PCK_ERR; return PCK_ERR;
if(!block && !first_recv) if(!block && !first_recv)
return PCK_MORE; return PCK_MORE;
auto bufferSize = this->buffer.length();
status = this->client.Receive status = this->client.Receive
(&this->buffer, SOSC_TCP_APPEND | (block ? SOSC_TCP_BLOCK : 0)); (&this->buffer, SOSC_TCP_APPEND | (block ? SOSC_TCP_BLOCK : 0));
if(status == -1) if(status == -1)
return PCK_ERR; return PCK_ERR;
if(this->IsCiphered())
this->cipher->Parse(&this->buffer, bufferSize);
first_recv = false; first_recv = false;
} }
@ -110,6 +123,9 @@ bool sosc::ScapeConnection::Send(const Packet& packet) {
std::string packet_raw; std::string packet_raw;
packet.ToString(&packet_raw); packet.ToString(&packet_raw);
if(this->IsCiphered())
this->cipher->Parse(&packet_raw);
return this->client.Send(packet_raw); return this->client.Send(packet_raw);
} }

View file

@ -4,11 +4,12 @@
#include <queue> #include <queue>
#include "../crypto/sha1.hpp" #include "../crypto/sha1.hpp"
#include "../crypto/base64.hpp" #include "../crypto/base64.hpp"
#include "../crypto/cipher.hpp"
#include "frame.hpp" #include "frame.hpp"
#include "packet.hpp" #include "packet.hpp"
#include "tcpsock.hpp" #include "tcpsock.hpp"
#define SOSC_SHAKE_ERR -1 #define SOSC_SHAKE_ERR (-1)
#define SOSC_SHAKE_CONT 0 #define SOSC_SHAKE_CONT 0
#define SOSC_SHAKE_DONE 1 #define SOSC_SHAKE_DONE 1
@ -16,7 +17,10 @@ namespace sosc {
class ScapeConnection { class ScapeConnection {
public: public:
ScapeConnection(); ScapeConnection();
bool IsCiphered() const;
void SetCipher(cgc::Cipher* cipher);
int Handshake(); int Handshake();
int Receive(Packet* packet, bool block = false); int Receive(Packet* packet, bool block = false);
bool Send(const Packet& packet); bool Send(const Packet& packet);
@ -38,11 +42,12 @@ public:
this->client.Close(); this->client.Close();
} }
private: private:
void Open(TcpClient client); void Open(const TcpClient& client);
bool client_open; bool client_open;
bool handshaked; bool handshaked;
TcpClient client; TcpClient client;
cgc::Cipher* cipher;
std::string buffer; std::string buffer;
std::string pck_frames; std::string pck_frames;

View file

@ -46,7 +46,7 @@ public:
bool Open(std::string host, uint16_t port); bool Open(std::string host, uint16_t port);
int Receive(std::string* str, int flags = 0); int Receive(std::string* str, int flags = 0);
int Send(const std::string& str); bool Send(const std::string& str);
bool IsDataReady(); bool IsDataReady();
inline bool IsOpen() const { inline bool IsOpen() const {

View file

@ -94,9 +94,9 @@ int sosc::TcpClient::Receive(std::string* str, int flags) {
return total_length; return total_length;
} }
int sosc::TcpClient::Send(const std::string& str) { bool sosc::TcpClient::Send(const std::string& str) {
if(!this->sock_open) if(!this->sock_open)
return -1; return false;
std::string::size_type total_sent = 0; std::string::size_type total_sent = 0;
while(total_sent < str.length()) { while(total_sent < str.length()) {
@ -107,12 +107,12 @@ int sosc::TcpClient::Send(const std::string& str) {
if(sent == -1) { if(sent == -1) {
this->Close(); this->Close();
return -1; return false;
} else } else
total_sent += sent; total_sent += sent;
} }
return 0; return true;
} }
bool sosc::TcpClient::IsDataReady() { bool sosc::TcpClient::IsDataReady() {

View file

@ -110,9 +110,9 @@ int sosc::TcpClient::Receive(std::string* str, int flags) {
return total_length; return total_length;
} }
int sosc::TcpClient::Send(const std::string& str) { bool sosc::TcpClient::Send(const std::string& str) {
if(!this->sock_open) if(!this->sock_open)
return -1; return false;
std::string::size_type total_sent = 0; std::string::size_type total_sent = 0;
while(total_sent < str.length()) { while(total_sent < str.length()) {
@ -123,12 +123,12 @@ int sosc::TcpClient::Send(const std::string& str) {
if(sent == SOCKET_ERROR) { if(sent == SOCKET_ERROR) {
this->Close(); this->Close();
return -1; return false;
} else } else
total_sent += sent; total_sent += sent;
} }
return 0; return true;
} }
bool sosc::TcpClient::IsDataReady() { bool sosc::TcpClient::IsDataReady() {
@ -155,7 +155,7 @@ void sosc::TcpClient::SetBlocking(bool will_block) {
if(!this->sock_open) if(!this->sock_open)
return; return;
u_long nblock = !will_block; auto nblock = (u_long)!will_block;
ioctlsocket(this->sock, FIONBIO, &nblock); ioctlsocket(this->sock, FIONBIO, &nblock);
} }
@ -190,7 +190,7 @@ bool sosc::TcpServer::Listen(uint16_t port) {
hints.ai_protocol = IPPROTO_TCP; hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
if(getaddrinfo(NULL, TOSTR(port).c_str(), &hints, &result) != 0) if(getaddrinfo(nullptr, TOSTR(port).c_str(), &hints, &result) != 0)
return false; return false;
this->sock = socket(result->ai_family, this->sock = socket(result->ai_family,