]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp
Реализована инициализация членов класса RPC_CONFIG через список
[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     : users(NULL),
117       admins(NULL),
118       tariffs(NULL),
119       store(NULL),
120       stgSettings(NULL),
121       rpcServer(NULL),
122       running(false),
123       stopped(true)
124 {
125 }
126
127 RPC_CONFIG::~RPC_CONFIG()
128 {
129 // delete server
130 delete rpcServer;
131 }
132
133 int RPC_CONFIG::ParseSettings()
134 {
135 int ret = rpcConfigSettings.ParseSettings(settings);
136
137 if (ret)
138     errorStr = rpcConfigSettings.GetStrError();
139
140 return ret;
141 }
142
143 int RPC_CONFIG::Start()
144 {
145 InitiateRegistry();
146 running = true;
147 rpcServer = new xmlrpc_c::serverAbyss(
148         rpcRegistry,
149         rpcConfigSettings.GetPort(),
150         "/var/log/stargazer_rpc.log"
151         );
152 if (pthread_create(&tid, NULL, Run, this))
153     {
154     errorStr = "Failed to create RPC thread";
155     printfd(__FILE__, "Failed to crate RPC thread\n");
156     return -1;
157     }
158 return 0;
159 }
160
161 int RPC_CONFIG::Stop()
162 {
163 running = false;
164 for (int i = 0; i < 5 && !stopped; ++i)
165     usleep(200000);
166 //rpcServer->terminate();
167 if (!stopped)
168     {
169     if (pthread_kill(tid, SIGTERM))
170         {
171         errorStr = "Failed to kill thread";
172         printfd(__FILE__, "Failed to kill thread\n");
173         }
174     for (int i = 0; i < 25 && !stopped; ++i)
175         usleep(200000);
176     if (!stopped)
177         {
178         printfd(__FILE__, "Failed to stop RPC thread\n");
179         errorStr = "Failed to stop RPC thread";
180         return -1;
181         }
182     else
183         {
184         pthread_join(tid, NULL);
185         }
186     }
187 return 0;
188 }
189
190 void * RPC_CONFIG::Run(void * rc)
191 {
192 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
193
194 config->stopped = false;
195 while (config->running)
196     {
197     config->rpcServer->runOnce();
198     }
199 config->stopped = true;
200
201 return NULL;
202 }
203
204 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
205                               ADMIN_INFO * info)
206 {
207 std::map<std::string,
208          ADMIN_INFO>::iterator it;
209
210 it = cookies.find(cookie);
211
212 if (it == cookies.end())
213     {
214     return true;
215     }
216
217 if (difftime(it->second.accessTime, time(NULL)) >
218     rpcConfigSettings.GetCookieTimeout())
219     {
220     cookies.erase(it);
221     return true;
222     }
223
224 // Update access time
225 time(&it->second.accessTime);
226 *info = it->second;
227 return false;
228 }
229
230 bool RPC_CONFIG::CheckAdmin(const std::string & login,
231                             const std::string & password,
232                             std::string * cookie)
233 {
234 ADMIN admin;
235
236 if (!admins->AdminCorrect(login, password, &admin))
237     {
238     return true;
239     }
240
241 ADMIN_INFO info;
242 time(&info.accessTime);
243 info.admin = login;
244 info.priviledges = *admin.GetPriv();
245 *cookie = GetCookie();
246 cookies[*cookie] = info;
247
248 return false;
249 }
250
251 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
252 {
253 std::map<std::string,
254          ADMIN_INFO>::iterator it;
255
256 it = cookies.find(cookie);
257
258 if (it == cookies.end())
259     {
260     return true;
261     }
262
263 cookies.erase(it);
264
265 return false;
266 }
267
268 std::string RPC_CONFIG::GetCookie() const
269 {
270 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
271 std::string cookie;
272
273 for (int i = 0; i < 64; ++i)
274     {
275     cookie += charset[rand() % charset.length()];
276     };
277
278 return cookie;
279 }
280
281 void RPC_CONFIG::InitiateRegistry()
282 {
283 // manage registry
284 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
285             tariffs,
286             users,
287             stgSettings
288             ));
289 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
290
291 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
292             this
293             ));
294 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
295
296 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
297             this
298             ));
299 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
300
301 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
302             this,
303             users
304             ));
305 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
306
307 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
308             this,
309             admins,
310             users
311             ));
312 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
313
314 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
315             this,
316             admins,
317             users
318             ));
319 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
320
321 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
322             this,
323             users
324             ));
325 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
326
327 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
328             this,
329             admins,
330             tariffs,
331             store,
332             users
333             ));
334 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
335
336 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
337             this,
338             admins,
339             store,
340             users
341             ));
342 rpcRegistry.addMethod("stargazer.add_cash", methodAddCashPtr);
343
344 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
345             this,
346             admins,
347             store,
348             users
349             ));
350 rpcRegistry.addMethod("stargazer.set_cash", methodSetCashPtr);
351
352 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
353             this,
354             admins,
355             tariffs,
356             store,
357             users
358             ));
359 rpcRegistry.addMethod("stargazer.tariff_change", methodTariffChangePtr);
360
361 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
362             this,
363             tariffs
364             ));
365 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
366
367 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
368             this,
369             admins,
370             tariffs
371             ));
372 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
373
374 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
375             this,
376             tariffs
377             ));
378 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
379
380 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
381             this,
382             admins,
383             tariffs
384             ));
385 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
386
387 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
388             this,
389             admins,
390             tariffs,
391             users
392             ));
393 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
394
395 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
396             this,
397             admins
398             ));
399 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
400
401 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
402             this,
403             admins
404             ));
405 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
406
407 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
408             this,
409             admins
410             ));
411 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
412
413 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
414             this,
415             admins
416             ));
417 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
418
419 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
420             this,
421             admins
422             ));
423 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
424
425 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
426             this,
427             users
428             ));
429 rpcRegistry.addMethod("stargazer.send_message", methodSendMessagePtr);
430
431 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
432             this,
433             users
434             ));
435 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);
436 }
437