summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMontel Laurent <montel@kde.org>2016-11-17 21:54:20 (GMT)
committerMontel Laurent <montel@kde.org>2016-11-17 21:54:20 (GMT)
commit067206792343932ffdb670561d5e9587c6163f25 (patch)
tree340d80a4583b58598652a3a40b93f22c9f4cc82e
parent84bca161c3cafc26a962c61b0f954ab0816d62d9 (diff)
improve download + create databasev16.11.80
-rw-r--r--webengineviewer/src/checkphishingurl/createphishingurldatabasejob.cpp16
-rw-r--r--webengineviewer/src/checkphishingurl/createphishingurldatabasejob.h21
-rw-r--r--webengineviewer/src/checkphishingurl/localdatabasemanager.cpp78
-rw-r--r--webengineviewer/src/checkphishingurl/localdatabasemanager.h6
4 files changed, 91 insertions, 30 deletions
diff --git a/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.cpp b/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.cpp
index eb76550..1686d82 100644
--- a/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.cpp
+++ b/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.cpp
@@ -365,6 +365,22 @@ bool Removal::operator==(const Removal &other) const
return value;
}
+bool Removal::isValid() const
+{
+ return !indexes.isEmpty();
+}
+
+Addition::Addition()
+ : prefixSize(0)
+{
+
+}
+
+bool Addition::isValid() const
+{
+ return !hashString.isEmpty() && (prefixSize > 0);
+}
+
bool Addition::operator==(const Addition &other) const
{
bool value = (hashString == other.hashString) &&
diff --git a/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.h b/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.h
index 48ba30e..a264e7d 100644
--- a/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.h
+++ b/webengineviewer/src/checkphishingurl/createphishingurldatabasejob.h
@@ -29,15 +29,8 @@ class QNetworkAccessManager;
namespace WebEngineViewer
{
struct Addition {
- Addition()
- : prefixSize(0)
- {
-
- }
- bool isValid() const
- {
- return !hashString.isEmpty() && (prefixSize > 0);
- }
+ Addition();
+ bool isValid() const;
bool operator==(const Addition &other) const;
QByteArray hashString;
@@ -45,16 +38,8 @@ struct Addition {
};
struct Removal {
- Removal()
- {
-
- }
-
bool operator==(const Removal &other) const;
- bool isValid() const
- {
- return !indexes.isEmpty();
- }
+ bool isValid() const;
QList<int> indexes;
};
diff --git a/webengineviewer/src/checkphishingurl/localdatabasemanager.cpp b/webengineviewer/src/checkphishingurl/localdatabasemanager.cpp
index c22fab5..6ee9d9d 100644
--- a/webengineviewer/src/checkphishingurl/localdatabasemanager.cpp
+++ b/webengineviewer/src/checkphishingurl/localdatabasemanager.cpp
@@ -19,6 +19,7 @@
#include "localdatabasemanager.h"
#include "webengineviewer_debug.h"
#include "checkphishingurlfromlocaldatabasejob.h"
+#include "createphishingurldatabasejob.h"
#include <QStandardPaths>
#include <QSqlDatabase>
@@ -35,6 +36,9 @@ namespace {
inline QString tableName() {
return QStringLiteral("malware");
}
+inline QString localDataBasePath() {
+ return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/phishingurl/");
+}
}
LocalDataBaseManager::LocalDataBaseManager(QObject *parent)
@@ -45,22 +49,76 @@ LocalDataBaseManager::LocalDataBaseManager(QObject *parent)
LocalDataBaseManager::~LocalDataBaseManager()
{
+ if (mDataBaseOk) {
+ mDataBase.close();
+ }
+}
+void LocalDataBaseManager::closeDataBaseAndDeleteIt()
+{
+ if (mDataBaseOk) {
+ mDataBase.close();
+ QFile f(localDataBasePath() + QStringLiteral("/malwaredb.sql"));
+ if (!f.remove()) {
+ qCWarning(WEBENGINEVIEWER_LOG) << "impossible to remove local database file";
+ }
+ }
}
void LocalDataBaseManager::start()
{
- mDataBaseOk = initializeDataBase();
- if (initializeDataBase()) {
- if ( !mDataBase.tables().contains( tableName() ) ) {
- createTable();
+ if (!mDataBaseOk) {
+ bool initDatabaseSuccess = initializeDataBase();
+ if (initDatabaseSuccess) {
+ if (!mDataBase.tables().contains(tableName())) {
+ if (createTable()) {
+ //TODO download full database
+ mDataBaseOk = true;
+ WebEngineViewer::CreatePhishingUrlDataBaseJob *job = new WebEngineViewer::CreatePhishingUrlDataBaseJob(this);
+ job->setDataBaseDownloadNeeded(WebEngineViewer::CreatePhishingUrlDataBaseJob::FullDataBase);
+ connect(job, &CreatePhishingUrlDataBaseJob::finished, this, &LocalDataBaseManager::slotDownloadFullDataBaseFinished);
+ job->start();
+ } else {
+ qCWarning(WEBENGINEVIEWER_LOG) << "Impossible to create Table";
+ }
+ } else {
+ mDataBaseOk = true;
+ }
}
+ } else {
+ qCWarning(WEBENGINEVIEWER_LOG) << "Database already initialized.";
}
}
-QString LocalDataBaseManager::localDataBasePath() const
+void LocalDataBaseManager::slotDownloadFullDataBaseFinished(const WebEngineViewer::UpdateDataBaseInfo &infoDataBase,
+ WebEngineViewer::CreatePhishingUrlDataBaseJob::DataBaseDownloadResult status)
{
- return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/phishingurl/");
+ qDebug() << "LocalDataBaseManager::slotDownloadFullDataBaseFinished "<<status;
+ switch(status) {
+ case CreatePhishingUrlDataBaseJob::InvalidData:
+ qCWarning(WEBENGINEVIEWER_LOG) << "Invalid Data.";
+ break;
+ case CreatePhishingUrlDataBaseJob::ValidData:
+ qCWarning(WEBENGINEVIEWER_LOG) << "Valid Data.";
+ break;
+ case CreatePhishingUrlDataBaseJob::UnknownError:
+ qCWarning(WEBENGINEVIEWER_LOG) << "Unknown data.";
+ break;
+ case CreatePhishingUrlDataBaseJob::BrokenNetwork:
+ qCWarning(WEBENGINEVIEWER_LOG) << "Broken Networks.";
+ break;
+ }
+ //qDebug() << "infoDataBase" << infoDataBase.additionList.count();
+ Q_FOREACH(const Addition &add, infoDataBase.additionList) {
+ //qDebug() << " add.size" << add.prefixSize;
+ //qDebug() << " add.hash" << QByteArray::fromBase64(add.hashString).size();
+ const QByteArray uncompressed = QByteArray::fromBase64(add.hashString);
+ for (int i = 0; i < uncompressed.size();) {
+ QByteArray m = uncompressed.mid(i, add.prefixSize);
+ i += add.prefixSize;
+ //qDebug() << "m " << m << " m.size" << m.size();
+ }
+ }
}
LocalDataBaseManager *LocalDataBaseManager::self()
@@ -80,16 +138,14 @@ bool LocalDataBaseManager::initializeDataBase()
bool LocalDataBaseManager::createTable()
{
- QSqlQuery query;
- query.exec(QStringLiteral("create table %1 (id int primary key, "
- "hash varchar(20))").arg(tableName()));
- return true;
+ QSqlQuery query(mDataBase);
+ return query.exec(QStringLiteral("create table %1 (id int primary key, "
+ "hash varchar(32))").arg(tableName()));
}
QSqlError LocalDataBaseManager::initDb()
{
mDataBase = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"));
- qDebug()<<" localDataBasePath()"<<localDataBasePath();
QDir().mkpath(localDataBasePath());
mDataBase.setDatabaseName(localDataBasePath() + QStringLiteral("/malwaredb.sql"));
if (!mDataBase.open()) {
diff --git a/webengineviewer/src/checkphishingurl/localdatabasemanager.h b/webengineviewer/src/checkphishingurl/localdatabasemanager.h
index c5803f8..045254c 100644
--- a/webengineviewer/src/checkphishingurl/localdatabasemanager.h
+++ b/webengineviewer/src/checkphishingurl/localdatabasemanager.h
@@ -24,6 +24,7 @@
#include "webengineviewer_export.h"
#include <QSqlDatabase>
#include <QUrl>
+#include "createphishingurldatabasejob.h"
class QSqlError;
namespace WebEngineViewer
{
@@ -45,13 +46,16 @@ public:
void checkUrl(const QUrl &url);
void start();
+
+ void closeDataBaseAndDeleteIt();
+
Q_SIGNALS:
void checkUrlFinished(const QUrl &url, WebEngineViewer::LocalDataBaseManager::UrlStatus status);
private:
+ void slotDownloadFullDataBaseFinished(const WebEngineViewer::UpdateDataBaseInfo &infoDataBase, WebEngineViewer::CreatePhishingUrlDataBaseJob::DataBaseDownloadResult status);
QSqlError initDb();
bool initializeDataBase();
bool createTable();
- QString localDataBasePath() const;
QSqlDatabase mDataBase;
bool mDataBaseOk;
};