fort/src/ui/task/tasktasix.cpp

145 lines
3.4 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
#include "../util/ip4range.h"
2017-09-19 04:46:49 +00:00
#define TASIX_MRLG_URL "http://mrlg.tas-ix.uz/index.cgi"
#define TASIX_MRLG_DATA "router=cisco&pass1=&query=1&arg="
#define TASIX_DOWNLOAD_TIMEOUT 30000 // 30 seconds timeout
#define TASIX_DOWNLOAD_MAXSIZE (32 * 1024)
2017-09-21 13:24:45 +00:00
TaskTasix::TaskTasix(QObject *parent) :
TaskWorker(parent),
2017-09-19 04:46:49 +00:00
m_networkManager(new QNetworkAccessManager(this)),
m_reply(nullptr)
{
2017-09-21 13:24:45 +00:00
connect(&m_timer, &QTimer::timeout, [this]() { cancel(); });
2017-09-19 04:46:49 +00:00
m_timer.setSingleShot(true);
}
void TaskTasix::run()
{
QNetworkRequest request(QUrl(TASIX_MRLG_URL));
2017-09-21 13:24:45 +00:00
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded");
2017-09-19 04:46:49 +00:00
2017-09-20 15:43:43 +00:00
m_reply = m_networkManager->post(request, TASIX_MRLG_DATA);
2017-09-19 04:46:49 +00:00
connect(m_reply, &QIODevice::readyRead,
this, &TaskTasix::requestReadyRead);
connect(m_reply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
this, &TaskTasix::cancel);
2017-10-01 03:11:38 +00:00
connect(m_reply, &QNetworkReply::finished,
this, &TaskTasix::requestFinished);
2017-09-21 13:24:45 +00:00
m_buffer.clear();
2017-09-19 04:46:49 +00:00
m_timer.start(TASIX_DOWNLOAD_TIMEOUT);
}
2017-09-21 13:24:45 +00:00
void TaskTasix::cancel(bool success)
2017-09-19 04:46:49 +00:00
{
if (!m_reply) return;
2017-10-01 03:11:38 +00:00
m_reply->disconnect(this); // to avoid recursive call on abort()
2017-09-19 04:46:49 +00:00
m_reply->abort();
m_reply->deleteLater();
m_reply = nullptr;
m_timer.stop();
2017-09-19 08:35:43 +00:00
2017-09-21 13:24:45 +00:00
emit finished(success);
2017-09-19 04:46:49 +00:00
}
void TaskTasix::requestReadyRead()
{
const QByteArray data = m_reply->read(
TASIX_DOWNLOAD_MAXSIZE - m_buffer.size());
m_buffer.append(data);
if (m_buffer.size() > TASIX_DOWNLOAD_MAXSIZE) {
cancel();
} else {
m_timer.start();
}
}
2017-10-01 03:11:38 +00:00
void TaskTasix::requestFinished()
{
cancel(true);
}
2017-09-21 13:24:45 +00:00
bool TaskTasix::processResult(FortManager *fortManager)
2017-09-19 04:46:49 +00:00
{
2017-09-19 08:35:43 +00:00
const QString rangeText = parseBufer(m_buffer);
2017-09-21 13:24:45 +00:00
if (rangeText.isEmpty())
return false;
#ifndef TASK_TEST
FirewallConf *conf = fortManager->firewallConf();
AddressGroup *addressGroup = conf->ipExclude();
if (addressGroup->text() == rangeText)
2017-09-21 13:46:34 +00:00
return false;
2017-09-21 13:24:45 +00:00
addressGroup->setText(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
}
QString TaskTasix::parseBufer(const QByteArray &buffer)
{
QStringList list;
// Parse lines
const QString text(buffer);
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();
}