fort/src/ui/task/tasktasix.cpp

130 lines
2.8 KiB
C++
Raw Normal View History

2017-09-19 04:46:49 +00:00
#include "tasktasix.h"
2017-09-21 13:24:45 +00:00
#include "../conf/addressgroup.h"
#include "../conf/firewallconf.h"
#ifndef TASK_TEST
#include "../fortmanager.h"
#endif
2017-11-07 07:42:42 +00:00
#include "../util/net/ip4range.h"
#include "../util/net/netdownloader.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()
{
m_downloader = new NetDownloader(this);
2017-09-19 04:46:49 +00:00
connect(m_downloader, &NetDownloader::finished,
2017-10-25 09:51:35 +00:00
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
setupDownloader();
downloader()->start();
2017-09-19 04:46:49 +00:00
}
void TaskTasix::setupDownloader() const
2017-09-19 04:46:49 +00:00
{
downloader()->setUrl("http://mrlg.tas-ix.uz/index.cgi");
downloader()->setData("router=cisco&pass1=&query=1&arg=");
2017-09-19 04:46:49 +00:00
}
void TaskTasix::abort(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
m_downloader->disconnect(this); // to avoid recursive call on abort()
m_downloader->abort();
2017-10-25 09:51:35 +00:00
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) {
2017-10-25 09:55:42 +00:00
m_rangeText = parseBuffer(m_downloader->buffer());
2017-10-25 09:51:35 +00:00
success = !m_rangeText.isEmpty();
}
2017-10-03 09:29:54 +00:00
abort(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
return fortManager->saveOriginConf(successMessage());
2017-09-21 13:24:45 +00:00
#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
}
QString TaskTasix::parseBuffer(const QByteArray &buffer) const
{
const QStringList list = parseCustomBuffer(buffer);
if (list.isEmpty())
return QString();
// Merge lines
Ip4Range ip4Range;
if (!ip4Range.fromText(list.join('\n')))
return QString();
return ip4Range.toText();
}
QStringList TaskTasix::parseTasixBuffer(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);
foreach (const QStringRef &line, text.splitRef(
'\n', QString::SkipEmptyParts)) {
2017-09-19 04:46:49 +00:00
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());
}
// Include local networks
if (!list.isEmpty()) {
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
return list;
2017-09-19 04:46:49 +00:00
}