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()
127 RPC_CONFIG::~RPC_CONFIG()
133 int RPC_CONFIG::ParseSettings()
135 int ret = rpcConfigSettings.ParseSettings(settings);
138 errorStr = rpcConfigSettings.GetStrError();
143 int RPC_CONFIG::Start()
147 rpcServer = new xmlrpc_c::serverAbyss(
149 rpcConfigSettings.GetPort(),
150 "/var/log/stargazer_rpc.log"
152 if (pthread_create(&tid, NULL, Run, this))
154 errorStr = "Failed to create RPC thread";
155 printfd(__FILE__, "Failed to crate RPC thread\n");
161 int RPC_CONFIG::Stop()
164 for (int i = 0; i < 5 && !stopped; ++i)
166 //rpcServer->terminate();
169 if (pthread_kill(tid, SIGTERM))
171 errorStr = "Failed to kill thread";
172 printfd(__FILE__, "Failed to kill thread\n");
174 for (int i = 0; i < 25 && !stopped; ++i)
178 printfd(__FILE__, "Failed to stop RPC thread\n");
179 errorStr = "Failed to stop RPC thread";
184 pthread_join(tid, NULL);
190 void * RPC_CONFIG::Run(void * rc)
192 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
194 config->stopped = false;
195 while (config->running)
197 config->rpcServer->runOnce();
199 config->stopped = true;
204 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
207 std::map<std::string,
208 ADMIN_INFO>::iterator it;
210 it = cookies.find(cookie);
212 if (it == cookies.end())
217 if (difftime(it->second.accessTime, time(NULL)) >
218 rpcConfigSettings.GetCookieTimeout())
224 // Update access time
225 time(&it->second.accessTime);
230 bool RPC_CONFIG::CheckAdmin(const std::string & login,
231 const std::string & password,
232 std::string * cookie)
236 if (!admins->AdminCorrect(login, password, &admin))
242 time(&info.accessTime);
244 info.priviledges = *admin.GetPriv();
245 *cookie = GetCookie();
246 cookies[*cookie] = info;
251 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
253 std::map<std::string,
254 ADMIN_INFO>::iterator it;
256 it = cookies.find(cookie);
258 if (it == cookies.end())
268 std::string RPC_CONFIG::GetCookie() const
270 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
273 for (int i = 0; i < 64; ++i)
275 cookie += charset[rand() % charset.length()];
281 void RPC_CONFIG::InitiateRegistry()
284 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
289 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
291 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
294 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
296 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
299 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
301 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
305 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
307 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
312 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
314 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
319 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
321 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
325 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
327 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
334 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
336 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
342 rpcRegistry.addMethod("stargazer.add_cash", methodAddCashPtr);
344 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
350 rpcRegistry.addMethod("stargazer.set_cash", methodSetCashPtr);
352 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
359 rpcRegistry.addMethod("stargazer.tariff_change", methodTariffChangePtr);
361 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
365 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
367 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
372 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
374 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
378 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
380 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
385 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
387 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
393 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
395 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
399 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
401 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
405 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
407 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
411 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
413 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
417 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
419 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
423 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
425 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
429 rpcRegistry.addMethod("stargazer.send_message", methodSendMessagePtr);
431 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
435 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);