cmdmanager.cpp 12 KB

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