Fix dosguard max_connection (#927)

Fix #928
This commit is contained in:
cyan317
2023-10-13 13:10:33 +01:00
committed by GitHub
parent ff3d2b5600
commit 939740494b
3 changed files with 35 additions and 5 deletions

View File

@@ -187,11 +187,16 @@ public:
if (boost::beast::websocket::is_upgrade(req_))
{
// Disable the timeout. The websocket::stream uses its own timeout settings.
boost::beast::get_lowest_layer(derived().stream()).expires_never();
if (dosGuard_.get().isOk(this->clientIp))
{
// Disable the timeout. The websocket::stream uses its own timeout settings.
boost::beast::get_lowest_layer(derived().stream()).expires_never();
upgraded = true;
return derived().upgrade();
upgraded = true;
return derived().upgrade();
}
return sender_(httpResponse(http::status::too_many_requests, "text/html", "Too many requests"));
}
if (req_.method() != http::verb::post)

View File

@@ -60,10 +60,10 @@ protected:
void
wsFail(boost::beast::error_code ec, char const* what)
{
LOG(perfLog_.error()) << tag() << ": " << what << ": " << ec.message();
if (!ec_ && ec != boost::asio::error::operation_aborted)
{
ec_ = ec;
LOG(perfLog_.error()) << tag() << ": " << what << ": " << ec.message();
boost::beast::get_lowest_layer(derived().ws()).socket().close(ec);
(*handler_)(ec, derived().shared_from_this());
}

View File

@@ -361,6 +361,31 @@ TEST_F(WebServerTest, WsPayloadOverload)
R"({"payload":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","warning":"load","warnings":[{"id":2003,"message":"You are about to be rate limited"}]})");
}
TEST_F(WebServerTest, WsTooManyConnection)
{
auto e = std::make_shared<EchoExecutor>();
auto server = makeServerSync(cfg, ctx, std::nullopt, dosGuardOverload, e);
// max connection is 2, exception should happen when the third connection is made
WebSocketSyncClient wsClient1;
wsClient1.connect("localhost", "8888");
WebSocketSyncClient wsClient2;
wsClient2.connect("localhost", "8888");
bool exceptionThrown = false;
try
{
WebSocketSyncClient wsClient3;
wsClient3.connect("localhost", "8888");
}
catch (boost::system::system_error const& ex)
{
exceptionThrown = true;
EXPECT_EQ(ex.code(), boost::beast::websocket::error::upgrade_declined);
}
wsClient1.disconnect();
wsClient2.disconnect();
EXPECT_TRUE(exceptionThrown);
}
static auto constexpr JSONServerConfigWithAdminPassword = R"JSON(
{
"server":{