aboutsummaryrefslogtreecommitdiffstats
path: root/core/checkcloudconnection.cpp
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2016-04-04 22:02:03 -0700
committerGravatar Dirk Hohndel <dirk@hohndel.org>2016-04-04 22:33:58 -0700
commit7be962bfc2879a72c32ff67518731347dcdff6de (patch)
treed05bf7ab234a448ee37a15b608e2b939f2285d07 /core/checkcloudconnection.cpp
parent2d760a7bff71c46c5aeba37c40d236ea16eefea2 (diff)
downloadsubsurface-7be962bfc2879a72c32ff67518731347dcdff6de.tar.gz
Move subsurface-core to core and qt-mobile to mobile-widgets
Having subsurface-core as a directory name really messes with autocomplete and is obviously redundant. Simmilarly, qt-mobile caused an autocomplete conflict and also was inconsistent with the desktop-widget name for the directory containing the "other" UI. And while cleaning up the resulting change in the path name for include files, I decided to clean up those even more to make them consistent overall. This could have been handled in more commits, but since this requires a make clean before the build, it seemed more sensible to do it all in one. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'core/checkcloudconnection.cpp')
-rw-r--r--core/checkcloudconnection.cpp106
1 files changed, 106 insertions, 0 deletions
diff --git a/core/checkcloudconnection.cpp b/core/checkcloudconnection.cpp
new file mode 100644
index 000000000..f29d971ba
--- /dev/null
+++ b/core/checkcloudconnection.cpp
@@ -0,0 +1,106 @@
+#include <QObject>
+#include <QTimer>
+#include <QNetworkAccessManager>
+#include <QNetworkReply>
+#include <QEventLoop>
+
+#include "pref.h"
+#include "helpers.h"
+#include "git-access.h"
+
+#include "checkcloudconnection.h"
+
+
+CheckCloudConnection::CheckCloudConnection(QObject *parent) :
+ QObject(parent),
+ reply(0)
+{
+
+}
+
+#define TEAPOT "/make-latte?number-of-shots=3"
+#define HTTP_I_AM_A_TEAPOT 418
+#define MILK "Linus does not like non-fat milk"
+bool CheckCloudConnection::checkServer()
+{
+ if (verbose)
+ fprintf(stderr, "Checking cloud connection...\n");
+
+ QTimer timer;
+ timer.setSingleShot(true);
+ QEventLoop loop;
+ QNetworkRequest request;
+ request.setRawHeader("Accept", "text/plain");
+ request.setRawHeader("User-Agent", getUserAgent().toUtf8());
+ request.setRawHeader("Client-Id", getUUID().toUtf8());
+ request.setUrl(QString(prefs.cloud_base_url) + TEAPOT);
+ QNetworkAccessManager *mgr = new QNetworkAccessManager();
+ reply = mgr->get(request);
+ connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
+ connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
+ connect(reply, &QNetworkReply::sslErrors, this, &CheckCloudConnection::sslErrors);
+ for (int seconds = 1; seconds <= 5; seconds++) {
+ timer.start(1000); // wait five seconds
+ loop.exec();
+ if (timer.isActive()) {
+ // didn't time out, did we get the right response?
+ timer.stop();
+ if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == HTTP_I_AM_A_TEAPOT &&
+ reply->readAll() == QByteArray(MILK)) {
+ reply->deleteLater();
+ mgr->deleteLater();
+ if (verbose > 1)
+ qWarning() << "Cloud storage: successfully checked connection to cloud server";
+ git_storage_update_progress(last_git_storage_update_val + 1, "successfully checked cloud connection");
+ return true;
+ }
+ } else if (seconds < 5) {
+ git_storage_update_progress(last_git_storage_update_val + 1, "waited 1 sec for cloud connection");
+ } else {
+ disconnect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
+ reply->abort();
+ }
+ }
+ git_storage_update_progress(last_git_storage_update_val + 1, "cloud connection failed");
+ if (verbose)
+ qDebug() << "connection test to cloud server failed" <<
+ reply->error() << reply->errorString() <<
+ reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() <<
+ reply->readAll();
+ reply->deleteLater();
+ mgr->deleteLater();
+ if (verbose)
+ qWarning() << "Cloud storage: unable to connect to cloud server";
+ return false;
+}
+
+void CheckCloudConnection::sslErrors(QList<QSslError> errorList)
+{
+ if (verbose) {
+ qDebug() << "Received error response trying to set up https connection with cloud storage backend:";
+ Q_FOREACH (QSslError err, errorList) {
+ qDebug() << err.errorString();
+ }
+ }
+ QSslConfiguration conf = reply->sslConfiguration();
+ QSslCertificate cert = conf.peerCertificate();
+ QByteArray hexDigest = cert.digest().toHex();
+ if (reply->url().toString().contains(prefs.cloud_base_url) &&
+ hexDigest == "13ff44c62996cfa5cd69d6810675490e") {
+ if (verbose)
+ qDebug() << "Overriding SSL check as I recognize the certificate digest" << hexDigest;
+ reply->ignoreSslErrors();
+ } else {
+ if (verbose)
+ qDebug() << "got invalid SSL certificate with hex digest" << hexDigest;
+ }
+}
+
+// helper to be used from C code
+extern "C" bool canReachCloudServer()
+{
+ if (verbose)
+ qWarning() << "Cloud storage: checking connection to cloud server";
+ CheckCloudConnection *checker = new CheckCloudConnection;
+ return checker->checkServer();
+}