#include "config.h" #include #include using boost::bad_lexical_cast; using boost::lexical_cast; namespace Config { std::map configuration; bool configValid; std::string configPath = "config.txt"; std::string configKeys[] = {"Autofill-IP", "Default-IP", "Autofill-Username", "Default-Username", "CLI-Path", "Covert-Channel-Method"}; } // namespace Config void Config::setupDefaultConfig() { configuration.clear(); setValue(configKeys[0], "0"); setValue(configKeys[1], "0.0.0.0"); setValue(configKeys[2], "0"); setValue(configKeys[3], "user"); setValue(configKeys[5], "0"); } bool Config::checkConfig() { if (!configValid || configuration.size() != (sizeof(configKeys) / sizeof(*configKeys))) return false; for (int i = 0; i < (sizeof(configKeys) / sizeof(*configKeys)); i++) { if (getValue(configKeys[i]) == "") return false; } std::string autofill_ip = getValue("Autofill-IP"); if (autofill_ip != "0" && autofill_ip != "1") return false; std::string autofill_user = getValue("Autofill-Username"); if (autofill_user != "0" && autofill_user != "1") return false; try { int covert_method = lexical_cast(getValue("Covert-Channel-Method")); } catch (bad_lexical_cast &e) { return false; } return true; } bool Config::loadFile() { std::ifstream ifile(configPath); std::string line; if (ifile.is_open()) { while (getline(ifile, line)) { std::stringstream ss(line); std::string segment; std::vector v; while (getline(ss, segment, '=')) { v.push_back(segment); } if (v.size() != 2) { // One line doesn't have the format *=* configValid = false; } else { configuration.insert(std::pair(v.at(0), v.at(1))); } } configValid = true; return true; } return false; } void Config::saveFile() { std::ofstream file; file.open(configPath); for (auto const &x : configuration) { file << x.first << "=" << x.second << std::endl; } file.close(); } std::string Config::getValue(const std::string &key) { auto it = configuration.find(key); if (it == configuration.end()) { return ""; } else { return it->second; } } void Config::setValue(const std::string &key, const std::string &value) { auto it = configuration.find(key); if (it == configuration.end()) { configuration.insert(std::pair(key, value)); } else { it->second = value; } }