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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
#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), StaticProxySpeedLow(50)
{
}
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<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(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(string &country)
{
string defState = "-";
return GetCities(country, defState);
}
vector<string> Config::GetCities(string &country, 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;
}
vector<string> Config::GetProxies(string &country, string &city)
{
string defState = "-";
return GetProxies(country, defState, city);
}
vector<string> Config::GetProxies(string &country, string &state, string &city)
{
vector<string> 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<ProxyEntryStatic> Config::GetStaticProxyGuiLine(int line)
{
vector<ProxyEntryStatic> 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()
{
int 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");
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, ';');
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");
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, ';');
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;
}
|