2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
33 #include "modules/httpd.h"
40 static const insp::flat_map<char, char const*>& entities;
43 static const insp::flat_map<char, char const*>& init_entities()
45 static insp::flat_map<char, char const*> entities;
48 entities['&'] = "amp";
49 entities['"'] = "quot";
53 const insp::flat_map<char, char const*>& Entities::entities = init_entities();
55 std::string Sanitize(const std::string& str)
58 ret.reserve(str.length() * 2);
60 for (std::string::const_iterator x = str.begin(); x != str.end(); ++x)
62 insp::flat_map<char, char const*>::const_iterator it = Entities::entities.find(*x);
64 if (it != Entities::entities.end())
70 else if (*x == 0x09 || *x == 0x0A || *x == 0x0D || ((*x >= 0x20) && (*x <= 0x7e)))
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]
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.
83 ret += BinToBase64(str);
91 void DumpMeta(std::ostream& data, Extensible* ext)
94 for (Extensible::ExtensibleStore::const_iterator i = ext->GetExtList().begin(); i != ext->GetExtList().end(); i++)
96 ExtensionItem* item = i->first;
97 std::string value = item->ToHuman(ext, i->second);
99 data << "<meta name=\"" << item->name << "\">" << Sanitize(value) << "</meta>";
100 else if (!item->name.empty())
101 data << "<meta name=\"" << item->name << "\"/>";
103 data << "</metadata>";
106 std::ostream& ServerInfo(std::ostream& data)
108 return data << "<server><name>" << ServerInstance->Config->ServerName << "</name><description>"
109 << Sanitize(ServerInstance->Config->ServerDesc) << "</description><version>"
110 << Sanitize(ServerInstance->GetVersionString(true)) << "</version></server>";
113 std::ostream& ISupport(std::ostream& data)
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)
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>";
123 return data << "</isupport>";
126 std::ostream& General(std::ostream& data)
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>";
138 return data << "</general>";
141 std::ostream& XLines(std::ostream& data)
144 std::vector<std::string> xltypes = ServerInstance->XLines->GetAllTypes();
145 for (std::vector<std::string>::iterator it = xltypes.begin(); it != xltypes.end(); ++it)
147 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
151 for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
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>";
160 return data << "</xlines>";
163 std::ostream& Modules(std::ostream& data)
165 data << "<modulelist>";
166 const ModuleManager::ModuleMap& mods = ServerInstance->Modules->GetModules();
168 for (ModuleManager::ModuleMap::const_iterator i = mods.begin(); i != mods.end(); ++i)
170 Version v = i->second->GetVersion();
171 data << "<module><name>" << i->first << "</name><description>" << Sanitize(v.description) << "</description></module>";
173 return data << "</modulelist>";
176 std::ostream& Channels(std::ostream& data)
178 data << "<channellist>";
180 const chan_hash& chans = ServerInstance->GetChans();
181 for (chan_hash::const_iterator i = chans.begin(); i != chans.end(); ++i)
183 Channel* c = i->second;
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>";
194 const Channel::MemberMap& ulist = c->GetUsers();
195 for (Channel::MemberMap::const_iterator x = ulist.begin(); x != ulist.end(); ++x)
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>";
207 data << "</channel>";
210 return data << "</channellist>";
213 std::ostream& DumpUser(std::ostream& data, User* u)
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>";
222 data << "<away>" << Sanitize(u->awaymsg) << "</away><awaytime>" << u->awaytime << "</awaytime>";
225 data << "<opertype>" << Sanitize(u->oper->name) << "</opertype>";
227 data << "<modes>" << u->GetModeLetters().substr(1) << "</modes><ident>" << Sanitize(u->ident) << "</ident>";
229 LocalUser* lu = IS_LOCAL(u);
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>";
236 data << "<ipaddress>" << u->GetIPString() << "</ipaddress>";
244 std::ostream& Users(std::ostream& data)
246 data << "<userlist>";
247 const user_hash& users = ServerInstance->Users->GetUsers();
248 for (user_hash::const_iterator i = users.begin(); i != users.end(); ++i)
252 if (u->registered != REG_ALL)
257 return data << "</userlist>";
260 std::ostream& Servers(std::ostream& data)
262 data << "<serverlist>";
264 ProtocolInterface::ServerList sl;
265 ServerInstance->PI->GetServerList(sl);
267 for (ProtocolInterface::ServerList::const_iterator b = sl.begin(); b != sl.end(); ++b)
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>";
280 return data << "</serverlist>";
283 std::ostream& Commands(std::ostream& data)
285 data << "<commandlist>";
287 const CommandParser::CommandMap& commands = ServerInstance->Parser.GetCommands();
288 for (CommandParser::CommandMap::const_iterator i = commands.begin(); i != commands.end(); ++i)
290 data << "<command><name>" << i->second->name << "</name><usecount>" << i->second->use_count << "</usecount></command>";
292 return data << "</commandlist>";
308 UserSorter(OrderBy Order, bool Desc = false) : order(Order), desc(Desc) {}
310 template <typename T>
311 inline bool Compare(const T& a, const T& b)
313 return desc ? a > b : a < b;
316 bool operator()(User* u1, User* u2)
320 return Compare(IS_LOCAL(u1)->idle_lastmsg, IS_LOCAL(u2)->idle_lastmsg);
323 return Compare(u1->nick, u2->nick);
333 std::ostream& ListUsers(std::ostream& data, const HTTPQueryParameters& params)
338 data << "<userlist>";
341 size_t limit = params.getNum<size_t>("limit");
342 bool showunreg = params.getBool("showunreg");
343 bool localonly = params.getBool("localonly");
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;
350 // We can only check idle times on local users
354 const std::string& sortmethod = params.getString("sortby");
355 bool desc = params.getBool("desc", false);
358 if (stdalgo::string::equalsci(sortmethod, "nick"))
360 else if (stdalgo::string::equalsci(sortmethod, "lastmsg"))
362 orderby = OB_LASTMSG;
363 // We can only check idle times on local users
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)
375 if (!showunreg && u->registered != REG_ALL)
378 LocalUser* lu = IS_LOCAL(u);
379 if (localonly && !lu)
382 if (min_idle && lu->idle_lastmsg > maxlastmsg)
385 user_list.push_back(u);
388 UserSorter sorter(orderby, desc);
389 if (sorter.order != OB_NONE && !(!localonly && sorter.order == OB_LASTMSG))
390 user_list.sort(sorter);
393 for (NewUserList::const_iterator i = user_list.begin(); i != user_list.end() && (!limit || count < limit); ++i, ++count)
396 data << "</userlist>";
401 class ModuleHttpStats : public Module, public HTTPRequestEventListener
408 : HTTPRequestEventListener(this)
410 , enableparams(false)
414 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
416 ConfigTag* conf = ServerInstance->Config->ConfValue("httpstats");
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");
424 ModResult HandleRequest(HTTPRequest* http)
426 std::string path = http->GetPath();
428 if (path != "/stats" && path.substr(0, 7) != "/stats/")
429 return MOD_RES_PASSTHRU;
431 if (path[path.size() - 1] == '/')
432 path.erase(path.size() - 1, 1);
434 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Handling httpd event");
437 std::stringstream data;
438 data << "<inspircdstats>";
440 if (path == "/stats")
442 data << Stats::ServerInfo << Stats::General
443 << Stats::XLines << Stats::Modules
444 << Stats::Channels << Stats::Users
445 << Stats::Servers << Stats::Commands;
447 else if (path == "/stats/general")
449 data << Stats::General;
451 else if (path == "/stats/users")
454 Stats::ListUsers(data, http->GetParsedURI().query_params);
456 data << Stats::Users;
465 data << "</inspircdstats>";
470 data.str(std::string());
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
481 ModResult OnHTTPRequest(HTTPRequest& req) CXX11_OVERRIDE
483 return HandleRequest(&req);
486 Version GetVersion() CXX11_OVERRIDE
488 return Version("Provides statistics over HTTP via m_httpd", VF_VENDOR);
492 MODULE_INIT(ModuleHttpStats)