cmdmanager.cpp 12 KB

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