qmlhandler.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 "config.h"
  15. #include "qmlhandler.h"
  16. #include <boost/asio.hpp>
  17. #include <boost/filesystem.hpp>
  18. #include <boost/lexical_cast.hpp>
  19. #include <iostream>
  20. #include <json/json.h>
  21. using boost::lexical_cast;
  22. using boost::asio::buffer;
  23. using namespace std;
  24. int inpipefd[2];
  25. int outpipefd[2];
  26. char buf[1025];
  27. QUrl sendFileUrl = QUrl("");
  28. QString _IP = "";
  29. bool _CLI_RUNNING = false;
  30. bool _RESTART = false;
  31. pid_t childpid;
  32. bool programActive = true;
  33. QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
  34. void QMLHandler::closeCLI() { _CLI_RUNNING = false; }
  35. void QMLHandler::loadSettingsToGUI() {
  36. int covertMethod = lexical_cast<int>(Config::getValue("Covert-Channel-Method"));
  37. bool saveIP = lexical_cast<bool>(Config::getValue("Autofill-IP"));
  38. bool saveUsername = lexical_cast<bool>(Config::getValue("Autofill-Username"));
  39. QString cliPath = QString::fromStdString(Config::getValue("CLI-Path"));
  40. emit loadSettings(covertMethod, saveIP, saveUsername, cliPath);
  41. }
  42. void QMLHandler::reopenCLI(QString ip) {
  43. if (_CLI_RUNNING) {
  44. waitpid(childpid, NULL, 0);
  45. closeCLI();
  46. }
  47. _IP = ip;
  48. pipe(inpipefd);
  49. pipe(outpipefd);
  50. childpid = fork();
  51. if (childpid == 0) {
  52. // Child
  53. dup2(outpipefd[0], STDIN_FILENO);
  54. dup2(inpipefd[1], STDOUT_FILENO);
  55. // dup2(inpipefd[1], STDERR_FILENO);
  56. // ask kernel to deliver SIGTERM in case the parent dies
  57. prctl(PR_SET_PDEATHSIG, SIGTERM);
  58. // Set the path to the CLI - pass argument h (help) for now
  59. // TODO: Change hardcoded path
  60. // execl("../../cli/build/ccats-cli", "ccats-cli", ip.toUtf8().constData(), "--machine", (char *)NULL);
  61. execl(Config::getValue("CLI-Path").c_str(), "ccats-cli", ip.toUtf8().constData(), "--machine", (char *)NULL);
  62. exit(1);
  63. }
  64. _CLI_RUNNING = true;
  65. close(outpipefd[0]);
  66. close(inpipefd[1]);
  67. std::thread(&QMLHandler::readPipeLoop, this).detach();
  68. }
  69. std::vector<std::string> tokenizeByNewlines(std::string in) {
  70. vector<string> res;
  71. size_t index;
  72. for (index = in.find("\n"); index != std::string::npos; index = in.find("\n")) {
  73. if (index != 0)
  74. res.push_back(in.substr(0, index));
  75. in = in.substr(index + 1);
  76. }
  77. if (in.length() > 0)
  78. res.push_back(in);
  79. return res;
  80. }
  81. void QMLHandler::onExit() { write(outpipefd[1], "disconnect\n", strlen("disconnect\n")); }
  82. // This method gets a string and tries to read it as Json
  83. // If it fails to do so, return and do nothing, else handle the content
  84. void QMLHandler::handleJSON(string buffer) {
  85. Json::Value root;
  86. Json::CharReaderBuilder builder;
  87. Json::CharReader *reader = builder.newCharReader();
  88. string jsonError;
  89. // Try to parse the string as Json and store the result of the pasring in a
  90. // boolean
  91. bool parsingSuccessful = reader->parse(buffer.c_str(), buffer.c_str() + buffer.size(), &root, &jsonError);
  92. // If the string is not correct Json, return
  93. if (!parsingSuccessful) {
  94. return;
  95. }
  96. const Json::Value command = root["command"];
  97. string cmd = command.asString();
  98. qInfo() << QString::fromStdString("Received command " + cmd);
  99. if (!cmd.compare("status")) {
  100. emit footerSetStatus(root["response"].asString().c_str());
  101. }
  102. else if (!cmd.compare("close")) {
  103. programActive = false;
  104. }
  105. else if (!cmd.compare("list")) {
  106. if (root["accept"] == true) {
  107. emit receivingClearFileList();
  108. // Get the array of file Names
  109. auto fileNames = root["names"];
  110. for (int i = 0; i < fileNames.size(); i++) {
  111. emit receivingListFile(QString::fromStdString(fileNames[i].asString().c_str()), boost::filesystem::exists(fileNames[i].asString()));
  112. }
  113. } else {
  114. emit log(root["error"].asString().c_str());
  115. }
  116. }
  117. else if (!cmd.compare("connect")) {
  118. if (root["accept"].asBool()) {
  119. } else {
  120. emit ipPopupSetStatus(root["error"].asString().c_str());
  121. closeCLI();
  122. emit ipPopupEnableConnectButton();
  123. }
  124. }
  125. else if (!cmd.compare("version")) {
  126. if (root["accept"] == true) {
  127. emit ipPopupClose();
  128. emit loginSignupPopupOpen();
  129. } else {
  130. QString errorMessage = QString::fromStdString(
  131. string("Version mismatch: \nClient: " + root["clientversion"].asString() + "\nServer: " + root["serverversion"].asString()));
  132. emit ipPopupSetStatus(errorMessage);
  133. closeCLI();
  134. emit ipPopupEnableConnectButton();
  135. }
  136. }
  137. else if (!cmd.compare("login")) {
  138. if (root["accept"] == true) {
  139. emit loginSignupPopupClose();
  140. write(outpipefd[1], "list\n", strlen("list\n"));
  141. loadSettingsToGUI();
  142. } else {
  143. emit loginSetStatus(root["error"].asString().c_str());
  144. reopenCLI(_IP);
  145. emit loginEnableLoginButton();
  146. }
  147. }
  148. else if (!cmd.compare("signup")) {
  149. if (root["accept"] == true) {
  150. emit loginSignupPopupClose();
  151. } else {
  152. emit signupSetStatus(root["error"].asString().c_str());
  153. reopenCLI(_IP);
  154. emit signupEnableRegisterButton();
  155. }
  156. }
  157. else if (!cmd.compare("put")) {
  158. if (root["accept"] == false) {
  159. QString errorMessage = QString::fromStdString(string("Error when uploading file " + root["file"].asString() + ":\n" + root["error"].asString()));
  160. emit log(errorMessage);
  161. }
  162. }
  163. else if (!cmd.compare("putdata")) {
  164. // TODO: Show speed and handle Error
  165. }
  166. else if (!cmd.compare("get")) {
  167. if (root["accept"] == false) {
  168. QString errorMessage = QString::fromStdString(string("Error when downloading file " + root["file"].asString() + ":\n" + root["error"].asString()));
  169. emit log(errorMessage);
  170. } else {
  171. string fileName = root["file"].asString();
  172. // TODO: Only do this in getdata when remaining is 0 (when the file is fully downloaded) - maybe set text to "downloading.." in between
  173. emit receivingDisableDownloadButton(QString::fromStdString(fileName));
  174. }
  175. }
  176. else if (!cmd.compare("getdata")) {
  177. // TODO: Show speed and handle Error
  178. }
  179. else if (!cmd.compare("deleteme")) {
  180. if (root["accept"] == true) {
  181. _RESTART = true;
  182. emit closeWindow();
  183. } else {
  184. QString errorMessage = QString::fromStdString(root["error"].asString());
  185. emit deleteMePopupSetStatus(errorMessage);
  186. }
  187. }
  188. else if (!cmd.compare("deletefile")) {
  189. emit receivingCloseConfirmDeletePopup();
  190. if (root["accept"] == false) {
  191. QString errorMessage = QString::fromStdString(string("Error when deleting file " + root["file"].asString() + ":\n" + root["error"].asString()));
  192. emit log(errorMessage);
  193. } else {
  194. QString message = QString::fromStdString(string("Deleted file " + root["file"].asString() + " from the server!"));
  195. emit log(message);
  196. }
  197. }
  198. }
  199. // This method is a loop which runs in a seperate thread.
  200. // If will read the Input Pipe, which containts the string that get printed
  201. // on stdout by the CLI/Server, and calls handleJSON with the read content
  202. // one it reads a newline.
  203. void QMLHandler::readPipeLoop() {
  204. unsigned int readOffset = 0;
  205. unsigned int pollCount = 0;
  206. struct pollfd inPipeStatus;
  207. inPipeStatus.fd = inpipefd[0];
  208. inPipeStatus.events = POLLIN;
  209. vector<string> inputs;
  210. string pipeInput;
  211. while (programActive && _CLI_RUNNING) {
  212. inputs = vector<string>();
  213. poll(&inPipeStatus, 1, 100);
  214. if (inPipeStatus.revents & POLLIN) {
  215. readOffset += read(inpipefd[0], buf + readOffset, 1);
  216. pollCount = 0;
  217. } else {
  218. pollCount++;
  219. }
  220. if (pollCount > 4 && (readOffset || pipeInput.size())) {
  221. pipeInput.append(buf);
  222. inputs = tokenizeByNewlines(pipeInput);
  223. for (string s : inputs) {
  224. emit log(QString::fromStdString(s));
  225. qInfo() << QString::fromStdString(s);
  226. handleJSON(s);
  227. }
  228. pipeInput = string();
  229. memset(buf, 0, 1025);
  230. pollCount = 0;
  231. readOffset = 0;
  232. if (waitpid(childpid, NULL, WNOHANG)) {
  233. // nonzero means error or childid has changed state
  234. // for us that means child has exited -> CLI is dead
  235. break;
  236. }
  237. }
  238. if (readOffset >= 1024) {
  239. pipeInput.append(buf);
  240. readOffset = 0;
  241. pollCount = 0;
  242. memset(buf, 0, 1025);
  243. }
  244. }
  245. }
  246. // ### QML Handlers ###
  247. void QMLHandler::onStart() {
  248. bool configExists = Config::loadFile();
  249. if (configExists == true) {
  250. // Config exists
  251. if (Config::checkConfig() == true) {
  252. // Config is valid
  253. if (!boost::filesystem::is_regular_file(Config::getValue("CLI-Path")) ||
  254. boost::filesystem::path(Config::getValue("CLI-Path")).filename().compare("ccats-cli")) {
  255. // Invalid CLI Path
  256. emit invalidCliPathPopupOpen();
  257. }
  258. if (Config::getValue("Autofill-IP") == "1") {
  259. emit ipPopupSetIP(QString::fromStdString(Config::getValue("Default-IP")));
  260. emit ipPopupCheckSaveCheckbox();
  261. }
  262. if (Config::getValue("Autofill-Username") == "1") {
  263. emit loginSetUsername(QString::fromStdString(Config::getValue("Default-Username")));
  264. emit loginSignupCheckSaveCheckbox();
  265. }
  266. } else {
  267. // Config is invalid
  268. emit invalidConfigPopupOpen();
  269. }
  270. } else {
  271. // Config doesn't exist
  272. Config::setupDefaultConfig();
  273. emit noConfigFoundPopupOpen();
  274. }
  275. }
  276. // No Config Found Popup
  277. void QMLHandler::onNoConfigFoundPopupContinueButton(QString cli_path) {
  278. Config::setValue("CLI-Path", cli_path.toUtf8().constData());
  279. Config::saveFile();
  280. }
  281. // Invalid Cli Path Popup
  282. void QMLHandler::onInvalidCliPathPopupContinueButton(QString cli_path) {
  283. Config::setValue("CLI-Path", cli_path.toUtf8().constData());
  284. Config::saveFile();
  285. }
  286. void QMLHandler::onInvalidCliPathPopupQuitButton() { emit closeWindow(); }
  287. // Invalid Config Popup
  288. void QMLHandler::onInvalidConfigPopupQuitButton() { emit closeWindow(); }
  289. void QMLHandler::onInvalidConfigPopupCreateDefaultButton() {
  290. Config::setupDefaultConfig();
  291. emit noConfigFoundPopupOpen();
  292. }
  293. // Sending
  294. void QMLHandler::onSendingSelectFileButton(QUrl url) {
  295. sendFileUrl = url.toLocalFile();
  296. emit log("File Selected: " + sendFileUrl.toString());
  297. emit sendingSetFileUrlText("Selected File: " + sendFileUrl.toString());
  298. emit sendingEnableSendButton();
  299. }
  300. void QMLHandler::onSendingSendFileButton() {
  301. QString command = "put " + sendFileUrl.toString() + "\n";
  302. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  303. }
  304. void QMLHandler::onSendingClearSelectionButton() {
  305. sendFileUrl = QUrl("");
  306. emit log("Cleared Selection");
  307. emit sendingSetFileUrlText("Selected File: None");
  308. emit sendingDisableSendButton();
  309. }
  310. // Receiving
  311. void QMLHandler::onReceivingListFilesButton() { write(outpipefd[1], "list\n", strlen("list\n")); }
  312. void QMLHandler::onReceivingDownloadFileButton(QString fileName) {
  313. QString command = "get " + fileName + "\n";
  314. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  315. }
  316. void QMLHandler::onReceivingConfirmDeleteFileButton(QString fileName) {
  317. QString command = "deletefile " + fileName + "\n";
  318. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  319. }
  320. // Messages
  321. void QMLHandler::onMessagesSendButton(QString msg) { emit message(msg); }
  322. // Settings
  323. void QMLHandler::onSettingsDeleteMeButton(QString password) {
  324. QString command = "deleteme " + password + "\n";
  325. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  326. }
  327. void QMLHandler::onSettingsRevertChangesButton() {
  328. loadSettingsToGUI();
  329. emit log("Settings changes reverted.");
  330. }
  331. void QMLHandler::onSettingsResetButton() {
  332. string cli_path = Config::getValue("CLI-Path");
  333. Config::setupDefaultConfig();
  334. Config::setValue("CLI-Path", cli_path);
  335. loadSettingsToGUI();
  336. emit log("Settings resetted to default.");
  337. }
  338. void QMLHandler::onSettingsSaveButton(int covertMethod, bool saveIP, bool saveUsername, QString cliPath) {
  339. Config::setValue("Covert-Channel-Method", lexical_cast<string>(covertMethod));
  340. Config::setValue("Autofill-IP", lexical_cast<string>(saveIP));
  341. Config::setValue("Autofill-Username", lexical_cast<string>(saveUsername));
  342. Config::setValue("CLI-Path", cliPath.toUtf8().constData());
  343. Config::saveFile();
  344. emit log("Settings saved.");
  345. }
  346. // Ip Popup
  347. void QMLHandler::onIpPopupConnectButton(QString ip, bool saveAsDefault) {
  348. reopenCLI(ip);
  349. emit ipPopupDisableConnectButton();
  350. if (saveAsDefault) {
  351. Config::setValue("Default-IP", ip.toUtf8().constData());
  352. Config::setValue("Autofill-IP", "1");
  353. Config::saveFile();
  354. }
  355. }
  356. // Login
  357. void QMLHandler::onLoginLoginButton(QString username, QString password, bool saveAsDefault) {
  358. QString command = "login " + username + " " + password + "\n";
  359. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  360. emit loginDisableLoginButton();
  361. if (saveAsDefault) {
  362. Config::setValue("Default-Username", username.toUtf8().constData());
  363. Config::setValue("Autofill-Username", "1");
  364. Config::saveFile();
  365. }
  366. }
  367. // Signup
  368. void QMLHandler::onSignupRegisterButton(QString username, QString passwordOne, QString passwordTwo, bool saveAsDefault) {
  369. if (QString::compare(passwordOne, passwordTwo, Qt::CaseSensitive)) {
  370. emit signupSetStatus("Passwords don't match");
  371. return;
  372. }
  373. QString command = "signup " + username + " " + passwordOne + "\n";
  374. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  375. emit signupDisableRegisterButton();
  376. if (saveAsDefault) {
  377. Config::setValue("Default-Username", username.toUtf8().constData());
  378. Config::setValue("Autofill-Username", "1");
  379. Config::saveFile();
  380. }
  381. }
  382. // Footer
  383. void QMLHandler::onFooterGetStatusButton() { write(outpipefd[1], "status\n", strlen("status\n")); }