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/notifer.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
59 using UserPtr = STG::User*;
61 //-----------------------------------------------------------------------------
62 class ADD_USER_NONIFIER: public STG::NotifierBase<UserPtr> {
64 explicit ADD_USER_NONIFIER(REMOTE_SCRIPT & r)
66 void Notify(const UserPtr & user);
69 ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rhs);
70 ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER);
74 //-----------------------------------------------------------------------------
75 class DEL_USER_NONIFIER: public STG::NotifierBase<UserPtr> {
77 explicit DEL_USER_NONIFIER(REMOTE_SCRIPT & r)
79 void Notify(const UserPtr & user);
82 DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rhs);
83 DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER);
87 //-----------------------------------------------------------------------------
88 class IP_NOTIFIER: public STG::PropertyNotifierBase<uint32_t> {
90 IP_NOTIFIER(REMOTE_SCRIPT & r, UserPtr u)
91 : user(u), rs(r) { user->AddCurrIPAfterNotifier(this); }
92 IP_NOTIFIER(const IP_NOTIFIER & rhs)
93 : user(rhs.user), rs(rhs.rs) { user->AddCurrIPAfterNotifier(this); }
94 ~IP_NOTIFIER() { user->DelCurrIPAfterNotifier(this); }
96 IP_NOTIFIER & operator=(const IP_NOTIFIER & rhs)
98 user->DelCurrIPAfterNotifier(this);
100 user->AddCurrIPAfterNotifier(this);
104 void Notify(const uint32_t & oldValue, const uint32_t & newValue);
105 UserPtr GetUser() const { return user; }
112 //-----------------------------------------------------------------------------
113 class CONNECTED_NOTIFIER: public STG::PropertyNotifierBase<bool> {
115 CONNECTED_NOTIFIER(REMOTE_SCRIPT & r, UserPtr u)
116 : user(u), rs(r) { user->AddConnectedAfterNotifier(this); }
117 CONNECTED_NOTIFIER(const CONNECTED_NOTIFIER & rhs)
118 : user(rhs.user), rs(rhs.rs) { user->AddConnectedAfterNotifier(this); }
119 ~CONNECTED_NOTIFIER() { user->DelConnectedAfterNotifier(this); }
121 CONNECTED_NOTIFIER & operator=(const CONNECTED_NOTIFIER & rhs)
123 user->DelConnectedAfterNotifier(this);
125 user->AddConnectedAfterNotifier(this);
129 void Notify(const bool & oldValue, const bool & newValue);
130 UserPtr GetUser() const { return user; }
137 //-----------------------------------------------------------------------------
139 USER(const std::vector<uint32_t> & r, UserPtr it)
143 shortPacketsCount(0),
144 ip(user->GetCurrIP())
149 std::vector<uint32_t> routers;
150 int shortPacketsCount;
153 //-----------------------------------------------------------------------------
157 virtual ~SETTINGS() {}
158 const std::string & GetStrError() const { return errorStr; }
159 int ParseSettings(const STG::ModuleSettings & s);
160 int GetSendPeriod() const { return sendPeriod; }
161 uint16_t GetPort() const { return port; }
162 const std::vector<NET_ROUTER> & GetSubnetsMap() const { return netRouters; }
163 const std::vector<std::string> & GetUserParams() const { return userParams; }
164 const std::string & GetPassword() const { return password; }
165 const std::string & GetMapFileName() const { return subnetFile; }
170 std::string errorStr;
171 std::vector<NET_ROUTER> netRouters;
172 std::vector<std::string> userParams;
173 std::string password;
174 std::string subnetFile;
176 //-----------------------------------------------------------------------------
177 class REMOTE_SCRIPT : public STG::Plugin {
181 void SetUsers(STG::Users * u) override { users = u; }
182 void SetSettings(const STG::ModuleSettings & s) override { settings = s; }
183 int ParseSettings() override;
185 int Start() override;
187 int Reload(const STG::ModuleSettings & ms) override;
188 bool IsRunning() override { return isRunning; }
190 const std::string & GetStrError() const override { return errorStr; }
191 std::string GetVersion() const override { return "Remote script v 0.3"; }
192 uint16_t GetStartPosition() const override { return 10; }
193 uint16_t GetStopPosition() const override { return 10; }
195 void DelUser(UserPtr u) { UnSetUserNotifiers(u); }
196 void AddUser(UserPtr u) { SetUserNotifiers(u); }
198 void AddRSU(UserPtr user);
199 void DelRSU(UserPtr user);
202 REMOTE_SCRIPT(const REMOTE_SCRIPT & rhs);
203 REMOTE_SCRIPT & operator=(const REMOTE_SCRIPT & rhs);
205 void Run(std::stop_token token);
209 bool Send(USER & rsu, bool forceDisconnect = false) const;
210 bool SendDirect(USER & rsu, uint32_t routerIP, bool forceDisconnect = false) const;
211 bool PreparePacket(char * buf, size_t bufSize, USER &rsu, bool forceDisconnect = false) const;
214 std::vector<uint32_t> IP2Routers(uint32_t ip);
217 void SetUserNotifiers(UserPtr u);
218 void UnSetUserNotifiers(UserPtr u);
220 void InitEncrypt(const std::string & password) const;
221 void Encrypt(void * dst, const void * src, size_t len8) const;
223 mutable BLOWFISH_CTX ctx;
225 std::list<IP_NOTIFIER> ipNotifierList;
226 std::list<CONNECTED_NOTIFIER> connNotifierList;
227 std::map<uint32_t, USER> authorizedUsers;
229 mutable std::string errorStr;
231 STG::ModuleSettings settings;
239 std::vector<NET_ROUTER> netRouters;
241 std::jthread m_thread;
246 ADD_USER_NONIFIER onAddUserNotifier;
247 DEL_USER_NONIFIER onDelUserNotifier;
249 STG::PluginLogger logger;
251 friend class RS::UpdateRouter;
252 friend class RS::DisconnectUser;
253 friend class RS::CONNECTED_NOTIFIER;
255 //-----------------------------------------------------------------------------
256 class DisconnectUser : public std::unary_function<std::pair<const uint32_t, USER> &, void> {
258 explicit DisconnectUser(REMOTE_SCRIPT & rs) : rscript(rs) {}
259 void operator()(std::pair<const uint32_t, USER> & p)
261 rscript.Send(p.second, true);
264 REMOTE_SCRIPT & rscript;
266 //-----------------------------------------------------------------------------
267 inline void ADD_USER_NONIFIER::Notify(const UserPtr & user)
271 //-----------------------------------------------------------------------------
272 inline void DEL_USER_NONIFIER::Notify(const UserPtr & user)
276 //-----------------------------------------------------------------------------