1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
#include <algorithm>
#include <fstream>
#include "client.h"
#include "Config.h"
#include "Proxy.h"
using namespace std;
Config* Config::self = NULL;
Config *Config::CurrentConfig()
{
if (self == NULL)
self = new Config();
return self;
}
Config::Config(): configValid(0)
{
}
void Config::AcquireConfig()
{
configValid = false;
if (ReadGenericProxy())
return ;
if (ReadStaticProxy())
return ;
configValid = true;
}
bool Config::IsConfigValid()
{
return configValid;
}
vector<string> Config::GetCountries()
{
vector<string> countries;
for (unsigned i = 0; i < genericProxy.size(); i++)
{
countries.push_back(genericProxy[i].country);
}
sort(countries.begin(), countries.end());
vector<string>::iterator end = unique(countries.begin(), countries.end());
countries.resize(end - countries.begin());
return countries;
}
vector<string> Config::GetStates(std::string &country)
{
vector<string> 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<string>::iterator end = unique(states.begin(), states.end());
states.resize(end - states.begin());
}
return states;
}
vector<string> Config::GetCities(std::string &country)
{
Logger::Trace("Getting cities in countre: %s", country.c_str());
string defState = "-";
return GetCities(country, defState);
}
vector<string> Config::GetCities(std::string &country, std::string &state)
{
vector<string> 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<string>::iterator end = unique(cities.begin(), cities.end());
cities.resize(end - cities.begin());
return cities;
}
int Config::ReadGenericProxy()
{
Logger::Info("Parsing generic proxy list\n");
ifstream proxyFile("./config/proxy_list.cfg", std::ios::in);
if (!proxyFile)
{
Logger::Error("Can't open file ./config/proxy_list.cfg");
return -1;
}
const int str_size = 512;
char str[str_size] = {0};
while (!proxyFile.eof())
{
proxyFile.getline(str, str_size, ';');
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");
ifstream proxyFile("./config/static_proxy_list.cfg", std::ios::in);
if (!proxyFile)
{
Logger::Error("Can't open file ./config/static_proxy_list.cfg\n");
return -1;
}
const int str_size = 512;
char str[str_size] = {0};
while (!proxyFile.eof())
{
proxyFile.getline(str, str_size, ';');
if (proxyFile.eof())
break;
string entry = str;
ProxyEntryStatic *proxy = new ProxyEntryStatic;
proxy->Parse(entry);
staticProxy.push_back(*proxy);
}
proxyFile.close();
return 0;
}
|