Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add first statistics version and implement multiple api method call p… #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions nymea-remoteproxy.conf → data/config/nymea-remoteproxy.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name=nymea-remoteproxy
writeLogs=false
logFile=/var/log/nymea-remoteproxy.log
logEngineEnabled=false
logEngineEnabled=true
monitorSocket=/tmp/nymea-remoteproxy-monitor.sock
jsonRpcTimeout=10000
authenticationTimeout=8000
Expand All @@ -20,7 +20,7 @@ certificateKey=/etc/ssl/private/ssl-cert-snakeoil.key
certificateChain=

[WebSocketServer]
host=127.0.0.1
host=0.0.0.0
port=443

[TcpServer]
Expand Down
10 changes: 10 additions & 0 deletions data/logrotate/nymea-remoteproxy
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/var/log/nymea-remoteproxy.log {
rotate 14
daily
dateext
compress
delaycompress
missingok
notifempty
create 755 root root
}
18 changes: 15 additions & 3 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ Source: nymea-remoteproxy
Section: utils
Priority: options
Maintainer: Simon Stürz <simon.stü[email protected]>
Build-depends: debhelper (>= 0.0.0),
Build-depends: debhelper (>= 9.0.0),
dh-systemd,
libqt5websockets5-dev,
libncurses5-dev,
lcov
Standards-Version: 3.9.3

Package: nymea-remoteproxy
Architecture: any
Depends: ${shlibs:Depends},
${misc:Depends},
awscli,
libnymea-remoteproxy (= ${binary:Version}),
Suggests: nymea-remoteproxy-monitor (= ${binary:Version})
Recommends: nymea-remoteproxy-monitor (= ${binary:Version}),
nymea-remoteproxy-statistics (= ${binary:Version})
Suggests: nymea-remoteproxy-client (= ${binary:Version})
Description: The nymea remote proxy server
The nymea remote proxy server

Expand Down Expand Up @@ -94,3 +96,13 @@ Depends: ${shlibs:Depends},
libncurses5,
Description: The nymea remote proxy monitor tool
The nymea remote proxy server tests

Package: nymea-remoteproxy-statistics
Architecture: any
Depends: ${shlibs:Depends},
${misc:Depends},
python,
python-gnuplot,
Description: The nymea remote proxy statistic util
The nymea remote proxy server statistic tool for generating
server statistic reports.
1 change: 1 addition & 0 deletions debian/nymea-remoteproxy-statistics.install.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nymea-remoteproxy-statistics/nymea-remoteproxy-statistics usr/bin
3 changes: 2 additions & 1 deletion debian/nymea-remoteproxy.install.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
usr/bin/nymea-remoteproxy
nymea-remoteproxy.conf etc/nymea/
data/config/nymea-remoteproxy.conf etc/nymea/
data/logrotate/nymea-remoteproxy etc/logrotate.d/
3 changes: 2 additions & 1 deletion libnymea-remoteproxy/jsonrpc/authenticationhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ AuthenticationHandler::AuthenticationHandler(QObject *parent) :
"id the other tunnel client can understand. Once the authentication was successfull, you "
"can wait for the RemoteProxy.TunnelEstablished notification. If you send any data before "
"getting this notification, the server will close the connection. If the tunnel client does "
"not show up within 10 seconds, the server will close the connection.");
"not show up within 10 seconds, the server will close the connection. This method can only be "
"called once, otherwise the connection will be killed.");
params.insert("uuid", JsonTypes::basicTypeToString(JsonTypes::String));
params.insert("name", JsonTypes::basicTypeToString(JsonTypes::String));
params.insert("token", JsonTypes::basicTypeToString(JsonTypes::String));
Expand Down
13 changes: 11 additions & 2 deletions libnymea-remoteproxy/jsonrpcserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ JsonRpcServer::JsonRpcServer(QObject *parent) :

params.clear(); returns.clear();
setDescription("Hello", "Once connected to this server, a client can get information about the server by saying Hello. "
"The response informs the client about this proxy server.");
"The response informs the client about this proxy server. This method can only be called once, "
"otherwise the connection will be killed.");
setParams("Hello", params);
returns.insert("server", JsonTypes::basicTypeToString(JsonTypes::String));
returns.insert("name", JsonTypes::basicTypeToString(JsonTypes::String));
Expand All @@ -47,7 +48,8 @@ JsonRpcServer::JsonRpcServer(QObject *parent) :
setReturns("Hello", returns);

params.clear(); returns.clear();
setDescription("Introspect", "Introspect this API.");
setDescription("Introspect", "Introspect this API. This method can only be called once, "
"otherwise the connection will be killed.");
setParams("Introspect", params);
returns.insert("methods", JsonTypes::basicTypeToString(JsonTypes::Object));
returns.insert("types", JsonTypes::basicTypeToString(JsonTypes::Object));
Expand Down Expand Up @@ -291,6 +293,13 @@ void JsonRpcServer::processData(ProxyClient *proxyClient, const QByteArray &data
return;
}

// Verfiy if this method was already called by this client
if (!proxyClient->validateMethodCall(message.value("method").toString())) {
sendErrorResponse(proxyClient, commandId, "Multiple method call not allowed. The method" + message.value("method").toString() + "has already been called by this client.");
proxyClient->killConnection("Multiple method call.");
return;
}

JsonReply *reply;
QMetaObject::invokeMethod(handler, method.toLatin1().data(), Q_RETURN_ARG(JsonReply*, reply), Q_ARG(QVariantMap, params), Q_ARG(ProxyClient *, proxyClient));
if (reply->type() == JsonReply::TypeAsync) {
Expand Down
7 changes: 6 additions & 1 deletion libnymea-remoteproxy/logengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "loggingcategories.h"

#include <QDateTime>
#include <QCryptographicHash>

namespace remoteproxy {

Expand All @@ -48,11 +49,13 @@ void LogEngine::logTunnel(const TunnelConnection &tunnel)
QStringList logString;
logString << createTimestamp();
logString << QString::number(tunnel.creationTime());
logString << tunnel.clientOne()->userName();
logString << QString::fromUtf8(QCryptographicHash::hash(tunnel.clientOne()->userName().toLatin1(), QCryptographicHash::Sha3_256).toHex());
logString << tunnel.clientOne()->peerAddress().toString();
logString << tunnel.clientTwo()->peerAddress().toString();
logString << QString::number(tunnel.clientOne()->rxDataCount() + tunnel.clientOne()->txDataCount());

qCDebug(dcLogEngine()) << "Logging tunnel" << logString;

QTextStream textStream(&m_tunnelsFile);
textStream << logString.join(" ") << endl;
}
Expand All @@ -69,6 +72,8 @@ void LogEngine::logStatistics(int tunnelCount, int connectionCount, int troughpu
logString << QString::number(connectionCount);
logString << QString::number(troughput);

qCDebug(dcLogEngine()) << "Logging statisitcs" << logString;

QTextStream textStream(&m_statisticsFile);
textStream << logString.join(" ") << endl;

Expand Down
1 change: 1 addition & 0 deletions libnymea-remoteproxy/loggingcategories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

Q_LOGGING_CATEGORY(dcApplication, "Application")
Q_LOGGING_CATEGORY(dcEngine, "Engine")
Q_LOGGING_CATEGORY(dcLogEngine, "LogEngine")
Q_LOGGING_CATEGORY(dcJsonRpc, "JsonRpc")
Q_LOGGING_CATEGORY(dcJsonRpcTraffic, "JsonRpcTraffic")
Q_LOGGING_CATEGORY(dcWebSocketServer, "WebSocketServer")
Expand Down
1 change: 1 addition & 0 deletions libnymea-remoteproxy/loggingcategories.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

Q_DECLARE_LOGGING_CATEGORY(dcApplication)
Q_DECLARE_LOGGING_CATEGORY(dcEngine)
Q_DECLARE_LOGGING_CATEGORY(dcLogEngine)
Q_DECLARE_LOGGING_CATEGORY(dcJsonRpc)
Q_DECLARE_LOGGING_CATEGORY(dcJsonRpcTraffic)
Q_DECLARE_LOGGING_CATEGORY(dcWebSocketServer)
Expand Down
11 changes: 11 additions & 0 deletions libnymea-remoteproxy/proxyclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,23 @@ void ProxyClient::killConnection(const QString &reason)
m_interface->killClientConnection(m_clientId, reason);
}

bool ProxyClient::validateMethodCall(const QString &method)
{
// Note: each method is allowed only once. If the method was already called, return false
if (m_calledMethods.contains(method))
return false;

m_calledMethods.append(method);
return true;
}

QDebug operator<<(QDebug debug, ProxyClient *proxyClient)
{
debug.nospace() << "ProxyClient(";
if (!proxyClient->name().isEmpty()) {
debug.nospace() << proxyClient->name() << ", ";
}

debug.nospace() << proxyClient->interface()->serverName();
debug.nospace() << ", " << proxyClient->clientId().toString();
debug.nospace() << ", " << proxyClient->userName();
Expand Down
3 changes: 3 additions & 0 deletions libnymea-remoteproxy/proxyclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class ProxyClient : public QObject
// Actions for this client
void sendData(const QByteArray &data);
void killConnection(const QString &reason);
bool validateMethodCall(const QString &method);

private:
TransportInterface *m_interface = nullptr;
Expand All @@ -100,6 +101,8 @@ class ProxyClient : public QObject
quint64 m_rxDataCount = 0;
quint64 m_txDataCount = 0;

QStringList m_calledMethods;

signals:
void authenticated();
void tunnelConnected();
Expand Down
3 changes: 2 additions & 1 deletion libnymea-remoteproxy/websocketserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ void WebSocketServer::onClientConnected()
void WebSocketServer::onClientDisconnected()
{
QWebSocket *client = static_cast<QWebSocket *>(sender());
QUuid clientId = m_clientList.key(client);
client->abort();

QUuid clientId = m_clientList.key(client);
qCDebug(dcWebSocketServer()) << "Client disconnected:" << client << client->peerAddress().toString() << clientId.toString() << client->closeReason();

m_clientList.take(clientId)->deleteLater();
Expand Down
5 changes: 2 additions & 3 deletions libnymea-remoteproxyclient/remoteproxyconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,11 @@ void RemoteProxyConnection::onConnectionChanged(bool isConnected)
{
if (isConnected) {
qCDebug(dcRemoteProxyClientConnection()) << "Connected to proxy server.";
setState(StateConnected);

setState(StateInitializing);
JsonReply *reply = m_jsonClient->callHello();
connect(reply, &JsonReply::finished, this, &RemoteProxyConnection::onHelloFinished);
} else {
qCDebug(dcRemoteProxyClientConnection()) << "Disconnected from proxy server.";
setState(StateDisconnected);
cleanUp();
}
}
Expand Down Expand Up @@ -270,6 +267,8 @@ void RemoteProxyConnection::onHelloFinished()
m_proxyServerVersion = responseParams.value("version").toString();
m_proxyServerApiVersion = responseParams.value("apiVersion").toString();

qCDebug(dcRemoteProxyClientConnection()) << "Connected to" << m_serverName << m_proxyServerName << m_proxyServerVersion << "API:" << m_proxyServerApiVersion;

setState(StateReady);
}

Expand Down
1 change: 0 additions & 1 deletion libnymea-remoteproxyclient/websocketconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ void WebSocketConnection::onStateChanged(QAbstractSocket::SocketState state)
setConnected(true);
break;
default:
setConnected(false);
break;
}

Expand Down
Loading