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"
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"
21 class RPC_CONFIG_CREATOR
24 RPC_CONFIG * rpcconfig;
28 : rpcconfig(new RPC_CONFIG())
36 RPC_CONFIG * GetPlugin()
42 RPC_CONFIG_CREATOR rpcc;
44 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
51 int RPC_CONFIG_SETTINGS::ParseIntInRange(const string & str,
56 if (str2x(str.c_str(), *val))
58 errorStr = "Incorrect value \'" + str + "\'.";
61 if (*val < min || *val > max)
63 errorStr = "Value \'" + str + "\' out of range.";
69 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
73 vector<PARAM_VALUE>::const_iterator pvi;
76 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
77 if (pvi == s.moduleParams.end())
79 errorStr = "Parameter \'Port\' not found.";
80 printfd(__FILE__, "Parameter 'Port' not found\n");
83 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
85 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
86 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
91 pv.param = "CookieTimeout";
92 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
93 if (pvi == s.moduleParams.end())
95 cookieTimeout = 1800; // 30 * 60
99 if (str2x(pvi->value[0], cookieTimeout))
101 errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
102 printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
110 BASE_PLUGIN * GetPlugin()
112 return rpcc.GetPlugin();
115 RPC_CONFIG::RPC_CONFIG()
121 RPC_CONFIG::~RPC_CONFIG()
127 int RPC_CONFIG::ParseSettings()
129 int ret = rpcConfigSettings.ParseSettings(settings);
132 errorStr = rpcConfigSettings.GetStrError();
137 int RPC_CONFIG::Start()
141 rpcServer = new xmlrpc_c::serverAbyss(
143 rpcConfigSettings.GetPort(),
144 "/var/log/stargazer_rpc.log"
146 if (pthread_create(&tid, NULL, Run, this))
148 errorStr = "Failed to create RPC thread";
149 printfd(__FILE__, "Failed to crate RPC thread\n");
155 int RPC_CONFIG::Stop()
158 for (int i = 0; i < 5 && !stopped; ++i)
160 //rpcServer->terminate();
163 if (pthread_kill(tid, SIGTERM))
165 errorStr = "Failed to kill thread";
166 printfd(__FILE__, "Failed to kill thread\n");
168 for (int i = 0; i < 25 && !stopped; ++i)
172 printfd(__FILE__, "Failed to stop RPC thread\n");
173 errorStr = "Failed to stop RPC thread";
178 pthread_join(tid, NULL);
184 void * RPC_CONFIG::Run(void * rc)
186 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
188 config->stopped = false;
189 while (config->running)
191 config->rpcServer->runOnce();
193 config->stopped = true;
198 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
201 std::map<std::string,
202 ADMIN_INFO>::iterator it;
204 it = cookies.find(cookie);
206 if (it == cookies.end())
211 if (difftime(it->second.accessTime, time(NULL)) >
212 rpcConfigSettings.GetCookieTimeout())
218 // Update access time
219 time(&it->second.accessTime);
224 bool RPC_CONFIG::CheckAdmin(const std::string & login,
225 const std::string & password,
226 std::string * cookie)
230 if (!admins->AdminCorrect(login, password, &admin))
236 time(&info.accessTime);
238 info.priviledges = *admin.GetPriv();
239 *cookie = GetCookie();
240 cookies[*cookie] = info;
245 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
247 std::map<std::string,
248 ADMIN_INFO>::iterator it;
250 it = cookies.find(cookie);
252 if (it == cookies.end())
262 std::string RPC_CONFIG::GetCookie() const
264 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
267 for (int i = 0; i < 64; ++i)
269 cookie += charset[rand() % charset.length()];
275 void RPC_CONFIG::InitiateRegistry()
278 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
283 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
285 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
288 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
290 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
293 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
295 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
299 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
301 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
306 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
308 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
313 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
315 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
319 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
321 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
328 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
330 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
336 rpcRegistry.addMethod("stargazer.add_cash", methodAddCashPtr);
338 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
344 rpcRegistry.addMethod("stargazer.set_cash", methodSetCashPtr);
346 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
353 rpcRegistry.addMethod("stargazer.tariff_change", methodTariffChangePtr);
355 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
359 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
361 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
366 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
368 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
372 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
374 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
379 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
381 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
387 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
389 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
393 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
395 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
399 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
401 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
405 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
407 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
411 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
413 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
417 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
419 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
423 rpcRegistry.addMethod("stargazer.send_message", methodSendMessagePtr);
425 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
429 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);