#include #include #include #include #include #include "Config.h" #include "SslClient.h" using namespace std; Config* Config::self = NULL; Config *Config::CurrentConfig() { if (self == NULL) self = new Config(); return self; } Config::Config(): QObject(), StaticProxySpeedLow(50) { configValid = false; Logger::Info("Parsing config.cfg to determine initial configuration\n"); ifstream configFile(QString(this_app->applicationDirPath()+ "/config.cfg").toLocal8Bit().data(), std::ios::in); if (!configFile) { Logger::Fatal("Can't open file: config.cfg\n"); return; } const int str_size = 512; char str[str_size] = {0}; while (!configFile.eof()) { configFile.getline(str, str_size, ';'); configFile.ignore(2, '\n'); if (configFile.eof()) break; string entry = str; size_t eqpos = entry.find('='); if (eqpos == string::npos) { Logger::Warn("No '=' at the config line: %s\n", entry.c_str()); continue; } string key = entry.substr(0, eqpos); string value = entry.substr(eqpos+1); Logger::Trace("Found option: %s = %s\n", key.c_str(), value.c_str()); if (key.compare("config_update_interval") == 0) { updateInterval = atoi(value.c_str()); } else if (key.compare("client_update_interval") == 0) { ClientUpdateInterval = atoi(value.c_str()); } else if (key.compare("server") == 0) { ServerEntry server(value); servers.push_back(server); } else if (key.compare("welcome_msg") == 0) { WelcomeMsg = value; } else if (key.compare("config_downloaded_msg") == 0) { ConfigLoadedMsg = value; } else if (key.compare("top_panel_text") == 0) { TopPanelText = value; } else if (key.compare("bottom_panel_text") == 0) { BottomPanelText = value; } else if (key.compare("speed_visibility") == 0) { IsSpeedVisible = false; if (value.compare("1") == 0) { IsSpeedVisible = true; } } else { Logger::Warn("Unrecognized config option: %s\n", key.c_str()); } } configUpdateTimer = new QTimer; configUpdateTimer->setInterval(updateInterval * 1000); connect(configUpdateTimer, SIGNAL(timeout()), this, SLOT(updateConfig())); client = new SslClient(servers[0].host); connect(client, SIGNAL(ReplyRecieved(SslClient::RequestType&, QByteArray&)), this, SLOT(gotServerReply(SslClient::RequestType&, QByteArray&))); client->SendRequest(SslClient::Config); } void Config::AcquireConfig() { /* reset existing values */ configValid = false; genericProxy.clear(); staticProxy.clear(); /* read new values */ if (ReadGenericProxy()) return ; if (ReadStaticProxy()) return ; configValid = true; } bool Config::IsConfigValid() { return configValid; } vector Config::GetCountries() { vector countries; for (unsigned i = 0; i < genericProxy.size(); i++) { countries.push_back(genericProxy[i].country); } sort(countries.begin(), countries.end()); vector::iterator end = unique(countries.begin(), countries.end()); countries.resize(end - countries.begin()); return countries; } vector Config::GetStates(string &country) { vector states; for (unsigned i = 0; i < genericProxy.size(); i++) { if ((genericProxy[i].state != "-") && (genericProxy[i].country == country)) { states.push_back(genericProxy[i].state); } } if (!states.empty()) { sort(states.begin(), states.end()); vector::iterator end = unique(states.begin(), states.end()); states.resize(end - states.begin()); } return states; } vector Config::GetCities(string &country) { string defState = "-"; return GetCities(country, defState); } vector Config::GetCities(string &country, string &state) { vector cities; for (unsigned i = 0; i < genericProxy.size(); i++) { if ((genericProxy[i].state == state) && (genericProxy[i].country == country)) { cities.push_back(genericProxy[i].city); } } sort(cities.begin(), cities.end()); vector::iterator end = unique(cities.begin(), cities.end()); cities.resize(end - cities.begin()); return cities; } vector Config::GetProxies(string &country, string &city) { string defState = "-"; return GetProxies(country, defState, city); } vector Config::GetProxies(string &country, string &state, string &city) { vector proxies; for (unsigned i = 0; i < genericProxy.size(); i++) { if ((genericProxy[i].state == state) && (genericProxy[i].country == country) && (genericProxy[i].city == city)) { proxies.push_back(genericProxy[i].host); } } sort(proxies.begin(), proxies.end()); return proxies; } vector Config::GetStaticProxyGuiLine(unsigned line) { vector staticProxyLine; for (unsigned i = 0; i < staticProxy.size(); i++) { if (staticProxy[i].position != line) staticProxyLine.push_back(staticProxy[i]); } /* sort manually using bubble sort - not too efficient, * but I suppose we're not going too sort millions of records */ for (unsigned i = 0; i < staticProxyLine.size(); i++) { for (unsigned j = 1; j < staticProxyLine.size(); j++) { if (staticProxyLine[j].speed > staticProxyLine[j-1].speed) swap(staticProxyLine[j], staticProxyLine[j-1]); } } return staticProxyLine; } unsigned Config::GetStaticProxyGuiLines() { unsigned maxLine = 0; for (unsigned i = 0; i < staticProxy.size(); i++) { if (staticProxy[i].position > maxLine) maxLine = staticProxy[i].position; } return maxLine; } int Config::ReadGenericProxy() { Logger::Info("Parsing generic proxy list\n"); string filname = QCoreApplication::applicationDirPath().toStdString() + "/config/proxy_list.cfg"; ifstream proxyFile(filname.c_str(), std::ios::in); if (!proxyFile) { Logger::Error("Can't open file %s\n", filname.c_str()); return -1; } const int str_size = 512; char str[str_size] = {0}; while (!proxyFile.eof()) { proxyFile.getline(str, str_size, ';'); proxyFile.ignore(2, '\n'); if (proxyFile.eof()) break; string entry = str; ProxyEntryGeneric *proxy = new ProxyEntryGeneric; proxy->Parse(entry); genericProxy.push_back(*proxy); } proxyFile.close(); return 0; } int Config::ReadStaticProxy() { Logger::Info("Parsing static proxy list\n"); string filename = QCoreApplication::applicationDirPath().toStdString() + "/config/static_proxy_list.cfg"; ifstream proxyFile(filename.c_str(), std::ios::in); if (!proxyFile) { Logger::Error("Can't open file %s\n", filename.c_str()); return -1; } const int str_size = 512; char str[str_size] = {0}; while (!proxyFile.eof()) { proxyFile.getline(str, str_size, ';'); proxyFile.ignore(2, '\n'); if (proxyFile.eof()) break; string entry = str; ProxyEntryStatic *proxy = new ProxyEntryStatic; proxy->Parse(entry); staticProxy.push_back(*proxy); } proxyFile.close(); return 0; } void Config::updateConfig() { Logger::Trace("Going to update entire configuration\n"); /** * @todo Count retries and switch between server records */ } void Config::gotServerReply(SslClient::RequestType &type, QByteArray &confdata) { Logger::Trace("Got server reply w type: %x\n", type); Logger::Trace("%s\n", confdata.data()); } /*** * Nested class section */ Config::ServerEntry::ServerEntry(): host("127.0.0.1"), timeout(120), retry(60) { } Config::ServerEntry::ServerEntry(string entry) { ServerEntry(); /* processing string: "8.8.8.8 600 60" */ size_t start = 0, end = 0; end = entry.find(' '); host = entry.substr(start, end); start = end+1; end = entry.find(' '); timeout = atoi(entry.substr(start, end).c_str()); start = end+1; end = entry.find(' '); retry = atoi(entry.substr(start, end).c_str()); }