config.cpp 2.4 KB

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