123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- #include <QDebug>
- #include <QGuiApplication>
- #include <csignal>
- #include <cstdio>
- #include <cstdlib>
- #include <iostream>
- #include <poll.h>
- #include <string>
- #include <sys/prctl.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- #include <thread>
- #include <unistd.h>
- #include "config.h"
- #include "qmlhandler.h"
- #include <boost/asio.hpp>
- #include <boost/filesystem.hpp>
- #include <boost/lexical_cast.hpp>
- #include <iostream>
- #include <json/json.h>
- using boost::lexical_cast;
- using boost::asio::buffer;
- using namespace std;
- int inpipefd[2];
- int outpipefd[2];
- char buf[1025];
- QUrl sendFileUrl = QUrl("");
- QString _IP = "";
- bool _CLI_RUNNING = false;
- bool _RESTART = false;
- pid_t childpid;
- bool programActive = true;
- QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
- void QMLHandler::closeCLI() { _CLI_RUNNING = false; }
- void QMLHandler::loadSettingsToGUI() {
- int covertMethod = lexical_cast<int>(Config::getValue("Covert-Channel-Method"));
- bool saveIP = lexical_cast<bool>(Config::getValue("Autofill-IP"));
- bool saveUsername = lexical_cast<bool>(Config::getValue("Autofill-Username"));
- QString cliPath = QString::fromStdString(Config::getValue("CLI-Path"));
- emit loadSettings(covertMethod, saveIP, saveUsername, cliPath);
- }
- void QMLHandler::reopenCLI(QString ip) {
- if (_CLI_RUNNING) {
- waitpid(childpid, NULL, 0);
- closeCLI();
- }
- _IP = ip;
- pipe(inpipefd);
- pipe(outpipefd);
- childpid = fork();
- if (childpid == 0) {
- // Child
- dup2(outpipefd[0], STDIN_FILENO);
- dup2(inpipefd[1], STDOUT_FILENO);
- // dup2(inpipefd[1], STDERR_FILENO);
- // ask kernel to deliver SIGTERM in case the parent dies
- prctl(PR_SET_PDEATHSIG, SIGTERM);
- // Set the path to the CLI - pass argument h (help) for now
- // TODO: Change hardcoded path
- // execl("../../cli/build/ccats-cli", "ccats-cli", ip.toUtf8().constData(), "--machine", (char *)NULL);
- execl(Config::getValue("CLI-Path").c_str(), "ccats-cli", ip.toUtf8().constData(), "--machine", (char *)NULL);
- exit(1);
- }
- _CLI_RUNNING = true;
- close(outpipefd[0]);
- close(inpipefd[1]);
- std::thread(&QMLHandler::readPipeLoop, this).detach();
- }
- std::vector<std::string> tokenizeByNewlines(std::string in) {
- vector<string> res;
- size_t index;
- for (index = in.find("\n"); index != std::string::npos; index = in.find("\n")) {
- if (index != 0)
- res.push_back(in.substr(0, index));
- in = in.substr(index + 1);
- }
- if (in.length() > 0)
- res.push_back(in);
- return res;
- }
- void QMLHandler::onExit() { write(outpipefd[1], "disconnect\n", strlen("disconnect\n")); }
- // This method gets a string and tries to read it as Json
- // If it fails to do so, return and do nothing, else handle the content
- void QMLHandler::handleJSON(string buffer) {
- Json::Value root;
- Json::CharReaderBuilder builder;
- Json::CharReader *reader = builder.newCharReader();
- string jsonError;
- // Try to parse the string as Json and store the result of the pasring in a
- // boolean
- bool parsingSuccessful = reader->parse(buffer.c_str(), buffer.c_str() + buffer.size(), &root, &jsonError);
- // If the string is not correct Json, return
- if (!parsingSuccessful) {
- return;
- }
- const Json::Value command = root["command"];
- string cmd = command.asString();
- qInfo() << QString::fromStdString("Received command " + cmd);
- if (!cmd.compare("status")) {
- emit footerSetStatus(root["response"].asString().c_str());
- }
- else if (!cmd.compare("close")) {
- programActive = false;
- }
- else if (!cmd.compare("list")) {
- if (root["accept"] == true) {
- emit receivingClearFileList();
- // Get the array of file Names
- auto fileNames = root["names"];
- for (int i = 0; i < fileNames.size(); i++) {
- emit receivingListFile(QString::fromStdString(fileNames[i].asString().c_str()), boost::filesystem::exists(fileNames[i].asString()));
- }
- } else {
- emit log(root["error"].asString().c_str());
- }
- }
- else if (!cmd.compare("connect")) {
- if (root["accept"].asBool()) {
- } else {
- emit ipPopupSetStatus(root["error"].asString().c_str());
- closeCLI();
- emit ipPopupEnableConnectButton();
- }
- }
- else if (!cmd.compare("version")) {
- if (root["accept"] == true) {
- emit ipPopupClose();
- emit loginSignupPopupOpen();
- } else {
- QString errorMessage = QString::fromStdString(
- string("Version mismatch: \nClient: " + root["clientversion"].asString() + "\nServer: " + root["serverversion"].asString()));
- emit ipPopupSetStatus(errorMessage);
- closeCLI();
- emit ipPopupEnableConnectButton();
- }
- }
- else if (!cmd.compare("login")) {
- if (root["accept"] == true) {
- emit loginSignupPopupClose();
- write(outpipefd[1], "list\n", strlen("list\n"));
- loadSettingsToGUI();
- } else {
- emit loginSetStatus(root["error"].asString().c_str());
- reopenCLI(_IP);
- emit loginEnableLoginButton();
- }
- }
- else if (!cmd.compare("signup")) {
- if (root["accept"] == true) {
- emit loginSignupPopupClose();
- } else {
- emit signupSetStatus(root["error"].asString().c_str());
- reopenCLI(_IP);
- emit signupEnableRegisterButton();
- }
- }
- else if (!cmd.compare("put")) {
- if (root["accept"] == false) {
- QString errorMessage = QString::fromStdString(string("Error when uploading file " + root["file"].asString() + ":\n" + root["error"].asString()));
- emit log(errorMessage);
- }
- }
- else if (!cmd.compare("putdata")) {
- // TODO: Show speed and handle Error
- }
- else if (!cmd.compare("get")) {
- if (root["accept"] == false) {
- QString errorMessage = QString::fromStdString(string("Error when downloading file " + root["file"].asString() + ":\n" + root["error"].asString()));
- emit log(errorMessage);
- } else {
- string fileName = root["file"].asString();
- // TODO: Only do this in getdata when remaining is 0 (when the file is fully downloaded) - maybe set text to "downloading.." in between
- emit receivingDisableDownloadButton(QString::fromStdString(fileName));
- }
- }
- else if (!cmd.compare("getdata")) {
- // TODO: Show speed and handle Error
- }
- else if (!cmd.compare("deleteme")) {
- if (root["accept"] == true) {
- _RESTART = true;
- emit closeWindow();
- } else {
- QString errorMessage = QString::fromStdString(root["error"].asString());
- emit deleteMePopupSetStatus(errorMessage);
- }
- }
- else if (!cmd.compare("deletefile")) {
- emit receivingCloseConfirmDeletePopup();
- if (root["accept"] == false) {
- QString errorMessage = QString::fromStdString(string("Error when deleting file " + root["file"].asString() + ":\n" + root["error"].asString()));
- emit log(errorMessage);
- } else {
- QString message = QString::fromStdString(string("Deleted file " + root["file"].asString() + " from the server!"));
- emit log(message);
- }
- }
- }
- // This method is a loop which runs in a seperate thread.
- // If will read the Input Pipe, which containts the string that get printed
- // on stdout by the CLI/Server, and calls handleJSON with the read content
- // one it reads a newline.
- void QMLHandler::readPipeLoop() {
- unsigned int readOffset = 0;
- unsigned int pollCount = 0;
- struct pollfd inPipeStatus;
- inPipeStatus.fd = inpipefd[0];
- inPipeStatus.events = POLLIN;
- vector<string> inputs;
- string pipeInput;
- while (programActive && _CLI_RUNNING) {
- inputs = vector<string>();
- poll(&inPipeStatus, 1, 100);
- if (inPipeStatus.revents & POLLIN) {
- readOffset += read(inpipefd[0], buf + readOffset, 1);
- pollCount = 0;
- } else {
- pollCount++;
- }
- if (pollCount > 4 && (readOffset || pipeInput.size())) {
- pipeInput.append(buf);
- inputs = tokenizeByNewlines(pipeInput);
- for (string s : inputs) {
- emit log(QString::fromStdString(s));
- qInfo() << QString::fromStdString(s);
- handleJSON(s);
- }
- pipeInput = string();
- memset(buf, 0, 1025);
- pollCount = 0;
- readOffset = 0;
- if (waitpid(childpid, NULL, WNOHANG)) {
- // nonzero means error or childid has changed state
- // for us that means child has exited -> CLI is dead
- break;
- }
- }
- if (readOffset >= 1024) {
- pipeInput.append(buf);
- readOffset = 0;
- pollCount = 0;
- memset(buf, 0, 1025);
- }
- }
- }
- // ### QML Handlers ###
- void QMLHandler::onStart() {
- bool configExists = Config::loadFile();
- if (configExists == true) {
- // Config exists
- if (Config::checkConfig() == true) {
- // Config is valid
- if (!boost::filesystem::is_regular_file(Config::getValue("CLI-Path")) ||
- boost::filesystem::path(Config::getValue("CLI-Path")).filename().compare("ccats-cli")) {
- // Invalid CLI Path
- emit invalidCliPathPopupOpen();
- }
- if (Config::getValue("Autofill-IP") == "1") {
- emit ipPopupSetIP(QString::fromStdString(Config::getValue("Default-IP")));
- emit ipPopupCheckSaveCheckbox();
- }
- if (Config::getValue("Autofill-Username") == "1") {
- emit loginSetUsername(QString::fromStdString(Config::getValue("Default-Username")));
- emit loginSignupCheckSaveCheckbox();
- }
- } else {
- // Config is invalid
- emit invalidConfigPopupOpen();
- }
- } else {
- // Config doesn't exist
- Config::setupDefaultConfig();
- emit noConfigFoundPopupOpen();
- }
- }
- // No Config Found Popup
- void QMLHandler::onNoConfigFoundPopupContinueButton(QString cli_path) {
- Config::setValue("CLI-Path", cli_path.toUtf8().constData());
- Config::saveFile();
- }
- // Invalid Cli Path Popup
- void QMLHandler::onInvalidCliPathPopupContinueButton(QString cli_path) {
- Config::setValue("CLI-Path", cli_path.toUtf8().constData());
- Config::saveFile();
- }
- void QMLHandler::onInvalidCliPathPopupQuitButton() { emit closeWindow(); }
- // Invalid Config Popup
- void QMLHandler::onInvalidConfigPopupQuitButton() { emit closeWindow(); }
- void QMLHandler::onInvalidConfigPopupCreateDefaultButton() {
- Config::setupDefaultConfig();
- emit noConfigFoundPopupOpen();
- }
- // Sending
- void QMLHandler::onSendingSelectFileButton(QUrl url) {
- sendFileUrl = url.toLocalFile();
- emit log("File Selected: " + sendFileUrl.toString());
- emit sendingSetFileUrlText("Selected File: " + sendFileUrl.toString());
- emit sendingEnableSendButton();
- }
- void QMLHandler::onSendingSendFileButton() {
- QString command = "put " + sendFileUrl.toString() + "\n";
- write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
- }
- void QMLHandler::onSendingClearSelectionButton() {
- sendFileUrl = QUrl("");
- emit log("Cleared Selection");
- emit sendingSetFileUrlText("Selected File: None");
- emit sendingDisableSendButton();
- }
- // Receiving
- void QMLHandler::onReceivingListFilesButton() { write(outpipefd[1], "list\n", strlen("list\n")); }
- void QMLHandler::onReceivingDownloadFileButton(QString fileName) {
- QString command = "get " + fileName + "\n";
- write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
- }
- void QMLHandler::onReceivingConfirmDeleteFileButton(QString fileName) {
- QString command = "deletefile " + fileName + "\n";
- write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
- }
- // Messages
- void QMLHandler::onMessagesSendButton(QString msg) { emit message(msg); }
- // Settings
- void QMLHandler::onSettingsDeleteMeButton(QString password) {
- QString command = "deleteme " + password + "\n";
- write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
- }
- void QMLHandler::onSettingsRevertChangesButton() {
- loadSettingsToGUI();
- emit log("Settings changes reverted.");
- }
- void QMLHandler::onSettingsResetButton() {
- string cli_path = Config::getValue("CLI-Path");
- Config::setupDefaultConfig();
- Config::setValue("CLI-Path", cli_path);
- loadSettingsToGUI();
- emit log("Settings resetted to default.");
- }
- void QMLHandler::onSettingsSaveButton(int covertMethod, bool saveIP, bool saveUsername, QString cliPath) {
- Config::setValue("Covert-Channel-Method", lexical_cast<string>(covertMethod));
- Config::setValue("Autofill-IP", lexical_cast<string>(saveIP));
- Config::setValue("Autofill-Username", lexical_cast<string>(saveUsername));
- Config::setValue("CLI-Path", cliPath.toUtf8().constData());
- Config::saveFile();
- emit log("Settings saved.");
- }
- // Ip Popup
- void QMLHandler::onIpPopupConnectButton(QString ip, bool saveAsDefault) {
- reopenCLI(ip);
- emit ipPopupDisableConnectButton();
- if (saveAsDefault) {
- Config::setValue("Default-IP", ip.toUtf8().constData());
- Config::setValue("Autofill-IP", "1");
- Config::saveFile();
- }
- }
- // Login
- void QMLHandler::onLoginLoginButton(QString username, QString password, bool saveAsDefault) {
- QString command = "login " + username + " " + password + "\n";
- write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
- emit loginDisableLoginButton();
- if (saveAsDefault) {
- Config::setValue("Default-Username", username.toUtf8().constData());
- Config::setValue("Autofill-Username", "1");
- Config::saveFile();
- }
- }
- // Signup
- void QMLHandler::onSignupRegisterButton(QString username, QString passwordOne, QString passwordTwo, bool saveAsDefault) {
- if (QString::compare(passwordOne, passwordTwo, Qt::CaseSensitive)) {
- emit signupSetStatus("Passwords don't match");
- return;
- }
- QString command = "signup " + username + " " + passwordOne + "\n";
- write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
- emit signupDisableRegisterButton();
- if (saveAsDefault) {
- Config::setValue("Default-Username", username.toUtf8().constData());
- Config::setValue("Autofill-Username", "1");
- Config::saveFile();
- }
- }
- // Footer
- void QMLHandler::onFooterGetStatusButton() { write(outpipefd[1], "status\n", strlen("status\n")); }
|