diff options
author | Alex Borisov <borisov.alexandr@rambler.ru> | 2011-12-12 03:57:23 +0200 |
---|---|---|
committer | Alex Borisov <borisov.alexandr@rambler.ru> | 2011-12-12 03:57:23 +0200 |
commit | f77a7f9a1e282dba6e39f6b7f0685971cfac7747 (patch) | |
tree | 4053e95d95c3a34f6b0cdaa11c61b50de9bbf05f /client/Proxifier.cpp | |
parent | 1ecb59ec27137a039a73289a3ded1f8dedcdab88 (diff) |
Proxifier config parsing
Diffstat (limited to 'client/Proxifier.cpp')
-rw-r--r-- | client/Proxifier.cpp | 310 |
1 files changed, 310 insertions, 0 deletions
diff --git a/client/Proxifier.cpp b/client/Proxifier.cpp new file mode 100644 index 0000000..5ef213d --- /dev/null +++ b/client/Proxifier.cpp @@ -0,0 +1,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; +} |