Reduced code duplication in fingerprint logic #4548
This commit is contained in:
parent
80fd3cc01b
commit
a649a0e3df
|
@ -32,12 +32,20 @@ Fingerprint::Fingerprint(const QString& filename)
|
||||||
m_Filename = filename;
|
m_Filename = filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Fingerprint::trust(const QString& fingerprintText)
|
void Fingerprint::trust(const QString& fingerprintText, bool append)
|
||||||
{
|
{
|
||||||
Fingerprint::persistDirectory();
|
Fingerprint::persistDirectory();
|
||||||
|
|
||||||
|
QIODevice::OpenMode openMode;
|
||||||
|
if (append) {
|
||||||
|
openMode = QIODevice::Append;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
openMode = QIODevice::WriteOnly;
|
||||||
|
}
|
||||||
|
|
||||||
QFile file(filePath());
|
QFile file(filePath());
|
||||||
if (file.open(QIODevice::Append))
|
if (file.open(openMode))
|
||||||
{
|
{
|
||||||
QTextStream out(&file);
|
QTextStream out(&file);
|
||||||
out << fingerprintText << "\n";
|
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();
|
QString dirName = Fingerprint::directoryPath();
|
||||||
if (!QDir(dirName).exists()) {
|
if (!QDir(dirName).exists()) {
|
||||||
return false;
|
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());
|
QFile file(filePath());
|
||||||
|
|
||||||
if (file.open(QIODevice::ReadOnly))
|
if (file.open(QIODevice::ReadOnly))
|
||||||
|
@ -59,15 +92,21 @@ bool Fingerprint::exists(const QString& fingerprintText)
|
||||||
QTextStream in(&file);
|
QTextStream in(&file);
|
||||||
while (!in.atEnd())
|
while (!in.atEnd())
|
||||||
{
|
{
|
||||||
QString trusted = in.readLine();
|
list.append(in.readLine());
|
||||||
if (fingerprintText == trusted) {
|
if (list.size() == readTo) {
|
||||||
return true;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString Fingerprint::readFirst()
|
||||||
|
{
|
||||||
|
QStringList list = readList(1);
|
||||||
|
return list.at(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString Fingerprint::filePath() const
|
QString Fingerprint::filePath() const
|
||||||
|
@ -94,26 +133,6 @@ QString Fingerprint::directoryPath()
|
||||||
.arg(kDirName);
|
.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()
|
Fingerprint Fingerprint::local()
|
||||||
{
|
{
|
||||||
return Fingerprint(kLocalFilename);
|
return Fingerprint(kLocalFilename);
|
||||||
|
@ -128,32 +147,3 @@ Fingerprint Fingerprint::trustedClients()
|
||||||
{
|
{
|
||||||
return Fingerprint(kTrustedClientsFilename);
|
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;
|
|
||||||
}
|
|
||||||
|
|
|
@ -25,9 +25,12 @@ private:
|
||||||
Fingerprint(const QString& filename);
|
Fingerprint(const QString& filename);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void trust(const QString& fingerprintText);
|
void trust(const QString& fingerprintText, bool append = true);
|
||||||
bool exists(const QString& fingerprintText);
|
bool isTrusted(const QString& fingerprintText);
|
||||||
|
QStringList readList(const int readTo = -1);
|
||||||
|
QString readFirst();
|
||||||
QString filePath() const;
|
QString filePath() const;
|
||||||
|
bool fileExists() const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static Fingerprint local();
|
static Fingerprint local();
|
||||||
|
|
|
@ -135,11 +135,10 @@ MainWindow::MainWindow(QSettings& settings, AppConfig& appConfig) :
|
||||||
|
|
||||||
m_pLabelPadlock->hide();
|
m_pLabelPadlock->hide();
|
||||||
|
|
||||||
if (Fingerprint::localFingerprintExists()) {
|
if (Fingerprint::local().fileExists()) {
|
||||||
m_pLabelFingerprint->setVisible(true);
|
m_pLabelFingerprint->setVisible(true);
|
||||||
m_pLabelLocalFingerprint->setVisible(true);
|
m_pLabelLocalFingerprint->setVisible(true);
|
||||||
m_pLabelLocalFingerprint->setText(
|
m_pLabelLocalFingerprint->setText(Fingerprint::local().readFirst());
|
||||||
Fingerprint::localFingerprint());
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
m_pLabelFingerprint->setVisible(false);
|
m_pLabelFingerprint->setVisible(false);
|
||||||
|
@ -421,7 +420,7 @@ void MainWindow::checkFingerprint(const QString& line)
|
||||||
}
|
}
|
||||||
|
|
||||||
QString fingerprint = fingerprintRegex.cap(1);
|
QString fingerprint = fingerprintRegex.cap(1);
|
||||||
if (Fingerprint::trustedServers().exists(fingerprint)) {
|
if (Fingerprint::trustedServers().isTrusted(fingerprint)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -239,16 +239,9 @@ void PluginManager::generateCertificate()
|
||||||
if (i != -1) {
|
if (i != -1) {
|
||||||
i++;
|
i++;
|
||||||
QString fingerprint = m_standardOutput.mid(i, m_standardOutput.size() - i);
|
QString fingerprint = m_standardOutput.mid(i, m_standardOutput.size() - i);
|
||||||
|
|
||||||
QFile file(filename);
|
Fingerprint::local().trust(fingerprint, false);
|
||||||
file.resize(0);
|
emit info(tr("SSL fingerprint generated"));
|
||||||
if (file.open(QIODevice::Append))
|
|
||||||
{
|
|
||||||
QTextStream out(&file);
|
|
||||||
out << fingerprint << "\n";
|
|
||||||
file.close();
|
|
||||||
emit info(tr("SSL fingerprint generated"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
emit generateCertificateFinished();
|
emit generateCertificateFinished();
|
||||||
|
|
Loading…
Reference in New Issue