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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
|
#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), actId(0)
{
}
/**
* Proxifier class
*/
Proxifier* Proxifier::GetInstance()
{
if (instance == NULL)
{
instance = new Proxifier;
}
return instance;
}
Proxifier::Proxifier(): valid(false)
{
filename = "Default.ppx";
ReadConfig();
}
bool Proxifier::IsValid()
{
return valid;
}
void Proxifier::ReadConfig()
{
valid = false;
QDomDocument configDom("config");
QFile config(filename);
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 (!ReadProxyList(root))
{
Logger::Fatal("Invalid Proxifier configuration file!");
return;
}
// parse chain list
if (!ReadChainList(root))
{
Logger::Fatal("Invalid Proxifier configuration file!");
return;
}
// parse rule list
if (!ReadRuleList(root))
{
Logger::Fatal("Invalid Proxifier configuration file!");
return;
}
config.close();
valid = true;
}
bool Proxifier::ReadProxyList(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
Logger::Trace("Auth section is empty\n");
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 not present\n");
proxyRecord.useAuth = false;
}
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::ReadChainList(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::ReadRuleList(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 actIdTxt = actionElem.text();
ruleRecord.actId = actIdTxt.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;
}
int Proxifier::GetNextId()
{
if (!valid)
{
return -1;
}
int max = 100; // first default proxifier ID
for (unsigned i = 0; i < proxyList.size(); i++)
{
if (proxyList[i].id > max)
{
max = proxyList[i].id;
}
}
for (unsigned i = 0; i < chainList.size(); i++)
{
if (chainList[i].id > max)
{
max = chainList[i].id;
}
}
return max;
}
bool Proxifier::AddProxy(Proxy& proxy, int* id)
{
Logger::Trace("Adding new proxy to Proxifier configuration\n");
if (!valid)
{
Logger::Error("Current Proxifier configuration is not valid. Update first!\n");
return false;
}
QDomDocument configDom("config");
QFile config(filename);
if (!config.open(QIODevice::WriteOnly))
{
Logger::Error("Can't open Proxifier configuration file\n");
return false;
}
if (!configDom.setContent(&config))
{
Logger::Fatal("Invalid Proxifier configuration file!");
return false;
}
QDomElement root = configDom.documentElement();
if (root.tagName() != "ProxifierProfile")
{
Logger::Error("Invalid Proxifier configuration file!");
return false;
}
QDomElement proxyListElem = root.firstChildElement("ProxyList");
if (proxyListElem.isNull())
{
return false;
}
// create proxy record
// root element
QDomElement proxyElem = configDom.createElement("Proxy");
*id = GetNextId();
if (*id == -1)
{
Logger::Error("Can't get valid ID number!\n");
return false;
}
proxyElem.setAttribute("id", *id);
proxyElem.setAttribute("type", QString::fromLocal8Bit(proxy.type.c_str()));
// address element
QDomElement addrElem = configDom.createElement("Address");
QDomText addrTxt = configDom.createTextNode(QString::fromLocal8Bit(proxy.host.c_str()));
addrElem.appendChild(addrTxt);
proxyElem.appendChild(addrElem);
// port element
QDomElement portElem = configDom.createElement("Port");
QString portStr;
QDomText portTxt = configDom.createTextNode(portStr.setNum(proxy.port));
portElem.appendChild(portTxt);
proxyElem.appendChild(portElem);
// option element
QDomElement optsElem = configDom.createElement("Options");
QString optsStr;
QDomText optsTxt = configDom.createTextNode(optsStr.setNum(proxy.option));
optsElem.appendChild(optsTxt);
proxyElem.appendChild(optsElem);
// auth element
if (proxy.useAuth)
{
QDomElement authElem = configDom.createElement("Authentication");
authElem.setAttribute("enabled", "true");
if (!proxy.emptyAuth)
{
// username
QDomElement userElem = configDom.createElement("Username");
QDomText userTxt = configDom.createTextNode(QString::fromLocal8Bit(proxy.login.c_str()));
userElem.appendChild(userTxt);
authElem.appendChild(userElem);
// password
QDomElement passElem = configDom.createElement("Password");
QDomText passTxt = configDom.createTextNode(QString::fromLocal8Bit(proxy.password.c_str()));
passElem.appendChild(passTxt);
authElem.appendChild(passElem);
}
proxyElem.appendChild(authElem);
}
proxyListElem.appendChild(proxyElem);
QTextStream(&config) << configDom.toString(indent);
config.close();
return true;
}
bool Proxifier::AddChain(string& name, int *id)
{
QDomDocument configDom("config");
QFile config(filename);
if (!config.open(QIODevice::WriteOnly))
{
Logger::Error("Can't open Proxifier configuration file\n");
return false;
}
if (!configDom.setContent(&config))
{
Logger::Fatal("Invalid Proxifier configuration file!");
return false;
}
QDomElement root = configDom.documentElement();
if (root.tagName() != "ProxifierProfile")
{
Logger::Error("Invalid Proxifier configuration file!");
return false;
}
QDomElement chainListElem = root.firstChildElement("ChainList");
if (chainListElem.isNull())
{
return false;
}
QDomElement chainElem = configDom.createElement("Chain");
*id = GetNextId();
if (*id == -1)
{
Logger::Error("Can't get valid ID number!\n");
return false;
}
chainElem.setAttribute("id", *id);
QDomElement nameElem = configDom.createElement("Name");
QDomText nameTxt = configDom.createTextNode(QString::fromLocal8Bit(name.c_str()));
nameElem.appendChild(nameTxt);
chainElem.appendChild(nameElem);
chainListElem.appendChild(chainElem);
QTextStream(&config) << configDom.toString(indent);
config.close();
return true;
}
bool Proxifier::AddChainProxy(int proxyId, int chainId)
{
QDomDocument configDom("config");
QFile config(filename);
if (!config.open(QIODevice::WriteOnly))
{
Logger::Error("Can't open Proxifier configuration file\n");
return false;
}
if (!configDom.setContent(&config))
{
Logger::Fatal("Invalid Proxifier configuration file!");
return false;
}
QDomElement root = configDom.documentElement();
if (root.tagName() != "ProxifierProfile")
{
Logger::Error("Invalid Proxifier configuration file!");
return false;
}
QDomElement chainListElem = root.firstChildElement("ChainList");
if (chainListElem.isNull())
{
return false;
}
QDomElement chainElem = chainListElem.firstChildElement("Chain");
while (!chainElem.isNull())
{
bool ok;
if (!chainElem.hasAttribute("id"))
{
Logger::Error("Invalid Chain entry\n");
chainElem = chainElem.nextSiblingElement("Chain");
continue;
}
QString idStr = chainElem.attribute("id");
int id = idStr.toInt(&ok);
if (!ok)
{
Logger::Error("Invalid Chain id\n");
chainElem = chainElem.nextSiblingElement("Chain");
continue;
}
if (id == chainId)
{
// chain found => add proxy
QDomElement proxyElem = configDom.createElement("Proxy");
proxyElem.setAttribute("enabled", "true");
QString idTxt;
idTxt.setNum(proxyId);
QDomText nameTxt = configDom.createTextNode(idTxt);
proxyElem.appendChild(nameTxt);
chainElem.appendChild(proxyElem);
break;
}
}
QTextStream(&config) << configDom.toString(indent);
config.close();
return true;
}
bool Proxifier::RemoveChainProxy(int proxyId, int chainId)
{
QDomDocument configDom("config");
QFile config(filename);
if (!config.open(QIODevice::WriteOnly))
{
Logger::Error("Can't open Proxifier configuration file\n");
return false;
}
if (!configDom.setContent(&config))
{
Logger::Fatal("Invalid Proxifier configuration file!");
return false;
}
QDomElement root = configDom.documentElement();
if (root.tagName() != "ProxifierProfile")
{
Logger::Error("Invalid Proxifier configuration file!");
return false;
}
QDomElement chainListElem = root.firstChildElement("ChainList");
if (chainListElem.isNull())
{
return false;
}
QDomElement chainElem = chainListElem.firstChildElement("Chain");
while (!chainElem.isNull())
{
bool ok;
if (!chainElem.hasAttribute("id"))
{
Logger::Error("Invalid Chain entry\n");
chainElem = chainElem.nextSiblingElement("Chain");
continue;
}
QString idStr = chainElem.attribute("id");
int id = idStr.toInt(&ok);
if (!ok)
{
Logger::Error("Invalid Chain id\n");
chainElem = chainElem.nextSiblingElement("Chain");
continue;
}
if (id == chainId)
{
// chain found => find proxy
QDomElement proxyElem = chainElem.firstChildElement("Proxy");
while (!proxyElem.isNull())
{
QString idTxt = proxyElem.text();
int id = idTxt.toInt();
if (id == proxyId)
{
chainElem.removeChild(proxyElem);
break;
}
}
break;
}
}
QTextStream(&config) << configDom.toString(indent);
config.close();
return true;
}
bool Proxifier::AddRule(Rule& rule)
{
QDomDocument configDom("config");
QFile config(filename);
if (!config.open(QIODevice::WriteOnly))
{
Logger::Error("Can't open Proxifier configuration file\n");
return false;
}
if (!configDom.setContent(&config))
{
Logger::Fatal("Invalid Proxifier configuration file!");
return false;
}
QDomElement root = configDom.documentElement();
if (root.tagName() != "ProxifierProfile")
{
Logger::Error("Invalid Proxifier configuration file!");
return false;
}
QDomElement ruleListElem = root.firstChildElement("RuleList");
if (ruleListElem.isNull())
{
return false;
}
QDomElement ruleElem = configDom.createElement("Rule");
if (rule.isEnabled)
{
ruleElem.setAttribute("enabled", "true");
}
else
{
ruleElem.setAttribute("enabled", "false");
}
// name element
QDomElement nameElem = configDom.createElement("Name");
QDomText nameTxt = configDom.createTextNode(QString::fromLocal8Bit(rule.name.c_str()));
nameElem.appendChild(nameTxt);
ruleElem.appendChild(nameElem);
// applications element
if (! rule.apps.empty())
{
QDomElement appsElem = configDom.createElement("Applications");
QDomText appsTxt = configDom.createTextNode(QString::fromLocal8Bit(rule.apps.c_str()));
appsElem.appendChild(appsTxt);
ruleElem.appendChild(appsElem);
}
// targets element
if (! rule.targets.empty())
{
QDomElement targetsElem = configDom.createElement("Applications");
QDomText targetsTxt = configDom.createTextNode(QString::fromLocal8Bit(rule.targets.c_str()));
targetsElem.appendChild(targetsTxt);
ruleElem.appendChild(targetsElem);
}
// ports element
if (! rule.targets.empty())
{
QDomElement portsElem = configDom.createElement("Ports");
QDomText portsTxt = configDom.createTextNode(QString::fromLocal8Bit(rule.ports.c_str()));
portsElem.appendChild(portsTxt);
ruleElem.appendChild(portsElem);
}
// action element
if (rule.action.empty())
{
Logger::Error("Invalid action value: %s\n", rule.action.c_str());
return false;
}
QDomElement actionElem = configDom.createElement("Action");
QString actionType = QString::fromLocal8Bit(rule.action.c_str());
actionElem.setAttribute("type", actionType);
if ((actionType == "Chain") || (actionType == "Proxy"))
{
QString actionId;
actionId.setNum(rule.actId);
QDomText portsTxt = configDom.createTextNode(actionId);
}
ruleElem.appendChild(actionElem);
QTextStream(&config) << configDom.toString(indent);
config.close();
return true;
}
bool Proxifier::RemoveRule(string name)
{
// !names only compared!
QDomDocument configDom("config");
QFile config(filename);
if (!config.open(QIODevice::WriteOnly))
{
Logger::Error("Can't open Proxifier configuration file\n");
return false;
}
if (!configDom.setContent(&config))
{
Logger::Fatal("Invalid Proxifier configuration file!");
return false;
}
QDomElement root = configDom.documentElement();
if (root.tagName() != "ProxifierProfile")
{
Logger::Error("Invalid Proxifier configuration file!");
return false;
}
QDomElement ruleListElem = root.firstChildElement("ChainList");
if (ruleListElem.isNull())
{
return false;
}
QDomElement ruleElem = ruleListElem.firstChildElement("Rule");
while (!ruleElem.isNull())
{
QDomElement nameElem = ruleElem.firstChildElement("Name");
QString nameStr = nameElem.text();
if (nameStr == QString::fromLocal8Bit(name.c_str()))
{
ruleListElem.removeChild(ruleElem);
break;
}
}
QTextStream(&config) << configDom.toString(indent);
config.close();
return true;
}
|