cmdmanager.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include <QDebug>
  2. #include <QGuiApplication>
  3. #include "../include/climanager.h"
  4. #include "../include/cmdmanager.h"
  5. #include "../include/config.h"
  6. #include <json/json.h>
  7. using namespace std;
  8. namespace CmdManager {
  9. QMLHandler *qmlHandler;
  10. map<string, void (*)(Json::Value)> cmdmap;
  11. map<string, struct fileEntry> filemap;
  12. } // namespace CmdManager
  13. void CmdManager::init() {
  14. cmdmap["error"] = &CmdManager::handleError;
  15. cmdmap["status"] = &CmdManager::handleStatus;
  16. cmdmap["close"] = &CmdManager::handleClose;
  17. cmdmap["list"] = &CmdManager::handleList;
  18. cmdmap["extendedlist"] = &CmdManager::handleExtendedList;
  19. cmdmap["connect"] = &CmdManager::handleConnect;
  20. cmdmap["version"] = &CmdManager::handleVersion;
  21. cmdmap["login"] = &CmdManager::handleLogin;
  22. cmdmap["signup"] = &CmdManager::handleSignup;
  23. cmdmap["put"] = &CmdManager::handlePut;
  24. cmdmap["putdata"] = &CmdManager::handlePutData;
  25. cmdmap["get"] = &CmdManager::handleGet;
  26. cmdmap["getdata"] = &CmdManager::handleGetData;
  27. cmdmap["deleteme"] = &CmdManager::handleDeleteMe;
  28. cmdmap["deletefile"] = &CmdManager::handleDeleteFile;
  29. cmdmap["notifications"] = &CmdManager::handleNotifications;
  30. cmdmap["queue"] = &CmdManager::handleQueue;
  31. cmdmap["dequeue"] = &CmdManager::handleDequeue;
  32. cmdmap["extendedstatus"] = &CmdManager::handleExtendedStatus;
  33. filemap.clear();
  34. }
  35. void CmdManager::setQmlHandler(QMLHandler *q) { qmlHandler = q; }
  36. void CmdManager::updateInternalFile(string name, string type, string method, int progress, float speed) {
  37. map<string, struct fileEntry>::iterator it = filemap.find(name);
  38. if (it != filemap.end()) {
  39. it->second.type = type;
  40. it->second.method = method;
  41. it->second.progress = progress;
  42. it->second.speed = speed;
  43. it->second.dirty = true;
  44. }
  45. }
  46. void CmdManager::emitFileList() {
  47. map<string, struct fileEntry>::iterator it;
  48. char speedbuf[256];
  49. string progstr;
  50. for (it = filemap.begin(); it != filemap.end(); it++) {
  51. // This is a transfer
  52. if (it->second.type.size()) {
  53. // using a covert channel
  54. if (it->second.method.size()) {
  55. snprintf(speedbuf, 256, "%.2f B/s", it->second.speed);
  56. progstr = it->second.type + " via " + it->second.method + "\n" + std::to_string(it->second.progress) + "%" + " @ " + speedbuf;
  57. } else
  58. progstr = it->second.type + "\n" + std::to_string(it->second.progress) + "%";
  59. emit qmlHandler->receivingUpdateFile(it->first.c_str(), progstr.c_str(), it->second.type == "Queued" || it->second.type == "Sending");
  60. }
  61. // else emit plain entry
  62. else
  63. emit qmlHandler->receivingUpdateFile(it->first.c_str(), "", false);
  64. }
  65. }
  66. void CmdManager::cleanInternalList() {
  67. map<string, struct fileEntry>::iterator it;
  68. for (it = filemap.begin(); it != filemap.end(); it++) {
  69. if (!it->second.dirty) {
  70. it->second.type.clear();
  71. it->second.method.clear();
  72. it->second.progress = 0;
  73. it->second.speed = 0;
  74. } else
  75. it->second.dirty = false;
  76. }
  77. }
  78. void printInternalList() {
  79. map<string, struct fileEntry>::iterator it;
  80. for (it = CmdManager::filemap.begin(); it != CmdManager::filemap.end(); it++)
  81. emit CmdManager::qmlHandler->log((string("IFL ENTRY ") + it->first + " " + it->second.type + " " + it->second.method + " " +
  82. std::to_string(it->second.progress) + " " + std::to_string(it->second.speed) + " " + std::to_string(it->second.dirty))
  83. .c_str());
  84. }
  85. void CmdManager::executeCmd(string cmd, Json::Value root) {
  86. map<string, void (*)(Json::Value)>::iterator it = cmdmap.find(cmd);
  87. if (it == cmdmap.end()) {
  88. return;
  89. }
  90. cmdmap[cmd](root);
  91. }
  92. void CmdManager::handleError(Json::Value root) { emit qmlHandler->log(root["error"].asString().c_str()); }
  93. void CmdManager::handleStatus(Json::Value root) { emit qmlHandler->footerSetStatus(root["response"].asString().c_str()); }
  94. void CmdManager::handleClose(Json::Value root) { CliManager::setProgramActive(false); }
  95. void CmdManager::handleList(Json::Value root) {
  96. char sizebuf[256];
  97. snprintf(sizebuf, 256, "%.2f", 4.2f);
  98. if (root["accept"] == true) {
  99. emit qmlHandler->receivingClearFileList();
  100. filemap.clear();
  101. // Get the array of file Names
  102. auto fileNames = root["names"];
  103. for (int i = 0; i < fileNames.size(); i++) {
  104. emit qmlHandler->receivingListFile(QString::fromStdString(fileNames[i].asString()), QString::fromStdString(sizebuf), QString("Decryptable"),
  105. !!ifstream(fileNames[i].asString()));
  106. filemap[fileNames[i].asString()] = {false, "", "", 0, 0};
  107. }
  108. } else {
  109. emit qmlHandler->log(root["error"].asString().c_str());
  110. }
  111. }
  112. void CmdManager::handleExtendedList(Json::Value root) {
  113. char sizebuf[256];
  114. if (root["accept"] == true) {
  115. emit qmlHandler->receivingClearFileList();
  116. filemap.clear();
  117. // Get the array of file Names
  118. auto files = root["files"];
  119. for (Json::Value f : files) {
  120. snprintf(sizebuf, 256, "%.2f", f["size"].asFloat());
  121. emit qmlHandler->receivingListFile(QString::fromStdString(f["name"].asString()), QString::fromStdString(sizebuf),
  122. QString::fromStdString(f["encrypted"].asString()), !!ifstream(f["name"].asString()));
  123. filemap[f["name"].asString()] = {false, "", "", 0, 0};
  124. }
  125. } else {
  126. emit qmlHandler->log(root["error"].asString().c_str());
  127. }
  128. }
  129. void CmdManager::handleConnect(Json::Value root) {
  130. if (!root["accept"].asBool()) {
  131. emit qmlHandler->ipPopupSetStatus(root["error"].asString().c_str());
  132. emit qmlHandler->ipPopupEnableConnectButton();
  133. }
  134. }
  135. void CmdManager::handleVersion(Json::Value root) {
  136. if (root["accept"] == true) {
  137. emit qmlHandler->ipPopupClose();
  138. emit qmlHandler->loginSignupPopupOpen();
  139. } else {
  140. QString errorMessage =
  141. QString::fromStdString(string("Version mismatch: \nClient: " + root["clientversion"].asString() + "\nServer: " + root["serverversion"].asString()));
  142. emit qmlHandler->ipPopupSetStatus(errorMessage);
  143. emit qmlHandler->ipPopupEnableConnectButton();
  144. }
  145. }
  146. void CmdManager::handleLogin(Json::Value root) {
  147. if (root["accept"] == true) {
  148. emit qmlHandler->loginSignupPopupClose();
  149. CliManager::writeToCli("extendedlist");
  150. qmlHandler->loadSettingsToGUI();
  151. } else {
  152. emit qmlHandler->loginSetStatus(root["error"].asString().c_str());
  153. emit qmlHandler->loginEnableLoginButton();
  154. }
  155. }
  156. void CmdManager::handleSignup(Json::Value root) {
  157. if (root["accept"] == true) {
  158. emit qmlHandler->loginSignupPopupClose();
  159. } else {
  160. emit qmlHandler->signupSetStatus(root["error"].asString().c_str());
  161. emit qmlHandler->signupEnableRegisterButton();
  162. }
  163. }
  164. void CmdManager::handlePut(Json::Value root) {
  165. if (root["accept"] == false) {
  166. QString errorMessage = QString::fromStdString(string("Error when uploading file " + root["file"].asString() + ":\n" + root["error"].asString()));
  167. emit qmlHandler->log(errorMessage);
  168. }
  169. }
  170. void CmdManager::handlePutData(Json::Value root) {
  171. // TODO: Show speed and handle Error
  172. }
  173. void CmdManager::handleGet(Json::Value root) {
  174. if (root["accept"] == false) {
  175. QString errorMessage = QString::fromStdString(string("Error when downloading file " + root["file"].asString() + ":\n" + root["error"].asString()));
  176. emit qmlHandler->log(errorMessage);
  177. } else {
  178. string fileName = root["file"].asString();
  179. // TODO: Only do this in getdata when remaining is 0 (when the file is fully downloaded) - maybe set text to "downloading.." in between
  180. emit qmlHandler->receivingDisableDownloadButton(QString::fromStdString(fileName));
  181. }
  182. }
  183. void CmdManager::handleGetData(Json::Value root) {
  184. // TODO: Show speed and handle Error
  185. }
  186. void CmdManager::handleDeleteMe(Json::Value root) {
  187. if (root["accept"] == true) {
  188. qmlHandler->setRestart(true);
  189. emit qmlHandler->closeWindow();
  190. } else {
  191. QString errorMessage = QString::fromStdString(root["error"].asString());
  192. emit qmlHandler->deleteMePopupSetStatus(errorMessage);
  193. }
  194. }
  195. void CmdManager::handleDeleteFile(Json::Value root) {
  196. emit qmlHandler->receivingCloseConfirmDeletePopup();
  197. if (root["accept"] == false) {
  198. QString errorMessage = QString::fromStdString(string("Error when deleting file " + root["file"].asString() + ":\n" + root["error"].asString()));
  199. emit qmlHandler->log(errorMessage);
  200. } else {
  201. QString message = QString::fromStdString(string("Deleted file " + root["file"].asString() + " from the server!"));
  202. emit qmlHandler->log(message);
  203. }
  204. }
  205. void CmdManager::handleNotifications(Json::Value root) {
  206. if (root["accept"] == true) {
  207. if (!root["messages"].isNull()) {
  208. // Get the array of notifications
  209. auto notifications = root["messages"];
  210. for (int i = 0; i < notifications.size(); i++) {
  211. emit qmlHandler->notification(QString::fromStdString(notifications[i].asString()));
  212. }
  213. if (notifications.size() > 1)
  214. emit qmlHandler->showDesktopNotification("Covert Channel - New Notifications",
  215. "You have multiple new notifications. Open the program to see them.");
  216. else if (notifications.size() == 1)
  217. emit qmlHandler->showDesktopNotification("Covert Channel - New Notification", QString::fromStdString(notifications[0].asString()));
  218. }
  219. } else {
  220. emit qmlHandler->log(root["error"].asString().c_str());
  221. }
  222. }
  223. void CmdManager::handleQueue(Json::Value root) {
  224. if (root["accept"] == false) {
  225. QString errorMessage = QString::fromStdString(string("Error when queueing file " + root["file"].asString() + ":\n" + root["error"].asString()));
  226. emit qmlHandler->log(errorMessage);
  227. }
  228. }
  229. void CmdManager::handleDequeue(Json::Value root) {
  230. if (root["accept"] == false) {
  231. QString errorMessage = QString::fromStdString(string("Error when dequeueing file " + root["file"].asString() + ":\n" + root["error"].asString()));
  232. emit qmlHandler->log(errorMessage);
  233. }
  234. }
  235. void CmdManager::handleExtendedStatus(Json::Value root) {
  236. Json::Value cs, ss;
  237. string typestring;
  238. if (!root["accept"].asBool()) {
  239. QString errorMessage = QString::fromStdString(string("Error when loading status " + root["error"].asString()));
  240. emit qmlHandler->log(errorMessage);
  241. } else {
  242. cs = root["transfersclientserver"];
  243. ss = root["transfersserverserver"];
  244. if (!cs.isNull()) {
  245. for (Json::Value t : cs) {
  246. updateInternalFile(t["file"].asString(), t["upload"].asBool() ? "Upload" : "Download", "", t["progress"].asInt(), 0);
  247. }
  248. }
  249. if (!ss.isNull()) {
  250. for (Json::Value t : ss) {
  251. if (t["type"] == "download")
  252. typestring = "Receiving";
  253. else if (t["type"] == "upload")
  254. typestring = "Sending";
  255. else if (t["type"] == "queued")
  256. typestring = "Queued";
  257. updateInternalFile(t["file"].asString(), typestring, t["method"].asString(), t["progress"].asInt(), t["speed"].asFloat());
  258. }
  259. }
  260. cleanInternalList();
  261. emitFileList();
  262. }
  263. }