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"
56 using UserPtr = STG::User*;
58 //-----------------------------------------------------------------------------
59 class ADD_USER_NONIFIER: public STG::NotifierBase<UserPtr> {
61 explicit ADD_USER_NONIFIER(REMOTE_SCRIPT & r)
63 void Notify(const UserPtr & user);
66 ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rhs);
67 ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER);
71 //-----------------------------------------------------------------------------
72 class DEL_USER_NONIFIER: public STG::NotifierBase<UserPtr> {
74 explicit DEL_USER_NONIFIER(REMOTE_SCRIPT & r)
76 void Notify(const UserPtr & user);
79 DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rhs);
80 DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER);
84 //-----------------------------------------------------------------------------
85 class IP_NOTIFIER: public STG::PropertyNotifierBase<uint32_t> {
87 IP_NOTIFIER(REMOTE_SCRIPT & r, UserPtr u)
88 : user(u), rs(r) { user->AddCurrIPAfterNotifier(this); }
89 IP_NOTIFIER(const IP_NOTIFIER & rhs)
90 : user(rhs.user), rs(rhs.rs) { user->AddCurrIPAfterNotifier(this); }
91 ~IP_NOTIFIER() { user->DelCurrIPAfterNotifier(this); }
93 IP_NOTIFIER & operator=(const IP_NOTIFIER & rhs)
95 user->DelCurrIPAfterNotifier(this);
97 user->AddCurrIPAfterNotifier(this);
101 void Notify(const uint32_t & oldValue, const uint32_t & newValue);
102 UserPtr GetUser() const { return user; }
109 //-----------------------------------------------------------------------------
110 class CONNECTED_NOTIFIER: public STG::PropertyNotifierBase<bool> {
112 CONNECTED_NOTIFIER(REMOTE_SCRIPT & r, UserPtr u)
113 : user(u), rs(r) { user->AddConnectedAfterNotifier(this); }
114 CONNECTED_NOTIFIER(const CONNECTED_NOTIFIER & rhs)
115 : user(rhs.user), rs(rhs.rs) { user->AddConnectedAfterNotifier(this); }
116 ~CONNECTED_NOTIFIER() { user->DelConnectedAfterNotifier(this); }
118 CONNECTED_NOTIFIER & operator=(const CONNECTED_NOTIFIER & rhs)
120 user->DelConnectedAfterNotifier(this);
122 user->AddConnectedAfterNotifier(this);
126 void Notify(const bool & oldValue, const bool & newValue);
127 UserPtr GetUser() const { return user; }
134 //-----------------------------------------------------------------------------
136 USER(const std::vector<uint32_t> & r, UserPtr it)
140 shortPacketsCount(0),
141 ip(user->GetCurrIP())
146 std::vector<uint32_t> routers;
147 int shortPacketsCount;
150 //-----------------------------------------------------------------------------
154 virtual ~SETTINGS() {}
155 const std::string & GetStrError() const { return errorStr; }
156 int ParseSettings(const STG::ModuleSettings & s);
157 int GetSendPeriod() const { return sendPeriod; }
158 uint16_t GetPort() const { return port; }
159 const std::vector<NET_ROUTER> & GetSubnetsMap() const { return netRouters; }
160 const std::vector<std::string> & GetUserParams() const { return userParams; }
161 const std::string & GetPassword() const { return password; }
162 const std::string & GetMapFileName() const { return subnetFile; }
167 std::string errorStr;
168 std::vector<NET_ROUTER> netRouters;
169 std::vector<std::string> userParams;
170 std::string password;
171 std::string subnetFile;
173 //-----------------------------------------------------------------------------
174 class REMOTE_SCRIPT : public STG::Plugin {
177 ~REMOTE_SCRIPT() override;
179 void SetUsers(STG::Users * u) override { users = u; }
180 void SetSettings(const STG::ModuleSettings & s) override { settings = s; }
181 int ParseSettings() override;
183 int Start() override;
185 int Reload(const STG::ModuleSettings & ms) override;
186 bool IsRunning() override { return isRunning; }
188 const std::string & GetStrError() const override { return errorStr; }
189 std::string GetVersion() const override { return "Remote script v 0.3"; }
190 uint16_t GetStartPosition() const override { return 10; }
191 uint16_t GetStopPosition() const override { return 10; }
193 void DelUser(UserPtr u) { UnSetUserNotifiers(u); }
194 void AddUser(UserPtr u) { SetUserNotifiers(u); }
196 void AddRSU(UserPtr user);
197 void DelRSU(UserPtr user);
200 REMOTE_SCRIPT(const REMOTE_SCRIPT & rhs);
201 REMOTE_SCRIPT & operator=(const REMOTE_SCRIPT & rhs);
203 static void * Run(void *);
207 bool Send(USER & rsu, bool forceDisconnect = false) const;
208 bool SendDirect(USER & rsu, uint32_t routerIP, bool forceDisconnect = false) const;
209 bool PreparePacket(char * buf, size_t bufSize, USER &rsu, bool forceDisconnect = false) const;
212 std::vector<uint32_t> IP2Routers(uint32_t ip);
215 void SetUserNotifiers(UserPtr u);
216 void UnSetUserNotifiers(UserPtr u);
218 void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password) const;
219 void Encrypt(BLOWFISH_CTX * ctx, void * dst, const void * src, size_t len8) const;
221 mutable BLOWFISH_CTX ctx;
223 std::list<IP_NOTIFIER> ipNotifierList;
224 std::list<CONNECTED_NOTIFIER> connNotifierList;
225 std::map<uint32_t, USER> authorizedUsers;
227 mutable std::string errorStr;
229 STG::ModuleSettings settings;
238 std::vector<NET_ROUTER> netRouters;
241 pthread_mutex_t mutex;
245 ADD_USER_NONIFIER onAddUserNotifier;
246 DEL_USER_NONIFIER onDelUserNotifier;
248 STG::PluginLogger logger;
250 friend class RS::UpdateRouter;
251 friend class RS::DisconnectUser;
252 friend class RS::CONNECTED_NOTIFIER;
254 //-----------------------------------------------------------------------------
255 class DisconnectUser : public std::unary_function<std::pair<const uint32_t, USER> &, void> {
257 explicit DisconnectUser(REMOTE_SCRIPT & rs) : rscript(rs) {}
258 void operator()(std::pair<const uint32_t, USER> & p)
260 rscript.Send(p.second, true);
263 REMOTE_SCRIPT & rscript;
265 //-----------------------------------------------------------------------------
266 inline void ADD_USER_NONIFIER::Notify(const UserPtr & user)
270 //-----------------------------------------------------------------------------
271 inline void DEL_USER_NONIFIER::Notify(const UserPtr & user)
275 //-----------------------------------------------------------------------------