|
@@ -12,6 +12,7 @@
|
|
#include <thread>
|
|
#include <thread>
|
|
#include <unistd.h>
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
+#include "../include/climanager.h"
|
|
#include "../include/config.h"
|
|
#include "../include/config.h"
|
|
#include "../include/jsonhandler.h"
|
|
#include "../include/jsonhandler.h"
|
|
#include "../include/qmlhandler.h"
|
|
#include "../include/qmlhandler.h"
|
|
@@ -26,22 +27,11 @@ using boost::asio::buffer;
|
|
|
|
|
|
using namespace std;
|
|
using namespace std;
|
|
|
|
|
|
-int inpipefd[2];
|
|
|
|
-int outpipefd[2];
|
|
|
|
-
|
|
|
|
-char buf[1025];
|
|
|
|
QUrl sendFileUrl = QUrl("");
|
|
QUrl sendFileUrl = QUrl("");
|
|
-QString _IP = "";
|
|
|
|
-bool _CLI_RUNNING = false;
|
|
|
|
bool _RESTART = false;
|
|
bool _RESTART = false;
|
|
-pid_t childpid;
|
|
|
|
-
|
|
|
|
-bool programActive = true;
|
|
|
|
|
|
|
|
QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
|
|
QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
|
|
|
|
|
|
-void QMLHandler::closeCLI() { _CLI_RUNNING = false; }
|
|
|
|
-
|
|
|
|
void QMLHandler::loadSettingsToGUI() {
|
|
void QMLHandler::loadSettingsToGUI() {
|
|
int covertMethod = lexical_cast<int>(Config::getValue("Covert-Channel-Method"));
|
|
int covertMethod = lexical_cast<int>(Config::getValue("Covert-Channel-Method"));
|
|
bool saveIP = lexical_cast<bool>(Config::getValue("Autofill-IP"));
|
|
bool saveIP = lexical_cast<bool>(Config::getValue("Autofill-IP"));
|
|
@@ -50,111 +40,6 @@ void QMLHandler::loadSettingsToGUI() {
|
|
emit loadSettings(covertMethod, saveIP, saveUsername, cliPath);
|
|
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 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);
|
|
|
|
- JsonHandler::parseJSON(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 ###
|
|
// ### QML Handlers ###
|
|
|
|
|
|
void QMLHandler::onStart() {
|
|
void QMLHandler::onStart() {
|
|
@@ -186,6 +71,8 @@ void QMLHandler::onStart() {
|
|
Config::setupDefaultConfig();
|
|
Config::setupDefaultConfig();
|
|
emit noConfigFoundPopupOpen();
|
|
emit noConfigFoundPopupOpen();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ CliManager::startCli();
|
|
}
|
|
}
|
|
|
|
|
|
// No Config Found Popup
|
|
// No Config Found Popup
|
|
@@ -219,8 +106,8 @@ void QMLHandler::onSendingSelectFileButton(QUrl url) {
|
|
}
|
|
}
|
|
|
|
|
|
void QMLHandler::onSendingSendFileButton() {
|
|
void QMLHandler::onSendingSendFileButton() {
|
|
- QString command = "put " + sendFileUrl.toString() + "\n";
|
|
|
|
- write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
|
|
|
|
|
|
+ QString command = "put " + sendFileUrl.toString();
|
|
|
|
+ CliManager::writeToCli(command);
|
|
}
|
|
}
|
|
|
|
|
|
void QMLHandler::onSendingClearSelectionButton() {
|
|
void QMLHandler::onSendingClearSelectionButton() {
|
|
@@ -231,16 +118,16 @@ void QMLHandler::onSendingClearSelectionButton() {
|
|
}
|
|
}
|
|
|
|
|
|
// Receiving
|
|
// Receiving
|
|
-void QMLHandler::onReceivingListFilesButton() { write(outpipefd[1], "list\n", strlen("list\n")); }
|
|
|
|
|
|
+void QMLHandler::onReceivingListFilesButton() { CliManager::writeToCli("list"); }
|
|
|
|
|
|
void QMLHandler::onReceivingDownloadFileButton(QString fileName) {
|
|
void QMLHandler::onReceivingDownloadFileButton(QString fileName) {
|
|
- QString command = "get " + fileName + "\n";
|
|
|
|
- write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
|
|
|
|
|
|
+ QString command = "get " + fileName;
|
|
|
|
+ CliManager::writeToCli(command);
|
|
}
|
|
}
|
|
|
|
|
|
void QMLHandler::onReceivingConfirmDeleteFileButton(QString fileName) {
|
|
void QMLHandler::onReceivingConfirmDeleteFileButton(QString fileName) {
|
|
- QString command = "deletefile " + fileName + "\n";
|
|
|
|
- write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
|
|
|
|
|
|
+ QString command = "deletefile " + fileName;
|
|
|
|
+ CliManager::writeToCli(command);
|
|
}
|
|
}
|
|
|
|
|
|
// Messages
|
|
// Messages
|
|
@@ -248,8 +135,8 @@ void QMLHandler::onMessagesSendButton(QString msg) { emit message(msg); }
|
|
|
|
|
|
// Settings
|
|
// Settings
|
|
void QMLHandler::onSettingsDeleteMeButton(QString password) {
|
|
void QMLHandler::onSettingsDeleteMeButton(QString password) {
|
|
- QString command = "deleteme " + password + "\n";
|
|
|
|
- write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
|
|
|
|
|
|
+ QString command = "deleteme " + password;
|
|
|
|
+ CliManager::writeToCli(command);
|
|
}
|
|
}
|
|
|
|
|
|
void QMLHandler::onSettingsRevertChangesButton() {
|
|
void QMLHandler::onSettingsRevertChangesButton() {
|
|
@@ -276,7 +163,9 @@ void QMLHandler::onSettingsSaveButton(int covertMethod, bool saveIP, bool saveUs
|
|
|
|
|
|
// Ip Popup
|
|
// Ip Popup
|
|
void QMLHandler::onIpPopupConnectButton(QString ip, bool saveAsDefault) {
|
|
void QMLHandler::onIpPopupConnectButton(QString ip, bool saveAsDefault) {
|
|
- reopenCLI(ip);
|
|
|
|
|
|
+ QString command = "connect " + ip;
|
|
|
|
+ CliManager::writeToCli(command);
|
|
|
|
+
|
|
emit ipPopupDisableConnectButton();
|
|
emit ipPopupDisableConnectButton();
|
|
if (saveAsDefault) {
|
|
if (saveAsDefault) {
|
|
Config::setValue("Default-IP", ip.toUtf8().constData());
|
|
Config::setValue("Default-IP", ip.toUtf8().constData());
|
|
@@ -287,8 +176,8 @@ void QMLHandler::onIpPopupConnectButton(QString ip, bool saveAsDefault) {
|
|
|
|
|
|
// Login
|
|
// Login
|
|
void QMLHandler::onLoginLoginButton(QString username, QString password, bool saveAsDefault) {
|
|
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()));
|
|
|
|
|
|
+ QString command = "login " + username + " " + password;
|
|
|
|
+ CliManager::writeToCli(command);
|
|
emit loginDisableLoginButton();
|
|
emit loginDisableLoginButton();
|
|
if (saveAsDefault) {
|
|
if (saveAsDefault) {
|
|
Config::setValue("Default-Username", username.toUtf8().constData());
|
|
Config::setValue("Default-Username", username.toUtf8().constData());
|
|
@@ -303,8 +192,8 @@ void QMLHandler::onSignupRegisterButton(QString username, QString passwordOne, Q
|
|
emit signupSetStatus("Passwords don't match");
|
|
emit signupSetStatus("Passwords don't match");
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- QString command = "signup " + username + " " + passwordOne + "\n";
|
|
|
|
- write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
|
|
|
|
|
|
+ QString command = "signup " + username + " " + passwordOne;
|
|
|
|
+ CliManager::writeToCli(command);
|
|
emit signupDisableRegisterButton();
|
|
emit signupDisableRegisterButton();
|
|
if (saveAsDefault) {
|
|
if (saveAsDefault) {
|
|
Config::setValue("Default-Username", username.toUtf8().constData());
|
|
Config::setValue("Default-Username", username.toUtf8().constData());
|
|
@@ -314,12 +203,6 @@ void QMLHandler::onSignupRegisterButton(QString username, QString passwordOne, Q
|
|
}
|
|
}
|
|
|
|
|
|
// Footer
|
|
// Footer
|
|
-void QMLHandler::onFooterGetStatusButton() { write(outpipefd[1], "status\n", strlen("status\n")); }
|
|
|
|
-
|
|
|
|
-void QMLHandler::setProgramActive(bool active) { programActive = active; }
|
|
|
|
-
|
|
|
|
-void QMLHandler::writeToCLI(QString command) { write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData())); }
|
|
|
|
-
|
|
|
|
-QString QMLHandler::getIP() { return _IP; }
|
|
|
|
|
|
+void QMLHandler::onFooterGetStatusButton() { CliManager::writeToCli("status"); }
|
|
|
|
|
|
void QMLHandler::setRestart(bool restart) { _RESTART = restart; }
|
|
void QMLHandler::setRestart(bool restart) { _RESTART = restart; }
|