diff --git a/protocol.md b/protocol.md index 0c51e1c..c7238fd 100644 --- a/protocol.md +++ b/protocol.md @@ -102,8 +102,8 @@ Communication between the master server and clients will be done over a WebSocke 1 - Error Message - String + Error Code + Packed Unsigned Short @@ -145,8 +145,8 @@ Communication between the master server and clients will be done over a WebSocke 2 - Error Message - String + Error Code + Packed Unsigned Short @@ -466,11 +466,16 @@ TODO: MAKE THIS SECTION NOT LOOK LIKE SHIT ### Master / Slave -#### S -> M (ID 1) +#### M -> S (ID 2) 0x01: KEY SIZE WAS INCORRECT 0x02: COULD NOT PARSE KEY +#### M -> S (ID 4) +0x01: LICENSE DATA INCORRECT + +0x02: LICENSE LIMIT REACHED + ### Master / Client ### Slave / Client \ No newline at end of file diff --git a/server/src/db/_init_sql.hpp b/server/src/db/_init_sql.hpp index 7fae755..be4a662 100644 --- a/server/src/db/_init_sql.hpp +++ b/server/src/db/_init_sql.hpp @@ -24,8 +24,8 @@ const std::vector _hard_db_sql = { /** START MIGRATION 0 **/ "CREATE TABLE `SERVER_LICENSES` (" "`KEY_ID` TEXT NOT NULL UNIQUE," - "`SECRET` TEXT NOT NULL UNIQUE," - "`ALLOWANCE` INTEGER NOT NULL" + "`SECRET` BLOB NOT NULL UNIQUE," + "`ALLOWANCE` INTEGER NOT NULL DEFAULT 0" ");\n" "CREATE TABLE `USERS` (" diff --git a/server/src/hosts/master.hpp b/server/src/hosts/master.hpp index 5fe1f6c..1c75cdd 100644 --- a/server/src/hosts/master.hpp +++ b/server/src/hosts/master.hpp @@ -8,6 +8,8 @@ #include "../crypto/keyex.hpp" #include "../crypto/cipher.hpp" +#include "../db/database.hpp" + namespace sosc { /** MASTER -> CLIENT **/ @@ -59,6 +61,11 @@ private: IntraClient sock; cgc::KeyExchange key; cgc::Cipher cipher; + + bool authed; + int auth_attempts; + const int MAX_AUTH_ATTEMPTS = 3; + std::string license; }; class MasterIntraPool : public Pool { diff --git a/server/src/hosts/master_intra.cpp b/server/src/hosts/master_intra.cpp index ab7d8a2..f128880 100644 --- a/server/src/hosts/master_intra.cpp +++ b/server/src/hosts/master_intra.cpp @@ -1,7 +1,10 @@ #include "master.hpp" +#include "../db/database.hpp" sosc::MasterIntra::MasterIntra(const IntraClient& client) { this->sock = client; + this->authed = false; + this->auth_attempts = 0; } bool sosc::MasterIntra::Process() { @@ -36,7 +39,20 @@ bool sosc::MasterIntra::InitAttempt(sosc::Packet &pck) { } bool sosc::MasterIntra::Authentication(sosc::Packet &pck) { + if(this->authed) + return true; + if(!pck.Check(2, PCK_ANY, 512)) + return this->Close(Packet(kNegativeAck, { "\x01" })); + + db::Query = db::Query::ScalarInt32( + "SELECT COUNT(*) FROM SERVER_LICENSES " + "WHERE KEY_ID = ? AND SECRET = ?" + ); + + if(isValid > 0) { + + } } bool sosc::MasterIntra::StatusUpdate(sosc::Packet &pck) {