From 3149f04fc16cfa4c4a065d471beab8bcc3d1500a Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Sat, 10 Apr 2021 02:22:42 -0400 Subject: [PATCH] . --- build.sh | 2 +- src/connection.cpp | 8 +++--- src/settings.cpp | 66 ++++++++++++++++++++++++++++++++++++---------- src/settings.h | 2 ++ 4 files changed, 58 insertions(+), 20 deletions(-) diff --git a/build.sh b/build.sh index 3476061..9a8211c 100755 --- a/build.sh +++ b/build.sh @@ -4,7 +4,7 @@ UNAME=$(uname) if [ "$UNAME" == "Linux" ] ; then - JOBS=4 + JOBS=2 elif [ "$UNAME" == "FreeBSD" ] ; then JOBS=$(nproc) elif [ "$UNAME" == "Darwin" ] ; then diff --git a/src/connection.cpp b/src/connection.cpp index 94022e0..98f3637 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -32,7 +32,7 @@ ConnectionLoader::ConnectionLoader(MainWindow* main, Controller* rpc) connD->setupUi(d); auto theme = Settings::getInstance()->get_theme_name(); - qDebug() << theme << "theme has loaded"; + qDebug() << theme << "theme " << theme << " has loaded"; auto size = QSize(512,512); if (theme == "Dark" || theme == "Midnight") { @@ -146,8 +146,7 @@ void ConnectionLoader::doAutoConnect() main->logger->write(QObject::tr("Attempting to initialize library with ") + config->server); // Check to see if there's an existing wallet - if (litelib_wallet_exists(Settings::getDefaultChainName().toStdString().c_str())) - { + if (litelib_wallet_exists(Settings::getDefaultChainName().toStdString().c_str())) { main->logger->write(QObject::tr("Using existing wallet.")); char* resp = litelib_initialize_existing( config->dangerous, @@ -155,8 +154,7 @@ void ConnectionLoader::doAutoConnect() ); QString response = litelib_process_response(resp); - if (response.toUpper().trimmed() != "OK") - { + if (response.toUpper().trimmed() != "OK") { showError(response); return; } diff --git a/src/settings.cpp b/src/settings.cpp index a78880b..a97f71e 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -3,6 +3,7 @@ #include "mainwindow.h" #include "settings.h" #include "camount.h" +#include "../lib/silentdragonlitelib.h" Settings* Settings::instance = nullptr; @@ -18,26 +19,41 @@ Settings* Settings::getInstance() { } Config Settings::getSettings() { + qDebug() << __func__; // Load from the QT Settings. QSettings s; // this domain is stolen and malicious! auto malicious = "https://lite.myhush.org"; auto server = s.value("connection/server").toString(); - if(server == malicious) { - server = "https://lite.hush.is"; - qDebug() << "Replacing malicious SDL server with " << server; + bool torOnly = s.value("connectoin/torOnly").toBool(); + + // default behavior is no server listed in conf, and randomly choose from those listed in code + if (server.trimmed().isEmpty()) { + server = Settings::getRandomServer(); + } + + // make sure existing server in conf is alive, otherwise choose random one + char* resp = litelib_initialize_existing(false, server.toStdString().c_str()); + QString response = litelib_process_response(resp); + + // if we see a valid connection, return this server + if (response.toUpper().trimmed() != "OK") { + qDebug() << "Lite server in conf " << server << " is down, getting a random one"; + server = Settings::getRandomServer(); s.setValue("connection/server", server); - s.sync(); - // re-init to load correct settings - init(); } - if (server.trimmed().isEmpty()) { - server = Settings::getDefaultServer(); + // Users that have old configs generated from old SDLs will have this hostname + if(server == malicious) { + qDebug() << "Replacing malicious SDL server with " << server; + s.setValue("connection/server", server); } + s.sync(); + // re-init to load correct settings + init(); - return Config{server}; + return Config{server, torOnly}; } void Settings::saveSettings(const QString& server) { @@ -250,9 +266,7 @@ void Settings::set_theme_name(QString theme_name) { QSettings().setValue("options/theme_name", theme_name); } -//================================= // Static Stuff -//================================= void Settings::saveRestore(QDialog* d) { d->restoreGeometry(QSettings().value(d->objectName() % "geometry").toByteArray()); @@ -271,6 +285,7 @@ void Settings::saveRestoreTableHeader(QTableView* table, QDialog* d, QString tab } QString Settings::getRandomServer() { + qDebug() << __func__; // we don't need cryptographic random-ness, but we want // clients to never get "stuck" with the same server, which // prevents various attacks @@ -278,12 +293,35 @@ QString Settings::getRandomServer() { //TODO: This should be a much larger list which we randomly choose from servers[0] = "https://lite.hush.is"; servers[1] = "https://devo.crabdance.com"; - //servers[2] = "https://hush.leto.net:5420"; - int x = rand() % 2; - return servers[x]; + servers[2] = "https://bies.xyz"; + servers[3] = "https://hush.leto.net"; + servers[4] = "https://milktoast.attackingzcash.com"; + + // start at a random place in the list + int x = rand() % servers.size(); + auto server = servers[x]; + int tries = 0; + + // We try every server,in order, starting from a random place in the list + while (tries <= servers.size() ) { + qDebug() << "Checking if lite server " << server << " is a alive, try=" << tries; + char* resp = litelib_initialize_existing(false, server.toStdString().c_str()); + QString response = litelib_process_response(resp); + + // if we see a valid connection, return this server + if (response.toUpper().trimmed() == "OK") { + qDebug() << "Choosing lite server " << server; + return server; + } + server = servers[++x % servers.size()]; + + tries++; + } + return server; } QString Settings::getDefaultServer() { + qDebug() << __func__; return "https://lite.hush.is"; } diff --git a/src/settings.h b/src/settings.h index e4c8673..2c536f4 100644 --- a/src/settings.h +++ b/src/settings.h @@ -9,7 +9,9 @@ using json = nlohmann::json; struct Config { + // The randomly chosen server we are talking to OR user-specific server QString server; + bool torOnly {false}; }; struct ToFields;