-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathrpcserver.cpp
More file actions
272 lines (245 loc) · 9.81 KB
/
rpcserver.cpp
File metadata and controls
272 lines (245 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/******************************************************************************\
* Copyright (c) 2021-2026
*
* Author(s):
* dtinth
* Christian Hoffmann
*
******************************************************************************
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
\******************************************************************************/
#include "global.h"
#include "rpcserver.h"
CRpcServer::CRpcServer ( QObject* parent, QString strBindIP, int iPort, QString strSecret ) :
QObject ( parent ),
strBindIP ( strBindIP ),
iPort ( iPort ),
strSecret ( strSecret ),
pTransportServer ( new QTcpServer ( this ) )
{
connect ( pTransportServer, &QTcpServer::newConnection, this, &CRpcServer::OnNewConnection );
/// @rpc_method jamulus/getVersion
/// @brief Returns Jamulus version.
/// @param {object} params - No parameters (empty object).
/// @result {string} result.version - The Jamulus version.
HandleMethod ( "jamulus/getVersion", [=] ( const QJsonObject& params, QJsonObject& response ) {
QJsonObject result{ { "version", VERSION } };
response["result"] = result;
Q_UNUSED ( params );
} );
}
CRpcServer::~CRpcServer()
{
if ( pTransportServer->isListening() )
{
qInfo() << "- stopping RPC server";
pTransportServer->close();
}
}
bool CRpcServer::Start()
{
if ( iPort < 0 )
{
return false;
}
if ( pTransportServer->listen ( QHostAddress ( strBindIP ), iPort ) )
{
qInfo() << qUtf8Printable ( QString ( "- JSON-RPC: Server started on %1:%2" )
.arg ( pTransportServer->serverAddress().toString() )
.arg ( pTransportServer->serverPort() ) );
return true;
}
qInfo() << "- JSON-RPC: Unable to start server:" << pTransportServer->errorString();
return false;
}
QJsonObject CRpcServer::CreateJsonRpcError ( int code, QString message )
{
QJsonObject error;
error["code"] = QJsonValue ( code );
error["message"] = QJsonValue ( message );
return error;
}
QJsonObject CRpcServer::CreateJsonRpcErrorReply ( int code, QString message )
{
QJsonObject object;
object["jsonrpc"] = QJsonValue ( "2.0" );
object["error"] = CreateJsonRpcError ( code, message );
return object;
}
void CRpcServer::OnNewConnection()
{
QTcpSocket* pSocket = pTransportServer->nextPendingConnection();
if ( !pSocket )
{
return;
}
qDebug() << "- JSON-RPC: received connection from:" << pSocket->peerAddress().toString();
vecClients.append ( pSocket );
isAuthenticated[pSocket] = false;
connect ( pSocket, &QTcpSocket::disconnected, [this, pSocket]() {
qDebug() << "- JSON-RPC: connection from:" << pSocket->peerAddress().toString() << "closed";
vecClients.removeAll ( pSocket );
isAuthenticated.remove ( pSocket );
pSocket->deleteLater();
} );
connect ( pSocket, &QTcpSocket::readyRead, [this, pSocket]() {
while ( pSocket->canReadLine() )
{
QByteArray line = pSocket->readLine();
if ( line.trimmed().isEmpty() )
{
Send ( pSocket, QJsonDocument ( CreateJsonRpcErrorReply ( iErrParseError, "Parse error: Blank line received" ) ) );
continue;
}
QJsonParseError parseError;
QJsonDocument data = QJsonDocument::fromJson ( line, &parseError );
if ( parseError.error != QJsonParseError::NoError )
{
Send ( pSocket, QJsonDocument ( CreateJsonRpcErrorReply ( iErrParseError, "Parse error: Invalid JSON received" ) ) );
pSocket->disconnectFromHost();
return;
}
if ( data.isArray() )
{
// JSON-RPC batch mode: multiple requests in an array
QJsonArray output;
for ( auto item : data.array() )
{
if ( !item.isObject() )
{
output.append (
CreateJsonRpcErrorReply ( iErrInvalidRequest, "Invalid request: Non-object item encountered in a batch request array" ) );
pSocket->disconnectFromHost();
return;
}
auto object = item.toObject();
QJsonObject response;
response["jsonrpc"] = QJsonValue ( "2.0" );
response["id"] = object["id"];
ProcessMessage ( pSocket, object, response );
output.append ( response );
}
if ( output.size() < 1 )
{
Send ( pSocket,
QJsonDocument ( CreateJsonRpcErrorReply ( iErrInvalidRequest, "Invalid request: Empty batch request encountered" ) ) );
pSocket->disconnectFromHost();
return;
}
Send ( pSocket, QJsonDocument ( output ) );
continue;
}
if ( data.isObject() )
{
auto object = data.object();
QJsonObject response;
response["jsonrpc"] = QJsonValue ( "2.0" );
response["id"] = object["id"];
ProcessMessage ( pSocket, object, response );
Send ( pSocket, QJsonDocument ( response ) );
continue;
}
Send (
pSocket,
QJsonDocument ( CreateJsonRpcErrorReply ( iErrInvalidRequest,
"Invalid request: Unrecognized JSON; a request must be either an object or an array" ) ) );
pSocket->disconnectFromHost();
return;
}
} );
}
void CRpcServer::Send ( QTcpSocket* pSocket, const QJsonDocument& aMessage ) { pSocket->write ( aMessage.toJson ( QJsonDocument::Compact ) + "\n" ); }
void CRpcServer::HandleApiAuth ( QTcpSocket* pSocket, const QJsonObject& params, QJsonObject& response )
{
auto userSecret = params["secret"];
if ( !userSecret.isString() )
{
response["error"] = CreateJsonRpcError ( iErrInvalidParams, "Invalid params: secret is not a string" );
return;
}
if ( userSecret == strSecret )
{
isAuthenticated[pSocket] = true;
response["result"] = "ok";
qInfo() << "- JSON-RPC: accepted valid authentication secret from" << pSocket->peerAddress().toString();
return;
}
response["error"] = CreateJsonRpcError ( CRpcServer::iErrAuthenticationFailed, "Authentication failed." );
qWarning() << "- JSON-RPC: rejected invalid authentication secret from" << pSocket->peerAddress().toString();
}
void CRpcServer::HandleMethod ( const QString& strMethod, CRpcHandler pHandler ) { mapMethodHandlers[strMethod] = pHandler; }
void CRpcServer::ProcessMessage ( QTcpSocket* pSocket, QJsonObject message, QJsonObject& response )
{
if ( !message["method"].isString() )
{
response["error"] = CreateJsonRpcError ( iErrInvalidRequest, "Invalid request: The `method` member is not a string" );
return;
}
// Obtain the params
auto jsonParams = message["params"];
if ( !jsonParams.isObject() )
{
response["error"] = CreateJsonRpcError ( iErrInvalidParams, "Invalid params: The `params` member is not an object" );
return;
}
auto params = jsonParams.toObject();
// Obtain the method name
auto method = message["method"].toString();
// Authentication must be allowed when un-authed
if ( method == "jamulus/apiAuth" )
{
/// @rpc_method jamulus/apiAuth
/// @brief Authenticates the connection which is a requirement for calling further methods.
/// @param {string} params.secret - The preshared secret key.
/// @result {string} result - "ok" on success
HandleApiAuth ( pSocket, params, response );
return;
}
// Require authentication for everything else
if ( !isAuthenticated[pSocket] )
{
response["error"] = CreateJsonRpcError ( iErrUnauthenticated, "Unauthenticated: Please authenticate using jamulus/apiAuth first" );
qInfo() << "- JSON-RPC: rejected unauthenticated request from" << pSocket->peerAddress().toString();
return;
}
// Obtain the method handler
auto it = mapMethodHandlers.find ( method );
if ( it == mapMethodHandlers.end() )
{
response["error"] = CreateJsonRpcError ( iErrMethodNotFound, "Method not found" );
return;
}
// Call the method handler
auto methodHandler = mapMethodHandlers[method];
methodHandler ( params, response );
Q_UNUSED ( pSocket );
}
void CRpcServer::BroadcastNotification ( const QString& strMethod, const QJsonObject& aParams )
{
for ( auto socket : vecClients )
{
if ( !isAuthenticated[socket] )
{
continue;
}
QJsonObject notification;
notification["jsonrpc"] = "2.0";
notification["method"] = strMethod;
notification["params"] = aParams;
Send ( socket, QJsonDocument ( notification ) );
}
}