2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 Peter Powell <petpow@saberuk.com>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /// $CompilerFlags: find_compiler_flags("libmaxminddb" "")
20 /// $LinkerFlags: find_linker_flags("libmaxminddb" "-lmaxminddb")
22 /// $PackageInfo: require_system("darwin") libmaxminddb pkg-config
23 /// $PackageInfo: require_system("debian" "9.0") libmaxminddb-dev pkg-config
24 /// $PackageInfo: require_system("ubuntu" "16.04") libmaxminddb-dev pkg-config
27 # pragma comment(lib, "libmaxminddb.lib")
31 #include "modules/geolocation.h"
32 #include <maxminddb.h>
34 class GeolocationExtItem : public LocalExtItem
37 GeolocationExtItem(Module* parent)
38 : LocalExtItem("geolocation", ExtensionItem::EXT_USER, parent)
42 void free(Extensible* container, void* item) CXX11_OVERRIDE
44 Geolocation::Location* old = static_cast<Geolocation::Location*>(item);
49 Geolocation::Location* get(const Extensible* item) const
51 return static_cast<Geolocation::Location*>(get_raw(item));
54 void set(Extensible* item, Geolocation::Location* value)
56 value->refcount_inc();
57 free(item, set_raw(item, value));
60 void unset(Extensible* container)
62 free(container, unset_raw(container));
66 typedef insp::flat_map<std::string, Geolocation::Location*> LocationMap;
68 class GeolocationAPIImpl : public Geolocation::APIBase
71 GeolocationExtItem ext;
72 LocationMap locations;
75 GeolocationAPIImpl(Module* parent)
76 : Geolocation::APIBase(parent)
81 Geolocation::Location* GetLocation(User* user) CXX11_OVERRIDE
83 // If we have the location cached then use that instead.
84 Geolocation::Location* location = ext.get(user);
88 // Attempt to locate this user.
89 location = GetLocation(user->client_sa);
93 // We found the user. Cache their location for future use.
94 ext.set(user, location);
98 Geolocation::Location* GetLocation(irc::sockets::sockaddrs& sa) CXX11_OVERRIDE
100 // Attempt to look up the socket address.
102 MMDB_lookup_result_s lookup = MMDB_lookup_sockaddr(&mmdb, &sa.sa, &result);
103 if (result != MMDB_SUCCESS || !lookup.found_entry)
106 // Attempt to retrieve the country code.
107 MMDB_entry_data_s country_code;
108 result = MMDB_get_value(&lookup.entry, &country_code, "country", "iso_code", NULL);
109 if (result != MMDB_SUCCESS || !country_code.has_data || country_code.type != MMDB_DATA_TYPE_UTF8_STRING || country_code.data_size != 2)
112 // If the country has been seen before then use our cached Location object.
113 const std::string code(country_code.utf8_string, country_code.data_size);
114 LocationMap::iterator liter = locations.find(code);
115 if (liter != locations.end())
116 return liter->second;
118 // Attempt to retrieve the country name.
119 MMDB_entry_data_s country_name;
120 result = MMDB_get_value(&lookup.entry, &country_name, "country", "names", "en", NULL);
121 if (result != MMDB_SUCCESS || !country_name.has_data || country_name.type != MMDB_DATA_TYPE_UTF8_STRING)
124 // Create a Location object and cache it.
125 const std::string cname(country_name.utf8_string, country_name.data_size);
126 Geolocation::Location* location = new Geolocation::Location(code, cname);
127 locations[code] = location;
132 class ModuleGeoMaxMind : public Module
135 GeolocationAPIImpl geoapi;
141 memset(&geoapi.mmdb, 0, sizeof(geoapi.mmdb));
146 MMDB_close(&geoapi.mmdb);
149 Version GetVersion() CXX11_OVERRIDE
151 return Version("Provides Geolocation lookups using the libMaxMindDB library", VF_VENDOR);
154 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
156 ConfigTag* tag = ServerInstance->Config->ConfValue("maxmind");
157 const std::string file = ServerInstance->Config->Paths.PrependConfig(tag->getString("file", "GeoLite2-Country.mmdb"));
159 // Try to read the new database.
161 int result = MMDB_open(file.c_str(), MMDB_MODE_MMAP, &mmdb);
162 if (result != MMDB_SUCCESS)
163 throw ModuleException(InspIRCd::Format("Unable to load the MaxMind database (%s): %s",
164 file.c_str(), MMDB_strerror(result)));
166 // Swap the new database with the old database.
167 std::swap(mmdb, geoapi.mmdb);
169 // Free the old database.
173 void OnGarbageCollect() CXX11_OVERRIDE
175 for (LocationMap::iterator iter = geoapi.locations.begin(); iter != geoapi.locations.end(); )
177 Geolocation::Location* location = iter->second;
178 if (location->GetUseCount())
180 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Preserving geolocation data for %s (%s) with use count %u... ",
181 location->GetName().c_str(), location->GetCode().c_str(), location->GetUseCount());
186 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Deleting unused geolocation data for %s (%s)",
187 location->GetName().c_str(), location->GetCode().c_str());
189 iter = geoapi.locations.erase(iter);
194 void OnSetUserIP(LocalUser* user) CXX11_OVERRIDE
196 // Unset the extension so that the location of this user is looked
197 // up again next time it is requested.
198 geoapi.ext.unset(user);
202 MODULE_INIT(ModuleGeoMaxMind)