]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/rscript/rscript.h
e734227c48e67d50b72fd985abe1eed248ef0e3f
[stg.git] / projects / stargazer / plugins / other / rscript / rscript.h
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #pragma once
23
24 #include "stg/plugin.h"
25 #include "stg/module_settings.h"
26 #include "stg/notifer.h"
27 #include "stg/user.h"
28 #include "stg/blowfish.h"
29 #include "stg/rs_packets.h"
30 #include "stg/logger.h"
31
32 #include "nrmap_parser.h"
33
34 #include <string>
35 #include <list>
36 #include <map>
37 #include <functional>
38 #include <utility>
39 #include <cstdint>
40
41 #include <pthread.h>
42
43 namespace STG
44 {
45 struct Settings;
46 struct Settings;
47 }
48
49 namespace RS
50 {
51
52 class REMOTE_SCRIPT;
53 class UpdateRouter;
54 class DisconnectUser;
55
56 using UserPtr = STG::User*;
57
58 //-----------------------------------------------------------------------------
59 class ADD_USER_NONIFIER: public STG::NotifierBase<UserPtr> {
60 public:
61     explicit ADD_USER_NONIFIER(REMOTE_SCRIPT & r)
62         : rs(r) {}
63     void Notify(const UserPtr & user);
64
65 private:
66     ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rhs);
67     ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER);
68
69     REMOTE_SCRIPT & rs;
70 };
71 //-----------------------------------------------------------------------------
72 class DEL_USER_NONIFIER: public STG::NotifierBase<UserPtr> {
73 public:
74     explicit DEL_USER_NONIFIER(REMOTE_SCRIPT & r)
75         : rs(r) {}
76     void Notify(const UserPtr & user);
77
78 private:
79     DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rhs);
80     DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER);
81
82     REMOTE_SCRIPT & rs;
83 };
84 //-----------------------------------------------------------------------------
85 class IP_NOTIFIER: public STG::PropertyNotifierBase<uint32_t> {
86 public:
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); }
92
93     IP_NOTIFIER & operator=(const IP_NOTIFIER & rhs)
94     {
95         user->DelCurrIPAfterNotifier(this);
96         user = rhs.user;
97         user->AddCurrIPAfterNotifier(this);
98         return *this;
99     }
100
101     void Notify(const uint32_t & oldValue, const uint32_t & newValue);
102     UserPtr GetUser() const { return user; }
103
104 private:
105
106     UserPtr user;
107     REMOTE_SCRIPT & rs;
108 };
109 //-----------------------------------------------------------------------------
110 class CONNECTED_NOTIFIER: public STG::PropertyNotifierBase<bool> {
111 public:
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); }
117
118     CONNECTED_NOTIFIER & operator=(const CONNECTED_NOTIFIER & rhs)
119     {
120         user->DelConnectedAfterNotifier(this);
121         user = rhs.user;
122         user->AddConnectedAfterNotifier(this);
123         return *this;
124     }
125
126     void Notify(const bool & oldValue, const bool & newValue);
127     UserPtr GetUser() const { return user; }
128
129 private:
130
131     UserPtr user;
132     REMOTE_SCRIPT & rs;
133 };
134 //-----------------------------------------------------------------------------
135 struct USER {
136     USER(const std::vector<uint32_t> & r, UserPtr it)
137         : lastSentTime(0),
138           user(it),
139           routers(r),
140           shortPacketsCount(0),
141           ip(user->GetCurrIP())
142     {}
143
144     time_t lastSentTime;
145     UserPtr user;
146     std::vector<uint32_t> routers;
147     int shortPacketsCount;
148     uint32_t ip;
149 };
150 //-----------------------------------------------------------------------------
151 class SETTINGS {
152 public:
153                         SETTINGS();
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; }
163
164 private:
165     int                 sendPeriod;
166     uint16_t            port;
167     std::string         errorStr;
168     std::vector<NET_ROUTER> netRouters;
169     std::vector<std::string> userParams;
170     std::string         password;
171     std::string         subnetFile;
172 };
173 //-----------------------------------------------------------------------------
174 class REMOTE_SCRIPT : public STG::Plugin {
175 public:
176                         REMOTE_SCRIPT();
177                         ~REMOTE_SCRIPT() override;
178
179     void                SetUsers(STG::Users * u) override { users = u; }
180     void                SetSettings(const STG::ModuleSettings & s) override { settings = s; }
181     int                 ParseSettings() override;
182
183     int                 Start() override;
184     int                 Stop() override;
185     int                 Reload(const STG::ModuleSettings & ms) override;
186     bool                IsRunning() override { return isRunning; }
187
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; }
192
193     void                DelUser(UserPtr u) { UnSetUserNotifiers(u); }
194     void                AddUser(UserPtr u) { SetUserNotifiers(u); }
195
196     void                AddRSU(UserPtr user);
197     void                DelRSU(UserPtr user);
198
199 private:
200     REMOTE_SCRIPT(const REMOTE_SCRIPT & rhs);
201     REMOTE_SCRIPT & operator=(const REMOTE_SCRIPT & rhs);
202
203     static void *       Run(void *);
204     bool                PrepareNet();
205     bool                FinalizeNet();
206
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;
210     void                PeriodicSend();
211
212     std::vector<uint32_t> IP2Routers(uint32_t ip);
213     bool                GetUsers();
214
215     void                SetUserNotifiers(UserPtr u);
216     void                UnSetUserNotifiers(UserPtr u);
217
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;
220
221     mutable BLOWFISH_CTX ctx;
222
223     std::list<IP_NOTIFIER> ipNotifierList;
224     std::list<CONNECTED_NOTIFIER> connNotifierList;
225     std::map<uint32_t, USER> authorizedUsers;
226
227     mutable std::string errorStr;
228     SETTINGS         rsSettings;
229     STG::ModuleSettings     settings;
230     int                 sendPeriod;
231     int                 halfPeriod;
232
233     bool                nonstop;
234     bool                isRunning;
235
236     STG::Users *             users;
237
238     std::vector<NET_ROUTER> netRouters;
239
240     pthread_t           thread;
241     pthread_mutex_t     mutex;
242
243     int                 sock;
244
245     ADD_USER_NONIFIER onAddUserNotifier;
246     DEL_USER_NONIFIER onDelUserNotifier;
247
248     STG::PluginLogger       logger;
249
250     friend class RS::UpdateRouter;
251     friend class RS::DisconnectUser;
252     friend class RS::CONNECTED_NOTIFIER;
253 };
254 //-----------------------------------------------------------------------------
255 class DisconnectUser : public std::unary_function<std::pair<const uint32_t, USER> &, void> {
256     public:
257         explicit DisconnectUser(REMOTE_SCRIPT & rs) : rscript(rs) {}
258         void operator()(std::pair<const uint32_t, USER> & p)
259         {
260             rscript.Send(p.second, true);
261         }
262     private:
263         REMOTE_SCRIPT & rscript;
264 };
265 //-----------------------------------------------------------------------------
266 inline void ADD_USER_NONIFIER::Notify(const UserPtr & user)
267 {
268 rs.AddUser(user);
269 }
270 //-----------------------------------------------------------------------------
271 inline void DEL_USER_NONIFIER::Notify(const UserPtr & user)
272 {
273 rs.DelUser(user);
274 }
275 //-----------------------------------------------------------------------------
276
277 } // namespace RS