2  *    This program is free software; you can redistribute it and/or modify
 
   3  *    it under the terms of the GNU General Public License as published by
 
   4  *    the Free Software Foundation; either version 2 of the License, or
 
   5  *    (at your option) any later version.
 
   7  *    This program is distributed in the hope that it will be useful,
 
   8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
   9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  10  *    GNU General Public License for more details.
 
  12  *    You should have received a copy of the GNU General Public License
 
  13  *    along with this program; if not, write to the Free Software
 
  14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
  18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
 
  19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
 
  24 #include "stg/plugin.h"
 
  25 #include "stg/module_settings.h"
 
  26 #include "stg/subscriptions.h"
 
  28 #include "stg/blowfish.h"
 
  29 #include "stg/rs_packets.h"
 
  30 #include "stg/logger.h"
 
  32 #include "nrmap_parser.h"
 
  40 #pragma GCC diagnostic push
 
  41 #pragma GCC diagnostic ignored "-Wshadow"
 
  42 #include <jthread.hpp>
 
  43 #pragma GCC diagnostic pop
 
  54 using UserPtr = User*;
 
  57 //-----------------------------------------------------------------------------
 
  60     USER(const std::vector<uint32_t> & r, UserPtr it)
 
  70     std::vector<uint32_t> routers;
 
  71     int shortPacketsCount;
 
  74 //-----------------------------------------------------------------------------
 
  79         virtual             ~SETTINGS() {}
 
  80         const std::string & GetStrError() const { return errorStr; }
 
  81         int                 ParseSettings(const ModuleSettings & s);
 
  82         int                 GetSendPeriod() const { return sendPeriod; }
 
  83         uint16_t            GetPort() const { return port; }
 
  84         const std::vector<NET_ROUTER> & GetSubnetsMap() const { return netRouters; }
 
  85         const std::vector<std::string> & GetUserParams() const { return userParams; }
 
  86         const std::string & GetPassword() const { return password; }
 
  87         const std::string & GetMapFileName() const { return subnetFile; }
 
  93         std::vector<NET_ROUTER> netRouters;
 
  94         std::vector<std::string> userParams;
 
  96         std::string         subnetFile;
 
  98 //-----------------------------------------------------------------------------
 
  99 class REMOTE_SCRIPT : public Plugin
 
 104         void                SetUsers(Users * u) override { users = u; }
 
 105         void                SetSettings(const ModuleSettings & s) override { settings = s; }
 
 106         int                 ParseSettings() override;
 
 108         int                 Start() override;
 
 110         int                 Reload(const ModuleSettings & ms) override;
 
 111         bool                IsRunning() override { return isRunning; }
 
 113         const std::string & GetStrError() const override { return errorStr; }
 
 114         std::string         GetVersion() const override { return "Remote script v 0.3"; }
 
 115         uint16_t            GetStartPosition() const override { return 10; }
 
 116         uint16_t            GetStopPosition() const override { return 10; }
 
 118         void                DelUser(UserPtr u) { UnSetUserNotifiers(u); }
 
 119         void                AddUser(UserPtr u) { SetUserNotifiers(u); }
 
 121         void                AddRSU(UserPtr user);
 
 122         void                DelRSU(UserPtr user);
 
 125         REMOTE_SCRIPT(const REMOTE_SCRIPT & rhs);
 
 126         REMOTE_SCRIPT & operator=(const REMOTE_SCRIPT & rhs);
 
 128         void                Run(std::stop_token token);
 
 132         bool                Send(USER & rsu, bool forceDisconnect = false) const;
 
 133         bool                SendDirect(USER & rsu, uint32_t routerIP, bool forceDisconnect = false) const;
 
 134         bool                PreparePacket(char * buf, size_t bufSize, USER &rsu, bool forceDisconnect = false) const;
 
 137         std::vector<uint32_t> IP2Routers(uint32_t ip);
 
 140         void                SetUserNotifiers(UserPtr u);
 
 141         void                UnSetUserNotifiers(UserPtr u);
 
 143         void                InitEncrypt(const std::string & password) const;
 
 144         void                Encrypt(void * dst, const void * src, size_t len8) const;
 
 146         mutable BLOWFISH_CTX ctx;
 
 147         std::map<uint32_t, USER> authorizedUsers;
 
 149         mutable std::string errorStr;
 
 151         ModuleSettings     settings;
 
 159         std::vector<NET_ROUTER> netRouters;
 
 161         std::jthread        m_thread;
 
 166         ScopedConnection m_onAddUserConn;
 
 167         ScopedConnection m_onDelUserConn;
 
 169         void addDelUser(UserPtr user, bool toAdd);
 
 171         using ConnHolder = std::tuple<int, ScopedConnection, ScopedConnection>;
 
 172         std::vector<ConnHolder> m_conns;
 
 176         friend class RS::UpdateRouter;
 
 178 //-----------------------------------------------------------------------------