config.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "config.h"
  2. #include <QDebug>
  3. namespace Config {
  4. std::map<std::string, std::string> configuration;
  5. bool configValid;
  6. std::string configPath = "config.txt";
  7. std::string configKeys[] = {"Autofill-IP", "Default-IP", "Autofill-Username", "Default-Username", "CLI-Path", "Covert-Channel-Method"};
  8. } // namespace Config
  9. void Config::setupDefaultConfig() {
  10. configuration.clear();
  11. setValue(configKeys[0], "0");
  12. setValue(configKeys[1], "0.0.0.0");
  13. setValue(configKeys[2], "0");
  14. setValue(configKeys[3], "user");
  15. setValue(configKeys[5], "0");
  16. }
  17. bool Config::checkConfig() {
  18. if (!configValid || configuration.size() != (sizeof(configKeys) / sizeof(*configKeys)))
  19. return false;
  20. for (int i = 0; i < (sizeof(configKeys) / sizeof(*configKeys)); i++) {
  21. if (getValue(configKeys[i]) == "")
  22. return false;
  23. }
  24. std::string autofill_ip = getValue("Autofill-IP");
  25. if (autofill_ip != "0" && autofill_ip != "1")
  26. return false;
  27. std::string autofill_user = getValue("Autofill-Username");
  28. if (autofill_user != "0" && autofill_user != "1")
  29. return false;
  30. std::istringstream iss(getValue("Covert-Channel-Method"));
  31. int covert_method;
  32. if (!(iss >> covert_method))
  33. return false;
  34. return true;
  35. }
  36. bool Config::loadFile() {
  37. std::ifstream ifile(configPath);
  38. std::string line;
  39. if (ifile.is_open()) {
  40. while (getline(ifile, line)) {
  41. std::stringstream ss(line);
  42. std::string segment;
  43. std::vector<std::string> v;
  44. while (getline(ss, segment, '=')) {
  45. v.push_back(segment);
  46. }
  47. if (v.size() != 2) {
  48. // One line doesn't have the format *=*
  49. configValid = false;
  50. } else {
  51. configuration.insert(std::pair<std::string, std::string>(v.at(0), v.at(1)));
  52. }
  53. }
  54. configValid = true;
  55. return true;
  56. }
  57. return false;
  58. }
  59. void Config::saveFile() {
  60. std::ofstream file;
  61. file.open(configPath);
  62. for (auto const &x : configuration) {
  63. file << x.first << "=" << x.second << std::endl;
  64. }
  65. file.close();
  66. }
  67. std::string Config::getValue(const std::string &key) {
  68. auto it = configuration.find(key);
  69. if (it == configuration.end()) {
  70. return "";
  71. } else {
  72. return it->second;
  73. }
  74. }
  75. void Config::setValue(const std::string &key, const std::string &value) {
  76. auto it = configuration.find(key);
  77. if (it == configuration.end()) {
  78. configuration.insert(std::pair<std::string, std::string>(key, value));
  79. } else {
  80. it->second = value;
  81. }
  82. }