From: Maksym Mamontov Date: Mon, 27 Jan 2025 15:03:37 +0000 (+0200) Subject: Merge remote-tracking branch 'github/master' X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/46d0fa38003ef0b122fea77dfaa252fa832cb5a3?hp=9e321f1d39023f4ba86cd354eda0c347ac15fca2 Merge remote-tracking branch 'github/master' --- diff --git a/projects/stargazer/plugins/CMakeLists.txt b/projects/stargazer/plugins/CMakeLists.txt index a53349ea..b6e4d326 100644 --- a/projects/stargazer/plugins/CMakeLists.txt +++ b/projects/stargazer/plugins/CMakeLists.txt @@ -14,7 +14,8 @@ endif ( BUILD_MOD_AO ) if ( BUILD_MOD_RADIUS ) find_package ( Boost REQUIRED ) - add_library ( mod_radius MODULE other/radius/radius.cpp ) + add_library ( mod_radius MODULE other/radius/radius.cpp + other/radius/server.cpp) target_link_libraries ( mod_radius PRIVATE scriptexecuter logger common ) set_target_properties ( mod_radius PROPERTIES PREFIX "" ) @@ -34,7 +35,8 @@ if ( BUILD_MOD_RADIUS ) add_dependencies ( mod_radius async-radius ) - target_link_libraries ( mod_radius PRIVATE radproto Boost::boost ) + find_package ( OpenSSL 1.0.0 REQUIRED ) + target_link_libraries ( mod_radius PRIVATE radproto Boost::boost OpenSSL::Crypto) if ( CLANG_TIDY_EXE ) set_target_properties ( mod_radius PROPERTIES CXX_CLANG_TIDY "${DO_CLANG_TIDY}" ) diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp index 3ec0f705..78c80b4d 100644 --- a/projects/stargazer/plugins/other/radius/radius.cpp +++ b/projects/stargazer/plugins/other/radius/radius.cpp @@ -1,4 +1,13 @@ #include "radius.h" +#include "server.h" +#include "radproto/error.h" + +#include "stg/common.h" + +#include +#include +#include +#include //uint8_t, uint32_t using STG::RADIUS; @@ -14,6 +23,57 @@ std::string RADIUS::GetVersion() const } RADIUS::RADIUS() + : m_logger(PluginLogger::get("radius")), + m_running(false) +{ +} + +int RADIUS::Start() +{ + m_thread = std::jthread([this](auto token){ Run(std::move(token)); }); + return 0; +} + +int RADIUS::Stop() +{ + if (!m_thread.joinable()) + return 0; + + m_thread.request_stop(); + + m_thread.join(); + return 0; +} + +void RADIUS::SetRunning(bool val) +{ + const std::lock_guard lock(m_mutex); + m_running = val; +} + +bool RADIUS::IsRunning() { + const std::lock_guard lock(m_mutex); + return m_running; } +int RADIUS::Run(std::stop_token token) +{ + SetRunning(true); + + try + { + boost::asio::io_service ioService; + Server server(ioService, "secret", 1812, "/usr/share/freeradius/dictionary"); + ioService.run(); + } + catch (const std::exception& e) + { + m_errorStr = "Exception in RADIUS::Run(): " + std::string(e.what()); + m_logger("Exception in RADIUS:: Run(): %s", e.what()); + printfd(__FILE__, "Exception in RADIUS:: Run(). Message: '%s'\n", e.what()); + } + + SetRunning(false); + return 0; +} diff --git a/projects/stargazer/plugins/other/radius/radius.h b/projects/stargazer/plugins/other/radius/radius.h index 4cc84e26..4e06e463 100644 --- a/projects/stargazer/plugins/other/radius/radius.h +++ b/projects/stargazer/plugins/other/radius/radius.h @@ -1,7 +1,11 @@ #pragma once #include "stg/auth.h" +#include "stg/logger.h" + #include +#include +#include namespace STG { @@ -10,12 +14,13 @@ namespace STG public: RADIUS(); - int Start() override { return 0; } - int Stop() override { return 0; } + int Start() override; + int Stop() override; int Reload(const ModuleSettings & /*ms*/) override { return 0; } - bool IsRunning() override { return isRunning; } + bool IsRunning() override; + void SetRunning(bool val); int ParseSettings() override { return 0; } - const std::string & GetStrError() const override { return errorStr; } + const std::string & GetStrError() const override { return m_errorStr; } std::string GetVersion() const override; uint16_t GetStartPosition() const override { return 0; } uint16_t GetStopPosition() const override { return 0; } @@ -23,8 +28,12 @@ namespace STG int SendMessage(const Message & msg, uint32_t ip) const override { return 0; } private: - mutable std::string errorStr; - bool isRunning; + std::mutex m_mutex; + mutable std::string m_errorStr; + std::jthread m_thread; + bool m_running; + PluginLogger m_logger; + int Run(std::stop_token token); }; } diff --git a/projects/stargazer/plugins/other/radius/server.cpp b/projects/stargazer/plugins/other/radius/server.cpp new file mode 100644 index 00000000..4d16b6cb --- /dev/null +++ b/projects/stargazer/plugins/other/radius/server.cpp @@ -0,0 +1,68 @@ +#include "server.h" +#include "radproto/packet_codes.h" +#include +#include + +using STG::Server; +using boost::system::error_code; + +Server::Server(boost::asio::io_service& io_service, const std::string& secret, uint16_t port, const std::string& filePath) + : m_radius(io_service, secret, port), + m_dictionaries(filePath) +{ + startReceive(); +} + +void Server::startReceive() +{ + m_radius.asyncReceive([this](const auto& error, const auto& packet, const boost::asio::ip::udp::endpoint& source){ handleReceive(error, packet, source); }); +} + +RadProto::Packet Server::makeResponse(const RadProto::Packet& request) +{ + std::vector attributes; + attributes.push_back(new RadProto::String(m_dictionaries.attributeCode("User-Name"), "test")); + attributes.push_back(new RadProto::Integer(m_dictionaries.attributeCode("NAS-Port"), 20)); + std::array address {127, 104, 22, 17}; + attributes.push_back(new RadProto::IpAddress(m_dictionaries.attributeCode("NAS-IP-Address"), address)); + std::vector bytes {'1', '2', '3', 'a', 'b', 'c'}; + attributes.push_back(new RadProto::Bytes(m_dictionaries.attributeCode("Callback-Number"), bytes)); + std::vector chapPassword {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g' }; + attributes.push_back(new RadProto::ChapPassword(m_dictionaries.attributeCode("CHAP-Password"), 1, chapPassword)); + + std::vector vendorSpecific; + std::vector vendorValue {0, 0, 0, 3}; + vendorSpecific.push_back(RadProto::VendorSpecific(m_dictionaries.vendorCode("Dlink"), m_dictionaries.vendorAttributeCode("Dlink", "Dlink-User-Level"), vendorValue)); + + if (request.type() == RadProto::ACCESS_REQUEST) + return RadProto::Packet(RadProto::ACCESS_ACCEPT, request.id(), request.auth(), attributes, vendorSpecific); + + return RadProto::Packet(RadProto::ACCESS_REJECT, request.id(), request.auth(), attributes, vendorSpecific); +} + +void Server::handleSend(const error_code& ec) +{ + if (ec) + std::cout << "Error asyncSend: " << ec.message() << "\n"; + + startReceive(); +} + +void Server::handleReceive(const error_code& error, const std::optional& packet, const boost::asio::ip::udp::endpoint& source) +{ + if (error) + { + std::cout << "Error asyncReceive: " << error.message() << "\n"; + return; + } + + if (packet == std::nullopt) + { + std::cout << "Error asyncReceive: the request packet is missing\n"; + return; + } + else + { + m_radius.asyncSend(makeResponse(*packet), source, [this](const auto& ec){ handleSend(ec); }); + } +} diff --git a/projects/stargazer/plugins/other/radius/server.h b/projects/stargazer/plugins/other/radius/server.h new file mode 100644 index 00000000..b88889be --- /dev/null +++ b/projects/stargazer/plugins/other/radius/server.h @@ -0,0 +1,26 @@ +#pragma once + +#include "radproto/socket.h" +#include "radproto/packet.h" +#include "radproto/dictionaries.h" +#include +#include +#include //uint8_t, uint32_t + +namespace STG +{ + class Server + { + public: + Server(boost::asio::io_service& io_service, const std::string& secret, uint16_t port, const std::string& filePath); + + private: + RadProto::Packet makeResponse(const RadProto::Packet& request); + void handleReceive(const boost::system::error_code& error, const std::optional& packet, const boost::asio::ip::udp::endpoint& source); + void handleSend(const boost::system::error_code& ec); + void startReceive(); + + RadProto::Socket m_radius; + RadProto::Dictionaries m_dictionaries; + }; +}