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"
27 #include "stg/notifer.h"
29 #include "stg/blowfish.h"
30 #include "stg/rs_packets.h"
31 #include "stg/logger.h"
33 #include "nrmap_parser.h"
41 #pragma GCC diagnostic push
42 #pragma GCC diagnostic ignored "-Wshadow"
43 #include <jthread.hpp>
44 #pragma GCC diagnostic pop
60 using UserPtr = STG::User*;
62 //-----------------------------------------------------------------------------
63 class IP_NOTIFIER: public STG::PropertyNotifierBase<uint32_t> {
65 IP_NOTIFIER(REMOTE_SCRIPT & r, UserPtr u)
66 : user(u), rs(r) { user->AddCurrIPAfterNotifier(this); }
67 IP_NOTIFIER(const IP_NOTIFIER & rhs)
68 : user(rhs.user), rs(rhs.rs) { user->AddCurrIPAfterNotifier(this); }
69 ~IP_NOTIFIER() { user->DelCurrIPAfterNotifier(this); }
71 IP_NOTIFIER & operator=(const IP_NOTIFIER & rhs)
73 user->DelCurrIPAfterNotifier(this);
75 user->AddCurrIPAfterNotifier(this);
79 void notify(const uint32_t & oldValue, const uint32_t & newValue) override;
80 UserPtr GetUser() const { return user; }
87 //-----------------------------------------------------------------------------
88 class CONNECTED_NOTIFIER: public STG::PropertyNotifierBase<bool> {
90 CONNECTED_NOTIFIER(REMOTE_SCRIPT & r, UserPtr u)
91 : user(u), rs(r) { user->AddConnectedAfterNotifier(this); }
92 CONNECTED_NOTIFIER(const CONNECTED_NOTIFIER & rhs)
93 : user(rhs.user), rs(rhs.rs) { user->AddConnectedAfterNotifier(this); }
94 ~CONNECTED_NOTIFIER() { user->DelConnectedAfterNotifier(this); }
96 CONNECTED_NOTIFIER & operator=(const CONNECTED_NOTIFIER & rhs)
98 user->DelConnectedAfterNotifier(this);
100 user->AddConnectedAfterNotifier(this);
104 void notify(const bool & oldValue, const bool & newValue) override;
105 UserPtr GetUser() const { return user; }
112 //-----------------------------------------------------------------------------
114 USER(const std::vector<uint32_t> & r, UserPtr it)
118 shortPacketsCount(0),
119 ip(user->GetCurrIP())
124 std::vector<uint32_t> routers;
125 int shortPacketsCount;
128 //-----------------------------------------------------------------------------
132 virtual ~SETTINGS() {}
133 const std::string & GetStrError() const { return errorStr; }
134 int ParseSettings(const STG::ModuleSettings & s);
135 int GetSendPeriod() const { return sendPeriod; }
136 uint16_t GetPort() const { return port; }
137 const std::vector<NET_ROUTER> & GetSubnetsMap() const { return netRouters; }
138 const std::vector<std::string> & GetUserParams() const { return userParams; }
139 const std::string & GetPassword() const { return password; }
140 const std::string & GetMapFileName() const { return subnetFile; }
145 std::string errorStr;
146 std::vector<NET_ROUTER> netRouters;
147 std::vector<std::string> userParams;
148 std::string password;
149 std::string subnetFile;
151 //-----------------------------------------------------------------------------
152 class REMOTE_SCRIPT : public STG::Plugin {
156 void SetUsers(STG::Users * u) override { users = u; }
157 void SetSettings(const STG::ModuleSettings & s) override { settings = s; }
158 int ParseSettings() override;
160 int Start() override;
162 int Reload(const STG::ModuleSettings & ms) override;
163 bool IsRunning() override { return isRunning; }
165 const std::string & GetStrError() const override { return errorStr; }
166 std::string GetVersion() const override { return "Remote script v 0.3"; }
167 uint16_t GetStartPosition() const override { return 10; }
168 uint16_t GetStopPosition() const override { return 10; }
170 void DelUser(UserPtr u) { UnSetUserNotifiers(u); }
171 void AddUser(UserPtr u) { SetUserNotifiers(u); }
173 void AddRSU(UserPtr user);
174 void DelRSU(UserPtr user);
177 REMOTE_SCRIPT(const REMOTE_SCRIPT & rhs);
178 REMOTE_SCRIPT & operator=(const REMOTE_SCRIPT & rhs);
180 void Run(std::stop_token token);
184 bool Send(USER & rsu, bool forceDisconnect = false) const;
185 bool SendDirect(USER & rsu, uint32_t routerIP, bool forceDisconnect = false) const;
186 bool PreparePacket(char * buf, size_t bufSize, USER &rsu, bool forceDisconnect = false) const;
189 std::vector<uint32_t> IP2Routers(uint32_t ip);
192 void SetUserNotifiers(UserPtr u);
193 void UnSetUserNotifiers(UserPtr u);
195 void InitEncrypt(const std::string & password) const;
196 void Encrypt(void * dst, const void * src, size_t len8) const;
198 mutable BLOWFISH_CTX ctx;
200 std::list<IP_NOTIFIER> ipNotifierList;
201 std::list<CONNECTED_NOTIFIER> connNotifierList;
202 std::map<uint32_t, USER> authorizedUsers;
204 mutable std::string errorStr;
206 STG::ModuleSettings settings;
214 std::vector<NET_ROUTER> netRouters;
216 std::jthread m_thread;
221 STG::ScopedConnection m_onAddUserConn;
222 STG::ScopedConnection m_onDelUserConn;
224 STG::PluginLogger logger;
226 friend class RS::UpdateRouter;
227 friend class RS::DisconnectUser;
228 friend class RS::CONNECTED_NOTIFIER;
230 //-----------------------------------------------------------------------------
231 class DisconnectUser : public std::unary_function<std::pair<const uint32_t, USER> &, void> {
233 explicit DisconnectUser(REMOTE_SCRIPT & rs) : rscript(rs) {}
234 void operator()(std::pair<const uint32_t, USER> & p)
236 rscript.Send(p.second, true);
239 REMOTE_SCRIPT & rscript;
241 //-----------------------------------------------------------------------------