]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/rscript/rscript.h
Start replacing notifiers with subscriptions.
[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/subscriptions.h"
27 #include "stg/notifer.h"
28 #include "stg/user.h"
29 #include "stg/blowfish.h"
30 #include "stg/rs_packets.h"
31 #include "stg/logger.h"
32
33 #include "nrmap_parser.h"
34
35 #include <string>
36 #include <list>
37 #include <map>
38 #include <functional>
39 #include <utility>
40 #include <mutex>
41 #pragma GCC diagnostic push
42 #pragma GCC diagnostic ignored "-Wshadow"
43 #include <jthread.hpp>
44 #pragma GCC diagnostic pop
45 #include <cstdint>
46
47 namespace STG
48 {
49 struct Settings;
50 struct Settings;
51 }
52
53 namespace RS
54 {
55
56 class REMOTE_SCRIPT;
57 class UpdateRouter;
58 class DisconnectUser;
59
60 using UserPtr = STG::User*;
61
62 //-----------------------------------------------------------------------------
63 class IP_NOTIFIER: public STG::PropertyNotifierBase<uint32_t> {
64 public:
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); }
70
71     IP_NOTIFIER & operator=(const IP_NOTIFIER & rhs)
72     {
73         user->DelCurrIPAfterNotifier(this);
74         user = rhs.user;
75         user->AddCurrIPAfterNotifier(this);
76         return *this;
77     }
78
79     void notify(const uint32_t & oldValue, const uint32_t & newValue) override;
80     UserPtr GetUser() const { return user; }
81
82 private:
83
84     UserPtr user;
85     REMOTE_SCRIPT & rs;
86 };
87 //-----------------------------------------------------------------------------
88 class CONNECTED_NOTIFIER: public STG::PropertyNotifierBase<bool> {
89 public:
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); }
95
96     CONNECTED_NOTIFIER & operator=(const CONNECTED_NOTIFIER & rhs)
97     {
98         user->DelConnectedAfterNotifier(this);
99         user = rhs.user;
100         user->AddConnectedAfterNotifier(this);
101         return *this;
102     }
103
104     void notify(const bool & oldValue, const bool & newValue) override;
105     UserPtr GetUser() const { return user; }
106
107 private:
108
109     UserPtr user;
110     REMOTE_SCRIPT & rs;
111 };
112 //-----------------------------------------------------------------------------
113 struct USER {
114     USER(const std::vector<uint32_t> & r, UserPtr it)
115         : lastSentTime(0),
116           user(it),
117           routers(r),
118           shortPacketsCount(0),
119           ip(user->GetCurrIP())
120     {}
121
122     time_t lastSentTime;
123     UserPtr user;
124     std::vector<uint32_t> routers;
125     int shortPacketsCount;
126     uint32_t ip;
127 };
128 //-----------------------------------------------------------------------------
129 class SETTINGS {
130 public:
131                         SETTINGS();
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; }
141
142 private:
143     int                 sendPeriod;
144     uint16_t            port;
145     std::string         errorStr;
146     std::vector<NET_ROUTER> netRouters;
147     std::vector<std::string> userParams;
148     std::string         password;
149     std::string         subnetFile;
150 };
151 //-----------------------------------------------------------------------------
152 class REMOTE_SCRIPT : public STG::Plugin {
153 public:
154                         REMOTE_SCRIPT();
155
156     void                SetUsers(STG::Users * u) override { users = u; }
157     void                SetSettings(const STG::ModuleSettings & s) override { settings = s; }
158     int                 ParseSettings() override;
159
160     int                 Start() override;
161     int                 Stop() override;
162     int                 Reload(const STG::ModuleSettings & ms) override;
163     bool                IsRunning() override { return isRunning; }
164
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; }
169
170     void                DelUser(UserPtr u) { UnSetUserNotifiers(u); }
171     void                AddUser(UserPtr u) { SetUserNotifiers(u); }
172
173     void                AddRSU(UserPtr user);
174     void                DelRSU(UserPtr user);
175
176 private:
177     REMOTE_SCRIPT(const REMOTE_SCRIPT & rhs);
178     REMOTE_SCRIPT & operator=(const REMOTE_SCRIPT & rhs);
179
180     void                Run(std::stop_token token);
181     bool                PrepareNet();
182     bool                FinalizeNet();
183
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;
187     void                PeriodicSend();
188
189     std::vector<uint32_t> IP2Routers(uint32_t ip);
190     bool                GetUsers();
191
192     void                SetUserNotifiers(UserPtr u);
193     void                UnSetUserNotifiers(UserPtr u);
194
195     void                InitEncrypt(const std::string & password) const;
196     void                Encrypt(void * dst, const void * src, size_t len8) const;
197
198     mutable BLOWFISH_CTX ctx;
199
200     std::list<IP_NOTIFIER> ipNotifierList;
201     std::list<CONNECTED_NOTIFIER> connNotifierList;
202     std::map<uint32_t, USER> authorizedUsers;
203
204     mutable std::string errorStr;
205     SETTINGS         rsSettings;
206     STG::ModuleSettings     settings;
207     int                 sendPeriod;
208     int                 halfPeriod;
209
210     bool                isRunning;
211
212     STG::Users *             users;
213
214     std::vector<NET_ROUTER> netRouters;
215
216     std::jthread        m_thread;
217     std::mutex          m_mutex;
218
219     int                 sock;
220
221     STG::ScopedConnection m_onAddUserConn;
222     STG::ScopedConnection m_onDelUserConn;
223
224     STG::PluginLogger       logger;
225
226     friend class RS::UpdateRouter;
227     friend class RS::DisconnectUser;
228     friend class RS::CONNECTED_NOTIFIER;
229 };
230 //-----------------------------------------------------------------------------
231 class DisconnectUser : public std::unary_function<std::pair<const uint32_t, USER> &, void> {
232     public:
233         explicit DisconnectUser(REMOTE_SCRIPT & rs) : rscript(rs) {}
234         void operator()(std::pair<const uint32_t, USER> & p)
235         {
236             rscript.Send(p.second, true);
237         }
238     private:
239         REMOTE_SCRIPT & rscript;
240 };
241 //-----------------------------------------------------------------------------
242
243 } // namespace RS