]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/rscript/rscript.h
Complete replacement 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/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 <mutex>
40 #pragma GCC diagnostic push
41 #pragma GCC diagnostic ignored "-Wshadow"
42 #include <jthread.hpp>
43 #pragma GCC diagnostic pop
44 #include <cstdint>
45
46 namespace STG
47 {
48 struct Settings;
49 struct Settings;
50
51 namespace RS
52 {
53
54 using UserPtr = User*;
55 class UpdateRouter;
56
57 //-----------------------------------------------------------------------------
58 struct USER
59 {
60     USER(const std::vector<uint32_t> & r, UserPtr it)
61         : lastSentTime(0),
62           user(it),
63           routers(r),
64           shortPacketsCount(0),
65           ip(user->GetCurrIP())
66     {}
67
68     time_t lastSentTime;
69     UserPtr user;
70     std::vector<uint32_t> routers;
71     int shortPacketsCount;
72     uint32_t ip;
73 };
74 //-----------------------------------------------------------------------------
75 class SETTINGS
76 {
77     public:
78                             SETTINGS();
79         virtual             ~SETTINGS() {}
80         const std::string & GetStrError() const { return errorStr; }
81         int                 ParseSettings(const ModuleSettings & s);
82         int                 GetSendPeriod() const { return sendPeriod; }
83         uint16_t            GetPort() const { return port; }
84         const std::vector<NET_ROUTER> & GetSubnetsMap() const { return netRouters; }
85         const std::vector<std::string> & GetUserParams() const { return userParams; }
86         const std::string & GetPassword() const { return password; }
87         const std::string & GetMapFileName() const { return subnetFile; }
88
89     private:
90         int                 sendPeriod;
91         uint16_t            port;
92         std::string         errorStr;
93         std::vector<NET_ROUTER> netRouters;
94         std::vector<std::string> userParams;
95         std::string         password;
96         std::string         subnetFile;
97 };
98 //-----------------------------------------------------------------------------
99 class REMOTE_SCRIPT : public Plugin
100 {
101     public:
102                             REMOTE_SCRIPT();
103
104         void                SetUsers(Users * u) override { users = u; }
105         void                SetSettings(const ModuleSettings & s) override { settings = s; }
106         int                 ParseSettings() override;
107
108         int                 Start() override;
109         int                 Stop() override;
110         int                 Reload(const ModuleSettings & ms) override;
111         bool                IsRunning() override { return isRunning; }
112
113         const std::string & GetStrError() const override { return errorStr; }
114         std::string         GetVersion() const override { return "Remote script v 0.3"; }
115         uint16_t            GetStartPosition() const override { return 10; }
116         uint16_t            GetStopPosition() const override { return 10; }
117
118         void                DelUser(UserPtr u) { UnSetUserNotifiers(u); }
119         void                AddUser(UserPtr u) { SetUserNotifiers(u); }
120
121         void                AddRSU(UserPtr user);
122         void                DelRSU(UserPtr user);
123
124     private:
125         REMOTE_SCRIPT(const REMOTE_SCRIPT & rhs);
126         REMOTE_SCRIPT & operator=(const REMOTE_SCRIPT & rhs);
127
128         void                Run(std::stop_token token);
129         bool                PrepareNet();
130         bool                FinalizeNet();
131
132         bool                Send(USER & rsu, bool forceDisconnect = false) const;
133         bool                SendDirect(USER & rsu, uint32_t routerIP, bool forceDisconnect = false) const;
134         bool                PreparePacket(char * buf, size_t bufSize, USER &rsu, bool forceDisconnect = false) const;
135         void                PeriodicSend();
136
137         std::vector<uint32_t> IP2Routers(uint32_t ip);
138         bool                GetUsers();
139
140         void                SetUserNotifiers(UserPtr u);
141         void                UnSetUserNotifiers(UserPtr u);
142
143         void                InitEncrypt(const std::string & password) const;
144         void                Encrypt(void * dst, const void * src, size_t len8) const;
145
146         mutable BLOWFISH_CTX ctx;
147         std::map<uint32_t, USER> authorizedUsers;
148
149         mutable std::string errorStr;
150         SETTINGS         rsSettings;
151         ModuleSettings     settings;
152         int                 sendPeriod;
153         int                 halfPeriod;
154
155         bool                isRunning;
156
157         Users *             users;
158
159         std::vector<NET_ROUTER> netRouters;
160
161         std::jthread        m_thread;
162         std::mutex          m_mutex;
163
164         int                 sock;
165
166         ScopedConnection m_onAddUserConn;
167         ScopedConnection m_onDelUserConn;
168
169         void addDelUser(UserPtr user, bool toAdd);
170
171         using ConnHolder = std::tuple<int, ScopedConnection, ScopedConnection>;
172         std::vector<ConnHolder> m_conns;
173
174         PluginLogger       logger;
175
176         friend class RS::UpdateRouter;
177 };
178 //-----------------------------------------------------------------------------
179
180 } // namespace RS
181 }