Core/Network: Socket refactors
* Devirtualize calls to Read and Update by marking concrete implementations as final * Removed derived class template argument * Specialize boost::asio::basic_stream_socket for boost::asio::io_context instead of type-erased any_io_executor * Make socket initialization easier composable (before entering Read loop) * Remove use of deprecated boost::asio::null_buffers and boost::beast::ssl_stream
This commit is contained in:
+42
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "IpBanCheckConnectionInitializer.h"
|
||||
#include "DatabaseEnv.h"
|
||||
|
||||
QueryCallback Trinity::Net::IpBanCheckHelpers::AsyncQuery(std::string_view ipAddress)
|
||||
{
|
||||
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_IP_INFO);
|
||||
stmt->setString(0, ipAddress);
|
||||
return LoginDatabase.AsyncQuery(stmt);
|
||||
}
|
||||
|
||||
bool Trinity::Net::IpBanCheckHelpers::IsBanned(PreparedQueryResult const& result)
|
||||
{
|
||||
if (result)
|
||||
{
|
||||
do
|
||||
{
|
||||
Field* fields = result->Fetch();
|
||||
if (fields[0].GetUInt64() != 0)
|
||||
return true;
|
||||
|
||||
} while (result->NextRow());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef TRINITYCORE_IP_BAN_CHECK_CONNECTION_INITIALIZER_H
|
||||
#define TRINITYCORE_IP_BAN_CHECK_CONNECTION_INITIALIZER_H
|
||||
|
||||
#include "DatabaseEnvFwd.h"
|
||||
#include "Log.h"
|
||||
#include "QueryCallback.h"
|
||||
#include "SocketConnectionInitializer.h"
|
||||
|
||||
namespace Trinity::Net
|
||||
{
|
||||
namespace IpBanCheckHelpers
|
||||
{
|
||||
TC_SHARED_API QueryCallback AsyncQuery(std::string_view ipAddress);
|
||||
TC_SHARED_API bool IsBanned(PreparedQueryResult const& result);
|
||||
}
|
||||
|
||||
template <typename SocketImpl>
|
||||
struct IpBanCheckConnectionInitializer final : SocketConnectionInitializer
|
||||
{
|
||||
explicit IpBanCheckConnectionInitializer(SocketImpl* socket) : _socket(socket) { }
|
||||
|
||||
void Start() override
|
||||
{
|
||||
_socket->QueueQuery(IpBanCheckHelpers::AsyncQuery(_socket->GetRemoteIpAddress().to_string()).WithPreparedCallback([socketRef = _socket->weak_from_this(), self = this->shared_from_this()](PreparedQueryResult const& result)
|
||||
{
|
||||
std::shared_ptr<SocketImpl> socket = static_pointer_cast<SocketImpl>(socketRef.lock());
|
||||
if (!socket)
|
||||
return;
|
||||
|
||||
if (IpBanCheckHelpers::IsBanned(result))
|
||||
{
|
||||
TC_LOG_ERROR("network", "IpBanCheckConnectionInitializer: IP {} is banned.", socket->GetRemoteIpAddress().to_string());
|
||||
socket->DelayedCloseSocket();
|
||||
return;
|
||||
}
|
||||
|
||||
if (self->next)
|
||||
self->next->Start();
|
||||
}));
|
||||
}
|
||||
|
||||
private:
|
||||
SocketImpl* _socket;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // TRINITYCORE_IP_BAN_CHECK_CONNECTION_INITIALIZER_H
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef TRINITYCORE_SOCKET_CONNECTION_INITIALIZER_H
|
||||
#define TRINITYCORE_SOCKET_CONNECTION_INITIALIZER_H
|
||||
|
||||
#include <memory>
|
||||
#include <span>
|
||||
|
||||
namespace Trinity::Net
|
||||
{
|
||||
struct SocketConnectionInitializer : public std::enable_shared_from_this<SocketConnectionInitializer>
|
||||
{
|
||||
SocketConnectionInitializer() = default;
|
||||
|
||||
SocketConnectionInitializer(SocketConnectionInitializer const&) = delete;
|
||||
SocketConnectionInitializer(SocketConnectionInitializer&&) noexcept = default;
|
||||
SocketConnectionInitializer& operator=(SocketConnectionInitializer const&) = delete;
|
||||
SocketConnectionInitializer& operator=(SocketConnectionInitializer&&) noexcept = default;
|
||||
|
||||
virtual ~SocketConnectionInitializer() = default;
|
||||
|
||||
virtual void Start() = 0;
|
||||
|
||||
std::shared_ptr<SocketConnectionInitializer> next;
|
||||
|
||||
static std::shared_ptr<SocketConnectionInitializer>& SetupChain(std::span<std::shared_ptr<SocketConnectionInitializer>> initializers)
|
||||
{
|
||||
for (std::size_t i = initializers.size(); i > 1; --i)
|
||||
initializers[i - 2]->next.swap(initializers[i - 1]);
|
||||
|
||||
return initializers[0];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif // TRINITYCORE_SOCKET_CONNECTION_INITIALIZER_H
|
||||
Reference in New Issue
Block a user