diff --git a/client/js/scape.js b/client/js/scape.js index a78e31c..5444380 100644 --- a/client/js/scape.js +++ b/client/js/scape.js @@ -34,6 +34,8 @@ var Entrypoint = (function () { }; Entrypoint.start = function () { var _this = this; + var conn = new Connection("ws://127.0.0.1:8080", []); + conn.open(); Key.init(function () { _this.initStatus.keyInit = true; _this.initCheck(); @@ -50,7 +52,7 @@ var Entrypoint = (function () { }); }; Entrypoint.ready = function () { - alert("ready"); + //alert("ready"); }; return Entrypoint; }()); diff --git a/client/src/Entrypoint.ts b/client/src/Entrypoint.ts index 05ccd0d..b53a2dd 100644 --- a/client/src/Entrypoint.ts +++ b/client/src/Entrypoint.ts @@ -34,6 +34,9 @@ class Entrypoint { } public static start(): void { + var conn = new Connection("ws://127.0.0.1:8080", []); + conn.open(); + Key.init(() => { this.initStatus.keyInit = true; this.initCheck(); @@ -54,6 +57,6 @@ class Entrypoint { } private static ready(): void { - alert("ready"); + //alert("ready"); } } \ No newline at end of file diff --git a/server/src/.main.cpp.kate-swp b/server/src/.main.cpp.kate-swp deleted file mode 100644 index 59be521..0000000 Binary files a/server/src/.main.cpp.kate-swp and /dev/null differ diff --git a/server/src/main.cpp b/server/src/main.cpp index b801bc4..e2a0f18 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -14,90 +14,19 @@ #include "utils/bigint.hpp" int main(int argc, char **argv) { - //auto sock = sosc::TcpClient(); + sosc::TcpServer server; + sosc::TcpClient client; + std::string buffer; - /*std::string a = "this!!is!!a!!test"; - auto b = sosc::str::split(a, "!!"); - std::for_each(b.begin(), b.end(), [](std::string& i) { - std::cout << i << std::endl; - });*/ + server.Listen(8080); + std::cout << "Listening ..." << std::endl; + server.Accept(&client); + std::cout << "Reading ..." << std::endl; - /*sosc::TcpClient client; - client.Open("127.0.0.1", 1111); - - client.Send("test"); - std::string got = "abc"; - - while(client.IsOpen()) { - int length = client.Receive(&got); - - if(length > 0) - std::cout << got << std::endl; - }*/ - - //std::string a = sosc::cgc::sha1("test", true); - - /*sosc::cgc::Blowfish fish("TESTKEY"); - - std::string test = fish.Encrypt("imagine a test"); - std::string testd = fish.Decrypt(test); - - uint32_t teest = sosc::csprng::next(); - std::cout << std::hex << teest;*/ - - /*std::string hash = sosc::cgc::bcrypt_hash("test pwd"); - std::cout << hash << std::endl; - std::cout << sosc::cgc::bcrypt_check("test pwd", hash);*/ - - sosc::BigUInt a, b, c; - - /*a = sosc::BigUInt::GenerateRandom(128); - b = sosc::BigUInt::GenerateRandom(128); - c = sosc::BigUInt::GenerateRandom(128);*/ - - //assert(a - b == sosc::BigUInt("feff01")); - - //auto d = sosc::BigUInt::DivideWithRemainder(a, b); - - /*for(int i = a.ByteCount() * 8 - 1; i >= 0; --i) { - std::cout << a.GetBit(i); - b.SetBit(i, a.GetBit(i)); - }*/ - - //std::cout << sosc::BigUInt::GenerateRandomPrime(64).ToString(); - //std::cout << a.IsProbablePrime(); - - //for(int i = 0; i < 250; ++i) - - time_t start = time(NULL); - - /*auto d = sosc::BigUInt::DivideWithRemainder(a, b); - std::cout << d.result.ToString() << std::endl - << d.remainder.ToString() << std::endl;*/ - - std::cout //<< a.ToString() << std::endl - //<< b.ToString() << std::endl - //<< (a * b).ToString() << std::endl; - //<< c.ToString() << std::endl << std::endl - //<< sosc::BigUInt::ModPow(a, b, c).ToString() << std::endl; - - << sosc::BigUInt::GenerateRandomPrime(16).ToString() << std::endl - << sosc::BigUInt::GenerateRandomPrime(16).ToString() << std::endl - << sosc::BigUInt::GenerateRandomPrime(16).ToString() << std::endl - << sosc::BigUInt::GenerateRandomPrime(16).ToString() << std::endl; - - - - std::cout << (time(NULL) - start) << std::endl; - - //std::cout << a.ToString(); - - //std::cout << a.ToString(); - - /*std::cout << std::endl << std::endl - << d.result.ToString() - << std::endl - << d.remainder.ToString();*/ + while(true) { + client.Receive(&buffer, SOSC_TCP_BLOCK); + std::cout << buffer; + } return 0; } diff --git a/server/src/sock/scapesock.cpp b/server/src/sock/scapesock.cpp index 7715141..19f7429 100644 --- a/server/src/sock/scapesock.cpp +++ b/server/src/sock/scapesock.cpp @@ -1,2 +1,69 @@ #include "scapesock.hpp" +/**********************************/ +/* BEGIN SCAPECONNECTION CODE */ +/**********************************/ + +sosc::ScapeConnection::ScapeConnection() { + this->client_open = false; + this->handshaked = false; +} + +void sosc::ScapeConnection::Open(TcpClient client) { + this->client = client; + this->client_open = true; +} + +int sosc::ScapeConnection::Handshake() { + if(this->handshaked) + return SOSC_SHAKE_DONE; + if(!this->client_open) + return SOSC_SHAKE_ERR; + + if(!this->client.IsDataReady()) + return SOSC_SHAKE_CONT; + + this->client.Receive(&this->buffer, SOSC_TCP_APPEND); + if(!str::contains(this->buffer, "\r\n\r\n")) + return SOSC_SHAKE_CONT; + + if(!str::starts(this->buffer, "GET")) { + this->Close(); + return SOSC_SHAKE_ERR; + } + + auto lines = str::split(this->buffer, "\r\n"); + + this->handshaked = true; + return SOSC_SHAKE_DONE; +} + +/******************************/ +/* END SCAPECONNECTION CODE */ +/******************************/ +/* BEGIN SCAPESERVER CODE */ +/******************************/ + +sosc::ScapeServer::ScapeServer() { + this->server_open = false; +} + +bool sosc::ScapeServer::Listen(uint16_t port) { + if(this->server_open) + return false; + + this->server = TcpServer(); + this->server.Listen(port); + this->server_open = true; + return true; +} + +int sosc::ScapeServer::Accept(ScapeConnection* client) { + TcpClient raw_client; + int status = this->server.Accept(&raw_client); + if(status != 0) + return status; + + + return 0; +} diff --git a/server/src/sock/scapesock.hpp b/server/src/sock/scapesock.hpp index a3accc1..2905306 100644 --- a/server/src/sock/scapesock.hpp +++ b/server/src/sock/scapesock.hpp @@ -4,11 +4,16 @@ #include "packet.hpp" #include "tcpsock.hpp" +#define SOSC_SHAKE_ERR -1 +#define SOSC_SHAKE_CONT 0 +#define SOSC_SHAKE_DONE 1 + namespace sosc { class ScapeConnection { public: ScapeConnection(); + int Handshake(); int Receive(Packet* packet, bool block = false); bool Send(const Packet& packet); @@ -16,6 +21,10 @@ public: return this->client_open; } + inline bool Handshaked() const { + return this->handshaked; + } + inline void Close() { this->client_open = false; this->client.Close(); @@ -26,6 +35,7 @@ private: void Open(TcpClient client); bool client_open; + bool handshaked; TcpClient client; std::string buffer; diff --git a/server/src/utils/string.cpp b/server/src/utils/string.cpp index 3cf199d..3af624a 100644 --- a/server/src/utils/string.cpp +++ b/server/src/utils/string.cpp @@ -114,3 +114,9 @@ bool sosc::str::ends end.begin(), end.end(), str.end() - end.length() ).first == end.end(); } + +bool sosc::str::contains + (const std::string& haystack, const std::string& needle) +{ + return haystack.find(needle) != std::string::npos; +} diff --git a/server/src/utils/string.hpp b/server/src/utils/string.hpp index 6f26dcb..266fd0c 100644 --- a/server/src/utils/string.hpp +++ b/server/src/utils/string.hpp @@ -33,6 +33,8 @@ std::string join(const std::vector& parts, bool starts(const std::string& str, const std::string& start); bool ends(const std::string& str, const std::string& end); + +bool contains(const std::string& haystack, const std::string& needle); }} #endif