diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 3ad69f6..2ee92f5 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -106,18 +106,25 @@ MainWindow::MainWindow(QWidget *parent) : restoreSavedStates(); if (AppDataServer::getInstance()->isAppConnected()) { - createWebsocket(); + auto ads = AppDataServer::getInstance(); + + QString wormholecode = ""; + if (ads->getAllowInternetConnection()) + wormholecode = ads->getWormholeCode(ads->getSecretHex()); + + createWebsocket(wormholecode); } } -void MainWindow::createWebsocket() { +void MainWindow::createWebsocket(QString wormholecode) { qDebug() << "Listening for app connections on port 8237"; // Create the websocket server, for listening to direct connections wsserver = new WSServer(8237, false, this); - // Connect to the wormhole service - wormhole = new WormholeClient(this, AppDataServer::getInstance()->getWormholeCode( - AppDataServer::getInstance()->getSecretHex())); + if (!wormholecode.isEmpty()) { + // Connect to the wormhole service + wormhole = new WormholeClient(this, wormholecode); + } } void MainWindow::stopWebsocket() { diff --git a/src/mainwindow.h b/src/mainwindow.h index 8229c19..c24d402 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -47,7 +47,7 @@ public: void replaceWormholeClient(WormholeClient* newClient); bool isWebsocketListening(); - void createWebsocket(); + void createWebsocket(QString wormholecode); void stopWebsocket(); Ui::MainWindow* ui; diff --git a/src/mobileappconnector.ui b/src/mobileappconnector.ui index 7681c8e..54f7151 100644 --- a/src/mobileappconnector.ui +++ b/src/mobileappconnector.ui @@ -6,8 +6,8 @@ 0 0 - 532 - 400 + 800 + 530 @@ -43,6 +43,25 @@ QR Code + + + + Connection String + + + + + + + + 9 + + + + true + + + @@ -59,17 +78,10 @@ - - + + - Connection String - - - - - - - true + Allow connections over the internet diff --git a/src/websockets.cpp b/src/websockets.cpp index 0025708..a2ec647 100644 --- a/src/websockets.cpp +++ b/src/websockets.cpp @@ -117,12 +117,6 @@ void WormholeClient::onTextMessageReceived(QString message) // ============================== AppDataServer* AppDataServer::instance = nullptr; -QString AppDataServer::getSecretHex() { - QSettings s; - - return s.value("mobileapp/secret", "").toString(); -} - QString AppDataServer::getWormholeCode(QString secretHex) { unsigned char* secret = new unsigned char[crypto_secretbox_KEYBYTES]; sodium_hex2bin(secret, crypto_secretbox_KEYBYTES, secretHex.toStdString().c_str(), crypto_secretbox_KEYBYTES*2, @@ -147,11 +141,25 @@ QString AppDataServer::getWormholeCode(QString secretHex) { return wmcodehex; } -void AppDataServer::saveNewSecret(QString secretHex) { +QString AppDataServer::getSecretHex() { QSettings s; - s.setValue("mobileapp/secret", secretHex); - s.sync(); + return s.value("mobileapp/secret", "").toString(); +} + +void AppDataServer::saveNewSecret(QString secretHex) { + QSettings().setValue("mobileapp/secret", secretHex); + + if (secretHex.isEmpty()) + setAllowInternetConnection(false); +} + +bool AppDataServer::getAllowInternetConnection() { + return QSettings().value("mobileapp/allowinternet", false).toBool(); +} + +void AppDataServer::setAllowInternetConnection(bool allow) { + QSettings().setValue("mobileapp/allowinternet", allow); } void AppDataServer::saveLastConnectedOver(AppConnectionType type) { @@ -203,9 +211,20 @@ void AppDataServer::connectAppDialog(MainWindow* parent) { ui->txtConnStr->selectAll(); }); + QObject::connect(ui->chkInternetConn, &QCheckBox::stateChanged, [=] (int state) { + if (state == Qt::Checked) { + + } + updateUIWithNewQRCode(parent); + }); + // If we're not listening for the app, then start the websockets if (!parent->isWebsocketListening()) { - parent->createWebsocket(); + QString wormholecode = ""; + if (getAllowInternetConnection()) + wormholecode = AppDataServer::getInstance()->getWormholeCode(AppDataServer::getInstance()->getSecretHex()); + + parent->createWebsocket(wormholecode); } d.exec(); @@ -217,7 +236,10 @@ void AppDataServer::connectAppDialog(MainWindow* parent) { // Cleanup tempSecret = ""; + delete tempWormholeClient; + tempWormholeClient = nullptr; + delete ui; ui = nullptr; } @@ -248,18 +270,26 @@ void AppDataServer::updateUIWithNewQRCode(MainWindow* mainwindow) { sodium_bin2hex(secretHex, crypto_secretbox_KEYBYTES*2+1, secretBin, crypto_secretbox_KEYBYTES); QString secretStr(secretHex); - registerNewTempSecret(secretStr, mainwindow); - QString codeStr = uri + "," + secretStr; + if (ui->chkInternetConn->isChecked()) { + codeStr = codeStr + ",1"; + } + + registerNewTempSecret(secretStr, ui->chkInternetConn->isChecked(), mainwindow); + ui->qrcode->setQrcodeString(codeStr); ui->txtConnStr->setText(codeStr); } -void AppDataServer::registerNewTempSecret(QString tmpSecretHex, MainWindow* main) { +void AppDataServer::registerNewTempSecret(QString tmpSecretHex, bool allowInternet, MainWindow* main) { tempSecret = tmpSecretHex; - tempWormholeClient = new WormholeClient(main, getWormholeCode(tempSecret)); + delete tempWormholeClient; + tempWormholeClient = nullptr; + + if (allowInternet) + tempWormholeClient = new WormholeClient(main, getWormholeCode(tempSecret)); } QString AppDataServer::connDesc(AppConnectionType t) { @@ -473,6 +503,7 @@ void AppDataServer::processMessage(QString message, MainWindow* mainWindow, QWeb // This is a new connection. So, update the the secret. Note the last seen remote nonce has already been updated by // decryptMessage() saveNewSecret(tempSecret); + setAllowInternetConnection(tempWormholeClient != nullptr); // Swap out the wormhole connection mainWindow->replaceWormholeClient(tempWormholeClient); diff --git a/src/websockets.h b/src/websockets.h index 48938f6..8418e09 100644 --- a/src/websockets.h +++ b/src/websockets.h @@ -91,11 +91,14 @@ public: QString getSecretHex(); void saveNewSecret(QString secretHex); - void registerNewTempSecret(QString tmpSecretHex, MainWindow* main); + void registerNewTempSecret(QString tmpSecretHex, bool allowInternet, MainWindow* main); QString getNonceHex(NonceType nt); void saveNonceHex(NonceType nt, QString noncehex); + bool getAllowInternetConnection(); + void setAllowInternetConnection(bool allow); + void saveLastSeenTime(); QDateTime getLastSeenTime();