3 #include "info_methods.h"
4 #include "users_methods.h"
5 #include "tariffs_methods.h"
6 #include "admins_methods.h"
7 #include "messages_methods.h"
9 #include "stg/admins.h"
10 #include "stg/admin.h"
11 #include "stg/module_settings.h"
12 #include "stg/settings.h"
13 #include "stg/common.h"
14 #include "stg/const.h"
18 #include <ostream> // xmlrpc-c devs have missed something :)
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
30 RPC_CONFIG_SETTINGS::RPC_CONFIG_SETTINGS()
36 int RPC_CONFIG_SETTINGS::ParseSettings(const STG::ModuleSettings & s)
40 std::vector<STG::ParamValue>::const_iterator pvi;
41 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
42 if (pvi == s.moduleParams.end() || pvi->value.empty())
44 errorStr = "Parameter \'Port\' not found.";
45 printfd(__FILE__, "Parameter 'Port' not found\n");
49 if (ParseIntInRange(pvi->value[0], 2, 65535, &p))
51 errorStr = "Cannot parse parameter \'Port\': " + errorStr;
52 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
55 port = static_cast<uint16_t>(p);
57 pv.param = "CookieTimeout";
58 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
59 if (pvi == s.moduleParams.end() || pvi->value.empty())
61 cookieTimeout = 1800; // 30 * 60
65 if (str2x(pvi->value[0], cookieTimeout))
67 errorStr = "Incorrect value of CookieTimeout: \'" + pvi->value[0] + "\'";
68 printfd(__FILE__, "Incorrect value of 'CookieTimeout'\n");
76 extern "C" STG::Plugin* GetPlugin()
78 static RPC_CONFIG plugin;
82 RPC_CONFIG::RPC_CONFIG()
91 logger(STG::PluginLogger::get("conf_rpc"))
95 RPC_CONFIG::~RPC_CONFIG()
101 int RPC_CONFIG::ParseSettings()
103 int ret = rpcConfigSettings.ParseSettings(settings);
106 errorStr = rpcConfigSettings.GetStrError();
111 void RPC_CONFIG::SetStgSettings(const STG::Settings * s)
113 dayFee = s->GetDayFee();
114 dirNames.erase(dirNames.begin(), dirNames.end());
115 for (size_t i = 0; i < DIR_NUM; ++i) {
116 dirNames.push_back(s->GetDirName(i));
120 int RPC_CONFIG::Start()
124 fd = socket(AF_INET, SOCK_STREAM, 0);
127 errorStr = "Failed to create socket";
128 logger("Cannot create a socket: %s", strerror(errno));
129 printfd(__FILE__, "Failed to create listening socket: %s\n", strerror(errno));
135 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)))
137 errorStr = "Setsockopt failed.";
138 logger("setsockopt error: %s", strerror(errno));
139 printfd(__FILE__, "Setsockopt failed: %s\n", strerror(errno));
143 struct sockaddr_in addr;
144 addr.sin_family = AF_INET;
145 addr.sin_port = htons(rpcConfigSettings.GetPort());
146 addr.sin_addr.s_addr = inet_addr("0.0.0.0");
148 if (bind(fd, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)))
150 logger("Cannot bind the socket: %s", strerror(errno));
151 errorStr = "Failed to bind socket";
152 printfd(__FILE__, "Failed to bind listening socket: %s\n", strerror(errno));
158 logger("Cannot listen the socket: %s", strerror(errno));
159 errorStr = "Failed to listen socket";
160 printfd(__FILE__, "Failed to listen listening socket: %s\n", strerror(errno));
164 rpcServer = new xmlrpc_c::serverAbyss(
165 xmlrpc_c::serverAbyss::constrOpt()
166 .registryP(&rpcRegistry)
167 .logFileName("/var/log/stargazer_rpc.log")
171 m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
176 int RPC_CONFIG::Stop()
178 m_thread.request_stop();
179 for (int i = 0; i < 5 && !stopped; ++i)
181 struct timespec ts = {0, 200000000};
182 nanosleep(&ts, NULL);
188 logger("Cannot stop RPC thread.");
189 printfd(__FILE__, "Failed to stop RPC thread\n");
190 errorStr = "Failed to stop RPC thread";
201 void RPC_CONFIG::Run(std::stop_token token)
204 sigfillset(&signalSet);
205 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
208 while (!token.stop_requested())
211 rpcServer->runOnce();
216 bool RPC_CONFIG::GetAdminInfo(const std::string & cookie,
219 std::map<std::string,
220 ADMIN_INFO>::iterator it;
222 it = cookies.find(cookie);
224 if (it == cookies.end())
229 if (difftime(it->second.accessTime, time(NULL)) >
230 rpcConfigSettings.GetCookieTimeout())
236 // Update access time
237 time(&it->second.accessTime);
242 bool RPC_CONFIG::CheckAdmin(const std::string & login,
243 const std::string & password,
244 std::string * cookie)
246 STG::Admin * admin = NULL;
248 if (!admins->correct(login, password, &admin))
250 logger("Attempt to connect with invalid credentials. Login: %s", login.c_str());
255 time(&info.accessTime);
257 info.priviledges = admin->priv();
258 *cookie = GetCookie();
259 cookies[*cookie] = info;
264 bool RPC_CONFIG::LogoutAdmin(const std::string & cookie)
266 std::map<std::string,
267 ADMIN_INFO>::iterator it;
269 it = cookies.find(cookie);
271 if (it == cookies.end())
281 std::string RPC_CONFIG::GetCookie() const
283 std::string charset("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
286 for (int i = 0; i < 64; ++i)
288 cookie += charset[rand() % charset.length()];
294 void RPC_CONFIG::InitiateRegistry()
297 xmlrpc_c::methodPtr const methodInfoPtr(new METHOD_INFO(
303 rpcRegistry.addMethod("stargazer.info", methodInfoPtr);
305 xmlrpc_c::methodPtr const methodLoginPtr(new METHOD_LOGIN(
308 rpcRegistry.addMethod("stargazer.login", methodLoginPtr);
310 xmlrpc_c::methodPtr const methodLogoutPtr(new METHOD_LOGOUT(
313 rpcRegistry.addMethod("stargazer.logout", methodLogoutPtr);
315 xmlrpc_c::methodPtr const methodGetUserPtr(new METHOD_USER_GET(
319 rpcRegistry.addMethod("stargazer.get_user", methodGetUserPtr);
321 xmlrpc_c::methodPtr const methodAddUserPtr(new METHOD_USER_ADD(
326 rpcRegistry.addMethod("stargazer.add_user", methodAddUserPtr);
328 xmlrpc_c::methodPtr const methodDelUserPtr(new METHOD_USER_DEL(
333 rpcRegistry.addMethod("stargazer.del_user", methodDelUserPtr);
335 xmlrpc_c::methodPtr const methodGetUsersPtr(new METHOD_USERS_GET(
339 rpcRegistry.addMethod("stargazer.get_users", methodGetUsersPtr);
341 xmlrpc_c::methodPtr const methodChgUserPtr(new METHOD_USER_CHG(
348 rpcRegistry.addMethod("stargazer.chg_user", methodChgUserPtr);
350 xmlrpc_c::methodPtr const methodAddCashPtr(new METHOD_USER_CASH_ADD(
356 rpcRegistry.addMethod("stargazer.add_user_cash", methodAddCashPtr);
358 xmlrpc_c::methodPtr const methodSetCashPtr(new METHOD_USER_CASH_SET(
364 rpcRegistry.addMethod("stargazer.set_user_cash", methodSetCashPtr);
366 xmlrpc_c::methodPtr const methodTariffChangePtr(new METHOD_USER_TARIFF_CHANGE(
373 rpcRegistry.addMethod("stargazer.chg_user_tariff", methodTariffChangePtr);
375 xmlrpc_c::methodPtr const methodGetTariffPtr(new METHOD_TARIFF_GET(
379 rpcRegistry.addMethod("stargazer.get_tariff", methodGetTariffPtr);
381 xmlrpc_c::methodPtr const methodChgTariffPtr(new METHOD_TARIFF_CHG(
386 rpcRegistry.addMethod("stargazer.chg_tariff", methodChgTariffPtr);
388 xmlrpc_c::methodPtr const methodGetTariffsPtr(new METHOD_TARIFFS_GET(
392 rpcRegistry.addMethod("stargazer.get_tariffs", methodGetTariffsPtr);
394 xmlrpc_c::methodPtr const methodAddTariffPtr(new METHOD_TARIFF_ADD(
399 rpcRegistry.addMethod("stargazer.add_tariff", methodAddTariffPtr);
401 xmlrpc_c::methodPtr const methodDelTariffPtr(new METHOD_TARIFF_DEL(
407 rpcRegistry.addMethod("stargazer.del_tariff", methodDelTariffPtr);
409 xmlrpc_c::methodPtr const methodGetAdminPtr(new METHOD_ADMIN_GET(
413 rpcRegistry.addMethod("stargazer.get_admin", methodGetAdminPtr);
415 xmlrpc_c::methodPtr const methodAddAdminPtr(new METHOD_ADMIN_ADD(
419 rpcRegistry.addMethod("stargazer.add_admin", methodAddAdminPtr);
421 xmlrpc_c::methodPtr const methodDelAdminPtr(new METHOD_ADMIN_DEL(
425 rpcRegistry.addMethod("stargazer.del_admin", methodDelAdminPtr);
427 xmlrpc_c::methodPtr const methodChgAdminPtr(new METHOD_ADMIN_CHG(
431 rpcRegistry.addMethod("stargazer.chg_admin", methodChgAdminPtr);
433 xmlrpc_c::methodPtr const methodGetAdminsPtr(new METHOD_ADMINS_GET(
437 rpcRegistry.addMethod("stargazer.get_admins", methodGetAdminsPtr);
439 xmlrpc_c::methodPtr const methodSendMessagePtr(new METHOD_MESSAGE_SEND(
443 rpcRegistry.addMethod("stargazer.send_user_message", methodSendMessagePtr);
445 xmlrpc_c::methodPtr const methodGetOnlinIPsPtr(new METHOD_GET_ONLINE_IPS(
449 rpcRegistry.addMethod("stargazer.get_online_ips", methodGetOnlinIPsPtr);
451 xmlrpc_c::methodPtr const methodGetUserAuthByPtr(new METHOD_GET_USER_AUTH_BY(
455 rpcRegistry.addMethod("stargazer.get_user_auth_by", methodGetUserAuthByPtr);