Websocket re-architecture with websocketd and websocat (#89)

- Replaced beast with websocketd and websocat. #79 #83 #84
- Implemented inbound/outbound peer connection merging.
- Added graceful shutdown of hpcore with sigint. #87
This commit is contained in:
Ravin Perera
2020-04-05 08:12:55 +05:30
committed by GitHub
parent 1904c1800a
commit 920be03ade
60 changed files with 1786 additions and 1753 deletions

View File

@@ -1,14 +1,11 @@
#include "../pchheader.hpp"
#include "../hplog.hpp"
#include "../jsonschema/usrmsg_helpers.hpp"
#include "../sock/socket_session.hpp"
#include "../sock/socket_message.hpp"
#include "../bill/corebill.h"
#include "usr.hpp"
#include "user_session_handler.hpp"
namespace net = boost::asio;
namespace beast = boost::beast;
namespace jusrmsg = jsonschema::usrmsg;
namespace usr
{
@@ -16,89 +13,81 @@ namespace usr
/**
* This gets hit every time a client connects to HP via the public port (configured in contract config).
*/
void user_session_handler::on_connect(sock::socket_session<user_outbound_message> *session)
int user_session_handler::on_connect(comm::comm_session &session) const
{
if (conf::cfg.pubmaxcons > 0 && ctx.users.size() >= conf::cfg.pubmaxcons)
{
session->close();
LOG_DBG << "Max user connections reached. Dropped connection " << session->uniqueid;
return;
LOG_DBG << "Max user connections reached. Dropped connection " << session.uniqueid;
return -1;
}
LOG_DBG << "User client connected " << session->uniqueid;
LOG_DBG << "User client connected " << session.uniqueid;
// As soon as a user connects, we issue them a challenge message. We remember the
// challenge we issued and later verifies the user's response with it.
session->send(
user_outbound_message(issue_challenge(session->uniqueid)));
std::string msgstr;
jusrmsg::create_user_challenge(msgstr, session.issued_challenge);
session.send(msgstr);
// Set the challenge-issued flag to help later checks in on_message.
session->flags.set(sock::SESSION_FLAG::USER_CHALLENGE_ISSUED);
session.flags.set(comm::SESSION_FLAG::USER_CHALLENGE_ISSUED);
return 0;
}
/**
* This gets hit every time we receive some data from a client connected to the HP public port.
*/
void user_session_handler::on_message(
sock::socket_session<user_outbound_message> *session,
std::string_view message)
int user_session_handler::on_message(comm::comm_session &session, std::string_view message) const
{
// First check whether this session is pending challenge.
// Meaning we have previously issued a challenge to the client,
if (session->flags[sock::SESSION_FLAG::USER_CHALLENGE_ISSUED])
if (session.flags[comm::SESSION_FLAG::USER_CHALLENGE_ISSUED])
{
if (verify_challenge(message, session) == 0)
return;
return 0;
}
// Check whether this session belongs to an authenticated (challenge-verified) user.
else if (session->flags[sock::SESSION_FLAG::USER_AUTHED])
else if (session.flags[comm::SESSION_FLAG::USER_AUTHED])
{
// Check whether this user is among authenticated users
// and perform authenticated msg processing.
const auto itr = ctx.users.find(session->uniqueid);
const auto itr = ctx.users.find(session.uniqueid);
if (itr != ctx.users.end())
{
// This is an authed user.
connected_user &user = itr->second;
if (handle_user_message(user, message) != 0)
{
session->increment_metric(sock::SESSION_THRESHOLDS::MAX_BADMSGS_PER_MINUTE, 1);
LOG_DBG << "Bad message from user " << session->uniqueid;
session.increment_metric(comm::SESSION_THRESHOLDS::MAX_BADMSGS_PER_MINUTE, 1);
LOG_DBG << "Bad message from user " << session.uniqueid;
}
}
else
{
session->increment_metric(sock::SESSION_THRESHOLDS::MAX_BADMSGS_PER_MINUTE, 1);
LOG_DBG << "User session id not found: " << session->uniqueid;
session.increment_metric(comm::SESSION_THRESHOLDS::MAX_BADMSGS_PER_MINUTE, 1);
LOG_DBG << "User session id not found: " << session.uniqueid;
}
return;
return 0;
}
// If for any reason we reach this point, we should drop the connection because none of the
// valid cases match.
session->close();
LOG_DBG << "Dropped the user connection " << session->uniqueid;
corebill::report_violation(session->address);
LOG_DBG << "Dropping the user connection " << session.uniqueid;
corebill::report_violation(session.address);
return -1;
}
/**
* This gets hit every time a client disconnects from the HP public port.
*/
void user_session_handler::on_close(sock::socket_session<user_outbound_message> *session)
void user_session_handler::on_close(const comm::comm_session &session) const
{
// Cleanup any resources related to this session.
// Session is awaiting challenge response.
if (session->flags[sock::SESSION_FLAG::USER_CHALLENGE_ISSUED])
ctx.pending_challenges.erase(session->uniqueid);
// Session belongs to an authed user.
else if (session->flags[sock::SESSION_FLAG::USER_AUTHED])
remove_user(session->uniqueid);
LOG_DBG << "User disconnected " << session->uniqueid;
if (session.flags[comm::SESSION_FLAG::USER_AUTHED])
remove_user(session.uniqueid);
}
} // namespace usr