scroob applied

This commit is contained in:
Alec Obradovich 2018-11-05 06:57:40 -06:00
parent 9932c52494
commit 9b3487d567
10 changed files with 72 additions and 41 deletions

Binary file not shown.

View file

@ -94,18 +94,21 @@ int sosc::Packet::Parse(const std::string& data, std::string* extra) {
switch(raw[ptr]) { switch(raw[ptr]) {
default: default:
region_length = raw[ptr]; region_length = raw[ptr];
++ptr;
break; break;
case 254: case 254:
if(ptr + 2 >= length) if(ptr + 2 >= length)
return PCK_ERR; return PCK_ERR;
region_length = net::ntohv<uint16_t>(data, ptr + 1); region_length = net::ntohv<uint16_t>(data, ptr + 1);
ptr += 3;
break; break;
case 255: case 255:
if(ptr + 4 >= length) if(ptr + 4 >= length)
return PCK_ERR; return PCK_ERR;
region_length = net::ntohv<uint32_t>(data, ptr + 1); region_length = net::ntohv<uint32_t>(data, ptr + 1);
ptr += 5;
break; break;
} }
@ -113,12 +116,12 @@ int sosc::Packet::Parse(const std::string& data, std::string* extra) {
body_length += region_length; body_length += region_length;
} }
if(body_length - ptr != 0) if(body_length + ptr < expected_length)
return PCK_ERR; return PCK_ERR;
for(int i = 0; i < region_count; ++i) { for(const auto region_length : region_lengths) {
this->regions[i] = data.substr(ptr, region_lengths[i]); this->regions.push_back(data.substr(ptr, region_length));
ptr += region_lengths[i]; ptr += region_length;
} }
if(length > expected_length && extra != nullptr) if(length > expected_length && extra != nullptr)

View file

@ -24,8 +24,14 @@ public:
bool Close(); bool Close();
bool Close(const Packet& message); bool Close(const Packet& message);
~MasterClient(); ~MasterClient() { this->Close(); };
private: private:
bool IsAuthed();
bool ProcessLogin(Packet& pck);
bool ProcessRegistration(Packet& pck);
bool ListServers(Packet& pck);
enum MasterToClientId { enum MasterToClientId {
kLoginResponse = 0, kLoginResponse = 0,
kRegisterResponse, kRegisterResponse,
@ -69,7 +75,7 @@ public:
bool Close(); bool Close();
bool Close(const Packet& message); bool Close(const Packet& message);
~MasterIntra(); ~MasterIntra() { this->Close(); }
private: private:
bool Authentication(Packet& pck); bool Authentication(Packet& pck);
bool StatusUpdate(Packet& pck); bool StatusUpdate(Packet& pck);

View file

@ -20,25 +20,31 @@ void sosc::MasterClientPool::SetupQueries(db::Queries *queries) {
"VALUES (?, ?, ?, 0, CURRENT_TIMESTAMP)" "VALUES (?, ?, ?, 0, CURRENT_TIMESTAMP)"
)); ));
#define QRY_USER_GET_PWD_HASH 2 #define QRY_USER_NAME_EXISTS 2
queries->push_back(new db::Query( queries->push_back(new db::Query(
"SELECT `ID`, `PASS_HASH` FROM `USERS` " "SELECT COUNT(*) FROM `USERS` "
"WHERE `USERNAME` = ?" "WHERE LOWER(`USERNAME`) = LOWER(?)"
)); ));
#define QRY_USER_GENERATE_KEY 3 #define QRY_USER_GET_PWD_HASH 3
queries->push_back(new db::Query(
"SELECT `ID`, `PASS_HASH` FROM `USERS` "
"WHERE LOWER(`USERNAME`) = LOWER(?)"
));
#define QRY_USER_GENERATE_KEY 4
queries->push_back(new db::Query( queries->push_back(new db::Query(
"INSERT OR IGNORE INTO `USER_KEYS` " "INSERT OR IGNORE INTO `USER_KEYS` "
"(`ID`, `SECRET`) VALUES (?, RANDOMBLOB(128))" "(`ID`, `SECRET`) VALUES (?, RANDOMBLOB(128))"
)); ));
#define QRY_USER_GET_KEY 4 #define QRY_USER_GET_KEY 5
queries->push_back(new db::Query( queries->push_back(new db::Query(
"SELECT `SECRET` FROM `USER_KEYS` " "SELECT `SECRET` FROM `USER_KEYS` "
"WHERE `ID` = ?" "WHERE `ID` = ?"
)); ));
#define QRY_USER_CHECK_KEY 5 #define QRY_USER_CHECK_KEY 6
queries->push_back(new db::Query( queries->push_back(new db::Query(
"SELECT COUNT(*) FROM `USER_KEYS` " "SELECT COUNT(*) FROM `USER_KEYS` "
"WHERE `ID` = ? AND `SECRET` = ?" "WHERE `ID` = ? AND `SECRET` = ?"
@ -67,19 +73,20 @@ bool sosc::MasterClient::Process(const db::Queries *queries) {
this->queries = queries; this->queries = queries;
switch(pck.GetId()) { switch(pck.GetId()) {
case kLoginRequest: case kLoginRequest:
return ProcessLogin(pck);
break;
case kRegisterRequest: case kRegisterRequest:
return ProcessRegistration(pck);
break;
case kServerListRequest: case kServerListRequest:
return ListServers(pck);
break;
default: default:
return this->Close(); return this->Close();
} }
} }
bool sosc::MasterClient::IsAuthed() {
}
bool sosc::MasterClient::Close() { bool sosc::MasterClient::Close() {
this->sock.Close(); this->sock.Close();
return false; return false;
@ -87,5 +94,5 @@ bool sosc::MasterClient::Close() {
bool sosc::MasterClient::Close(const Packet& message) { bool sosc::MasterClient::Close(const Packet& message) {
this->sock.Send(message); this->sock.Send(message);
this->Close(); return this->Close();
} }

View file

@ -4,3 +4,12 @@ sosc::SlaveClient::SlaveClient(const ScapeConnection& client) {
this->sock = client; this->sock = client;
} }
bool sosc::SlaveClient::Close() {
this->sock.Close();
return false;
}
bool sosc::SlaveClient::Close(const Packet& message) {
this->sock.Send(message);
return this->Close();
}

View file

@ -13,7 +13,10 @@ public:
SlaveClient() = delete; SlaveClient() = delete;
explicit SlaveClient(const ScapeConnection& client); explicit SlaveClient(const ScapeConnection& client);
~SlaveClient(); bool Close();
bool Close(const Packet& message);
~SlaveClient() { this->Close(); }
private: private:
ScapeConnection sock; ScapeConnection sock;
}; };

View file

@ -28,6 +28,7 @@ int main(int argc, char **argv) {
return -1; return -1;
if(argv[1][0] == 'm') { if(argv[1][0] == 'm') {
master_client(8008, sosc::poolinfo_t()); master_client(8008, sosc::poolinfo_t());
//master_intra(1234, sosc::poolinfo_t()); //master_intra(1234, sosc::poolinfo_t());
} else { } else {
@ -41,7 +42,7 @@ bool master_intra(uint16_t port, const sosc::poolinfo_t& info) {
using namespace sosc; using namespace sosc;
IntraServer server; IntraServer server;
IntraClient* client; IntraClient client;
if(!server.Listen(port)) if(!server.Listen(port))
return false; return false;
@ -49,8 +50,8 @@ bool master_intra(uint16_t port, const sosc::poolinfo_t& info) {
pool.Configure(info); pool.Configure(info);
pool.Start(); pool.Start();
while(server.Accept(client = new IntraClient())) while(server.Accept(&client))
pool.AddClient(MasterIntra(client)); pool.AddClient(new MasterIntra(client));
pool.Stop(); pool.Stop();
return true; return true;
@ -60,7 +61,7 @@ bool master_client(uint16_t port, const sosc::poolinfo_t& info) {
using namespace sosc; using namespace sosc;
ScapeServer server; ScapeServer server;
ScapeConnection* client; ScapeConnection client;
if(!server.Listen(port, true)) if(!server.Listen(port, true))
return false; return false;
@ -68,8 +69,8 @@ bool master_client(uint16_t port, const sosc::poolinfo_t& info) {
pool.Configure(info); pool.Configure(info);
pool.Start(); pool.Start();
while(server.Accept(client = new ScapeConnection())) while(server.Accept(&client))
pool.AddClient(MasterClient(client)); pool.AddClient(new MasterClient(client));
pool.Stop(); pool.Stop();
return true; return true;
@ -79,7 +80,7 @@ bool slave(uint16_t port, const sosc::poolinfo_t& info) {
using namespace sosc; using namespace sosc;
ScapeServer server; ScapeServer server;
ScapeConnection* client; ScapeConnection client;
if(!server.Listen(port)) if(!server.Listen(port))
return false; return false;
@ -87,8 +88,8 @@ bool slave(uint16_t port, const sosc::poolinfo_t& info) {
pool.Configure(info); pool.Configure(info);
pool.Start(); pool.Start();
while(server.Accept(client = new ScapeConnection())) while(server.Accept(&client))
pool.AddClient(SlaveClient(client)); pool.AddClient(new SlaveClient(client));
pool.Stop(); pool.Stop();
return true; return true;

View file

@ -240,7 +240,7 @@ void Pool<T,U>::Stack::StackThread() {
while(this->is_running) { while(this->is_running) {
for(auto client = this->clients.begin(); for(auto client = this->clients.begin();
client != this->clients.end() && this->clients.size() > 0; client != this->clients.end();
++client) ++client)
{ {
if(!this->is_running) if(!this->is_running)
@ -248,10 +248,10 @@ void Pool<T,U>::Stack::StackThread() {
this->clients_mtx.lock(); this->clients_mtx.lock();
if(!this->pool->ProcessClient if(!this->pool->ProcessClient
(client, &this->pool->context, &this->queries)) (*client, &this->pool->context, &this->queries))
{ {
this->clients.erase(client); delete *client;
delete client; client = this->clients.erase(client);
} }
this->clients_mtx.unlock(); this->clients_mtx.unlock();
} }

View file

@ -81,8 +81,8 @@ function pack(id, regions) {
throw Error("REGIONS ARGUMENT MALFORMED"); throw Error("REGIONS ARGUMENT MALFORMED");
let pck; let pck;
let size = 8; let size = 0;
let head_size = size; let head_size = 8;
for(let i in regions) { for(let i in regions) {
if(typeof regions[i] === "string") if(typeof regions[i] === "string")
regions[i] = regions[i].toByteArray(); regions[i] = regions[i].toByteArray();
@ -103,8 +103,8 @@ function pack(id, regions) {
pck = new Uint8Array(size); pck = new Uint8Array(size);
pck.set(MAHOU, 0); pck.set(MAHOU, 0);
pck.set(size.packUint32(), 2); pck.set(size.packUint32(), 2);
pck[6] = regions.length; pck[6] = Math.min(id, 255);
pck[7] = Math.min(id, 255); pck[7] = regions.length;
let head_ptr = 8; let head_ptr = 8;
let body_ptr = head_size; let body_ptr = head_size;
@ -127,6 +127,8 @@ function pack(id, regions) {
body_ptr += length; body_ptr += length;
} }
console.log(regions);
console.log(pck);
return pck; return pck;
} }

View file

@ -2,10 +2,10 @@ body {
padding-top: 36px; padding-top: 36px;
font-family: monospace; font-family: monospace;
text-align: center; text-align: center;
/*background: #000; background: #000;
color: #fff;*/ color: #fff;
background: #fff; /*background: #fff;
color: #000; color: #000;*/
} }
.hidden { .hidden {