diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 1c1d0fc..3ad69f6 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -105,10 +105,13 @@ MainWindow::MainWindow(QWidget *parent) : restoreSavedStates(); - createWebsocket(); + if (AppDataServer::getInstance()->isAppConnected()) { + createWebsocket(); + } } void MainWindow::createWebsocket() { + qDebug() << "Listening for app connections on port 8237"; // Create the websocket server, for listening to direct connections wsserver = new WSServer(8237, false, this); @@ -117,6 +120,20 @@ void MainWindow::createWebsocket() { AppDataServer::getInstance()->getSecretHex())); } +void MainWindow::stopWebsocket() { + delete wsserver; + wsserver = nullptr; + + delete wormhole; + wormhole = nullptr; + + qDebug() << "Websockets for app connections shut down"; +} + +bool MainWindow::isWebsocketListening() { + return wsserver != nullptr; +} + void MainWindow::replaceWormholeClient(WormholeClient* newClient) { delete wormhole; wormhole = newClient; diff --git a/src/mainwindow.h b/src/mainwindow.h index a520411..8229c19 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -46,6 +46,9 @@ public: void setDefaultPayFrom(); void replaceWormholeClient(WormholeClient* newClient); + bool isWebsocketListening(); + void createWebsocket(); + void stopWebsocket(); Ui::MainWindow* ui; @@ -107,8 +110,6 @@ private: void restoreSavedStates(); - void createWebsocket(); - WSServer* wsserver = nullptr; WormholeClient* wormhole = nullptr; diff --git a/src/websockets.cpp b/src/websockets.cpp index 943a761..0025708 100644 --- a/src/websockets.cpp +++ b/src/websockets.cpp @@ -170,6 +170,19 @@ QDateTime AppDataServer::getLastSeenTime() { return QDateTime::fromSecsSinceEpoch(QSettings().value("mobileapp/lastseentime", 0).toLongLong()); } +void AppDataServer::setConnectedName(QString name) { + QSettings().setValue("mobileapp/connectedname", name); +} + +QString AppDataServer::getConnectedName() { + return QSettings().value("mobileapp/connectedname", "").toString(); +} + +bool AppDataServer::isAppConnected() { + return !getConnectedName().isEmpty() && + getLastSeenTime().daysTo(QDateTime::currentDateTime()) < 14; +} + void AppDataServer::connectAppDialog(MainWindow* parent) { QDialog d(parent); ui = new Ui_MobileAppConnector(); @@ -190,8 +203,18 @@ void AppDataServer::connectAppDialog(MainWindow* parent) { ui->txtConnStr->selectAll(); }); + // If we're not listening for the app, then start the websockets + if (!parent->isWebsocketListening()) { + parent->createWebsocket(); + } + d.exec(); + // If there is nothing connected when the dialog exits, then shutdown the websockets + if (!isAppConnected()) { + parent->stopWebsocket(); + } + // Cleanup tempSecret = ""; delete tempWormholeClient; @@ -252,7 +275,7 @@ void AppDataServer::updateConnectedUI() { if (ui == nullptr) return; - auto remoteName = QSettings().value("mobileapp/connectedname", "").toString(); + auto remoteName = getConnectedName(); ui->lblRemoteName->setText(remoteName.isEmpty() ? "(Not connected to any device)" : remoteName); ui->lblLastSeen->setText(remoteName.isEmpty() ? "" : getLastSeenTime().toString(Qt::SystemLocaleLongDate)); @@ -267,10 +290,10 @@ QString AppDataServer::getNonceHex(NonceType nt) { if (nt == NonceType::LOCAL) { // The default local nonce starts from 1, to always keep it odd auto defaultLocalNonce = "01" + QString("00").repeated(crypto_secretbox_NONCEBYTES-1); - hex = s.value("mobileapp/localnonce", defaultLocalNonce).toString(); + hex = s.value("mobileapp/localnoncehex", defaultLocalNonce).toString(); } else { - hex = s.value("mobileapp/remotenonce", QString("00").repeated(crypto_secretbox_NONCEBYTES)).toString(); + hex = s.value("mobileapp/remotenoncehex", QString("00").repeated(crypto_secretbox_NONCEBYTES)).toString(); } return hex; } @@ -279,10 +302,10 @@ void AppDataServer::saveNonceHex(NonceType nt, QString noncehex) { QSettings s; assert(noncehex.length() == crypto_secretbox_NONCEBYTES * 2); if (nt == NonceType::LOCAL) { - s.setValue("mobileapp/localnonce", noncehex); + s.setValue("mobileapp/localnoncehex", noncehex); } else { - s.setValue("mobileapp/remotenonce", noncehex); + s.setValue("mobileapp/remotenoncehex", noncehex); } s.sync(); } @@ -324,8 +347,6 @@ QString AppDataServer::encryptOutgoing(QString msg) { sodium_memzero(encryptedHex, encryptedHexSize); sodium_bin2hex(encryptedHex, encryptedHexSize, encrpyted, msgSize + crypto_secretbox_MACBYTES); - qDebug() << "Encrypted to " << QString(encryptedHex); - auto json = QJsonDocument(QJsonObject{ {"nonce", QString(newLocalNonce)}, {"payload", QString(encryptedHex)}, @@ -634,10 +655,7 @@ void AppDataServer::processGetInfo(QJsonObject jobj, MainWindow* mainWindow, QWe } } - { - QSettings s; - s.setValue("mobileapp/connectedname", connectedName); - } + setConnectedName(connectedName); auto r = QJsonDocument(QJsonObject{ {"version", 1.0}, diff --git a/src/websockets.h b/src/websockets.h index 580b187..48938f6 100644 --- a/src/websockets.h +++ b/src/websockets.h @@ -99,6 +99,10 @@ public: void saveLastSeenTime(); QDateTime getLastSeenTime(); + void setConnectedName(QString name); + QString getConnectedName(); + bool isAppConnected(); + QString connDesc(AppConnectionType t); void saveLastConnectedOver(AppConnectionType type);