123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- #include "config.h"
- #include <QDebug>
- #include <boost/lexical_cast.hpp>
- using boost::bad_lexical_cast;
- using boost::lexical_cast;
- namespace Config {
- std::map<std::string, std::string> 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<int>(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<std::string> 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<std::string, std::string>(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<std::string, std::string>(key, value));
- } else {
- it->second = value;
- }
- }
|