qmlhandler.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include <QDebug>
  2. #include <QGuiApplication>
  3. #include <csignal>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <poll.h>
  8. #include <string>
  9. #include <sys/prctl.h>
  10. #include <sys/stat.h>
  11. #include <sys/wait.h>
  12. #include <thread>
  13. #include <unistd.h>
  14. #include "../include/config.h"
  15. #include "../include/jsonhandler.h"
  16. #include "../include/qmlhandler.h"
  17. #include <boost/asio.hpp>
  18. #include <boost/filesystem.hpp>
  19. #include <boost/lexical_cast.hpp>
  20. #include <iostream>
  21. #include <json/json.h>
  22. using boost::lexical_cast;
  23. using boost::asio::buffer;
  24. using namespace std;
  25. int inpipefd[2];
  26. int outpipefd[2];
  27. char buf[1025];
  28. QUrl sendFileUrl = QUrl("");
  29. QString _IP = "";
  30. bool _CLI_RUNNING = false;
  31. bool _RESTART = false;
  32. pid_t childpid;
  33. bool programActive = true;
  34. QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
  35. void QMLHandler::closeCLI() { _CLI_RUNNING = false; }
  36. void QMLHandler::loadSettingsToGUI() {
  37. int covertMethod = lexical_cast<int>(Config::getValue("Covert-Channel-Method"));
  38. bool saveIP = lexical_cast<bool>(Config::getValue("Autofill-IP"));
  39. bool saveUsername = lexical_cast<bool>(Config::getValue("Autofill-Username"));
  40. QString cliPath = QString::fromStdString(Config::getValue("CLI-Path"));
  41. emit loadSettings(covertMethod, saveIP, saveUsername, cliPath);
  42. }
  43. void QMLHandler::reopenCLI(QString ip) {
  44. if (_CLI_RUNNING) {
  45. waitpid(childpid, NULL, 0);
  46. closeCLI();
  47. }
  48. _IP = ip;
  49. pipe(inpipefd);
  50. pipe(outpipefd);
  51. childpid = fork();
  52. if (childpid == 0) {
  53. // Child
  54. dup2(outpipefd[0], STDIN_FILENO);
  55. dup2(inpipefd[1], STDOUT_FILENO);
  56. // dup2(inpipefd[1], STDERR_FILENO);
  57. // ask kernel to deliver SIGTERM in case the parent dies
  58. prctl(PR_SET_PDEATHSIG, SIGTERM);
  59. // Set the path to the CLI - pass argument h (help) for now
  60. // TODO: Change hardcoded path
  61. // execl("../../cli/build/ccats-cli", "ccats-cli", ip.toUtf8().constData(), "--machine", (char *)NULL);
  62. execl(Config::getValue("CLI-Path").c_str(), "ccats-cli", ip.toUtf8().constData(), "--machine", (char *)NULL);
  63. exit(1);
  64. }
  65. _CLI_RUNNING = true;
  66. close(outpipefd[0]);
  67. close(inpipefd[1]);
  68. std::thread(&QMLHandler::readPipeLoop, this).detach();
  69. }
  70. std::vector<std::string> tokenizeByNewlines(std::string in) {
  71. vector<string> res;
  72. size_t index;
  73. for (index = in.find("\n"); index != std::string::npos; index = in.find("\n")) {
  74. if (index != 0)
  75. res.push_back(in.substr(0, index));
  76. in = in.substr(index + 1);
  77. }
  78. if (in.length() > 0)
  79. res.push_back(in);
  80. return res;
  81. }
  82. void QMLHandler::onExit() { write(outpipefd[1], "disconnect\n", strlen("disconnect\n")); }
  83. // This method is a loop which runs in a seperate thread.
  84. // If will read the Input Pipe, which containts the string that get printed
  85. // on stdout by the CLI/Server, and calls handleJSON with the read content
  86. // one it reads a newline.
  87. void QMLHandler::readPipeLoop() {
  88. unsigned int readOffset = 0;
  89. unsigned int pollCount = 0;
  90. struct pollfd inPipeStatus;
  91. inPipeStatus.fd = inpipefd[0];
  92. inPipeStatus.events = POLLIN;
  93. vector<string> inputs;
  94. string pipeInput;
  95. while (programActive && _CLI_RUNNING) {
  96. inputs = vector<string>();
  97. poll(&inPipeStatus, 1, 100);
  98. if (inPipeStatus.revents & POLLIN) {
  99. readOffset += read(inpipefd[0], buf + readOffset, 1);
  100. pollCount = 0;
  101. } else {
  102. pollCount++;
  103. }
  104. if (pollCount > 4 && (readOffset || pipeInput.size())) {
  105. pipeInput.append(buf);
  106. inputs = tokenizeByNewlines(pipeInput);
  107. for (string s : inputs) {
  108. emit log(QString::fromStdString(s));
  109. qInfo() << QString::fromStdString(s);
  110. // handleJSON(s);
  111. JsonHandler::parseJSON(s);
  112. }
  113. pipeInput = string();
  114. memset(buf, 0, 1025);
  115. pollCount = 0;
  116. readOffset = 0;
  117. if (waitpid(childpid, NULL, WNOHANG)) {
  118. // nonzero means error or childid has changed state
  119. // for us that means child has exited -> CLI is dead
  120. break;
  121. }
  122. }
  123. if (readOffset >= 1024) {
  124. pipeInput.append(buf);
  125. readOffset = 0;
  126. pollCount = 0;
  127. memset(buf, 0, 1025);
  128. }
  129. }
  130. }
  131. // ### QML Handlers ###
  132. void QMLHandler::onStart() {
  133. bool configExists = Config::loadFile();
  134. if (configExists == true) {
  135. // Config exists
  136. if (Config::checkConfig() == true) {
  137. // Config is valid
  138. if (!boost::filesystem::is_regular_file(Config::getValue("CLI-Path")) ||
  139. boost::filesystem::path(Config::getValue("CLI-Path")).filename().compare("ccats-cli")) {
  140. // Invalid CLI Path
  141. emit invalidCliPathPopupOpen();
  142. }
  143. if (Config::getValue("Autofill-IP") == "1") {
  144. emit ipPopupSetIP(QString::fromStdString(Config::getValue("Default-IP")));
  145. emit ipPopupCheckSaveCheckbox();
  146. }
  147. if (Config::getValue("Autofill-Username") == "1") {
  148. emit loginSetUsername(QString::fromStdString(Config::getValue("Default-Username")));
  149. emit loginSignupCheckSaveCheckbox();
  150. }
  151. } else {
  152. // Config is invalid
  153. emit invalidConfigPopupOpen();
  154. }
  155. } else {
  156. // Config doesn't exist
  157. Config::setupDefaultConfig();
  158. emit noConfigFoundPopupOpen();
  159. }
  160. }
  161. // No Config Found Popup
  162. void QMLHandler::onNoConfigFoundPopupContinueButton(QString cli_path) {
  163. Config::setValue("CLI-Path", cli_path.toUtf8().constData());
  164. Config::saveFile();
  165. }
  166. // Invalid Cli Path Popup
  167. void QMLHandler::onInvalidCliPathPopupContinueButton(QString cli_path) {
  168. Config::setValue("CLI-Path", cli_path.toUtf8().constData());
  169. Config::saveFile();
  170. }
  171. void QMLHandler::onInvalidCliPathPopupQuitButton() { emit closeWindow(); }
  172. // Invalid Config Popup
  173. void QMLHandler::onInvalidConfigPopupQuitButton() { emit closeWindow(); }
  174. void QMLHandler::onInvalidConfigPopupCreateDefaultButton() {
  175. Config::setupDefaultConfig();
  176. emit noConfigFoundPopupOpen();
  177. }
  178. // Sending
  179. void QMLHandler::onSendingSelectFileButton(QUrl url) {
  180. sendFileUrl = url.toLocalFile();
  181. emit log("File Selected: " + sendFileUrl.toString());
  182. emit sendingSetFileUrlText("Selected File: " + sendFileUrl.toString());
  183. emit sendingEnableSendButton();
  184. }
  185. void QMLHandler::onSendingSendFileButton() {
  186. QString command = "put " + sendFileUrl.toString() + "\n";
  187. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  188. }
  189. void QMLHandler::onSendingClearSelectionButton() {
  190. sendFileUrl = QUrl("");
  191. emit log("Cleared Selection");
  192. emit sendingSetFileUrlText("Selected File: None");
  193. emit sendingDisableSendButton();
  194. }
  195. // Receiving
  196. void QMLHandler::onReceivingListFilesButton() { write(outpipefd[1], "list\n", strlen("list\n")); }
  197. void QMLHandler::onReceivingDownloadFileButton(QString fileName) {
  198. QString command = "get " + fileName + "\n";
  199. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  200. }
  201. void QMLHandler::onReceivingConfirmDeleteFileButton(QString fileName) {
  202. QString command = "deletefile " + fileName + "\n";
  203. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  204. }
  205. // Messages
  206. void QMLHandler::onMessagesSendButton(QString msg) { emit message(msg); }
  207. // Settings
  208. void QMLHandler::onSettingsDeleteMeButton(QString password) {
  209. QString command = "deleteme " + password + "\n";
  210. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  211. }
  212. void QMLHandler::onSettingsRevertChangesButton() {
  213. loadSettingsToGUI();
  214. emit log("Settings changes reverted.");
  215. }
  216. void QMLHandler::onSettingsResetButton() {
  217. string cli_path = Config::getValue("CLI-Path");
  218. Config::setupDefaultConfig();
  219. Config::setValue("CLI-Path", cli_path);
  220. loadSettingsToGUI();
  221. emit log("Settings resetted to default.");
  222. }
  223. void QMLHandler::onSettingsSaveButton(int covertMethod, bool saveIP, bool saveUsername, QString cliPath) {
  224. Config::setValue("Covert-Channel-Method", lexical_cast<string>(covertMethod));
  225. Config::setValue("Autofill-IP", lexical_cast<string>(saveIP));
  226. Config::setValue("Autofill-Username", lexical_cast<string>(saveUsername));
  227. Config::setValue("CLI-Path", cliPath.toUtf8().constData());
  228. Config::saveFile();
  229. emit log("Settings saved.");
  230. }
  231. // Ip Popup
  232. void QMLHandler::onIpPopupConnectButton(QString ip, bool saveAsDefault) {
  233. reopenCLI(ip);
  234. emit ipPopupDisableConnectButton();
  235. if (saveAsDefault) {
  236. Config::setValue("Default-IP", ip.toUtf8().constData());
  237. Config::setValue("Autofill-IP", "1");
  238. Config::saveFile();
  239. }
  240. }
  241. // Login
  242. void QMLHandler::onLoginLoginButton(QString username, QString password, bool saveAsDefault) {
  243. QString command = "login " + username + " " + password + "\n";
  244. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  245. emit loginDisableLoginButton();
  246. if (saveAsDefault) {
  247. Config::setValue("Default-Username", username.toUtf8().constData());
  248. Config::setValue("Autofill-Username", "1");
  249. Config::saveFile();
  250. }
  251. }
  252. // Signup
  253. void QMLHandler::onSignupRegisterButton(QString username, QString passwordOne, QString passwordTwo, bool saveAsDefault) {
  254. if (QString::compare(passwordOne, passwordTwo, Qt::CaseSensitive)) {
  255. emit signupSetStatus("Passwords don't match");
  256. return;
  257. }
  258. QString command = "signup " + username + " " + passwordOne + "\n";
  259. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  260. emit signupDisableRegisterButton();
  261. if (saveAsDefault) {
  262. Config::setValue("Default-Username", username.toUtf8().constData());
  263. Config::setValue("Autofill-Username", "1");
  264. Config::saveFile();
  265. }
  266. }
  267. // Footer
  268. void QMLHandler::onFooterGetStatusButton() { write(outpipefd[1], "status\n", strlen("status\n")); }
  269. void QMLHandler::setProgramActive(bool active) { programActive = active; }
  270. void QMLHandler::writeToCLI(QString command) { write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData())); }
  271. QString QMLHandler::getIP() { return _IP; }
  272. void QMLHandler::setRestart(bool restart) { _RESTART = restart; }