X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/9e321f1d39023f4ba86cd354eda0c347ac15fca2..46d0fa38003ef0b122fea77dfaa252fa832cb5a3:/projects/stargazer/plugins/other/radius/server.cpp?ds=sidebyside 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); }); + } +}