1 #include <unistd.h> // TODO: usleep
9 #include "stg/common.h"
10 #include "stg/admin.h"
11 #include "stg/module_settings.h"
12 #include "stg/settings.h"
14 #include "rpcconfig.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 {
23 RPC_CONFIG * rpcconfig;
27 : rpcconfig(new RPC_CONFIG())
35 RPC_CONFIG * GetPlugin()
41 RPC_CONFIG_CREATOR rpcc;
43 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
50 int RPC_CONFIG_SETTINGS::ParseIntInRange(const std::string & str,
55 if (str2x(str.c_str(), *val))
57 errorStr = "Incorrect value \'" + str + "\'.";
60 if (*val < min || *val > max)
62 errorStr = "Value \'" + str + "\' out of range.";
68 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
72 std::vector<PARAM_VALUE>::const_iterator pvi;
75 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
76 if (pvi == s.moduleParams.end())
78 errorStr = "Parameter \'Port\' not found.";
79 printfd(__FILE__, "Parameter 'Port' not found\n");
82 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
84 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
85 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
90 pv.param = "CookieTimeout";
91 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
92 if (pvi == s.moduleParams.end())
94 cookieTimeout = 1800; // 30 * 60
98 if (str2x(pvi->value[0], cookieTimeout))
100 errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
101 printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
111 return rpcc.GetPlugin();
114 RPC_CONFIG::RPC_CONFIG()
126 RPC_CONFIG::~RPC_CONFIG()
132 int RPC_CONFIG::ParseSettings()
134 int ret = rpcConfigSettings.ParseSettings(settings);
137 errorStr = rpcConfigSettings.GetStrError();
142 void RPC_CONFIG::SetStgSettings(const SETTINGS * settings)
144 dayFee = settings->GetDayFee();
145 dirNames.erase(dirNames.begin(), dirNames.end());
146 for (size_t i = 0; i < DIR_NUM; ++i) {
147 dirNames.push_back(settings->GetDirName(i));
151 int RPC_CONFIG::Start()
155 rpcServer = new xmlrpc_c::serverAbyss(
157 rpcConfigSettings.GetPort(),
158 "/var/log/stargazer_rpc.log"
160 if (pthread_create(&tid, NULL, Run, this))
162 errorStr = "Failed to create RPC thread";
163 printfd(__FILE__, "Failed to crate RPC thread\n");
169 int RPC_CONFIG::Stop()
172 for (int i = 0; i < 5 && !stopped; ++i)
174 //rpcServer->terminate();
177 if (pthread_kill(tid, SIGTERM))
179 errorStr = "Failed to kill thread";
180 printfd(__FILE__, "Failed to kill thread\n");
182 for (int i = 0; i < 25 && !stopped; ++i)
186 printfd(__FILE__, "Failed to stop RPC thread\n");
187 errorStr = "Failed to stop RPC thread";
192 pthread_join(tid, NULL);
198 void * RPC_CONFIG::Run(void * rc)
200 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
202 config->stopped = false;
203 while (config->running)
205 config->rpcServer->runOnce();
207 config->stopped = true;
212 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
215 std::map<std::string,
216 ADMIN_INFO>::iterator it;
218 it = cookies.find(cookie);
220 if (it == cookies.end())
225 if (difftime(it->second.accessTime, time(NULL)) >
226 rpcConfigSettings.GetCookieTimeout())
232 // Update access time
233 time(&it->second.accessTime);
238 bool RPC_CONFIG::CheckAdmin(const std::string & login,
239 const std::string & password,
240 std::string * cookie)
242 ADMIN * admin = NULL;
244 if (!admins->AdminCorrect(login, password, &admin))
250 time(&info.accessTime);
252 info.priviledges = *admin->GetPriv();
253 *cookie = GetCookie();
254 cookies[*cookie] = info;
259 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
261 std::map<std::string,
262 ADMIN_INFO>::iterator it;
264 it = cookies.find(cookie);
266 if (it == cookies.end())
276 std::string RPC_CONFIG::GetCookie() const
278 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
281 for (int i = 0; i < 64; ++i)
283 cookie += charset[rand() % charset.length()];
289 void RPC_CONFIG::InitiateRegistry()
292 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
298 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
300 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
303 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
305 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
308 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
310 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
314 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
316 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
321 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
323 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
328 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
330 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
334 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
336 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
343 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
345 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
351 rpcRegistry.addMethod("stargazer.add_user_cash", methodAddCashPtr);
353 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
359 rpcRegistry.addMethod("stargazer.set_user_cash", methodSetCashPtr);
361 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
368 rpcRegistry.addMethod("stargazer.chg_user_tariff", methodTariffChangePtr);
370 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
374 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
376 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
381 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
383 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
387 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
389 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
394 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
396 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
402 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
404 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
408 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
410 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
414 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
416 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
420 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
422 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
426 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
428 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
432 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
434 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
438 rpcRegistry.addMethod("stargazer.send_user_message", methodSendMessagePtr);
440 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
444 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);