mirror of https://github.com/debauchee/barrier.git
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue