files correcting commit

This commit is contained in:
acetone
2022-11-18 21:32:09 +03:00
parent cc19774a57
commit 419ab1a2b5
2 changed files with 296 additions and 10 deletions
-10
View File
@@ -1,10 +0,0 @@
<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 219.3 493.4" style="enable-background:new 0 0 219.3 493.4;" xml:space="preserve">
<g>
<g>
<path d="M218.4,122.5c-1.5,3.6-4.3,5.4-8.3,5.4h-64v356.3c0,2.7-0.9,4.9-2.6,6.6c-1.7,1.7-3.9,2.6-6.6,2.6H82.2
c-2.7,0-4.9-0.9-6.6-2.6c-1.7-1.7-2.6-3.9-2.6-6.6V127.9h-64c-3.8,0-6.6-1.8-8.3-5.4c-1.5-3.6-1-6.9,1.4-10L103.7,2.9
c1.9-1.9,4.2-2.9,6.9-2.9c2.5,0,4.7,0.9,6.6,2.9L217,112.5C219.5,115.5,220,118.9,218.4,122.5z"/>
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 585 B

+296
View File
@@ -0,0 +1,296 @@
/*
3proxy-eagle: Accumulate ethical 3proxy statistics with web interface.
Source code: https://notabug.org/acetone/3proxy-eagle.
Copyright (C) 2022, acetone
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "webpage.h"
#include "g.h"
#include "statistics.h"
#include <QFile>
#include <QDebug>
#include <cmath>
#include <algorithm>
const QString WebPage::m_lastDestinationItem = "\
<li class=\"lastDestinations__item lastDestinations__item_allowed\" title=\"{{VALUE}}\">\n\
{{VALUE}}\n\
</li>\n";
const QString WebPage::m_dailyTopDestinationItem = "\
<li class=\"topDestinations__item\" title=\"{{VALUE}}\">\n\
{{VALUE}}\n\
</li>\n";
const QString WebPage::m_totalTopDestinationItem = "\
<li class=\"topDestinations__item\" title=\"{{VALUE}}\">\n\
{{VALUE}}\n\
</li>\n";
const QString WebPage::m_blockedDestinationsBlock = "\
<section class=\"proxyService__blockedDestinations blockedDestinations section\">\n\
<div class=\"blockedDestinations__title section__title\">\n\
Blocked destinations\n\
</div>\n\
<ul class=\"blockedDestinations__list accordeo\">\n\
{{VALUE}}\n\
</ul>\n\
</section>\n";
const QString WebPage::m_blockedDestinationItemLvl1 = "\
<li class=\"blockedDestinations__item accordeo__item\">\n\
<label class=\"blockedDestinations__url accordeo__visible\" for=\"{{VALUE}}\">\n\
{{VALUE}}\n\
</label>\n\
<input type=\"radio\" class=\"accordeo__radio\" name=\"blockedDestinations__accordeo\" id=\"{{VALUE}}\">\n\
<ul class=\"blockedDestinations__ipList accordeo__dropdown\">\n\
{{LIST}}\n\
</ul>\n\
</li>\n";
const QString WebPage::m_blockedDestinationItemLvl2 = "\
<li class=\"blockedDestinations__ipItem\">\n\
{{VALUE}}\n\
</li>\n";
const QString WebPage::m_informationBlock = "\
<section class=\"proxyService__information information section\">\n\
<div class=\"information__title section__title\">\n\
Information\n\
</div>\n\
<div class=\"information__text\">\n\
{{VALUE}}\n\
</div>\n\
</section>\n";
QByteArray WebPage::document()
{
QFile src(":/html/index.html");
if (not src.open(QIODevice::ReadOnly))
{
qCritical() << "Can not read built-in index.html";
return QByteArray();
}
QString page = src.readAll();
src.close();
setTitle(page);
customStyles(page);
lastDestinations(page);
trafficBlock(page);
blockedDestinations(page);
informationBlock(page);
copyright(page);
return page.toUtf8();
}
void WebPage::setTitle(QString &document)
{
document.replace(g::c::HA_PAGE_TITLE, g::p::SERVICE_TITLE);
}
void WebPage::customStyles(QString &document)
{
bool exists = QFile::exists(g::p::WORKING_DIR + "/html/styles.css");
document.replace(g::c::HA_CUSTOM_CSS, exists ? "\n <link rel=\"stylesheet\" href=\"/styles.css\">" : "");
}
void WebPage::lastDestinations(QString &document)
{
QStringList dests = Statistics::lastDestinations();
QString destinationsBlock;
for (const auto& dest: dests)
{
destinationsBlock += lastDestinationItem(dest);
}
document.replace(g::c::HA_LAST_DESTINATIONS_LIST, destinationsBlock);
}
void WebPage::trafficBlock(QString &document)
{
auto dailyUpload = bytesToHumanReadableString( Statistics::dailyUpload() );
document.replace(g::c::HA_DAILY_UPLOAD, dailyUpload.first);
document.replace(g::c::HA_DAILY_UPLOAD_MEASURE, dailyUpload.second);
auto dailyDownload = bytesToHumanReadableString( Statistics::dailyDownload() );
document.replace(g::c::HA_DAILY_DOWNLOAD, dailyDownload.first);
document.replace(g::c::HA_DAILY_DOWNLOAD_MEASURE, dailyDownload.second);
auto totalUpload = bytesToHumanReadableString( Statistics::totalUpload() );
document.replace(g::c::HA_TOTAL_UPLOAD, totalUpload.first + " " + totalUpload.second);
auto totalDownload = bytesToHumanReadableString( Statistics::totalDownload() );
document.replace(g::c::HA_TOTAL_DOWNLOAD, totalDownload.first + " " + totalDownload.second);
}
void WebPage::topDestinations(QString &document)
{
// QString dailyTopList;
// for (const auto& dTop: Statistics::dailyTopDestinations())
// {
// QStringList
// }
}
void WebPage::blockedDestinations(QString &document)
{
QMap<QString,QSet<QString>> uniqueRecords;
QString blockedDestinationsBlock {m_blockedDestinationsBlock};
QString blockedDestinationsList;
for (const auto& inst: g::instanses)
{
QMapIterator<QString, QStringList> mIter(inst.second->blackList());
while (mIter.hasNext())
{
auto record = mIter.next();
for (const auto& addr: record.value())
{
uniqueRecords[record.key()].insert(addr);
}
if (not uniqueRecords.contains(record.key()))
{
uniqueRecords.insert(record.key(), QSet<QString>());
}
}
}
QMapIterator<QString,QSet<QString>> mIter(uniqueRecords);
while (mIter.hasNext())
{
auto record = mIter.next();
blockedDestinationsList += blockedDestinationItem(record.key(), record.value().values());
}
blockedDestinationsBlock.replace("{{VALUE}}", blockedDestinationsList);
document.replace(g::c::HA_BLOCKED_DESTINTIONS, blockedDestinationsList.isEmpty() ? "" : "\n"+blockedDestinationsBlock);
}
void WebPage::informationBlock(QString &document)
{
QFile f(g::p::WORKING_DIR + "/information.html");
if (not f.exists())
{
document.replace(g::c::HA_INFORMATION, "");
return;
}
QString infoBlock {m_informationBlock};
QString customText;
if (f.exists())
{
if (not f.open(QIODevice::ReadOnly))
{
qWarning() << "information.html exists, but reading failed";
}
else
{
customText = f.readAll();
f.close();
}
}
infoBlock.replace("{{VALUE}}", customText);
document.replace(g::c::HA_INFORMATION, "\n"+infoBlock);
}
void WebPage::copyright(QString &document)
{
document.replace(g::c::HA_COPYRIGHT, g::c::COPYRIGHT);
}
QString WebPage::lastDestinationItem(const QString &destination)
{
QString result {m_lastDestinationItem};
result.replace("{{VALUE}}", destination);
return result;
}
QString WebPage::dailyTopDestinationItem(const QString &destination)
{
QString result {m_dailyTopDestinationItem};
result.replace("{{VALUE}}", destination);
return result;
}
QString WebPage::totalTopDestinationItem(const QString &destination)
{
QString result {m_totalTopDestinationItem};
result.replace("{{VALUE}}", destination);
return result;
}
QString WebPage::blockedDestinationItem(const QString &destination, const QStringList &addresses)
{
QString result {m_blockedDestinationItemLvl1};
result.replace("{{VALUE}}", destination);
QString addressesItems;
for (const auto& addr: addresses)
{
QString addrItem {m_blockedDestinationItemLvl2};
addrItem.replace("{{VALUE}}", addr);
addressesItems += addrItem;
}
result.replace("{{LIST}}", addressesItems);
return result;
}
QPair<QString, QString> WebPage::bytesToHumanReadableString(quint64 bytes)
{
QPair<QString, QString> result;
constexpr const qint64 thresh = 1024;
if (bytes < thresh)
{
result.first = QString::number(bytes);
result.second = "B";
}
else if (bytes <= thresh*thresh)
{
result.first = QString::number(bytes/thresh);
result.second = "KiB";
}
else if (bytes <= thresh*thresh*thresh)
{
result.first = QString::number(bytes/thresh/thresh);
result.second = "MiB";
}
else if (bytes <= thresh*thresh*thresh*thresh)
{
result.first = QString::number(bytes/thresh/thresh/thresh);
result.second = "GiB";
}
else if (bytes <= thresh*thresh*thresh*thresh*thresh)
{
result.first = QString::number(bytes/thresh/thresh/thresh/thresh);
result.second = "TiB";
}
else if (bytes <= thresh*thresh*thresh*thresh*thresh*thresh)
{
result.first = QString::number(bytes/thresh/thresh/thresh/thresh/thresh);
result.second = "PiB";
}
return result;
}