forked from CollaboraOnline/online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebSocketDump.cpp
291 lines (245 loc) · 9 KB
/
WebSocketDump.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/* A simple tool that accepts web-socket connections and dumps the contents */
#include <config.h>
#include <unistd.h>
#include <Poco/URI.h>
#include <Poco/MemoryStream.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Util/XMLConfiguration.h>
#include <Log.hpp>
#include <Util.hpp>
#include <Protocol.hpp>
#include <ServerSocket.hpp>
#include <WebSocketHandler.hpp>
#if !MOBILEAPP
#include <net/HttpHelper.hpp>
#endif
#if ENABLE_SSL
# include <SslSocket.hpp>
#endif
SocketPoll DumpSocketPoll("websocket");
// Dumps incoming websocket messages and doesn't respond.
class DumpSocketHandler : public WebSocketHandler
{
public:
DumpSocketHandler(const std::weak_ptr<StreamSocket>& socket,
const Poco::Net::HTTPRequest& request)
: WebSocketHandler(socket.lock(), request)
{
}
private:
/// Process incoming websocket messages
void handleMessage(const std::vector<char> &data) override
{
std::cout << "WebSocket message data:\n";
Util::dumpHex(std::cout, data, "", " ", false);
}
};
/// Handles incoming connections and dispatches to the appropriate handler.
class ClientRequestDispatcher final : public SimpleSocketHandler
{
public:
ClientRequestDispatcher()
{
}
private:
/// Set the socket associated with this ResponseClient.
void onConnect(const std::shared_ptr<StreamSocket>& socket) override
{
_socket = socket;
LOG_TRC('#' << socket->getFD() << " Connected to ClientRequestDispatcher.");
}
/// Called after successful socket reads.
void handleIncomingMessage(SocketDisposition &disposition) override
{
std::shared_ptr<StreamSocket> socket = _socket.lock();
if (!socket)
{
LOG_ERR("Invalid socket while reading client message.");
return;
}
Buffer& in = socket->getInBuffer();
LOG_TRC('#' << socket->getFD() << " handling incoming " << in.size() << " bytes.");
// Find the end of the header, if any.
static const std::string marker("\r\n\r\n");
auto itBody = std::search(in.begin(), in.end(),
marker.begin(), marker.end());
if (itBody == in.end())
{
LOG_DBG('#' << socket->getFD() << " doesn't have enough data yet.");
return;
}
// Skip the marker.
itBody += marker.size();
Poco::MemoryInputStream message(&in[0], in.size());
Poco::Net::HTTPRequest request;
try
{
request.read(message);
LOG_INF('#' << socket->getFD() << ": Client HTTP Request: " << request.getMethod()
<< ' ' << request.getURI() << ' ' << request.getVersion() <<
[&](auto& log)
{
for (const auto& it : request)
{
log << " / " << it.first << ": " << it.second;
}
});
const std::streamsize contentLength = request.getContentLength();
const auto offset = itBody - in.begin();
const std::streamsize available = in.size() - offset;
if (contentLength != Poco::Net::HTTPMessage::UNKNOWN_CONTENT_LENGTH && available < contentLength)
{
LOG_DBG("Not enough content yet: ContentLength: " << contentLength << ", available: " << available);
return;
}
}
catch (const std::exception& exc)
{
// Probably don't have enough data just yet.
// TODO: timeout if we never get enough.
return;
}
try
{
// Routing
Poco::URI requestUri(request.getURI());
std::vector<std::string> reqPathSegs;
requestUri.getPathSegments(reqPathSegs);
LOG_INF("Incoming websocket request: " << request.getURI());
const std::string& requestURI = request.getURI();
StringVector pathTokens(StringVector::tokenize(requestURI, '/'));
if (request.find("Upgrade") != request.end()
&& Util::iequal(request["Upgrade"], "websocket"))
{
auto dumpHandler = std::make_shared<DumpSocketHandler>(_socket, request);
socket->setHandler(dumpHandler);
dumpHandler->sendMessage("version");
dumpHandler->sendMessage("documents");
}
else
{
Poco::Net::HTTPResponse response;
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST);
response.setContentLength(0);
LOG_INF("DumpWebSockets bad request");
socket->send(response);
disposition.setClosed();
}
}
catch (const std::exception& exc)
{
// Bad request.
HttpHelper::sendErrorAndShutdown(400, socket);
// NOTE: Check _wsState to choose between HTTP response or WebSocket (app-level) error.
LOG_INF('#' << socket->getFD() << " Exception while processing incoming request: [" <<
COOLProtocol::getAbbreviatedMessage(in) << "]: " << exc.what());
}
// if we succeeded - remove the request from our input buffer
// we expect one request per socket
in.erase(in.begin(), itBody);
}
int getPollEvents(std::chrono::steady_clock::time_point /* now */,
int64_t & /* timeoutMaxMicroS */) override
{
return POLLIN;
}
void performWrites(std::size_t /*capacity*/) override {}
private:
// The socket that owns us (we can't own it).
std::weak_ptr<StreamSocket> _socket;
};
class DumpSocketFactory final : public SocketFactory
{
private:
bool _isSSL = false;
public:
DumpSocketFactory(bool isSSL) : _isSSL(isSSL) {}
std::shared_ptr<Socket> create(const int physicalFd) override
{
#if ENABLE_SSL
if (_isSSL)
return StreamSocket::create<SslStreamSocket>(
std::string(), physicalFd, false, std::make_shared<ClientRequestDispatcher>());
#else
(void)_isSSL;
#endif
return StreamSocket::create<StreamSocket>(std::string(), physicalFd, false,
std::make_shared<ClientRequestDispatcher>());
}
};
namespace Util
{
void alertAllUsers(const std::string& cmd, const std::string& kind)
{
std::cout << "error: cmd=" << cmd << " kind=" << kind << std::endl;
}
}
class CoolConfig final: public Poco::Util::XMLConfiguration
{
public:
CoolConfig()
{}
};
int main (int argc, char **argv)
{
(void) argc; (void) argv;
if (!UnitWSD::init(UnitWSD::UnitType::Wsd, ""))
{
throw std::runtime_error("Failed to load wsd unit test library.");
}
Log::initialize("WebSocketDump", "trace", true, false,
std::map<std::string, std::string>());
CoolConfig config;
config.load("coolwsd.xml");
// read the port & ssl support
int port = 9042;
bool isSSL = false;
std::string monitorAddress = config.getString("monitors.monitor");
if (!monitorAddress.empty())
{
Poco::URI monitorURI(monitorAddress);
port = monitorURI.getPort();
isSSL = (monitorURI.getScheme() == "wss");
}
#if ENABLE_SSL
// hard coded but easy for now.
const std::string ssl_cert_file_path = "etc/cert.pem";
const std::string ssl_key_file_path = "etc/key.pem";
const std::string ssl_ca_file_path = "etc/ca-chain.cert.pem";
const std::string ssl_cipher_list = "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH";
// Initialize the non-blocking socket SSL.
if (isSSL)
ssl::Manager::initializeServerContext(ssl_cert_file_path, ssl_key_file_path,
ssl_ca_file_path, ssl_cipher_list,
ssl::CertificateVerification::Disabled);
#endif
SocketPoll acceptPoll("accept");
// Setup listening socket with a factory for connected sockets.
auto serverSocket = std::make_shared<ServerSocket>(
Socket::Type::All, DumpSocketPoll,
std::make_shared<DumpSocketFactory>(isSSL));
if (!serverSocket->bind(ServerSocket::Type::Public, port))
{
fprintf(stderr, "Failed to bind websocket to port %d\n", port);
return -1;
}
if (!serverSocket->listen())
{
fprintf(stderr, "Failed to listen on websocket, port %d\n", port);
return -1;
}
acceptPoll.startThread();
acceptPoll.insertNewSocket(serverSocket);
while (true)
{
DumpSocketPoll.poll(std::chrono::seconds(100));
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */