]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_httpd_stats.cpp
Improve HTTP logging.
[user/henk/code/inspircd.git] / src / modules / m_httpd_stats.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
5  *   Copyright (C) 2018 Matt Schatz <genius3000@g3k.solutions>
6  *   Copyright (C) 2016 Johanna A <johanna-a@users.noreply.github.com>
7  *   Copyright (C) 2013-2016 Attila Molnar <attilamolnar@hush.com>
8  *   Copyright (C) 2013, 2017, 2019 Sadie Powell <sadie@witchery.services>
9  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
10  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
12  *   Copyright (C) 2008-2009 Robin Burchell <robin+git@viroteck.net>
13  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
14  *   Copyright (C) 2007 John Brooks <special@inspircd.org>
15  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
16  *   Copyright (C) 2006-2008, 2010 Craig Edwards <brain@inspircd.org>
17  *
18  * This file is part of InspIRCd.  InspIRCd is free software: you can
19  * redistribute it and/or modify it under the terms of the GNU General Public
20  * License as published by the Free Software Foundation, version 2.
21  *
22  * This program is distributed in the hope that it will be useful, but WITHOUT
23  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
25  * details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  */
30
31
32 #include "inspircd.h"
33 #include "modules/httpd.h"
34 #include "xline.h"
35
36 namespace Stats
37 {
38         struct Entities
39         {
40                 static const insp::flat_map<char, char const*>& entities;
41         };
42
43         static const insp::flat_map<char, char const*>& init_entities()
44         {
45                 static insp::flat_map<char, char const*> entities;
46                 entities['<'] = "lt";
47                 entities['>'] = "gt";
48                 entities['&'] = "amp";
49                 entities['"'] = "quot";
50                 return entities;
51         }
52
53         const insp::flat_map<char, char const*>& Entities::entities = init_entities();
54
55         std::string Sanitize(const std::string& str)
56         {
57                 std::string ret;
58                 ret.reserve(str.length() * 2);
59
60                 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
61                 {
62                         insp::flat_map<char, char const*>::const_iterator it = Entities::entities.find(*x);
63
64                         if (it != Entities::entities.end())
65                         {
66                                 ret += '&';
67                                 ret += it->second;
68                                 ret += ';';
69                         }
70                         else if (*x == 0x09 || *x == 0x0A || *x == 0x0D || ((*x >= 0x20) && (*x <= 0x7e)))
71                         {
72                                 // The XML specification defines the following characters as valid inside an XML document:
73                                 // Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
74                                 ret += *x;
75                         }
76                         else
77                         {
78                                 // If we reached this point then the string contains characters which can
79                                 // not be represented in XML, even using a numeric escape. Therefore, we
80                                 // Base64 encode the entire string and wrap it in a CDATA.
81                                 ret.clear();
82                                 ret += "<![CDATA[";
83                                 ret += BinToBase64(str);
84                                 ret += "]]>";
85                                 break;
86                         }
87                 }
88                 return ret;
89         }
90
91         void DumpMeta(std::ostream& data, Extensible* ext)
92         {
93                 data << "<metadata>";
94                 for (Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
95                 {
96                         ExtensionItem* item = i->first;
97                         std::string value = item->ToHuman(ext, i->second);
98                         if (!value.empty())
99                                 data << "<meta name=\"" << item->name << "\">" << Sanitize(value) << "</meta>";
100                         else if (!item->name.empty())
101                                 data << "<meta name=\"" << item->name << "\"/>";
102                 }
103                 data << "</metadata>";
104         }
105
106         std::ostream& ServerInfo(std::ostream& data)
107         {
108                 return data << "<server><name>" << ServerInstance->Config->ServerName << "</name><description>"
109                         << Sanitize(ServerInstance->Config->ServerDesc) << "</description><version>"
110                         << Sanitize(ServerInstance->GetVersionString(true)) << "</version></server>";
111         }
112
113         std::ostream& ISupport(std::ostream& data)
114         {
115                 data << "<isupport>";
116                 const std::vector<Numeric::Numeric>& isupport = ServerInstance->ISupport.GetLines();
117                 for (std::vector<Numeric::Numeric>::const_iterator i = isupport.begin(); i != isupport.end(); ++i)
118                 {
119                         const Numeric::Numeric& num = *i;
120                         for (std::vector<std::string>::const_iterator j = num.GetParams().begin(); j != num.GetParams().end() - 1; ++j)
121                                 data << "<token>" << Sanitize(*j) << "</token>";
122                 }
123                 return data << "</isupport>";
124         }
125
126         std::ostream& General(std::ostream& data)
127         {
128                 data << "<general>";
129                 data << "<usercount>" << ServerInstance->Users->GetUsers().size() << "</usercount>";
130                 data << "<localusercount>" << ServerInstance->Users->GetLocalUsers().size() << "</localusercount>";
131                 data << "<channelcount>" << ServerInstance->GetChans().size() << "</channelcount>";
132                 data << "<opercount>" << ServerInstance->Users->all_opers.size() << "</opercount>";
133                 data << "<socketcount>" << (SocketEngine::GetUsedFds()) << "</socketcount><socketmax>" << SocketEngine::GetMaxFds() << "</socketmax>";
134                 data << "<uptime><boot_time_t>" << ServerInstance->startup_time << "</boot_time_t></uptime>";
135                 data << "<currenttime>" << ServerInstance->Time() << "</currenttime>";
136
137                 data << ISupport;
138                 return data << "</general>";
139         }
140
141         std::ostream& XLines(std::ostream& data)
142         {
143                 data << "<xlines>";
144                 std::vector<std::string> xltypes = ServerInstance->XLines->GetAllTypes();
145                 for (std::vector<std::string>::iterator it = xltypes.begin(); it != xltypes.end(); ++it)
146                 {
147                         XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
148
149                         if (!lookup)
150                                 continue;
151                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
152                         {
153                                 data << "<xline type=\"" << it->c_str() << "\"><mask>"
154                                         << Sanitize(i->second->Displayable()) << "</mask><settime>"
155                                         << i->second->set_time << "</settime><duration>" << i->second->duration
156                                         << "</duration><reason>" << Sanitize(i->second->reason)
157                                         << "</reason></xline>";
158                         }
159                 }
160                 return data << "</xlines>";
161         }
162
163         std::ostream& Modules(std::ostream& data)
164         {
165                 data << "<modulelist>";
166                 const ModuleManager::ModuleMap& mods = ServerInstance->Modules->GetModules();
167
168                 for (ModuleManager::ModuleMap::const_iterator i = mods.begin(); i != mods.end(); ++i)
169                 {
170                         Version v = i->second->GetVersion();
171                         data << "<module><name>" << i->first << "</name><description>" << Sanitize(v.description) << "</description></module>";
172                 }
173                 return data << "</modulelist>";
174         }
175
176         std::ostream& Channels(std::ostream& data)
177         {
178                 data << "<channellist>";
179
180                 const chan_hash& chans = ServerInstance->GetChans();
181                 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
182                 {
183                         Channel* c = i->second;
184
185                         data << "<channel>";
186                         data << "<usercount>" << c->GetUsers().size() << "</usercount><channelname>" << Sanitize(c->name) << "</channelname>";
187                         data << "<channeltopic>";
188                         data << "<topictext>" << Sanitize(c->topic) << "</topictext>";
189                         data << "<setby>" << Sanitize(c->setby) << "</setby>";
190                         data << "<settime>" << c->topicset << "</settime>";
191                         data << "</channeltopic>";
192                         data << "<channelmodes>" << Sanitize(c->ChanModes(true)) << "</channelmodes>";
193
194                         const Channel::MemberMap& ulist = c->GetUsers();
195                         for (Channel::MemberMap::const_iterator x = ulist.begin(); x != ulist.end(); ++x)
196                         {
197                                 Membership* memb = x->second;
198                                 data << "<channelmember><uid>" << memb->user->uuid << "</uid><privs>"
199                                         << Sanitize(memb->GetAllPrefixChars()) << "</privs><modes>"
200                                         << memb->modes << "</modes>";
201                                 DumpMeta(data, memb);
202                                 data << "</channelmember>";
203                         }
204
205                         DumpMeta(data, c);
206
207                         data << "</channel>";
208                 }
209
210                 return data << "</channellist>";
211         }
212
213         std::ostream& DumpUser(std::ostream& data, User* u)
214         {
215                 data << "<user>";
216                 data << "<nickname>" << u->nick << "</nickname><uuid>" << u->uuid << "</uuid><realhost>"
217                         << u->GetRealHost() << "</realhost><displayhost>" << u->GetDisplayedHost() << "</displayhost><realname>"
218                         << Sanitize(u->GetRealName()) << "</realname><server>" << u->server->GetName() << "</server><signon>"
219                         << u->signon << "</signon><age>" << u->age << "</age>";
220
221                 if (u->IsAway())
222                         data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
223
224                 if (u->IsOper())
225                         data << "<opertype>" << Sanitize(u->oper->name) << "</opertype>";
226
227                 data << "<modes>" << u->GetModeLetters().substr(1) << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
228
229                 LocalUser* lu = IS_LOCAL(u);
230                 if (lu)
231                         data << "<local/><port>" << lu->server_sa.port() << "</port><servaddr>"
232                                 << lu->server_sa.str() << "</servaddr><connectclass>"
233                                 << lu->GetClass()->GetName() << "</connectclass><lastmsg>"
234                                 << lu->idle_lastmsg << "</lastmsg>";
235
236                 data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
237
238                 DumpMeta(data, u);
239
240                 data << "</user>";
241                 return data;
242         }
243
244         std::ostream& Users(std::ostream& data)
245         {
246                 data << "<userlist>";
247                 const user_hash& users = ServerInstance->Users->GetUsers();
248                 for (user_hash::const_iterator i = users.begin(); i != users.end(); ++i)
249                 {
250                         User* u = i->second;
251
252                         if (u->registered != REG_ALL)
253                                 continue;
254
255                         DumpUser(data, u);
256                 }
257                 return data << "</userlist>";
258         }
259
260         std::ostream& Servers(std::ostream& data)
261         {
262                 data << "<serverlist>";
263
264                 ProtocolInterface::ServerList sl;
265                 ServerInstance->PI->GetServerList(sl);
266
267                 for (ProtocolInterface::ServerList::const_iterator b = sl.begin(); b != sl.end(); ++b)
268                 {
269                         data << "<server>";
270                         data << "<servername>" << b->servername << "</servername>";
271                         data << "<parentname>" << b->parentname << "</parentname>";
272                         data << "<description>" << Sanitize(b->description) << "</description>";
273                         data << "<usercount>" << b->usercount << "</usercount>";
274 // This is currently not implemented, so, commented out.
275 //                                      data << "<opercount>" << b->opercount << "</opercount>";
276                         data << "<lagmillisecs>" << b->latencyms << "</lagmillisecs>";
277                         data << "</server>";
278                 }
279
280                 return data << "</serverlist>";
281         }
282
283         std::ostream& Commands(std::ostream& data)
284         {
285                 data << "<commandlist>";
286
287                 const CommandParser::CommandMap& commands = ServerInstance->Parser.GetCommands();
288                 for (CommandParser::CommandMap::const_iterator i = commands.begin(); i != commands.end(); ++i)
289                 {
290                         data << "<command><name>" << i->second->name << "</name><usecount>" << i->second->use_count << "</usecount></command>";
291                 }
292                 return data << "</commandlist>";
293         }
294
295         enum OrderBy
296         {
297                 OB_NICK,
298                 OB_LASTMSG,
299
300                 OB_NONE
301         };
302
303         struct UserSorter
304         {
305                 OrderBy order;
306                 bool desc;
307
308                 UserSorter(OrderBy Order, bool Desc = false) : order(Order), desc(Desc) {}
309
310                 template <typename T>
311                 inline bool Compare(const T& a, const T& b)
312                 {
313                         return desc ? a > b : a < b;
314                 }
315
316                 bool operator()(User* u1, User* u2)
317                 {
318                         switch (order) {
319                                 case OB_LASTMSG:
320                                         return Compare(IS_LOCAL(u1)->idle_lastmsg, IS_LOCAL(u2)->idle_lastmsg);
321                                         break;
322                                 case OB_NICK:
323                                         return Compare(u1->nick, u2->nick);
324                                         break;
325                                 default:
326                                 case OB_NONE:
327                                         return false;
328                                         break;
329                         }
330                 }
331         };
332
333         std::ostream& ListUsers(std::ostream& data, const HTTPQueryParameters& params)
334         {
335                 if (params.empty())
336                         return Users(data);
337
338                 data << "<userlist>";
339
340                 // Filters
341                 size_t limit = params.getNum<size_t>("limit");
342                 bool showunreg = params.getBool("showunreg");
343                 bool localonly = params.getBool("localonly");
344
345                 // Minimum time since a user's last message
346                 unsigned long min_idle = params.getDuration("minidle");
347                 time_t maxlastmsg = ServerInstance->Time() - min_idle;
348
349                 if (min_idle)
350                         // We can only check idle times on local users
351                         localonly = true;
352
353                 // Sorting
354                 const std::string& sortmethod = params.getString("sortby");
355                 bool desc = params.getBool("desc", false);
356
357                 OrderBy orderby;
358                 if (stdalgo::string::equalsci(sortmethod, "nick"))
359                         orderby = OB_NICK;
360                 else if (stdalgo::string::equalsci(sortmethod, "lastmsg"))
361                 {
362                         orderby = OB_LASTMSG;
363                         // We can only check idle times on local users
364                         localonly = true;
365                 }
366                 else
367                         orderby = OB_NONE;
368
369                 typedef std::list<User*> NewUserList;
370                 NewUserList user_list;
371                 user_hash users = ServerInstance->Users->GetUsers();
372                 for (user_hash::iterator i = users.begin(); i != users.end(); ++i)
373                 {
374                         User* u = i->second;
375                         if (!showunreg && u->registered != REG_ALL)
376                                 continue;
377
378                         LocalUser* lu = IS_LOCAL(u);
379                         if (localonly && !lu)
380                                 continue;
381
382                         if (min_idle && lu->idle_lastmsg > maxlastmsg)
383                                 continue;
384
385                         user_list.push_back(u);
386                 }
387
388                 UserSorter sorter(orderby, desc);
389                 if (sorter.order != OB_NONE && !(!localonly && sorter.order == OB_LASTMSG))
390                         user_list.sort(sorter);
391
392                 size_t count = 0;
393                 for (NewUserList::const_iterator i = user_list.begin(); i != user_list.end() && (!limit || count < limit); ++i, ++count)
394                         DumpUser(data, *i);
395
396                 data << "</userlist>";
397                 return data;
398         }
399 }
400
401 class ModuleHttpStats : public Module, public HTTPRequestEventListener
402 {
403         HTTPdAPI API;
404         bool enableparams;
405
406  public:
407         ModuleHttpStats()
408                 : HTTPRequestEventListener(this)
409                 , API(this)
410                 , enableparams(false)
411         {
412         }
413
414         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
415         {
416                 ConfigTag* conf = ServerInstance->Config->ConfValue("httpstats");
417
418                 // Parameterized queries may cause a performance issue
419                 // Due to the sheer volume of data
420                 // So default them to disabled
421                 enableparams = conf->getBool("enableparams");
422         }
423
424         ModResult HandleRequest(HTTPRequest* http)
425         {
426                 std::string path = http->GetPath();
427
428                 if (path != "/stats" && path.substr(0, 7) != "/stats/")
429                         return MOD_RES_PASSTHRU;
430
431                 if (path[path.size() - 1] == '/')
432                         path.erase(path.size() - 1, 1);
433
434                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling HTTP request for %s", http->GetPath().c_str());
435
436                 bool found = true;
437                 std::stringstream data;
438                 data << "<inspircdstats>";
439
440                 if (path == "/stats")
441                 {
442                         data << Stats::ServerInfo << Stats::General
443                                 << Stats::XLines << Stats::Modules
444                                 << Stats::Channels << Stats::Users
445                                 << Stats::Servers << Stats::Commands;
446                 }
447                 else if (path == "/stats/general")
448                 {
449                         data << Stats::General;
450                 }
451                 else if (path == "/stats/users")
452                 {
453                         if (enableparams)
454                                 Stats::ListUsers(data, http->GetParsedURI().query_params);
455                         else
456                                 data << Stats::Users;
457                 }
458                 else
459                 {
460                         found = false;
461                 }
462
463                 if (found)
464                 {
465                         data << "</inspircdstats>";
466                 }
467                 else
468                 {
469                         data.clear();
470                         data.str(std::string());
471                 }
472
473                 /* Send the document back to m_httpd */
474                 HTTPDocumentResponse response(this, *http, &data, found ? 200 : 404);
475                 response.headers.SetHeader("X-Powered-By", MODNAME);
476                 response.headers.SetHeader("Content-Type", "text/xml");
477                 API->SendResponse(response);
478                 return MOD_RES_DENY; // Handled
479         }
480
481         ModResult OnHTTPRequest(HTTPRequest& req) CXX11_OVERRIDE
482         {
483                 return HandleRequest(&req);
484         }
485
486         Version GetVersion() CXX11_OVERRIDE
487         {
488                 return Version("Provides XML-serialised statistics about the server, channels, and users over HTTP via the /stats path.", VF_VENDOR);
489         }
490 };
491
492 MODULE_INIT(ModuleHttpStats)