subsurface/core/settings/qPrefCloudStorage.cpp
Dirk Hohndel 7fa031b648 cloudstorage: try to pick between multiple cloud servers
The backend infrastructure will soon be able to support more than one
cloud server which automagically stay in sync with each other.

One critical requirement for that to work is that once a session was
started with one of the servers, the complete session happens with that
server - we must not switch from server to server while doing a git
transaction. To make sure that's the case, we aren't trying to use DNS
tricks to make this load balancing scheme work, but instead try to
determine at program start which server is the best one to use.

Right now this is super simplistic. Two servers, one in the US, one in
Europe. By default we use the European server (most of our users appear
to be in Europe), but if we can figure out that the client is actually
in the Americas, use the US server. We might improve that heuristic over
time, but as a first attempt it seems not entirely bogus.

The way this is implemented is a simple combination of two free
webservices that together appear to give us a very reliable estimate
which continent the user is located on.

api.ipify.org gives us our external IP address
ip-api.com gives us the continent that IP address is on

If any of this fails or takes too long to respond, we simply ignore it
since either server will work. One oddity is that if we decide to change
servers we only change the settings that are stored on disk, not the
runtime preferences. This goes back to the comment above that we have to
avoid changing servers in mid sync.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2021-04-19 12:51:01 -07:00

124 lines
4.2 KiB
C++

// SPDX-License-Identifier: GPL-2.0
#include "qPrefCloudStorage.h"
#include "qPrefPrivate.h"
static const QString group = QStringLiteral("CloudStorage");
qPrefCloudStorage *qPrefCloudStorage::instance()
{
static qPrefCloudStorage *self = new qPrefCloudStorage;
return self;
}
void qPrefCloudStorage::loadSync(bool doSync)
{
disk_cloud_auto_sync(doSync);
disk_cloud_base_url(doSync);
disk_cloud_storage_email(doSync);
disk_cloud_storage_email_encoded(doSync);
disk_cloud_storage_password(doSync);
disk_cloud_storage_pin(doSync);
disk_cloud_timeout(doSync);
disk_cloud_verification_status(doSync);
disk_save_password_local(doSync);
}
HANDLE_PREFERENCE_BOOL(CloudStorage, "cloud_auto_sync", cloud_auto_sync);
void qPrefCloudStorage::set_cloud_base_url(const QString &value)
{
if (value != prefs.cloud_base_url) {
// only free and set if not default
if (prefs.cloud_base_url != default_prefs.cloud_base_url) {
qPrefPrivate::copy_txt(&prefs.cloud_base_url, value);
}
disk_cloud_base_url(true);
emit instance()->cloud_base_urlChanged(value);
}
}
void qPrefCloudStorage::store_cloud_base_url(const QString &value)
{
// this is used if we want to update the on-disk settings without changing
// the runtime preference
qPrefPrivate::propSetValue(keyFromGroupAndName(group, "cloud_base_url"), value, default_prefs.cloud_base_url);
}
void qPrefCloudStorage::disk_cloud_base_url(bool doSync)
{
// we don't allow to automatically write back the prefs value for the cloud_base_url.
// in order to do that you need to use the explicit function above store_cloud_base_url()
if (!doSync)
prefs.cloud_base_url = copy_qstring(qPrefPrivate::propValue(keyFromGroupAndName(group, "cloud_base_url"), default_prefs.cloud_base_url).toString());
}
HANDLE_PREFERENCE_TXT(CloudStorage, "email", cloud_storage_email);
HANDLE_PREFERENCE_TXT(CloudStorage, "email_encoded", cloud_storage_email_encoded);
void qPrefCloudStorage::set_cloud_storage_password(const QString &value)
{
if (value != prefs.cloud_storage_password) {
qPrefPrivate::copy_txt(&prefs.cloud_storage_password, value);
disk_cloud_storage_password(true);
emit instance()->cloud_storage_passwordChanged(value);
}
}
void qPrefCloudStorage::disk_cloud_storage_password(bool doSync)
{
if (doSync) {
if (prefs.save_password_local)
qPrefPrivate::propSetValue(keyFromGroupAndName(group, "password"), prefs.cloud_storage_password, default_prefs.cloud_storage_password);
} else {
prefs.cloud_storage_password = copy_qstring(qPrefPrivate::propValue(keyFromGroupAndName(group, "password"), default_prefs.cloud_storage_password).toString());
}
}
HANDLE_PREFERENCE_TXT(CloudStorage, "pin", cloud_storage_pin);
HANDLE_PREFERENCE_INT(CloudStorage, "timeout", cloud_timeout);
HANDLE_PREFERENCE_INT(CloudStorage, "cloud_verification_status", cloud_verification_status);
HANDLE_PREFERENCE_BOOL(CloudStorage, "save_password_local", save_password_local);
QString qPrefCloudStorage::diveshare_uid()
{
return qPrefPrivate::propValue(keyFromGroupAndName("", "diveshareExport/uid"), "").toString();
}
void qPrefCloudStorage::set_diveshare_uid(const QString &value)
{
qPrefPrivate::propSetValue(keyFromGroupAndName("", "diveshareExport/uid"), value, "");
emit instance()->diveshare_uidChanged(value);
}
bool qPrefCloudStorage::diveshare_private()
{
return qPrefPrivate::propValue(keyFromGroupAndName("", "diveshareExport/private"), "").toBool();
}
void qPrefCloudStorage::set_diveshare_private(bool value)
{
qPrefPrivate::propSetValue(keyFromGroupAndName("", "diveshareExport/private"), value, false);
emit instance()->diveshare_privateChanged(value);
}
QString qPrefCloudStorage::divelogde_user()
{
return qPrefPrivate::propValue(keyFromGroupAndName("", "divelogde_user"), "").toString();
}
void qPrefCloudStorage::set_divelogde_user(const QString &value)
{
qPrefPrivate::propSetValue(keyFromGroupAndName("", "divelogde_user"), value, "");
emit instance()->divelogde_userChanged(value);
}
QString qPrefCloudStorage::divelogde_pass()
{
return qPrefPrivate::propValue(keyFromGroupAndName("", "divelogde_pass"), "").toString();
}
void qPrefCloudStorage::set_divelogde_pass(const QString &value)
{
qPrefPrivate::propSetValue(keyFromGroupAndName("", "divelogde_pass"), value, "");
emit instance()->divelogde_passChanged(value);
}