blob: 5ef213d010fda8ec9bccb4a0e36be98619420514 (
plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
#include <QtCore>
#include <QtXml>
#include "Logger.h"
#include "Proxifier.h"
Proxifier* Proxifier::instance = NULL;
Proxifier::Proxy::Proxy(): port(0), id(0), option(0), emptyAuth(true)
{
}
Proxifier::Chain::Chain(): id(0)
{
}
Proxifier::Rule::Rule(): isEnabled(false), id(0)
{
}
/**
* Proxifier class
*/
Proxifier* Proxifier::GetInstance()
{
if (instance == NULL)
{
instance = new Proxifier;
}
return instance;
}
Proxifier::Proxifier(): valid(false)
{
Update();
}
bool Proxifier::IsValid()
{
return valid;
}
void Proxifier::Update()
{
valid = false;
configDom = new QDomDocument("config");
QFile config("Default.ppx");
if (!config.open(QIODevice::ReadOnly))
{
Logger::Error("Can't open Proxifier configuration file\n");
return;
}
if (!configDom->setContent(&config))
{
Logger::Fatal("Invalid Proxifier configuration file!");
return;
}
QDomElement root = configDom->documentElement();
if (root.tagName() != "ProxifierProfile")
{
Logger::Fatal("Invalid Proxifier configuration file!");
return;
}
QDomElement options = root.firstChildElement("Options");
/**
* @todo process Options if needed
*/
// parse proxy list
if (!UpdateProxyList(root))
{
Logger::Fatal("Invalid Proxifier configuration file!");
return;
}
// parse chain list
if (!UpdateChainList(root))
{
Logger::Fatal("Invalid Proxifier configuration file!");
return;
}
// parse rule list
if (!UpdateRuleList(root))
{
Logger::Fatal("Invalid Proxifier configuration file!");
return;
}
valid = true;
}
bool Proxifier::UpdateProxyList(QDomElement& root)
{
Logger::Trace("Processing Proxifier proxy list\n");
QDomElement proxyListElem = root.firstChildElement("ProxyList");
if (proxyListElem.isNull())
{
return false;
}
proxyList.clear();
QDomElement proxy = proxyListElem.firstChildElement("Proxy");
while (!proxy.isNull())
{
bool ok = true; // used when converting values to int
Proxy proxyRecord;
// check whether attributes are present
if (! (proxy.hasAttribute("id") && proxy.hasAttribute("type")))
{
Logger::Error("Invalid Proxy entry\n");
proxy = proxy.nextSiblingElement("Proxy");
continue;
}
// proxy id
QString id = proxy.attribute("id");
proxyRecord.id = id.toInt(&ok);
if (!ok)
{
Logger::Error("Invalid Proxy id\n");
proxy = proxy.nextSiblingElement("Proxy");
continue;
}
//proxy type
proxyRecord.type = proxy.attribute("type").toStdString();
// proxy address
QDomElement addrElem = proxy.firstChildElement("Address");
proxyRecord.host = addrElem.text().toStdString();
// proxy port
QDomElement portElem = proxy.firstChildElement("Port");
QString port = portElem.text();
proxyRecord.port = port.toShort(&ok);
if (!ok)
{
Logger::Error("Invalid Proxy port\n");
proxy = proxy.nextSiblingElement("Proxy");
continue;
}
// proxy options
QDomElement optElem = proxy.firstChildElement("Options");
QString opt = optElem.text();
proxyRecord.option = opt.toInt(&ok);
if (!ok)
{
Logger::Error("Invalid Proxy option\n");
continue;
}
// auth section
QDomElement authElem = proxy.firstChildElement("Authentication");
if (!authElem.isNull())
{
if (! authElem.hasChildNodes())
{
// in case if "NTLM + userLogin" = selfclosing auth tag
proxyRecord.emptyAuth = true;
}
else
{
// username
QDomElement userElem = authElem.firstChildElement("Username");
proxyRecord.login = userElem.text().toStdString();
// pass
QDomElement passElem = authElem.firstChildElement("Password");
proxyRecord.password = passElem.text().toStdString();
}
}
else
{
Logger::Trace("Auth section is empty\n");
}
Logger::Trace("Found proxy in proxifier config: id=%d\n", proxyRecord.id);
proxyList.push_back(proxyRecord);
// process next proxy element
proxy = proxy.nextSiblingElement("Proxy");
}
return true;
}
bool Proxifier::UpdateChainList(QDomElement& root)
{
Logger::Trace("Processing Proxifier chain list\n");
QDomElement chainListElem = root.firstChildElement("ChainList");
if (chainListElem.isNull())
{
return false;
}
chainList.clear();
QDomElement chain = chainListElem.firstChildElement("Chain");
while (!chain.isNull())
{
bool ok;
Chain chainRecord;
// chain id
if (!chain.hasAttribute("id"))
{
Logger::Error("Invalid Chain entry\n");
chain = chain.nextSiblingElement("Chain");
continue;
}
QString id = chain.attribute("id");
chainRecord.id = id.toInt(&ok);
if (!ok)
{
Logger::Error("Invalid Chain id\n");
chain = chain.nextSiblingElement("Chain");
continue;
}
// chain name
QDomElement nameElem = chain.firstChildElement("Name");
chainRecord.name = nameElem.text().toStdString();
//chain proxies
QDomElement proxyElem = chain.firstChildElement("Proxy");
while (!proxyElem.isNull())
{
Logger::Trace("Processing Chain proxy\n");
bool isEnabled = false;
QString enAttr = proxyElem.attribute("enabled");
if (enAttr == "true")
{
isEnabled = true;
}
QString idTxt = proxyElem.text();
int id = idTxt.toInt();
chainRecord.proxies[id] = isEnabled;
// proceed to the next chain proxy
proxyElem = proxyElem.nextSiblingElement("Proxy");
}
Logger::Trace("Found chain in proxifier config: id=%d\n", chainRecord.id);
chainList.push_back(chainRecord);
// proceed to the next chain
chain = chain.nextSiblingElement("Chain");
}
return true;
}
bool Proxifier::UpdateRuleList(QDomElement& root)
{
Logger::Trace("Processing Proxifier rule list\n");
QDomElement ruleListElem = root.firstChildElement("RuleList");
if (ruleListElem.isNull())
{
return false;
}
ruleList.clear();
QDomElement rule = ruleListElem.firstChildElement("Rule");
while (!rule.isNull())
{
Rule ruleRecord;
// rule enabled attr
if (!rule.hasAttribute("enabled"))
{
Logger::Error("Invalid Rule entry\n");
rule = rule.nextSiblingElement("Rule");
continue;
}
QString enAttr = rule.attribute("enabled");
if (enAttr == "true")
{
ruleRecord.isEnabled = true;
}
// rule name
QDomElement nameElem = rule.firstChildElement("Name");
ruleRecord.name = nameElem.text().toStdString();
// rule applications
QDomElement appsElem = rule.firstChildElement("Applications");
if (!appsElem.isNull())
{
ruleRecord.apps = appsElem.text().toStdString();
}
// rule targets
QDomElement targetsElem = rule.firstChildElement("Targets");
if (!targetsElem.isNull())
{
ruleRecord.targets = targetsElem.text().toStdString();
}
// rule ports
QDomElement portsElem = rule.firstChildElement("Ports");
if (!portsElem.isNull())
{
ruleRecord.ports = targetsElem.text().toStdString();
}
// rule action
QDomElement actionElem = rule.firstChildElement("Action");
QString actionTxt = actionElem.attribute("type");
if ((actionTxt == "Chain") || (actionTxt == "Proxy"))
{
QString idTxt = actionElem.text();
ruleRecord.id = idTxt.toInt();
}
ruleRecord.action = actionTxt.toStdString();
Logger::Trace("Found rule in proxifier config: name=%s\n", ruleRecord.name.c_str());
ruleList.push_back(ruleRecord);
// proceed to the next rule
rule = rule.nextSiblingElement("Rule");
}
return true;
}
|