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"
13 #include "stg/plugin_creator.h"
15 #include "rpcconfig.h"
16 #include "info_methods.h"
17 #include "users_methods.h"
18 #include "tariffs_methods.h"
19 #include "admins_methods.h"
20 #include "messages_methods.h"
22 PLUGIN_CREATOR<RPC_CONFIG> rpcc;
24 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
31 int RPC_CONFIG_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
35 std::vector<PARAM_VALUE>::const_iterator pvi;
38 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
39 if (pvi == s.moduleParams.end())
41 errorStr = "Parameter \'Port\' not found.";
42 printfd(__FILE__, "Parameter 'Port' not found\n");
45 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
47 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
48 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
53 pv.param = "CookieTimeout";
54 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
55 if (pvi == s.moduleParams.end())
57 cookieTimeout = 1800; // 30 * 60
61 if (str2x(pvi->value[0], cookieTimeout))
63 errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
64 printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
74 return rpcc.GetPlugin();
77 RPC_CONFIG::RPC_CONFIG()
96 RPC_CONFIG::~RPC_CONFIG()
102 int RPC_CONFIG::ParseSettings()
104 int ret = rpcConfigSettings.ParseSettings(settings);
107 errorStr = rpcConfigSettings.GetStrError();
112 void RPC_CONFIG::SetStgSettings(const SETTINGS * settings)
114 dayFee = settings->GetDayFee();
115 dirNames.erase(dirNames.begin(), dirNames.end());
116 for (size_t i = 0; i < DIR_NUM; ++i) {
117 dirNames.push_back(settings->GetDirName(i));
121 int RPC_CONFIG::Start()
125 rpcServer = new xmlrpc_c::serverAbyss(
127 rpcConfigSettings.GetPort(),
128 "/var/log/stargazer_rpc.log"
130 if (pthread_create(&tid, NULL, Run, this))
132 errorStr = "Failed to create RPC thread";
133 printfd(__FILE__, "Failed to crate RPC thread\n");
139 int RPC_CONFIG::Stop()
142 for (int i = 0; i < 5 && !stopped; ++i)
144 //rpcServer->terminate();
147 if (pthread_kill(tid, SIGTERM))
149 errorStr = "Failed to kill thread";
150 printfd(__FILE__, "Failed to kill thread\n");
152 for (int i = 0; i < 25 && !stopped; ++i)
156 printfd(__FILE__, "Failed to stop RPC thread\n");
157 errorStr = "Failed to stop RPC thread";
162 pthread_join(tid, NULL);
168 void * RPC_CONFIG::Run(void * rc)
170 RPC_CONFIG * config = static_cast<RPC_CONFIG *>(rc);
172 config->stopped = false;
173 while (config->running)
175 config->rpcServer->runOnce();
177 config->stopped = true;
182 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
185 std::map<std::string,
186 ADMIN_INFO>::iterator it;
188 it = cookies.find(cookie);
190 if (it == cookies.end())
195 if (difftime(it->second.accessTime, time(NULL)) >
196 rpcConfigSettings.GetCookieTimeout())
202 // Update access time
203 time(&it->second.accessTime);
208 bool RPC_CONFIG::CheckAdmin(const std::string & login,
209 const std::string & password,
210 std::string * cookie)
212 ADMIN * admin = NULL;
214 if (!admins->Correct(login, password, &admin))
220 time(&info.accessTime);
222 info.priviledges = *admin->GetPriv();
223 *cookie = GetCookie();
224 cookies[*cookie] = info;
229 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
231 std::map<std::string,
232 ADMIN_INFO>::iterator it;
234 it = cookies.find(cookie);
236 if (it == cookies.end())
246 std::string RPC_CONFIG::GetCookie() const
248 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
251 for (int i = 0; i < 64; ++i)
253 cookie += charset[rand() % charset.length()];
259 void RPC_CONFIG::InitiateRegistry()
262 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
268 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
270 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
273 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
275 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
278 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
280 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
284 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
286 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
291 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
293 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
298 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
300 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
304 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
306 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
313 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
315 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
321 rpcRegistry.addMethod("stargazer.add_user_cash", methodAddCashPtr);
323 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
329 rpcRegistry.addMethod("stargazer.set_user_cash", methodSetCashPtr);
331 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
338 rpcRegistry.addMethod("stargazer.chg_user_tariff", methodTariffChangePtr);
340 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
344 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
346 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
351 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
353 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
357 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
359 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
364 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
366 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
372 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
374 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
378 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
380 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
384 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
386 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
390 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
392 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
396 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
398 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
402 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
404 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
408 rpcRegistry.addMethod("stargazer.send_user_message", methodSendMessagePtr);
410 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
414 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);