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 {
180 ~REMOTE_SCRIPT() override;
182 void SetUsers(STG::Users * u) override { users = u; }
183 void SetSettings(const STG::ModuleSettings & s) override { settings = s; }
184 int ParseSettings() override;
186 int Start() override;
188 int Reload(const STG::ModuleSettings & ms) override;
189 bool IsRunning() override { return isRunning; }
191 const std::string & GetStrError() const override { return errorStr; }
192 std::string GetVersion() const override { return "Remote script v 0.3"; }
193 uint16_t GetStartPosition() const override { return 10; }
194 uint16_t GetStopPosition() const override { return 10; }
196 void DelUser(UserPtr u) { UnSetUserNotifiers(u); }
197 void AddUser(UserPtr u) { SetUserNotifiers(u); }
199 void AddRSU(UserPtr user);
200 void DelRSU(UserPtr user);
203 REMOTE_SCRIPT(const REMOTE_SCRIPT & rhs);
204 REMOTE_SCRIPT & operator=(const REMOTE_SCRIPT & rhs);
206 void Run(std::stop_token token);
210 bool Send(USER & rsu, bool forceDisconnect = false) const;
211 bool SendDirect(USER & rsu, uint32_t routerIP, bool forceDisconnect = false) const;
212 bool PreparePacket(char * buf, size_t bufSize, USER &rsu, bool forceDisconnect = false) const;
215 std::vector<uint32_t> IP2Routers(uint32_t ip);
218 void SetUserNotifiers(UserPtr u);
219 void UnSetUserNotifiers(UserPtr u);
221 void InitEncrypt(const std::string & password) const;
222 void Encrypt(void * dst, const void * src, size_t len8) const;
224 mutable BLOWFISH_CTX ctx;
226 std::list<IP_NOTIFIER> ipNotifierList;
227 std::list<CONNECTED_NOTIFIER> connNotifierList;
228 std::map<uint32_t, USER> authorizedUsers;
230 mutable std::string errorStr;
232 STG::ModuleSettings settings;
240 std::vector<NET_ROUTER> netRouters;
242 std::jthread m_thread;
247 ADD_USER_NONIFIER onAddUserNotifier;
248 DEL_USER_NONIFIER onDelUserNotifier;
250 STG::PluginLogger logger;
252 friend class RS::UpdateRouter;
253 friend class RS::DisconnectUser;
254 friend class RS::CONNECTED_NOTIFIER;
256 //-----------------------------------------------------------------------------
257 class DisconnectUser : public std::unary_function<std::pair<const uint32_t, USER> &, void> {
259 explicit DisconnectUser(REMOTE_SCRIPT & rs) : rscript(rs) {}
260 void operator()(std::pair<const uint32_t, USER> & p)
262 rscript.Send(p.second, true);
265 REMOTE_SCRIPT & rscript;
267 //-----------------------------------------------------------------------------
268 inline void ADD_USER_NONIFIER::Notify(const UserPtr & user)
272 //-----------------------------------------------------------------------------
273 inline void DEL_USER_NONIFIER::Notify(const UserPtr & user)
277 //-----------------------------------------------------------------------------