File indexing completed on 2025-01-26 04:57:22

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "localdatabasemanager.h"
0007 #include "backoffmodemanager.h"
0008 #include "checkphishingurlutil.h"
0009 #include "createdatabasefilejob.h"
0010 #include "localdatabasemanager_p.h"
0011 #include "urlhashing.h"
0012 #include "webengineviewer_debug.h"
0013 
0014 #include <KSharedConfig>
0015 
0016 #include <QCryptographicHash>
0017 
0018 using namespace WebEngineViewer;
0019 
0020 Q_GLOBAL_STATIC(LocalDataBaseManagerPrivate, s_localDataBaseManager)
0021 
0022 LocalDataBaseManager::LocalDataBaseManager(LocalDataBaseManagerPrivate *impl, QObject *parent)
0023     : QObject(parent)
0024     , d(impl)
0025 {
0026     qRegisterMetaType<WebEngineViewer::UpdateDataBaseInfo>();
0027     qRegisterMetaType<WebEngineViewer::CreatePhishingUrlDataBaseJob::DataBaseDownloadResult>();
0028     qRegisterMetaType<WebEngineViewer::CreatePhishingUrlDataBaseJob::ContraintsCompressionType>();
0029 }
0030 
0031 LocalDataBaseManager::LocalDataBaseManager(QObject *parent)
0032     : LocalDataBaseManager(s_localDataBaseManager, parent)
0033 {
0034 }
0035 
0036 LocalDataBaseManager::~LocalDataBaseManager() = default;
0037 
0038 void LocalDataBaseManager::initialize()
0039 {
0040     d->initialize();
0041 }
0042 
0043 void LocalDataBaseManager::checkUrl(const QUrl &url)
0044 {
0045     if (d->mDataBaseOk) {
0046         // TODO fixme short hash! we don't need to use it.
0047         WebEngineViewer::UrlHashing urlHashing(url);
0048         QHash<QByteArray, QByteArray> hashList = urlHashing.hashList();
0049         QHash<QByteArray, QByteArray> conflictHashs;
0050 
0051         QHashIterator<QByteArray, QByteArray> i(hashList);
0052         while (i.hasNext()) {
0053             i.next();
0054             const QByteArray ba = i.value();
0055             QByteArray result = d->mFile.searchHash(ba);
0056             if (ba.contains(result)) {
0057                 conflictHashs.insert(i.key().toBase64(), ba.toBase64());
0058             }
0059         }
0060         if (conflictHashs.isEmpty()) {
0061             Q_EMIT checkUrlFinished(url, WebEngineViewer::CheckPhishingUrlUtil::Ok);
0062         } else {
0063             // qCWarning(WEBENGINEVIEWER_LOG) << " We need to Check Server Database";
0064             if (d->mNewClientState.isEmpty()) {
0065                 qCWarning(WEBENGINEVIEWER_LOG) << "Database client state is unknown";
0066                 Q_EMIT checkUrlFinished(url, WebEngineViewer::CheckPhishingUrlUtil::Unknown);
0067             } else {
0068                 auto job = new WebEngineViewer::SearchFullHashJob(this);
0069                 job->setDatabaseState(QStringList() << d->mNewClientState);
0070                 job->setSearchHashs(conflictHashs);
0071                 job->setSearchFullHashForUrl(url);
0072                 connect(job, &SearchFullHashJob::result, this, [this](CheckPhishingUrlUtil::UrlStatus status, const QUrl &url) {
0073                     Q_EMIT checkUrlFinished(url, status);
0074                 });
0075                 job->start();
0076             }
0077         }
0078     } else {
0079         qCWarning(WEBENGINEVIEWER_LOG) << "Database not ok";
0080         Q_EMIT checkUrlFinished(url, WebEngineViewer::CheckPhishingUrlUtil::Unknown);
0081     }
0082     if (d->mFile.checkFileChanged()) {
0083         d->mFile.reload();
0084     }
0085 }
0086 
0087 #include "moc_localdatabasemanager.cpp"