2018-04-17 08:01:49 -05:00
|
|
|
/* -*- 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>
|
2018-05-04 09:08:32 -05:00
|
|
|
#include <Poco/Util/XMLConfiguration.h>
|
2018-04-17 08:01:49 -05:00
|
|
|
|
|
|
|
#include <Log.hpp>
|
|
|
|
#include <Util.hpp>
|
|
|
|
#include <Protocol.hpp>
|
|
|
|
#include <ServerSocket.hpp>
|
|
|
|
#include <WebSocketHandler.hpp>
|
2020-10-26 07:55:54 -05:00
|
|
|
#if !MOBILEAPP
|
|
|
|
#include <net/HttpHelper.hpp>
|
|
|
|
#endif
|
2018-04-17 08:01:49 -05:00
|
|
|
#if ENABLE_SSL
|
|
|
|
# include <SslSocket.hpp>
|
|
|
|
#endif
|
|
|
|
|
2018-04-18 13:20:54 -05:00
|
|
|
SocketPoll DumpSocketPoll("websocket");
|
|
|
|
|
2018-04-17 08:01:49 -05:00
|
|
|
// Dumps incoming websocket messages and doesn't respond.
|
|
|
|
class DumpSocketHandler : public WebSocketHandler
|
|
|
|
{
|
|
|
|
public:
|
2018-04-18 13:20:54 -05:00
|
|
|
DumpSocketHandler(const std::weak_ptr<StreamSocket>& socket,
|
2021-03-21 14:58:10 -05:00
|
|
|
const Poco::Net::HTTPRequest& request)
|
|
|
|
: WebSocketHandler(socket.lock(), request)
|
2018-04-17 08:01:49 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// Process incoming websocket messages
|
2020-03-06 02:38:09 -06:00
|
|
|
void handleMessage(const std::vector<char> &data) override
|
2018-04-17 08:01:49 -05:00
|
|
|
{
|
2020-03-06 02:38:09 -06:00
|
|
|
std::cout << "WebSocket message data:\n";
|
2021-03-11 12:12:20 -06:00
|
|
|
Util::dumpHex(std::cout, data, "", " ", false);
|
2018-04-17 08:01:49 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Handles incoming connections and dispatches to the appropriate handler.
|
2020-10-31 14:08:08 -05:00
|
|
|
class ClientRequestDispatcher final : public SimpleSocketHandler
|
2018-04-17 08:01:49 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ClientRequestDispatcher()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/// Set the socket associated with this ResponseClient.
|
|
|
|
void onConnect(const std::shared_ptr<StreamSocket>& socket) override
|
|
|
|
{
|
|
|
|
_socket = socket;
|
2020-05-24 08:10:18 -05:00
|
|
|
LOG_TRC('#' << socket->getFD() << " Connected to ClientRequestDispatcher.");
|
2018-04-17 08:01:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Called after successful socket reads.
|
2018-04-18 13:20:54 -05:00
|
|
|
void handleIncomingMessage(SocketDisposition &disposition) override
|
2018-04-17 08:01:49 -05:00
|
|
|
{
|
|
|
|
std::shared_ptr<StreamSocket> socket = _socket.lock();
|
2020-04-19 13:46:01 -05:00
|
|
|
if (!socket)
|
|
|
|
{
|
|
|
|
LOG_ERR("Invalid socket while reading client message.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-25 11:05:54 -05:00
|
|
|
Buffer& in = socket->getInBuffer();
|
2020-05-24 08:10:18 -05:00
|
|
|
LOG_TRC('#' << socket->getFD() << " handling incoming " << in.size() << " bytes.");
|
2018-04-17 08:01:49 -05:00
|
|
|
|
|
|
|
// 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())
|
|
|
|
{
|
2020-05-24 08:10:18 -05:00
|
|
|
LOG_DBG('#' << socket->getFD() << " doesn't have enough data yet.");
|
2018-04-17 08:01:49 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip the marker.
|
|
|
|
itBody += marker.size();
|
|
|
|
|
|
|
|
Poco::MemoryInputStream message(&in[0], in.size());
|
|
|
|
Poco::Net::HTTPRequest request;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
request.read(message);
|
|
|
|
|
|
|
|
Log::StreamLogger logger = Log::info();
|
|
|
|
if (logger.enabled())
|
|
|
|
{
|
2020-05-24 08:10:18 -05:00
|
|
|
logger << '#' << socket->getFD() << ": Client HTTP Request: "
|
2018-04-17 08:01:49 -05:00
|
|
|
<< request.getMethod() << ' '
|
|
|
|
<< request.getURI() << ' '
|
|
|
|
<< request.getVersion();
|
|
|
|
|
|
|
|
for (const auto& it : request)
|
|
|
|
{
|
|
|
|
logger << " / " << it.first << ": " << it.second;
|
|
|
|
}
|
|
|
|
|
2018-06-04 09:23:58 -05:00
|
|
|
LOG_END(logger, true);
|
2018-04-17 08:01:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2020-05-21 09:22:49 -05:00
|
|
|
StringVector pathTokens(Util::tokenize(requestURI, '/'));
|
2021-03-22 15:58:35 -05:00
|
|
|
if (request.find("Upgrade") != request.end()
|
|
|
|
&& Util::iequal(request["Upgrade"], "websocket"))
|
2018-04-18 13:20:54 -05:00
|
|
|
{
|
2018-05-03 11:52:35 -05:00
|
|
|
auto dumpHandler = std::make_shared<DumpSocketHandler>(_socket, request);
|
|
|
|
socket->setHandler(dumpHandler);
|
|
|
|
dumpHandler->sendMessage("version");
|
|
|
|
dumpHandler->sendMessage("documents");
|
2018-04-18 13:20:54 -05:00
|
|
|
}
|
2018-04-17 08:01:49 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST);
|
|
|
|
response.setContentLength(0);
|
|
|
|
LOG_INF("DumpWebSockets bad request");
|
|
|
|
socket->send(response);
|
2018-04-18 13:20:54 -05:00
|
|
|
disposition.setClosed();
|
2018-04-17 08:01:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
|
|
|
// Bad request.
|
2020-10-26 07:55:54 -05:00
|
|
|
HttpHelper::sendErrorAndShutdown(400, socket);
|
2018-04-17 08:01:49 -05:00
|
|
|
|
|
|
|
// NOTE: Check _wsState to choose between HTTP response or WebSocket (app-level) error.
|
2020-05-24 08:10:18 -05:00
|
|
|
LOG_INF('#' << socket->getFD() << " Exception while processing incoming request: [" <<
|
2018-04-17 08:01:49 -05:00
|
|
|
LOOLProtocol::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);
|
|
|
|
}
|
|
|
|
|
2020-04-10 06:34:15 -05:00
|
|
|
int getPollEvents(std::chrono::steady_clock::time_point /* now */,
|
|
|
|
int64_t & /* timeoutMaxMicroS */) override
|
2018-04-17 08:01:49 -05:00
|
|
|
{
|
|
|
|
return POLLIN;
|
|
|
|
}
|
|
|
|
|
2021-03-07 11:57:13 -06:00
|
|
|
void performWrites(std::size_t /*capacity*/) override {}
|
2018-04-17 08:01:49 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
// The socket that owns us (we can't own it).
|
|
|
|
std::weak_ptr<StreamSocket> _socket;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DumpSocketFactory final : public SocketFactory
|
|
|
|
{
|
2018-05-04 09:08:32 -05:00
|
|
|
private:
|
|
|
|
bool _isSSL = false;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DumpSocketFactory(bool isSSL) : _isSSL(isSSL) {}
|
|
|
|
|
2018-04-17 08:01:49 -05:00
|
|
|
std::shared_ptr<Socket> create(const int physicalFd) override
|
|
|
|
{
|
2018-05-03 12:03:56 -05:00
|
|
|
#if ENABLE_SSL
|
2018-05-04 09:08:32 -05:00
|
|
|
if (_isSSL)
|
2021-06-27 10:38:59 -05:00
|
|
|
return StreamSocket::create<SslStreamSocket>(
|
|
|
|
std::string(), physicalFd, false, std::make_shared<ClientRequestDispatcher>());
|
2018-06-01 08:42:37 -05:00
|
|
|
#else
|
2021-06-27 10:38:59 -05:00
|
|
|
(void)_isSSL;
|
2018-04-17 08:01:49 -05:00
|
|
|
#endif
|
2021-06-27 10:38:59 -05:00
|
|
|
return StreamSocket::create<StreamSocket>(std::string(), physicalFd, false,
|
|
|
|
std::make_shared<ClientRequestDispatcher>());
|
2018-04-17 08:01:49 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace Util
|
|
|
|
{
|
|
|
|
void alertAllUsers(const std::string& cmd, const std::string& kind)
|
|
|
|
{
|
|
|
|
std::cout << "error: cmd=" << cmd << " kind=" << kind << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 07:47:53 -06:00
|
|
|
class CoolConfig final: public Poco::Util::XMLConfiguration
|
2018-05-04 09:08:32 -05:00
|
|
|
{
|
|
|
|
public:
|
2021-11-15 07:47:53 -06:00
|
|
|
CoolConfig()
|
2018-05-04 09:08:32 -05:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2018-04-17 08:01:49 -05:00
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
(void) argc; (void) argv;
|
|
|
|
|
2018-05-01 11:50:13 -05:00
|
|
|
if (!UnitWSD::init(UnitWSD::UnitType::Wsd, ""))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to load wsd unit test library.");
|
|
|
|
}
|
|
|
|
|
2018-04-18 13:20:54 -05:00
|
|
|
Log::initialize("WebSocketDump", "trace", true, false,
|
|
|
|
std::map<std::string, std::string>());
|
|
|
|
|
2021-11-15 07:47:53 -06:00
|
|
|
CoolConfig config;
|
2018-05-04 09:08:32 -05:00
|
|
|
config.load("loolwsd.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");
|
|
|
|
}
|
|
|
|
|
2018-05-03 12:03:56 -05:00
|
|
|
#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.
|
2018-05-04 09:08:32 -05:00
|
|
|
if (isSSL)
|
2021-06-23 20:30:28 -05:00
|
|
|
ssl::Manager::initializeServerContext(ssl_cert_file_path, ssl_key_file_path,
|
2021-06-27 14:22:17 -05:00
|
|
|
ssl_ca_file_path, ssl_cipher_list,
|
|
|
|
ssl::CertificateVerification::Disabled);
|
2018-05-03 12:03:56 -05:00
|
|
|
#endif
|
|
|
|
|
2018-04-17 08:01:49 -05:00
|
|
|
SocketPoll acceptPoll("accept");
|
|
|
|
|
|
|
|
// Setup listening socket with a factory for connected sockets.
|
|
|
|
auto serverSocket = std::make_shared<ServerSocket>(
|
2018-04-18 13:20:54 -05:00
|
|
|
Socket::Type::All, DumpSocketPoll,
|
2018-05-04 09:08:32 -05:00
|
|
|
std::make_shared<DumpSocketFactory>(isSSL));
|
2018-04-17 08:01:49 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2020-12-14 14:18:01 -06:00
|
|
|
DumpSocketPoll.poll(std::chrono::seconds(100));
|
2018-04-17 08:01:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|