-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsslserver.cpp
133 lines (113 loc) · 4.76 KB
/
sslserver.cpp
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
#include "sslserver.h"
#include "app.h"
#include "QtWebSockets/QWebSocketServer"
#include "QtWebSockets/QWebSocket"
#include <QMetaEnum>
#include <QtCore/QFile>
#include <QtNetwork/QSslCertificate>
#include <QtNetwork/QSslKey>
#include "websocketclient.h"
SslServer::SslServer(QObject *parent) :
QObject(parent),
m_pWebSocketServer(nullptr)
{
m_pWebSocketServer = new QWebSocketServer(QStringLiteral("SSL Espruino Host"),
QWebSocketServer::SecureMode,
this);
QSslConfiguration sslConfiguration;
QFile certFile(QStringLiteral(":/keys/localhost.cert"));
QFile keyFile(QStringLiteral(":/keys/localhost.key"));
certFile.open(QIODevice::ReadOnly);
keyFile.open(QIODevice::ReadOnly);
QSslCertificate certificate(&certFile, QSsl::Pem);
QSslKey sslKey(&keyFile, QSsl::Rsa, QSsl::Pem);
certFile.close();
keyFile.close();
sslConfiguration.setPeerVerifyMode(QSslSocket::VerifyNone);
sslConfiguration.setLocalCertificate(certificate);
sslConfiguration.setPrivateKey(sslKey);
sslConfiguration.setProtocol(QSsl::TlsV1SslV3);
m_pWebSocketServer->setSslConfiguration(sslConfiguration);
int port = WEBSOCKET_PORT;
if (m_pWebSocketServer->listen(QHostAddress::Any, port))
{
g_app->status(QString("WebSocket listening on wss://localhost:") + QString::number(port));
connect(m_pWebSocketServer, &QWebSocketServer::acceptError,
this, &SslServer::onAcceptError);
connect(m_pWebSocketServer, &QWebSocketServer::peerVerifyError,
this, &SslServer::onPeerVerifyError);
connect(m_pWebSocketServer, &QWebSocketServer::preSharedKeyAuthenticationRequired,
this, &SslServer::onPreSharedKeyAuthenticationRequired);
connect(m_pWebSocketServer, &QWebSocketServer::serverError,
this, &SslServer::onServerError);
connect(m_pWebSocketServer, &QWebSocketServer::newConnection,
this, &SslServer::onNewConnection);
connect(m_pWebSocketServer, &QWebSocketServer::sslErrors,
this, &SslServer::onSslErrors);
}
}
SslServer::~SslServer()
{
m_pWebSocketServer->close();
qDeleteAll(m_clients.begin(), m_clients.end());
m_clients.clear();
}
void SslServer::onAcceptError(QAbstractSocket::SocketError socketError)
{
QMetaEnum metaEnum = QMetaEnum::fromType<QAbstractSocket::SocketError>();
g_app->error(QString("WebSocket Accept Error:") + metaEnum.valueToKey(socketError));
}
void SslServer::onPeerVerifyError(QSslError socketError)
{
g_app->error(QString("WebSocket Peer Verify Error:") + socketError.errorString());
}
void SslServer::onPreSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator) {
g_app->warn(QString("preSharedKeyAuthenticationRequired"));
}
void SslServer::onServerError(QWebSocketProtocol::CloseCode closeCode) {
g_app->error(QString("WebSocket Server Error:") + QString::number((int)closeCode));
}
void SslServer::onNewConnection()
{
QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
WebSocketClient *wsClient = new WebSocketClient(nullptr);
wsClient->pWebSocket = pSocket;
g_app->log(QString("WebSocket Client connected:") + pSocket->peerName() + " " + pSocket->origin());
connect(pSocket, &QWebSocket::textMessageReceived, this, &SslServer::processTextMessage);
connect(pSocket, &QWebSocket::binaryMessageReceived, this, &SslServer::processBinaryMessage);
connect(pSocket, &QWebSocket::disconnected, this, &SslServer::socketDisconnected);
m_clients << wsClient;
g_app->status(QString::number(m_clients.length()) + QString(" clients connected"));
}
void SslServer::processTextMessage(QString message)
{
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
WebSocketClient *wsClient = SslServer::findClientForSocket(pClient);
if (wsClient) wsClient->receive(message);
}
void SslServer::processBinaryMessage(QByteArray message)
{
g_app->warn("Binary WebSocket message not handled");
}
void SslServer::socketDisconnected()
{
g_app->log("WebSocket Client disconnected");
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
WebSocketClient *wsClient = SslServer::findClientForSocket(pClient);
if (wsClient) {
m_clients.removeAll(wsClient);
wsClient->deleteLater();
}
g_app->status(QString::number(m_clients.length()) + QString(" clients connected"));
}
void SslServer::onSslErrors(const QList<QSslError> &)
{
g_app->error("SSL errors occurred");
}
WebSocketClient *SslServer::findClientForSocket(QWebSocket* socket)
{
for (int i = 0; i < m_clients.size(); ++i)
if (m_clients.at(i)->pWebSocket == socket)
return m_clients.at(i);
return nullptr;
}