]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp
Уменьшена зависимостть между заголовочными файлами путем использования
[stg.git] / projects / stargazer / plugins / configuration / rpcconfig / rpcconfig.cpp
1 #include <cstdlib>
2 #include <csignal>
3
4 #include "rpcconfig.h"
5
6 #include "../../../admin.h"
7 #include "../../../admins.h"
8 #include "../../../users.h"
9 #include "../../../tariffs.h"
10 #include "../../../traffcounter.h"
11 #include "../../../settings.h"
12 #include "base_store.h"
13 #include "base_settings.h"
14
15 #include "info_methods.h"
16 #include "users_methods.h"
17 #include "tariffs_methods.h"
18 #include "admins_methods.h"
19 #include "messages_methods.h"
20
21 class RPC_CONFIG_CREATOR
22 {
23 private:
24     RPC_CONFIG * rpcconfig;
25
26 public:
27     RPC_CONFIG_CREATOR()
28         : rpcconfig(new RPC_CONFIG())
29         {
30         };
31     ~RPC_CONFIG_CREATOR()
32         {
33         delete rpcconfig;
34         };
35
36     RPC_CONFIG * GetPlugin()
37         {
38         return rpcconfig;
39         };
40 };
41
42 RPC_CONFIG_CREATOR rpcc;
43
44 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
45     : errorStr(),
46       port(0),
47       cookieTimeout(0)
48 {
49 }
50
51 int RPC_CONFIG_SETTINGS::ParseIntInRange(const string & str,
52                                          int min,
53                                          int max,
54                                          int * val)
55 {
56 if (str2x(str.c_str(), *val))
57     {
58     errorStr = "Incorrect value \'" + str + "\'.";
59     return -1;
60     }
61 if (*val < min || *val > max)
62     {
63     errorStr = "Value \'" + str + "\' out of range.";
64     return -1;
65     }
66 return 0;
67 }
68
69 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
70 {
71 int p;
72 PARAM_VALUE pv;
73 vector<PARAM_VALUE>::const_iterator pvi;
74
75 pv.param = "Port";
76 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
77 if (pvi == s.moduleParams.end())
78     {
79     errorStr = "Parameter \'Port\' not found.";
80     printfd(__FILE__, "Parameter 'Port' not found\n");
81     return -1;
82     }
83 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
84     {
85     errorStr = "Cannot parse parameter \'Port\': " + errorStr;
86     printfd(__FILE__, "Cannot parse parameter 'Port'\n");
87     return -1;
88     }
89 port = p;
90
91 pv.param = "CookieTimeout";
92 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
93 if (pvi == s.moduleParams.end())
94     {
95     cookieTimeout = 1800; // 30 * 60
96     }
97 else
98     {
99     if (str2x(pvi->value[0], cookieTimeout))
100         {
101         errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
102         printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
103         return -1;
104         }
105     }
106
107 return 0;
108 }
109
110 BASE_PLUGIN * GetPlugin()
111 {
112 return rpcc.GetPlugin();
113 }
114
115 RPC_CONFIG::RPC_CONFIG()
116     : rpcServer(NULL)
117 {
118
119 }
120
121 RPC_CONFIG::~RPC_CONFIG()
122 {
123 // delete server
124 delete rpcServer;
125 }
126
127 int RPC_CONFIG::ParseSettings()
128 {
129 int ret = rpcConfigSettings.ParseSettings(settings);
130
131 if (ret)
132     errorStr = rpcConfigSettings.GetStrError();
133
134 return ret;
135 }
136
137 int RPC_CONFIG::Start()
138 {
139 InitiateRegistry();
140 running = true;
141 rpcServer = new xmlrpc_c::serverAbyss(
142         rpcRegistry,
143         rpcConfigSettings.GetPort(),
144         "/var/log/stargazer_rpc.log"
145         );
146 if (pthread_create(&tid, NULL, Run, this))
147     {
148     errorStr = "Failed to create RPC thread";
149     printfd(__FILE__, "Failed to crate RPC thread\n");
150     return -1;
151     }
152 return 0;
153 }
154
155 int RPC_CONFIG::Stop()
156 {
157 running = false;
158 for (int i = 0; i < 5 && !stopped; ++i)
159     usleep(200000);
160 //rpcServer->terminate();
161 if (!stopped)
162     {
163     if (pthread_kill(tid, SIGTERM))
164         {
165         errorStr = "Failed to kill thread";
166         printfd(__FILE__, "Failed to kill thread\n");
167         }
168     for (int i = 0; i < 25 && !stopped; ++i)
169         usleep(200000);
170     if (!stopped)
171         {
172         printfd(__FILE__, "Failed to stop RPC thread\n");
173         errorStr = "Failed to stop RPC thread";
174         return -1;
175         }
176     else
177         {
178         pthread_join(tid, NULL);
179         }
180     }
181 return 0;
182 }
183
184 void * RPC_CONFIG::Run(void * rc)
185 {
186 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
187
188 config->stopped = false;
189 while (config->running)
190     {
191     config->rpcServer->runOnce();
192     }
193 config->stopped = true;
194
195 return NULL;
196 }
197
198 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
199                               ADMIN_INFO * info)
200 {
201 std::map<std::string,
202          ADMIN_INFO>::iterator it;
203
204 it = cookies.find(cookie);
205
206 if (it == cookies.end())
207     {
208     return true;
209     }
210
211 if (difftime(it->second.accessTime, time(NULL)) >
212     rpcConfigSettings.GetCookieTimeout())
213     {
214     cookies.erase(it);
215     return true;
216     }
217
218 // Update access time
219 time(&it->second.accessTime);
220 *info = it->second;
221 return false;
222 }
223
224 bool RPC_CONFIG::CheckAdmin(const std::string & login,
225                             const std::string & password,
226                             std::string * cookie)
227 {
228 ADMIN admin;
229
230 if (!admins->AdminCorrect(login, password, &admin))
231     {
232     return true;
233     }
234
235 ADMIN_INFO info;
236 time(&info.accessTime);
237 info.admin = login;
238 info.priviledges = *admin.GetPriv();
239 *cookie = GetCookie();
240 cookies[*cookie] = info;
241
242 return false;
243 }
244
245 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
246 {
247 std::map<std::string,
248          ADMIN_INFO>::iterator it;
249
250 it = cookies.find(cookie);
251
252 if (it == cookies.end())
253     {
254     return true;
255     }
256
257 cookies.erase(it);
258
259 return false;
260 }
261
262 std::string RPC_CONFIG::GetCookie() const
263 {
264 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
265 std::string cookie;
266
267 for (int i = 0; i < 64; ++i)
268     {
269     cookie += charset[rand() % charset.length()];
270     };
271
272 return cookie;
273 }
274
275 void RPC_CONFIG::InitiateRegistry()
276 {
277 // manage registry
278 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
279             tariffs,
280             users,
281             stgSettings
282             ));
283 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
284
285 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
286             this
287             ));
288 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
289
290 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
291             this
292             ));
293 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
294
295 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
296             this,
297             users
298             ));
299 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
300
301 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
302             this,
303             admins,
304             users
305             ));
306 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
307
308 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
309             this,
310             admins,
311             users
312             ));
313 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
314
315 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
316             this,
317             users
318             ));
319 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
320
321 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
322             this,
323             admins,
324             tariffs,
325             store,
326             users
327             ));
328 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
329
330 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
331             this,
332             admins,
333             store,
334             users
335             ));
336 rpcRegistry.addMethod("stargazer.add_cash", methodAddCashPtr);
337
338 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
339             this,
340             admins,
341             store,
342             users
343             ));
344 rpcRegistry.addMethod("stargazer.set_cash", methodSetCashPtr);
345
346 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
347             this,
348             admins,
349             tariffs,
350             store,
351             users
352             ));
353 rpcRegistry.addMethod("stargazer.tariff_change", methodTariffChangePtr);
354
355 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
356             this,
357             tariffs
358             ));
359 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
360
361 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
362             this,
363             admins,
364             tariffs
365             ));
366 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
367
368 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
369             this,
370             tariffs
371             ));
372 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
373
374 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
375             this,
376             admins,
377             tariffs
378             ));
379 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
380
381 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
382             this,
383             admins,
384             tariffs,
385             users
386             ));
387 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
388
389 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
390             this,
391             admins
392             ));
393 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
394
395 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
396             this,
397             admins
398             ));
399 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
400
401 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
402             this,
403             admins
404             ));
405 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
406
407 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
408             this,
409             admins
410             ));
411 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
412
413 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
414             this,
415             admins
416             ));
417 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
418
419 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
420             this,
421             users
422             ));
423 rpcRegistry.addMethod("stargazer.send_message", methodSendMessagePtr);
424
425 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
426             this,
427             users
428             ));
429 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);
430 }
431