cmdmanager.cpp 12 KB

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