summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMontel Laurent <montel@kde.org>2016-11-20 07:03:48 (GMT)
committerMontel Laurent <montel@kde.org>2016-11-20 07:03:48 (GMT)
commit3b52378d13d2166e426aa7ae73d82939aa617136 (patch)
treeae8d03b7ff2650a9264d989bf5f688ccc620b460
parent222b5a2388355c0bc2166e92a741e2130a96690a (diff)
Add full search method
-rw-r--r--webengineviewer/src/checkphishingurl/searchfullhashjob.cpp131
-rw-r--r--webengineviewer/src/checkphishingurl/searchfullhashjob.h33
2 files changed, 161 insertions, 3 deletions
diff --git a/webengineviewer/src/checkphishingurl/searchfullhashjob.cpp b/webengineviewer/src/checkphishingurl/searchfullhashjob.cpp
index 41307e3..bfd9522 100644
--- a/webengineviewer/src/checkphishingurl/searchfullhashjob.cpp
+++ b/webengineviewer/src/checkphishingurl/searchfullhashjob.cpp
@@ -18,20 +18,147 @@
*/
#include "searchfullhashjob.h"
+
+#include "checkphishingurlutil.h"
+#include <QNetworkAccessManager>
+#include <QNetworkConfigurationManager>
+#include <PimCommon/NetworkManager>
+#include <QJsonDocument>
+#include <webengineviewer_debug.h>
+
using namespace WebEngineViewer;
SearchFullHashJob::SearchFullHashJob(QObject *parent)
- : QObject(parent)
+ : QObject(parent),
+ mUseCompactJson(true)
{
-
+ mNetworkAccessManager = new QNetworkAccessManager(this);
+ connect(mNetworkAccessManager, &QNetworkAccessManager::finished, this, &SearchFullHashJob::slotCheckUrlFinished);
+ connect(mNetworkAccessManager, &QNetworkAccessManager::sslErrors, this, &SearchFullHashJob::slotSslErrors);
}
SearchFullHashJob::~SearchFullHashJob()
{
+}
+
+void SearchFullHashJob::slotSslErrors(QNetworkReply *reply, const QList<QSslError> &error)
+{
+ qCDebug(WEBENGINEVIEWER_LOG) << " void SearchFullHashJob::slotSslErrors(QNetworkReply *reply, const QList<QSslError> &error)" << error.count();
+ reply->ignoreSslErrors(error);
+}
+
+void SearchFullHashJob::setUseCompactJson(bool useCompactJson)
+{
+ mUseCompactJson = useCompactJson;
+}
+
+void SearchFullHashJob::parse(const QByteArray &replyStr)
+{
+ QJsonDocument document = QJsonDocument::fromJson(replyStr);
+ if (document.isNull()) {
+ Q_EMIT result(WebEngineViewer::SearchFullHashJob::Unknown, mUrl);
+ } else {
+ const QVariantMap answer = document.toVariant().toMap();
+ if (answer.isEmpty()) {
+ Q_EMIT result(WebEngineViewer::SearchFullHashJob::Ok, mUrl);
+ return;
+ } else {
+ const QVariantList info = answer.value(QStringLiteral("matches")).toList();
+ if (info.count() == 1) {
+ const QVariantMap map = info.at(0).toMap();
+ const QString threatTypeStr = map[QStringLiteral("threatType")].toString();
+ if (threatTypeStr == QStringLiteral("MALWARE")) {
+ const QVariantMap urlMap = map[QStringLiteral("threat")].toMap();
+ if (urlMap.count() == 1) {
+ if (urlMap[QStringLiteral("url")].toString() == mUrl.toString()) {
+ Q_EMIT result(WebEngineViewer::SearchFullHashJob::MalWare, mUrl);
+ return;
+ }
+ }
+ } else {
+ qWarning() << " SearchFullHashJob::parse threatTypeStr : " << threatTypeStr;
+ }
+ }
+ Q_EMIT result(WebEngineViewer::SearchFullHashJob::Unknown, mUrl);
+ }
+ }
+}
+
+void SearchFullHashJob::slotCheckUrlFinished(QNetworkReply *reply)
+{
+ parse(reply->readAll());
+ reply->deleteLater();
+ deleteLater();
+}
+
+void SearchFullHashJob::setUrl(const QUrl &url)
+{
+ mUrl = url;
+}
+
+QByteArray SearchFullHashJob::jsonRequest() const
+{
+ QVariantMap clientMap;
+ QVariantMap map;
+
+ clientMap.insert(QStringLiteral("clientId"), QStringLiteral("KDE"));
+ clientMap.insert(QStringLiteral("clientVersion"), QStringLiteral("5.4.0")); //FIXME
+ map.insert(QStringLiteral("client"), clientMap);
+
+ QVariantMap threatMap;
+ const QVariantList platformList = { QStringLiteral("WINDOWS") };
+ threatMap.insert(QStringLiteral("platformTypes"), platformList);
+
+ const QVariantList threatTypesList = { QStringLiteral("MALWARE") };
+ threatMap.insert(QStringLiteral("threatTypes"), threatTypesList);
+ const QVariantList threatEntryTypesList = { QStringLiteral("URL") };
+ threatMap.insert(QStringLiteral("threatEntryTypes"), threatEntryTypesList);
+ QVariantList threatEntriesList;
+ QVariantMap urlMap;
+ urlMap.insert(QStringLiteral("url"), mUrl.toString());
+ threatEntriesList.append(urlMap);
+ threatMap.insert(QStringLiteral("threatEntries"), threatEntriesList);
+ map.insert(QStringLiteral("threatInfo"), threatMap);
+
+ const QJsonDocument postData = QJsonDocument::fromVariant(map);
+ const QByteArray baPostData = postData.toJson(mUseCompactJson ? QJsonDocument::Compact : QJsonDocument::Indented);
+ return baPostData;
}
void SearchFullHashJob::start()
{
+ if (!PimCommon::NetworkManager::self()->networkConfigureManager()->isOnline()) {
+ Q_EMIT result(WebEngineViewer::SearchFullHashJob::BrokenNetwork, mUrl);
+ deleteLater();
+ } else if (canStart()) {
+ QUrl safeUrl = QUrl(QStringLiteral("https://safebrowsing.googleapis.com/v4/threatMatches:find"));
+ safeUrl.addQueryItem(QStringLiteral("key"), WebEngineViewer::CheckPhishingUrlUtil::apiKey());
+ QNetworkRequest request(safeUrl);
+ request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json"));
+
+ const QByteArray baPostData = jsonRequest();
+ qCDebug(WEBENGINEVIEWER_LOG) << " postData.toJson()" << baPostData;
+ Q_EMIT debugJson(baPostData);
+ //curl -H "Content-Type: application/json" -X POST -d '{"client":{"clientId":"KDE","clientVersion":"5.4.0"},"threatInfo":{"platformTypes":["WINDOWS"],"threatEntries":[{"url":"http://www.kde.org"}],"threatEntryTypes":["URL"],"threatTypes":["MALWARE"]}}' https://safebrowsing.googleapis.com/v4/threatMatches:find?key=AIzaSyBS62pXATjabbH2RM_jO2EzDg1mTMHlnyo
+
+ QNetworkReply *reply = mNetworkAccessManager->post(request, baPostData);
+ connect(reply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), this, &SearchFullHashJob::slotError);
+ } else {
+ Q_EMIT result(WebEngineViewer::SearchFullHashJob::InvalidUrl, mUrl);
+ deleteLater();
+ }
+}
+
+void SearchFullHashJob::slotError(QNetworkReply::NetworkError error)
+{
+ QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
+ qCWarning(WEBENGINEVIEWER_LOG) << " error " << error << " error string : " << reply->errorString();
+ reply->deleteLater();
+ deleteLater();
+}
+bool SearchFullHashJob::canStart() const
+{
+ return mUrl.isValid();
}
diff --git a/webengineviewer/src/checkphishingurl/searchfullhashjob.h b/webengineviewer/src/checkphishingurl/searchfullhashjob.h
index 463644c..1f8e48b 100644
--- a/webengineviewer/src/checkphishingurl/searchfullhashjob.h
+++ b/webengineviewer/src/checkphishingurl/searchfullhashjob.h
@@ -21,18 +21,49 @@
#define SEARCHFULLHASHJOB_H
#include <QObject>
+#include <QUrl>
+#include <QNetworkReply>
#include "webengineviewer_export.h"
-
+class QNetworkAccessManager;
namespace WebEngineViewer
{
+/* https://developers.google.com/safe-browsing/v4/lookup-api */
class WEBENGINEVIEWER_EXPORT SearchFullHashJob : public QObject
{
Q_OBJECT
public:
explicit SearchFullHashJob(QObject *parent = Q_NULLPTR);
~SearchFullHashJob();
+
+ enum UrlStatus {
+ Ok = 0,
+ MalWare,
+ BrokenNetwork,
+ InvalidUrl,
+ Unknown
+ };
+
+ void setUrl(const QUrl &url);
+
void start();
+ bool canStart() const;
+
+ QByteArray jsonRequest() const;
+ void parse(const QByteArray &replyStr);
+ void setUseCompactJson(bool useCompactJson);
+
+Q_SIGNALS:
+ void result(WebEngineViewer::SearchFullHashJob::UrlStatus status, const QUrl &url);
+ void debugJson(const QByteArray &ba);
+private Q_SLOTS:
+ void slotSslErrors(QNetworkReply *reply, const QList<QSslError> &error);
+ void slotError(QNetworkReply::NetworkError error);
+ void slotCheckUrlFinished(QNetworkReply *reply);
+private:
+ QUrl mUrl;
+ bool mUseCompactJson;
+ QNetworkAccessManager *mNetworkAccessManager;
};
}