]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/other/radius/radius.cpp
Update async-radius.
[stg.git] / projects / stargazer / plugins / other / radius / radius.cpp
index 78c80b4df9e32a2d832038f2da5acf6711d7a494..8e5b37a895edc3f5b2cdb195087faf5787276721 100644 (file)
@@ -1,15 +1,12 @@
 #include "radius.h"
 #include "radius.h"
-#include "server.h"
 #include "radproto/error.h"
 #include "radproto/error.h"
-
 #include "stg/common.h"
 
 #include "stg/common.h"
 
-#include <boost/asio.hpp>
-#include <string>
+#include <iterator>
 #include <iostream>
 #include <iostream>
-#include <cstdint> //uint8_t, uint32_t
 
 using STG::RADIUS;
 
 using STG::RADIUS;
+using STG::RAD_SETTINGS;
 
 extern "C" STG::Plugin* GetPlugin()
 {
 
 extern "C" STG::Plugin* GetPlugin()
 {
@@ -17,15 +14,67 @@ extern "C" STG::Plugin* GetPlugin()
     return &plugin;
 }
 
     return &plugin;
 }
 
-std::string RADIUS::GetVersion() const
+RAD_SETTINGS::RAD_SETTINGS()
+    : m_port(1812),
+      m_dictionaries("/usr/share/freeradius/dictionary")
+{}
+
+int RAD_SETTINGS::ParseSettings(const ModuleSettings & s)
 {
 {
-    return "Radius v.1.0";
+    ParamValue pv;
+    int p;
+
+    pv.param = "Port";
+    auto pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
+    if (pvi != s.moduleParams.end() && !pvi->value.empty())
+    {
+        if (ParseIntInRange(pvi->value[0], 2, 65535, &p) != 0)
+        {
+            m_errorStr = "Cannot parse parameter \'Port\': " + m_errorStr;
+            printfd(__FILE__, "Cannot parse parameter 'Port'\n");
+            return -1;
+        }
+        m_port = static_cast<uint16_t>(p);
+    }
+
+    pv.param = "Secret";
+    pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
+    if (pvi == s.moduleParams.end() || pvi->value.empty())
+    {
+        m_errorStr = "Parameter \'Secret\' not found.";
+        printfd(__FILE__, "Parameter 'Secret' not found\n");
+        m_secret = "";
+    }
+    else
+    {
+        m_secret = pvi->value[0];
+    }
+
+    pv.param = "Dictionaries";
+    pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
+    if (pvi != s.moduleParams.end() && !pvi->value.empty())
+        m_dictionaries = pvi->value[0];
+    return 0;
 }
 
 RADIUS::RADIUS()
 }
 
 RADIUS::RADIUS()
-    : m_logger(PluginLogger::get("radius")),
-      m_running(false)
+    : m_running(false),
+      m_logger(PluginLogger::get("radius"))
+{
+}
+
+int RADIUS::ParseSettings()
+{
+    auto ret = m_radSettings.ParseSettings(m_settings);
+    if (ret != 0)
+        m_errorStr = m_radSettings.GetStrError();
+
+    return ret;
+}
+
+std::string RADIUS::GetVersion() const
 {
 {
+    return "Radius v.1.0";
 }
 
 int RADIUS::Start()
 }
 
 int RADIUS::Start()
@@ -41,20 +90,23 @@ int RADIUS::Stop()
 
     m_thread.request_stop();
 
 
     m_thread.request_stop();
 
+    if (m_server)
+        m_server->stop();
+
     m_thread.join();
     return 0;
 }
 
     m_thread.join();
     return 0;
 }
 
-void RADIUS::SetRunning(bool val)
+bool RADIUS::IsRunning()
 {
     const std::lock_guard lock(m_mutex);
 {
     const std::lock_guard lock(m_mutex);
-    m_running = val;
+    return m_running;
 }
 
 }
 
-bool RADIUS::IsRunning()
+void RADIUS::SetRunning(bool val)
 {
     const std::lock_guard lock(m_mutex);
 {
     const std::lock_guard lock(m_mutex);
-    return m_running;
+    m_running = val;
 }
 
 int RADIUS::Run(std::stop_token token)
 }
 
 int RADIUS::Run(std::stop_token token)
@@ -63,9 +115,9 @@ int RADIUS::Run(std::stop_token token)
 
     try
     {
 
     try
     {
-        boost::asio::io_service ioService;
-        Server server(ioService, "secret", 1812, "/usr/share/freeradius/dictionary");
-        ioService.run();
+        if (!m_server)
+           m_server = std::make_unique<Server>(m_ioService, m_radSettings.GetSecret(), m_radSettings.GetPort(), m_radSettings.GetDictionaries(), std::move(token), m_logger);
+        m_ioService.run();
     }
     catch (const std::exception& e)
     {
     }
     catch (const std::exception& e)
     {