summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex <b0ris@b0ris-satellite.localdomain>2011-11-04 03:02:16 +0200
committerAlex <b0ris@b0ris-satellite.localdomain>2011-11-04 03:02:16 +0200
commitb7e0b79e7ce53bf467b1bcb8d60e86fac509f776 (patch)
tree8c239c3b5a0f6833ae75618fd15414682631a019
parentfb6fef711742bf92f4812ea70423f19443049df0 (diff)
config.cfg parser
-rw-r--r--client/Config.cpp98
-rw-r--r--client/Config.h134
-rw-r--r--client/client.h2
-rw-r--r--client/config.cfg18
4 files changed, 191 insertions, 61 deletions
diff --git a/client/Config.cpp b/client/Config.cpp
index b3c66a4..429d615 100644
--- a/client/Config.cpp
+++ b/client/Config.cpp
@@ -1,13 +1,12 @@
#include <algorithm>
#include <fstream>
-#include "client.h"
+#include <sstream>
+#include <stdlib.h>
#include "Config.h"
-#include "Proxy.h"
using namespace std;
-
Config* Config::self = NULL;
Config *Config::CurrentConfig()
@@ -19,6 +18,74 @@ Config *Config::CurrentConfig()
Config::Config(): StaticProxySpeedLow(50), configValid(0)
{
+ Logger::Info("Parsing config.cfg to determine initial configuration\n");
+ ifstream configFile("config.cfg", 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("true"))
+ {
+ IsSpeedVisible = true;
+ }
+ }
+ else
+ {
+ Logger::Warn("Unrecognized config option: %s\n", key.c_str());
+ }
+ }
}
void Config::AcquireConfig()
@@ -138,7 +205,6 @@ vector<ProxyEntryStatic> Config::GetStaticProxyGuiLine(unsigned line)
swap(staticProxyLine[j], staticProxyLine[j-1]);
}
}
-
return staticProxyLine;
}
@@ -207,4 +273,28 @@ int Config::ReadStaticProxy()
}
proxyFile.close();
return 0;
+}
+
+
+/***
+ * 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());
} \ No newline at end of file
diff --git a/client/Config.h b/client/Config.h
index 3701cd7..c95d9c6 100644
--- a/client/Config.h
+++ b/client/Config.h
@@ -3,9 +3,12 @@
#ifndef CONFIG_H
#define CONFIG_H
-#include <vector>
+#include <client.h>
#include "Proxy.h"
+using std::vector;
+using std::string;
+
class ProxyEntryGeneric;
class ProxyEntryStatic;
@@ -22,10 +25,10 @@ public:
/**
* @brief Class to represent 'firewall' record in the config file
*/
- class FirewallRecord
+ class FirewallEntry
{
public:
- FirewallRecord(): block(false), host("127.0.0.1")
+ FirewallEntry(): block(false), host("127.0.0.1")
{
}
/**
@@ -36,33 +39,7 @@ public:
/**
* @brief Hostname or address to allow/block access to
*/
- std::string host;
- };
-
- /**
- * @brief Class to represend 'server' record in the config file
- */
- class ServerRecord
- {
- public:
- ServerRecord(): host("127.0.0.1"), timeout(120), retry(60)
- {
- }
- /**
- * @brief Hostname or address of server<br/>
- * Note that default port is 13666 as specified in SslClient
- */
- std::string host;
- /**
- * @brief Cumulative timeout value (in seconds)<br/>
- * When this time is expired then no further attempts is made
- * to connect to this particular server
- */
- unsigned timeout;
- /**
- * @brief Time (in seconds) between consecutive retries
- */
- unsigned retry;
+ string host;
};
/**
@@ -72,6 +49,37 @@ public:
static Config *CurrentConfig();
/**
+ * @brief Time between consecutive program updates
+ */
+ unsigned ClientUpdateInterval;
+
+ /**
+ * @brief Welcome message that is displayed after program start
+ */
+ string WelcomeMsg;
+
+ /**
+ * @brief Message that is shown when the config.cfg downloaded/updated
+ * successfully
+ */
+ string ConfigLoadedMsg;
+
+ /**
+ * @brief Text that is displayed in as the top panel caption in the UI
+ */
+ string TopPanelText;
+
+ /**
+ * @brief Text that is displayed in as the bottom panel caption in the UI
+ */
+ string BottomPanelText;
+
+ /**
+ * @brief Determines whether proxy speed displayed in the UI or not
+ */
+ bool IsSpeedVisible;
+
+ /**
* @brief Update client configuration
* @todo update config periodically on timer events
*/
@@ -90,7 +98,7 @@ public:
* (generic proxy records only)
* @return Alphabetically sorted vector<string> with unique country names
*/
- std::vector<std::string> GetCountries();
+ vector<string> GetCountries();
/**
* @brief Get list of country states where at least one proxy available<br/>
@@ -99,7 +107,7 @@ public:
* @return Alphabetically sorted vector<string> with unique state names.<br/>
* Resultant vector<string> may be empty in case if there are no states in specified country
*/
- std::vector<std::string> GetStates(std::string &country);
+ vector<string> GetStates(std::string &country);
/**
* @brief Get list of cities in particular country without states<br/>
@@ -107,7 +115,7 @@ public:
* @param country name of country to get list of states for
* @return Alphabetically sorted vector<string> with unique city names
*/
- std::vector<std::string> GetCities(std::string &country);
+ vector<string> GetCities(string &country);
/**
* @brief Get list of cities in particular country and state<br/>
@@ -116,7 +124,7 @@ public:
* @param state name of state to get list of cities for
* @return Alphabetically sorted vector<string> with unique city names
*/
- std::vector<std::string> GetCities(std::string &country, std::string &state);
+ vector<string> GetCities(string &country, string &state);
/**
* @brief Get list of proxy server addresses in particular country and city (without states)<br/>
@@ -125,7 +133,7 @@ public:
* @param city name of city to get list of proxies for
* @return Alphabetically sorted vector<string> with unique proxy names
*/
- std::vector<std::string> GetProxies(std::string &country, std::string &city);
+ vector<string> GetProxies(string &country, string &city);
/**
* @brief Get list of proxy server addresses in particular country, state and city<br/>
@@ -135,14 +143,14 @@ public:
* @param city name of city to get list of proxies for
* @return Alphabetically sorted vector<string> with unique proxy names
*/
- std::vector<std::string> GetProxies(std::string &country, std::string &state, std::string &city);
+ vector<string> GetProxies(string &country, string &state, string &city);
/**
* @brief Get list of static proxy entries
* @param line number of GUI line proxy list associated with
* @return List of static proxy entries sorted by speed in descending order
*/
- std::vector<ProxyEntryStatic> GetStaticProxyGuiLine(unsigned line);
+ vector<ProxyEntryStatic> GetStaticProxyGuiLine(unsigned line);
/**
* @brief Get number of static proxy GUI lines
@@ -159,12 +167,56 @@ public:
protected:
Config();
private:
+ /**
+ * @brief Class to represend 'server' record in the config file
+ */
+ class ServerEntry
+ {
+ public:
+ /**
+ * @brief Initialize ServerEntry instance to default values
+ */
+ ServerEntry();
+
+ /**
+ * @brief initilize ServerEntry instance from config file entry<br/>
+ * @param entry config file entry value, e.g.: '8.8.8.8:8080 600 60'
+ */
+ ServerEntry(string entry);
+
+ /**
+ * @brief Hostname or address of server<br/>
+ * Note that default port is 13666 as specified in SslClient
+ */
+ string host;
+
+ /**
+ * @brief Cumulative timeout value (in seconds)<br/>
+ * When this time is expired then no further attempts is made
+ * to connect to this particular server
+ */
+ unsigned timeout;
+
+ /**
+ * @brief Time (in seconds) between consecutive retries
+ */
+ unsigned retry;
+ };
+
+ /**
+ * @brief Pointer to the singleton Config instance
+ */
static Config *self;
+
+ /**
+ * @brief time interval between consequtive config updates
+ */
+ unsigned updateInterval;
bool configValid;
- std::vector<ProxyEntryGeneric> genericProxy;
- std::vector<ProxyEntryStatic> staticProxy;
- std::vector<FirewallRecord> firewallList;
-
+ vector<ProxyEntryGeneric> genericProxy;
+ vector<ProxyEntryStatic> staticProxy;
+ vector<FirewallEntry> firewalls;
+ vector<ServerEntry> servers;
int ReadGenericProxy();
int ReadStaticProxy();
};
diff --git a/client/client.h b/client/client.h
index 5e01be9..6306434 100644
--- a/client/client.h
+++ b/client/client.h
@@ -4,8 +4,6 @@
#ifdef WIN32
#include <windows.h>
-#else
-#include <unistd.h>
#endif
#include <string>
diff --git a/client/config.cfg b/client/config.cfg
index 8c88639..c12c136 100644
--- a/client/config.cfg
+++ b/client/config.cfg
@@ -1,20 +1,10 @@
config_update_interval=600;
client_update_interval=60000;
-
-#0 - invisible, 1 - visible
-speed_visibility=0;
-
-#server option format (all values required)
-#keyword ip:port timeout retry_interval
-server=8.8.8.8:8080 600 60;
-server=8.8.4.4:3124 100 50;
-
+server=8.8.8.8 600 60;
+server=8.8.4.4 100 50;
welcome_msg="Hello, world!";
+config_downloaded_msg="Конфиг успешно загружен!";
top_panel_text="Верхняя панель";
bottom_panel_text="Нижняя панель";
+speed_visibility=0;
-#firewall record format (all values required)
-#keyword action address_pattern
-firewall block *.adobeereg.com
-firewall block ereg.adobe.com
-firewall block practivate.adobe.com