Skip to content
This repository has been archived by the owner on Jan 28, 2022. It is now read-only.

Commit

Permalink
feat: Configurable port for websocket server
Browse files Browse the repository at this point in the history
WebSocket server port can be configured with the environment variable YIO_WEBSOCKET_PORT.
If not specified, the old default port 946 is used.

This relates to #489
  • Loading branch information
zehnm committed Oct 16, 2020
1 parent b39b94f commit 0076733
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 59 deletions.
5 changes: 4 additions & 1 deletion sources/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ int main(int argc, char* argv[]) {
hwFactory->initialize();

// YIO API
YioAPI* yioapi = new YioAPI(&engine);
YioAPI* yioapi = YioAPI::getInstance();
bool ok;
int webSocketPort = qEnvironmentVariableIntValue("YIO_WEBSOCKET_PORT", &ok);
yioapi->setWebSocketPort(ok ? webSocketPort : 946);
engine.rootContext()->setContextProperty("api", yioapi);

// FACTORY RESET HANDLER
Expand Down
46 changes: 22 additions & 24 deletions sources/yioapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

#include "yioapi.h"

#include <QHostInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkInterface>
#include <QTimer>

#include "launcher.h"
Expand All @@ -39,48 +39,42 @@
qCDebug(lcApi) << message << client->peerAddress().toString(); \
}

YioAPI *YioAPI::s_instance = nullptr;
YioAPI *YioAPI::getInstance() {
static YioAPI api;
return &api;
}

YioAPI::YioAPI(QQmlApplicationEngine *engine) : m_engine(engine) {
s_instance = this;
YioAPI::YioAPI() : m_port(946), m_server(nullptr), m_context(nullptr) {
m_entities = Entities::getInstance();
m_integrations = Integrations::getInstance();
m_config = Config::getInstance();
}

YioAPI::~YioAPI() {
s_instance = nullptr;
if (m_server) {
delete m_server;
}
}

void YioAPI::start() {
m_server = new QWebSocketServer(QStringLiteral("YIO API"), QWebSocketServer::NonSecureMode, this);

// start websocket server on port 946(YIO)
if (m_server->listen(QHostAddress::Any, 946)) {
// start websocket server
if (m_server->listen(QHostAddress::Any, m_port)) {
connect(m_server, &QWebSocketServer::newConnection, this, &YioAPI::onNewConnection);
connect(m_server, &QWebSocketServer::closed, this, &YioAPI::onClosed);

m_running = true;
emit runningChanged();
}

QString macAddr;
for (QNetworkInterface interface : QNetworkInterface::allInterfaces()) {
if (!(interface.flags() & QNetworkInterface::IsLoopBack)) {
macAddr = interface.hardwareAddress();
break;
}
}

macAddr.replace(":", "");
m_hostname = "";
m_hostname.append("YIO-Remote-").append(macAddr);
qCDebug(lcApi) << "NAME" << m_hostname;
m_hostname = QHostInfo::localHostName().split(QLatin1Char('.')).at(0);
qCDebug(lcApi) << "YIO api published as ZeroConf service name:" << m_hostname;
emit hostnameChanged();

m_zeroConf.startServicePublish(m_hostname.toUtf8(), "_yio-remote._tcp", "local", 946);
m_zeroConf.startServicePublish(m_hostname.toUtf8(), "_yio-remote._tcp", "local", m_port);

qCDebug(lcApi) << "YIO api started";
qCDebug(lcApi) << "YIO api started on port" << m_port;
}

void YioAPI::stop() {
Expand All @@ -89,6 +83,7 @@ void YioAPI::stop() {
m_running = false;
m_zeroConf.stopServicePublish();
emit runningChanged();
qCDebug(lcApi) << "YIO api stopped";
}

void YioAPI::sendMessage(QString message) {
Expand Down Expand Up @@ -852,7 +847,7 @@ void YioAPI::apiSystemSubscribeToEvents(QWebSocket *client, const int &id) {
// add client to the subscribed client list
m_subscribed_clients.append(client);

// connect to signals
// connect to signals. FIXME handle multiple clients and perform proper cleanup
m_context = new QObject(this);

QObject::connect(m_config, &Config::configChanged, m_context, [=]() { subscribeOnSignalEvent("config_changed"); });
Expand Down Expand Up @@ -882,8 +877,11 @@ void YioAPI::apiSystemUnsubscribeFromEvents(QWebSocket *client, const int &id) {
return;
}

QObject::disconnect(m_context);
m_context->deleteLater();
// FIXME handle multiple clients and perform proper cleanup
if (m_context) {
QObject::disconnect(m_context);
m_context->deleteLater();
}

for (int i = 0; i < m_subscribed_clients.length(); i++) {
if (m_subscribed_clients[i] == client) {
Expand Down
67 changes: 34 additions & 33 deletions sources/yioapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class YioAPI : public YioAPIInterface {
Q_INTERFACES(YioAPIInterface)

public:
static YioAPI* getInstance();

Q_PROPERTY(bool running READ running NOTIFY runningChanged) // returns the state of the API
Q_PROPERTY(QString hostname READ hostname NOTIFY hostnameChanged) // returns the hostname of the remote

Expand All @@ -51,6 +53,9 @@ class YioAPI : public YioAPIInterface {
*/
Q_INVOKABLE void sendMessage(QString message) override;

void setWebSocketPort(quint16 port) { m_port = port; }
quint16 webSocketPort() { return m_port; }

// CONFIG MANIPULATION METHODS
QVariantMap getConfig() override;
bool setConfig(QVariantMap config);
Expand All @@ -67,11 +72,6 @@ class YioAPI : public YioAPIInterface {
void discoverNetworkServices() override;
Q_INVOKABLE void discoverNetworkServices(QString mdns) override;

explicit YioAPI(QQmlApplicationEngine* engine = nullptr);
~YioAPI() override;

static YioAPI* getInstance() { return s_instance; }

signals:
// void closed();
// void messageReceived(QVariantMap message);
Expand All @@ -87,35 +87,10 @@ class YioAPI : public YioAPIInterface {
void onClientDisconnected();

private:
QWebSocketServer* m_server;
QMap<QWebSocket*, bool> m_clients; // websocket client, true if authentication was successful

QList<QWebSocket*> m_subscribed_clients;
QObject* m_context;
void subscribeOnSignalEvent(const QString& event);

bool m_running = false;

static YioAPI* s_instance;
QQmlApplicationEngine* m_engine;

QString m_token = "0"; // "c82b5fd6bea6fc3faf9a30bb864a9ee2"
// QCryptographicHash::hash(m_token.toLocal8Bit(), QCryptographicHash::Sha512);
QByteArray m_hash =
"{U\xC0<$\xF7\n\xA7PA\xC3=\xBEk\xF5\xC1\xCA\x8B\t\x91\xA0\x9Et\xBA"
"E\xE9\xA0)\xE4\x07^E\x04\x17Xg\xE4)\x04\xB7\xD4\x9D,\x19%\xD7\xA1\xDC\x84U\x83\xA2\xAA\x1D\xD7:\xBE\xF6"
"1\xFA\x90\xED\x16\xBB";
QString m_hostname;

QZeroConf m_zeroConf;
QZeroConf* m_zeroConfBrowser;
YioAPI();
~YioAPI() override;

QStringList m_discoverableServices;
QString m_prevIp;

Entities* m_entities;
Integrations* m_integrations;
Config* m_config;
void subscribeOnSignalEvent(const QString& event);

// API CALLS
void apiSendResponse(QWebSocket* client, const int& id, const bool& success, QVariantMap response);
Expand Down Expand Up @@ -168,4 +143,30 @@ class YioAPI : public YioAPIInterface {
void apiSettingsSetDarkMode(QWebSocket* client, const int& id, const QVariantMap& map);

void apiLoggerControl(QWebSocket* client, const int& id, const QVariantMap& map);

private:
quint16 m_port;
bool m_running = false;
QWebSocketServer* m_server;
QMap<QWebSocket*, bool> m_clients; // websocket client, true if authentication was successful

QList<QWebSocket*> m_subscribed_clients;
QObject* m_context;

QString m_token = "0"; // "c82b5fd6bea6fc3faf9a30bb864a9ee2"
// QCryptographicHash::hash(m_token.toLocal8Bit(), QCryptographicHash::Sha512);
QByteArray m_hash =
"{U\xC0<$\xF7\n\xA7PA\xC3=\xBEk\xF5\xC1\xCA\x8B\t\x91\xA0\x9Et\xBA"
"E\xE9\xA0)\xE4\x07^E\x04\x17Xg\xE4)\x04\xB7\xD4\x9D,\x19%\xD7\xA1\xDC\x84U\x83\xA2\xAA\x1D\xD7:\xBE\xF6"
"1\xFA\x90\xED\x16\xBB";
QString m_hostname;

QZeroConf m_zeroConf;

QStringList m_discoverableServices;
QString m_prevIp;

Entities* m_entities;
Integrations* m_integrations;
Config* m_config;
};
2 changes: 1 addition & 1 deletion translations/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ to set up YIO remote</source>
<context>
<name>QGuiApplication</name>
<message>
<location filename="../sources/main.cpp" line="216"/>
<location filename="../sources/main.cpp" line="217"/>
<source>Factory reset failed.
%1</source>
<translation type="unfinished"></translation>
Expand Down

0 comments on commit 0076733

Please sign in to comment.