-
Notifications
You must be signed in to change notification settings - Fork 19
/
mainwindow.cpp
158 lines (134 loc) · 5.14 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <QLoggingCategory>
#include <QMenuBar>
#include <QStandardPaths>
#include <QTextStream>
#include <QWebEngineCookieStore>
#include <QWebEngineHistory>
#include <QWebEnginePage>
#include <QWebEngineProfile>
#include <QWebEngineView>
#include <iostream>
Q_LOGGING_CATEGORY(category, "webview")
MainWindow::MainWindow(const bool keepOpen,
const QRegularExpression& urlToWaitForRegex,
const QString certificateHashToTrust,
QWidget *parent) :
QMainWindow(parent),
webEnginePage(new QWebEnginePage()),
webEngineProfile(new QWebEngineProfile("vpn", parent)),
webEngine(new QWebEngineView(webEngineProfile, parent)),
urlToWaitForRegex(urlToWaitForRegex),
certificateHashToTrust(certificateHashToTrust),
keepOpen(keepOpen)
{
setCentralWidget(webEngine);
webEngine->setPage(webEnginePage);
createMenuBar();
QString appDataLocation = QStandardPaths::locate(QStandardPaths::AppDataLocation,
QString(),
QStandardPaths::LocateDirectory);
QWebEngineProfile *webEngineProfile = webEngine->page()->profile();
webEngineProfile->setPersistentCookiesPolicy(QWebEngineProfile::AllowPersistentCookies);
webEngineProfile->setCachePath(appDataLocation);
webEngineProfile->setPersistentStoragePath(appDataLocation);
connect(webEngine, &QWebEngineView::titleChanged, this, &MainWindow::updateTitle);
connect(webEngine, &QWebEngineView::urlChanged, this, &MainWindow::handleUrlChange);
connect(webEngineProfile->cookieStore(), &QWebEngineCookieStore::cookieAdded, this,
&MainWindow::onCookieAdded);
connect(webEngineProfile->cookieStore(), &QWebEngineCookieStore::cookieRemoved, this,
&MainWindow::onCookieRemoved);
connect(webEnginePage, &QWebEnginePage::certificateError, this, &MainWindow::onCertificateError);
}
MainWindow::~MainWindow()
{
delete webEngine;
delete webEngineProfile;
delete webEnginePage;
}
void MainWindow::loadUrl(const QString &url)
{
webEngine->setUrl(url);
}
void MainWindow::onCookieAdded(const QNetworkCookie &cookie)
{
if (cookie.name() == "SVPNCOOKIE") {
svpncookie = QString(cookie.name()) + "=" + QString(cookie.value());
qCDebug(category) << "SVPNCOOKIE has been received";
// This should maybe also check that the cookie is not empty.
if (didSeeUrlToWaitFor) {
std::cout << svpncookie.toStdString() << std::endl;
if (!keepOpen) {
QApplication::exit(0);
}
}
}
}
void MainWindow::onCookieRemoved(const QNetworkCookie &cookie)
{
if (cookie.name() == "SVPNCOOKIE") {
qCDebug(category) << "SVPNCOOKIE has been removed";
svpncookie = QString();
}
}
void MainWindow::onCertificateError(QWebEngineCertificateError certificateError) {
auto sha256base64 = certificateError.certificateChain().constFirst().digest(QCryptographicHash::Sha256).toBase64();
auto hashString = "sha256/" + sha256base64;
if (certificateHashToTrust == hashString) {
certificateError.acceptCertificate();
return;
}
qCDebug(category) << "Found an invalid certificate:";
for (auto& certificate : certificateError.certificateChain()) {
qCDebug(category).noquote() << certificate.toText();
}
qCDebug(category).noquote() << "If you know that this certificate can be trusted, relaunch the application passing the following argument to ignore the error:";
qCDebug(category).noquote() << "--trusted-cert='" + hashString + "'";
}
void MainWindow::handleUrlChange(const QUrl &url)
{
qCDebug(category) << url.toString();
if (didSeeUrlToWaitFor) return;
if (urlToWaitForRegex.match(url.toString()).hasMatch()) {
qCDebug(category) << "The current URL matches the given regex";
didSeeUrlToWaitFor = true;
bool hasCookieSet = !svpncookie.isEmpty();
if (hasCookieSet) {
std::cout << svpncookie.toStdString() << std::endl;
}
if (!keepOpen) {
QApplication::exit(hasCookieSet ? 0 : 1);
}
}
}
void MainWindow::updateTitle(const QString &title)
{
setWindowTitle(title);
}
void MainWindow::createMenuBar()
{
QAction *reload = new QAction(tr("&Reload"), this);
connect(reload, &QAction::triggered, this,
[this]() {
webEngine->reload();
});
QAction *clearData = new QAction(tr("&Clear data"), this);
connect(clearData, &QAction::triggered, this,
[this]() {
auto profile = webEngine->page()->profile();
profile->clearHttpCache();
profile->clearAllVisitedLinks();
profile->cookieStore()->deleteAllCookies();
webEngine->history()->clear();
});
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(reload);
fileMenu->addSeparator();
fileMenu->addAction(clearData);
}
void MainWindow::closeEvent(QCloseEvent *)
{
QApplication::exit(keepOpen ? 0 : 1);
}