fort/src/ui/task/tasktasix.cpp

119 lines
2.5 KiB
C++
Raw Normal View History

2017-09-19 04:46:49 +00:00
#include "tasktasix.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
2017-09-21 13:24:45 +00:00
#include "../conf/addressgroup.h"
#include "../conf/firewallconf.h"
#ifndef TASK_TEST
#include "../fortmanager.h"
#endif
2017-10-25 09:51:35 +00:00
#include "../util/httpdownloader.h"
2017-09-21 13:24:45 +00:00
#include "../util/ip4range.h"
2017-09-19 04:46:49 +00:00
2017-09-21 13:24:45 +00:00
TaskTasix::TaskTasix(QObject *parent) :
TaskWorker(parent),
2017-10-25 09:51:35 +00:00
m_downloader(nullptr)
2017-09-19 04:46:49 +00:00
{
}
void TaskTasix::run()
{
2017-10-25 09:51:35 +00:00
m_downloader = new HttpDownloader(this);
2017-09-19 04:46:49 +00:00
2017-10-25 09:51:35 +00:00
connect(m_downloader, &HttpDownloader::finished,
this, &TaskTasix::downloadFinished);
2017-09-21 13:24:45 +00:00
2017-10-03 09:29:54 +00:00
m_rangeText = QString();
2017-09-19 04:46:49 +00:00
2017-10-25 09:51:35 +00:00
startDownloader();
2017-09-19 04:46:49 +00:00
}
2017-10-25 09:51:35 +00:00
void TaskTasix::startDownloader() const
2017-09-19 04:46:49 +00:00
{
2017-10-25 09:51:35 +00:00
downloader()->post(QUrl("http://mrlg.tas-ix.uz/index.cgi"),
"router=cisco&pass1=&query=1&arg=");
2017-09-19 04:46:49 +00:00
}
2017-10-25 09:51:35 +00:00
void TaskTasix::cancel(bool success)
2017-09-19 04:46:49 +00:00
{
2017-10-25 09:51:35 +00:00
if (!m_downloader) return;
2017-09-19 04:46:49 +00:00
2017-10-25 09:51:35 +00:00
m_downloader->cancel();
m_downloader->deleteLater();
m_downloader = nullptr;
2017-10-03 09:29:54 +00:00
2017-10-25 09:51:35 +00:00
emit finished(success);
2017-10-03 09:29:54 +00:00
}
2017-10-25 09:51:35 +00:00
void TaskTasix::downloadFinished(bool success)
2017-10-01 03:11:38 +00:00
{
2017-10-25 09:51:35 +00:00
if (success) {
m_rangeText = parseBufer(m_downloader->buffer());
success = !m_rangeText.isEmpty();
}
2017-10-03 09:29:54 +00:00
2017-10-25 09:51:35 +00:00
cancel(success);
2017-10-01 03:11:38 +00:00
}
2017-09-21 13:24:45 +00:00
bool TaskTasix::processResult(FortManager *fortManager)
2017-09-19 04:46:49 +00:00
{
2017-09-21 13:24:45 +00:00
#ifndef TASK_TEST
FirewallConf *conf = fortManager->firewallConf();
2017-10-25 09:51:35 +00:00
AddressGroup *ipExclude = conf->ipExclude();
2017-09-21 13:24:45 +00:00
2017-10-25 09:51:35 +00:00
if (ipExclude->text() == m_rangeText)
2017-09-21 13:46:34 +00:00
return false;
2017-09-21 13:24:45 +00:00
2017-10-25 09:51:35 +00:00
ipExclude->setText(m_rangeText);
2017-09-19 04:46:49 +00:00
2017-09-21 13:24:45 +00:00
return fortManager->saveOriginConf(tr("TAS-IX addresses updated!"));
#else
Q_UNUSED(fortManager)
2017-09-19 04:46:49 +00:00
2017-09-21 13:24:45 +00:00
return true;
#endif
2017-09-19 04:46:49 +00:00
}
2017-10-25 09:51:35 +00:00
QString TaskTasix::parseTasixBufer(const QByteArray &buffer)
2017-09-19 04:46:49 +00:00
{
QStringList list;
// Parse lines
2017-10-03 09:29:54 +00:00
const QString text = QString::fromLatin1(buffer);
2017-09-19 04:46:49 +00:00
foreach (const QStringRef &line, text.splitRef('\n')) {
if (!line.startsWith('*'))
continue;
QStringRef addrStr = line.mid(3, 18);
const int lastSpacePos = addrStr.lastIndexOf(' ');
if (lastSpacePos > 0) {
addrStr = addrStr.left(lastSpacePos);
}
addrStr = addrStr.trimmed();
if (addrStr.isEmpty())
continue;
list.append(addrStr.toString());
}
if (list.isEmpty())
return QString();
// Include local networks
list.append("10.0.0.0/8");
list.append("127.0.0.0/8");
list.append("169.254.0.0/16");
list.append("172.16.0.0/12");
list.append("192.168.0.0/16");
2017-09-19 04:46:49 +00:00
// Merge lines
Ip4Range ip4Range;
if (!ip4Range.fromText(list.join('\n')))
return QString();
return ip4Range.toText();
}