From a649a0e3dfc70d9d0479e8b8e1506ade57ab302a Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Mon, 20 Apr 2015 16:28:26 +0100 Subject: [PATCH] Reduced code duplication in fingerprint logic #4548 --- src/gui/src/Fingerprint.cpp | 102 +++++++++++++++------------------- src/gui/src/Fingerprint.h | 7 ++- src/gui/src/MainWindow.cpp | 7 +-- src/gui/src/PluginManager.cpp | 13 +---- 4 files changed, 57 insertions(+), 72 deletions(-) diff --git a/src/gui/src/Fingerprint.cpp b/src/gui/src/Fingerprint.cpp index 502a4a06..6412dcea 100644 --- a/src/gui/src/Fingerprint.cpp +++ b/src/gui/src/Fingerprint.cpp @@ -32,12 +32,20 @@ Fingerprint::Fingerprint(const QString& filename) m_Filename = filename; } -void Fingerprint::trust(const QString& fingerprintText) +void Fingerprint::trust(const QString& fingerprintText, bool append) { Fingerprint::persistDirectory(); + QIODevice::OpenMode openMode; + if (append) { + openMode = QIODevice::Append; + } + else { + openMode = QIODevice::WriteOnly; + } + QFile file(filePath()); - if (file.open(QIODevice::Append)) + if (file.open(openMode)) { QTextStream out(&file); out << fingerprintText << "\n"; @@ -45,13 +53,38 @@ void Fingerprint::trust(const QString& fingerprintText) } } -bool Fingerprint::exists(const QString& fingerprintText) +bool Fingerprint::fileExists() const { QString dirName = Fingerprint::directoryPath(); if (!QDir(dirName).exists()) { return false; } + QFile file(filePath()); + return file.exists(); +} + +bool Fingerprint::isTrusted(const QString& fingerprintText) +{ + QStringList list = readList(); + foreach (QString trusted, list) + { + if (trusted == fingerprintText) { + return true; + } + } + return false; +} + +QStringList Fingerprint::readList(const int readTo) +{ + QStringList list; + + QString dirName = Fingerprint::directoryPath(); + if (!QDir(dirName).exists()) { + return list; + } + QFile file(filePath()); if (file.open(QIODevice::ReadOnly)) @@ -59,15 +92,21 @@ bool Fingerprint::exists(const QString& fingerprintText) QTextStream in(&file); while (!in.atEnd()) { - QString trusted = in.readLine(); - if (fingerprintText == trusted) { - return true; + list.append(in.readLine()); + if (list.size() == readTo) { + break; } } file.close(); } - return false; + return list; +} + +QString Fingerprint::readFirst() +{ + QStringList list = readList(1); + return list.at(0); } QString Fingerprint::filePath() const @@ -94,26 +133,6 @@ QString Fingerprint::directoryPath() .arg(kDirName); } -bool Fingerprint::localFingerprintExists() -{ - CoreInterface coreInterface; - QString profileDir = coreInterface.getProfileDir(); - - QString dirName = QString("%1/%2") - .arg(profileDir) - .arg(kDirName); - - QString path = QString("%1/%2").arg(dirName).arg(kLocalFilename); - QFile file(path); - - bool exist = false; - if (file.exists()) { - exist = true; - } - - return exist; -} - Fingerprint Fingerprint::local() { return Fingerprint(kLocalFilename); @@ -128,32 +147,3 @@ Fingerprint Fingerprint::trustedClients() { return Fingerprint(kTrustedClientsFilename); } - -QString Fingerprint::localFingerprint() -{ - CoreInterface coreInterface; - QString profileDir = coreInterface.getProfileDir(); - - QString dirName = QString("%1/%2") - .arg(profileDir) - .arg(kDirName); - - QString path = QString("%1/%2").arg(dirName).arg(kLocalFilename); - - QFile file(path); - QString fingerprint; - if (file.open(QIODevice::ReadOnly)) - { - QTextStream in(&file); - while (!in.atEnd()) - { - QString context = in.readLine(); - if (!context.isEmpty()) { - fingerprint = context; - } - } - file.close(); - } - - return fingerprint; -} diff --git a/src/gui/src/Fingerprint.h b/src/gui/src/Fingerprint.h index 0da2a7fc..29ef5f7e 100644 --- a/src/gui/src/Fingerprint.h +++ b/src/gui/src/Fingerprint.h @@ -25,9 +25,12 @@ private: Fingerprint(const QString& filename); public: - void trust(const QString& fingerprintText); - bool exists(const QString& fingerprintText); + void trust(const QString& fingerprintText, bool append = true); + bool isTrusted(const QString& fingerprintText); + QStringList readList(const int readTo = -1); + QString readFirst(); QString filePath() const; + bool fileExists() const; public: static Fingerprint local(); diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp index 9da5b94a..6ca2d549 100644 --- a/src/gui/src/MainWindow.cpp +++ b/src/gui/src/MainWindow.cpp @@ -135,11 +135,10 @@ MainWindow::MainWindow(QSettings& settings, AppConfig& appConfig) : m_pLabelPadlock->hide(); - if (Fingerprint::localFingerprintExists()) { + if (Fingerprint::local().fileExists()) { m_pLabelFingerprint->setVisible(true); m_pLabelLocalFingerprint->setVisible(true); - m_pLabelLocalFingerprint->setText( - Fingerprint::localFingerprint()); + m_pLabelLocalFingerprint->setText(Fingerprint::local().readFirst()); } else { m_pLabelFingerprint->setVisible(false); @@ -421,7 +420,7 @@ void MainWindow::checkFingerprint(const QString& line) } QString fingerprint = fingerprintRegex.cap(1); - if (Fingerprint::trustedServers().exists(fingerprint)) { + if (Fingerprint::trustedServers().isTrusted(fingerprint)) { return; } diff --git a/src/gui/src/PluginManager.cpp b/src/gui/src/PluginManager.cpp index d62432be..3587ecf2 100644 --- a/src/gui/src/PluginManager.cpp +++ b/src/gui/src/PluginManager.cpp @@ -239,16 +239,9 @@ void PluginManager::generateCertificate() if (i != -1) { i++; QString fingerprint = m_standardOutput.mid(i, m_standardOutput.size() - i); - - QFile file(filename); - file.resize(0); - if (file.open(QIODevice::Append)) - { - QTextStream out(&file); - out << fingerprint << "\n"; - file.close(); - emit info(tr("SSL fingerprint generated")); - } + + Fingerprint::local().trust(fingerprint, false); + emit info(tr("SSL fingerprint generated")); } emit generateCertificateFinished();