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