]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/rscript/rscript.cpp
More subscriptions, less notifiers.
[stg.git] / projects / stargazer / plugins / other / rscript / rscript.cpp
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 #include "rscript.h"
23
24 #include "ur_functor.h"
25
26 #include "stg/common.h"
27 #include "stg/locker.h"
28 #include "stg/users.h"
29 #include "stg/user_property.h"
30 #include "stg/logger.h"
31
32 #include <algorithm>
33
34 #include <csignal>
35 #include <cassert>
36 #include <cstdlib>
37 #include <cerrno>
38 #include <cstring>
39
40 #include <sys/time.h>
41 #include <netinet/ip.h>
42
43 #define RS_DEBUG (1)
44 #define MAX_SHORT_PCKT  (3)
45
46 extern volatile time_t stgTime;
47
48 using RS::REMOTE_SCRIPT;
49
50 namespace {
51
52 template<typename T>
53 struct USER_IS
54 {
55     explicit USER_IS(RS::UserPtr u) : user(u) {}
56     bool operator()(const T & notifier) { return notifier.GetUser() == user; }
57
58     RS::UserPtr user;
59 };
60
61 } // namespace anonymous
62
63 extern "C" STG::Plugin* GetPlugin()
64 {
65     static REMOTE_SCRIPT plugin;
66     return &plugin;
67 }
68 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
71 RS::SETTINGS::SETTINGS()
72     : sendPeriod(0),
73       port(0)
74 {
75 }
76 //-----------------------------------------------------------------------------
77 int RS::SETTINGS::ParseSettings(const STG::ModuleSettings & s)
78 {
79 int p;
80 STG::ParamValue pv;
81 netRouters.clear();
82 ///////////////////////////
83 pv.param = "Port";
84 auto pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
85 if (pvi == s.moduleParams.end() || pvi->value.empty())
86     {
87     errorStr = "Parameter \'Port\' not found.";
88     printfd(__FILE__, "Parameter 'Port' not found\n");
89     return -1;
90     }
91 if (ParseIntInRange(pvi->value[0], 2, 65535, &p) != 0)
92     {
93     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
94     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
95     return -1;
96     }
97 port = static_cast<uint16_t>(p);
98 ///////////////////////////
99 pv.param = "SendPeriod";
100 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
101 if (pvi == s.moduleParams.end() || pvi->value.empty())
102     {
103     errorStr = "Parameter \'SendPeriod\' not found.";
104     printfd(__FILE__, "Parameter 'SendPeriod' not found\n");
105     return -1;
106     }
107
108 if (ParseIntInRange(pvi->value[0], 5, 600, &sendPeriod) != 0)
109     {
110     errorStr = "Cannot parse parameter \'SendPeriod\': " + errorStr;
111     printfd(__FILE__, "Cannot parse parameter 'SendPeriod'\n");
112     return -1;
113     }
114 ///////////////////////////
115 pv.param = "UserParams";
116 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
117 if (pvi == s.moduleParams.end() || pvi->value.empty())
118     {
119     errorStr = "Parameter \'UserParams\' not found.";
120     printfd(__FILE__, "Parameter 'UserParams' not found\n");
121     return -1;
122     }
123 userParams = pvi->value;
124 ///////////////////////////
125 pv.param = "Password";
126 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
127 if (pvi == s.moduleParams.end() || pvi->value.empty())
128     {
129     errorStr = "Parameter \'Password\' not found.";
130     printfd(__FILE__, "Parameter 'Password' not found\n");
131     return -1;
132     }
133 password = pvi->value[0];
134 ///////////////////////////
135 pv.param = "SubnetFile";
136 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
137 if (pvi == s.moduleParams.end() || pvi->value.empty())
138     {
139     errorStr = "Parameter \'SubnetFile\' not found.";
140     printfd(__FILE__, "Parameter 'SubnetFile' not found\n");
141     return -1;
142     }
143 subnetFile = pvi->value[0];
144
145 NRMapParser nrMapParser;
146
147 if (!nrMapParser.ReadFile(subnetFile))
148     netRouters = nrMapParser.GetMap();
149 else
150     STG::PluginLogger::get("rscript")("mod_rscript: error opening subnets file '%s'", subnetFile.c_str());
151
152 return 0;
153 }
154 //-----------------------------------------------------------------------------
155 //-----------------------------------------------------------------------------
156 //-----------------------------------------------------------------------------
157 REMOTE_SCRIPT::REMOTE_SCRIPT()
158     : sendPeriod(15),
159       halfPeriod(8),
160       isRunning(false),
161       users(nullptr),
162       sock(0),
163       logger(STG::PluginLogger::get("rscript"))
164 {
165 }
166 //-----------------------------------------------------------------------------
167 void REMOTE_SCRIPT::Run(std::stop_token token)
168 {
169 sigset_t signalSet;
170 sigfillset(&signalSet);
171 pthread_sigmask(SIG_BLOCK, &signalSet, nullptr);
172
173 isRunning = true;
174
175 while (!token.stop_requested())
176     {
177     PeriodicSend();
178     sleep(2);
179     }
180
181 isRunning = false;
182 }
183 //-----------------------------------------------------------------------------
184 int REMOTE_SCRIPT::ParseSettings()
185 {
186 auto ret = rsSettings.ParseSettings(settings);
187 if (ret != 0)
188     errorStr = rsSettings.GetStrError();
189
190 sendPeriod = rsSettings.GetSendPeriod();
191 halfPeriod = sendPeriod / 2;
192
193 return ret;
194 }
195 //-----------------------------------------------------------------------------
196 int REMOTE_SCRIPT::Start()
197 {
198 netRouters = rsSettings.GetSubnetsMap();
199
200 InitEncrypt(rsSettings.GetPassword());
201
202 m_onAddUserConn = users->onAdd([this](auto user){ AddUser(user); });
203 m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); });
204
205 if (GetUsers())
206     return -1;
207
208 if (PrepareNet())
209     return -1;
210
211 if (!isRunning)
212     m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
213
214 errorStr = "";
215 return 0;
216 }
217 //-----------------------------------------------------------------------------
218 int REMOTE_SCRIPT::Stop()
219 {
220 if (!IsRunning())
221     return 0;
222
223 m_thread.request_stop();
224
225 std::for_each(
226         authorizedUsers.begin(),
227         authorizedUsers.end(),
228         DisconnectUser(*this)
229         );
230
231 FinalizeNet();
232
233 if (isRunning)
234     {
235     //5 seconds to thread stops itself
236     for (int i = 0; i < 25 && isRunning; i++)
237         {
238         struct timespec ts = {0, 200000000};
239         nanosleep(&ts, nullptr);
240         }
241     }
242
243 if (isRunning)
244     {
245     logger("Cannot stop thread.");
246     m_thread.detach();
247     }
248 else
249     m_thread.join();
250
251 return 0;
252 }
253 //-----------------------------------------------------------------------------
254 int REMOTE_SCRIPT::Reload(const STG::ModuleSettings & /*ms*/)
255 {
256 NRMapParser nrMapParser;
257
258 if (nrMapParser.ReadFile(rsSettings.GetMapFileName()))
259     {
260     errorStr = nrMapParser.GetErrorStr();
261     logger("Map file reading error: %s", errorStr.c_str());
262     return -1;
263     }
264
265     {
266     std::lock_guard lock(m_mutex);
267
268     printfd(__FILE__, "REMOTE_SCRIPT::Reload()\n");
269
270     netRouters = nrMapParser.GetMap();
271     }
272
273 std::for_each(authorizedUsers.begin(),
274               authorizedUsers.end(),
275               UpdateRouter(*this));
276
277 logger("%s reloaded successfully.", rsSettings.GetMapFileName().c_str());
278 printfd(__FILE__, "REMOTE_SCRIPT::Reload() %s reloaded successfully.\n");
279
280 return 0;
281 }
282 //-----------------------------------------------------------------------------
283 bool REMOTE_SCRIPT::PrepareNet()
284 {
285 sock = socket(AF_INET, SOCK_DGRAM, 0);
286
287 if (sock < 0)
288     {
289     errorStr = "Cannot create socket.";
290     logger("Canot create a socket: %s", strerror(errno));
291     printfd(__FILE__, "Cannot create socket\n");
292     return true;
293     }
294
295 return false;
296 }
297 //-----------------------------------------------------------------------------
298 bool REMOTE_SCRIPT::FinalizeNet()
299 {
300 close(sock);
301 return false;
302 }
303 //-----------------------------------------------------------------------------
304 void REMOTE_SCRIPT::PeriodicSend()
305 {
306 std::lock_guard lock(m_mutex);
307
308 auto it = authorizedUsers.begin();
309 while (it != authorizedUsers.end())
310     {
311     if (difftime(stgTime, it->second.lastSentTime) - (rand() % halfPeriod) > sendPeriod)
312         {
313         Send(it->second);
314         }
315     ++it;
316     }
317 }
318 //-----------------------------------------------------------------------------
319 #ifdef NDEBUG
320 bool REMOTE_SCRIPT::PreparePacket(char * buf, size_t, RS::USER & rsu, bool forceDisconnect) const
321 #else
322 bool REMOTE_SCRIPT::PreparePacket(char * buf, size_t bufSize, RS::USER & rsu, bool forceDisconnect) const
323 #endif
324 {
325 RS::PACKET_HEADER packetHead;
326
327 memset(packetHead.padding, 0, sizeof(packetHead.padding));
328 memcpy(packetHead.magic, RS_ID, sizeof(RS_ID));
329 packetHead.protoVer[0] = '0';
330 packetHead.protoVer[1] = '2';
331 if (forceDisconnect)
332     {
333     packetHead.packetType = RS_DISCONNECT_PACKET;
334     printfd(__FILE__, "RSCRIPT: force disconnect for '%s'\n", rsu.user->GetLogin().c_str());
335     }
336 else
337     {
338     if (rsu.shortPacketsCount % MAX_SHORT_PCKT == 0)
339         {
340         //SendLong
341         packetHead.packetType = rsu.user->IsInetable() ? RS_CONNECT_PACKET : RS_DISCONNECT_PACKET;
342         if (rsu.user->IsInetable())
343             printfd(__FILE__, "RSCRIPT: connect for '%s'\n", rsu.user->GetLogin().c_str());
344         else
345             printfd(__FILE__, "RSCRIPT: disconnect for '%s'\n", rsu.user->GetLogin().c_str());
346         }
347     else
348         {
349         //SendShort
350         packetHead.packetType = rsu.user->IsInetable() ? RS_ALIVE_PACKET : RS_DISCONNECT_PACKET;
351         if (rsu.user->IsInetable())
352             printfd(__FILE__, "RSCRIPT: alive for '%s'\n", rsu.user->GetLogin().c_str());
353         else
354             printfd(__FILE__, "RSCRIPT: disconnect for '%s'\n", rsu.user->GetLogin().c_str());
355         }
356     }
357 rsu.shortPacketsCount++;
358 rsu.lastSentTime = stgTime;
359
360 packetHead.ip = htonl(rsu.ip);
361 packetHead.id = htonl(rsu.user->GetID());
362 strncpy(reinterpret_cast<char*>(packetHead.login), rsu.user->GetLogin().c_str(), RS_LOGIN_LEN);
363 packetHead.login[RS_LOGIN_LEN - 1] = 0;
364
365 memcpy(buf, &packetHead, sizeof(packetHead));
366
367 if (packetHead.packetType == RS_ALIVE_PACKET)
368     {
369     return false;
370     }
371
372 RS::PACKET_TAIL packetTail;
373
374 memset(packetTail.padding, 0, sizeof(packetTail.padding));
375 memcpy(packetTail.magic, RS_ID, sizeof(RS_ID));
376 std::string params;
377 for (const auto& param : rsSettings.GetUserParams())
378     {
379     auto value = rsu.user->GetParamValue(param);
380     if (params.length() + value.length() > RS_PARAMS_LEN - 1)
381     {
382         logger("Script params string length %d exceeds the limit of %d symbols.", params.length() + value.length(), RS_PARAMS_LEN);
383         break;
384     }
385     params += value + " ";
386     }
387 strncpy(reinterpret_cast<char*>(packetTail.params), params.c_str(), RS_PARAMS_LEN);
388 packetTail.params[RS_PARAMS_LEN - 1] = 0;
389
390 assert(sizeof(packetHead) + sizeof(packetTail) <= bufSize && "Insufficient buffer space");
391
392 Encrypt(buf + sizeof(packetHead), reinterpret_cast<char *>(&packetTail), sizeof(packetTail) / 8);
393
394 return false;
395 }
396 //-----------------------------------------------------------------------------
397 bool REMOTE_SCRIPT::Send(RS::USER & rsu, bool forceDisconnect) const
398 {
399 char buffer[RS_MAX_PACKET_LEN];
400
401 memset(buffer, 0, sizeof(buffer));
402
403 if (PreparePacket(buffer, sizeof(buffer), rsu, forceDisconnect))
404     {
405     printfd(__FILE__, "REMOTE_SCRIPT::Send() - Invalid packet length!\n");
406     return true;
407     }
408
409 for (const auto& ip : rsu.routers)
410 {
411     struct sockaddr_in sendAddr;
412
413     sendAddr.sin_family = AF_INET;
414     sendAddr.sin_port = htons(rsSettings.GetPort());
415     sendAddr.sin_addr.s_addr = ip;
416
417     return sendto(sock, buffer, sizeof(buffer), 0, reinterpret_cast<struct sockaddr*>(&sendAddr), sizeof(sendAddr)) > 0;
418 }
419
420 return false;
421 }
422 //-----------------------------------------------------------------------------
423 bool REMOTE_SCRIPT::SendDirect(RS::USER & rsu, uint32_t routerIP, bool forceDisconnect) const
424 {
425 char buffer[RS_MAX_PACKET_LEN];
426
427 if (PreparePacket(buffer, sizeof(buffer), rsu, forceDisconnect))
428     {
429     printfd(__FILE__, "REMOTE_SCRIPT::SendDirect() - Invalid packet length!\n");
430     return true;
431     }
432
433 struct sockaddr_in sendAddr;
434
435 sendAddr.sin_family = AF_INET;
436 sendAddr.sin_port = htons(rsSettings.GetPort());
437 sendAddr.sin_addr.s_addr = routerIP;
438
439 ssize_t res = sendto(sock, buffer, sizeof(buffer), 0, reinterpret_cast<struct sockaddr *>(&sendAddr), sizeof(sendAddr));
440
441 if (res < 0)
442     logger("sendto error: %s", strerror(errno));
443
444 return (res != sizeof(buffer));
445 }
446 //-----------------------------------------------------------------------------
447 bool REMOTE_SCRIPT::GetUsers()
448 {
449 UserPtr u;
450
451 int h = users->OpenSearch();
452 assert(h && "USERS::OpenSearch is always correct");
453
454 while (users->SearchNext(h, &u) != 0)
455     SetUserNotifiers(u);
456
457 users->CloseSearch(h);
458 return false;
459 }
460 //-----------------------------------------------------------------------------
461 std::vector<uint32_t> REMOTE_SCRIPT::IP2Routers(uint32_t ip)
462 {
463 std::lock_guard lock(m_mutex);
464 for (auto& nr : netRouters)
465     if ((ip & nr.subnetMask) == (nr.subnetIP & nr.subnetMask))
466         return nr.routers;
467 return {};
468 }
469 //-----------------------------------------------------------------------------
470 void REMOTE_SCRIPT::SetUserNotifiers(UserPtr u)
471 {
472 ipNotifierList.push_front(RS::IP_NOTIFIER(*this, u));
473 connNotifierList.push_front(RS::CONNECTED_NOTIFIER(*this, u));
474 }
475 //-----------------------------------------------------------------------------
476 void REMOTE_SCRIPT::UnSetUserNotifiers(UserPtr u)
477 {
478 ipNotifierList.erase(std::remove_if(ipNotifierList.begin(),
479                                     ipNotifierList.end(),
480                                     USER_IS<IP_NOTIFIER>(u)),
481                      ipNotifierList.end());
482 connNotifierList.erase(std::remove_if(connNotifierList.begin(),
483                                       connNotifierList.end(),
484                                       USER_IS<CONNECTED_NOTIFIER>(u)),
485                        connNotifierList.end());
486
487 }
488 //-----------------------------------------------------------------------------
489 void REMOTE_SCRIPT::AddRSU(UserPtr user)
490 {
491 RS::USER rsu(IP2Routers(user->GetCurrIP()), user);
492 Send(rsu);
493
494 std::lock_guard lock(m_mutex);
495 authorizedUsers.insert(std::make_pair(user->GetCurrIP(), rsu));
496 }
497 //-----------------------------------------------------------------------------
498 void REMOTE_SCRIPT::DelRSU(UserPtr user)
499 {
500 std::lock_guard lock(m_mutex);
501 auto it = authorizedUsers.begin();
502 while (it != authorizedUsers.end())
503     {
504     if (it->second.user == user)
505         {
506         Send(it->second, true);
507         authorizedUsers.erase(it);
508         return;
509         }
510     ++it;
511     }
512 /*const auto it = authorizedUsers.find(user->GetCurrIP());
513 if (it != authorizedUsers.end())
514     {
515     Send(it->second, true);
516     authorizedUsers.erase(it);
517     }*/
518 }
519 //-----------------------------------------------------------------------------
520 void RS::IP_NOTIFIER::notify(const uint32_t & /*oldValue*/, const uint32_t & newValue)
521 {
522 if (newValue != 0)
523     rs.AddRSU(user);
524 else
525     rs.DelRSU(user);
526 }
527 //-----------------------------------------------------------------------------
528 void RS::CONNECTED_NOTIFIER::notify(const bool & /*oldValue*/, const bool & newValue)
529 {
530 if (newValue)
531     rs.AddRSU(user);
532 else
533     rs.DelRSU(user);
534 }
535 //-----------------------------------------------------------------------------
536 void REMOTE_SCRIPT::InitEncrypt(const std::string & password) const
537 {
538 unsigned char keyL[PASSWD_LEN];  // Пароль для шифровки
539 memset(keyL, 0, PASSWD_LEN);
540 strncpy(reinterpret_cast<char*>(keyL), password.c_str(), PASSWD_LEN);
541 Blowfish_Init(&ctx, keyL, PASSWD_LEN);
542 }
543 //-----------------------------------------------------------------------------
544 void REMOTE_SCRIPT::Encrypt(void * dst, const void * src, size_t len8) const
545 {
546 if (dst != src)
547     memcpy(dst, src, len8 * 8);
548 for (size_t i = 0; i < len8; ++i)
549     Blowfish_Encrypt(&ctx, static_cast<uint32_t *>(dst) + i * 2, static_cast<uint32_t *>(dst) + i * 2 + 1);
550 }
551 //-----------------------------------------------------------------------------