From 1963562d588e2ee39dc4f1e46f2a5daa13d2d52e Mon Sep 17 00:00:00 2001 From: Philipp Schafft Date: Sat, 23 Sep 2023 15:47:57 +0000 Subject: [PATCH] Feature: Added basic geoip lookup --- configure.ac | 8 ++++ src/Makefile.am | 2 + src/admin.c | 6 +++ src/cfgfile.c | 12 +++++ src/cfgfile.h | 1 + src/client.c | 3 ++ src/connection.h | 4 ++ src/geoip.c | 115 +++++++++++++++++++++++++++++++++++++++++++++ src/geoip.h | 25 ++++++++++ src/global.c | 1 + src/global.h | 1 + src/icecasttypes.h | 5 ++ src/main.c | 5 ++ 13 files changed, 188 insertions(+) create mode 100644 src/geoip.c create mode 100644 src/geoip.h diff --git a/configure.ac b/configure.ac index cca2f51e..553bb0b0 100644 --- a/configure.ac +++ b/configure.ac @@ -243,6 +243,14 @@ PKG_HAVE_WITH_MODULES([OPENSSL], [openssl >= 1.1.0], [ LIBS="${LIBS} ${OPENSSL_LIBS}" ]) +dnl +dnl libmaxminddb +dnl +PKG_HAVE_WITH_MODULES([MAXMINDDB], [libmaxminddb >= 1.3.2], [ + CFLAGS="${CFLAGS} ${MAXMINDDB_CFLAGS}" + LIBS="${LIBS} ${MAXMINDDB_LIBS}" +]) + dnl dnl librhash - first try pkgconfig and then basic search dnl since the function is defined in rhash.h we need to check for that first, diff --git a/src/Makefile.am b/src/Makefile.am index 82f4f40b..d7548649 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -34,6 +34,7 @@ noinst_HEADERS = \ prng.h \ matchfile.h \ tls.h \ + geoip.h \ refobject.h \ module.h \ reportxml.h \ @@ -88,6 +89,7 @@ icecast_SOURCES = \ prng.c \ matchfile.c \ tls.c \ + geoip.c \ refobject.c \ module.c \ reportxml.c \ diff --git a/src/admin.c b/src/admin.c index 07a973c8..a022c879 100644 --- a/src/admin.c +++ b/src/admin.c @@ -922,6 +922,12 @@ static inline xmlNodePtr __add_listener(client_t *client, xmlNewTextChild(node, NULL, XMLSTR("protocol"), XMLSTR(client_protocol_to_string(client->protocol))); + if (client->con && *client->con->geoip.iso_3166_1_alpha_2) { + xmlNodePtr geoip = xmlNewChild(node, NULL, XMLSTR("geoip"), NULL); + xmlNodePtr country = xmlNewChild(geoip, NULL, XMLSTR("country"), NULL); + xmlSetProp(country, XMLSTR("iso-alpha-2"), XMLSTR(client->con->geoip.iso_3166_1_alpha_2)); + } + do { xmlNodePtr history = xmlNewChild(node, NULL, XMLSTR("history"), NULL); size_t i; diff --git a/src/cfgfile.c b/src/cfgfile.c index 42880d17..62bde152 100644 --- a/src/cfgfile.c +++ b/src/cfgfile.c @@ -55,6 +55,7 @@ #include "slave.h" #include "xslt.h" #include "prng.h" +#include "geoip.h" #define CATMODULE "CONFIG" #define RANGE_PORT 1, 65535 @@ -914,6 +915,7 @@ void config_clear(ice_config_t *c) if (c->log_dir) xmlFree(c->log_dir); if (c->webroot_dir) xmlFree(c->webroot_dir); if (c->adminroot_dir) xmlFree(c->adminroot_dir); + if (c->geoipdbfile) xmlFree(c->geoipdbfile); if (c->null_device) xmlFree(c->null_device); if (c->pidfile) xmlFree(c->pidfile); if (c->banfile) xmlFree(c->banfile); @@ -1004,6 +1006,8 @@ void config_reread_config(void) restart_logging(config); prng_configure(config); main_config_reload(config); + igloo_ro_unref(&global.geoip_db); + global.geoip_db = geoip_db_new(config->geoipdbfile); connection_reread_config(config); yp_recheck_config(config); fserve_recheck_mime_types(config); @@ -2719,6 +2723,14 @@ static void _parse_paths(xmlDocPtr doc, } } xmlFree(temp); + } else if (xmlStrcmp(node->name, XMLSTR("geoipdb")) == 0) { + if (!(temp = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1))) { + ICECAST_LOG_WARN(" setting must not be empty."); + continue; + } + if (configuration->geoipdbfile) + xmlFree(configuration->geoipdbfile); + configuration->geoipdbfile = (char *)temp; } else if (xmlStrcmp(node->name, XMLSTR("resource")) == 0 || xmlStrcmp(node->name, XMLSTR("alias")) == 0) { _parse_resource(doc, node, configuration); } else { diff --git a/src/cfgfile.h b/src/cfgfile.h index 8a151f78..257dd1eb 100644 --- a/src/cfgfile.h +++ b/src/cfgfile.h @@ -288,6 +288,7 @@ struct ice_config_tag { char *allowfile; char *webroot_dir; char *adminroot_dir; + char *geoipdbfile; prng_seed_config_t *prng_seed; resource_t *resources; reportxml_database_t *reportxml_db; diff --git a/src/client.c b/src/client.c index fcaf8c03..60e93d3b 100644 --- a/src/client.c +++ b/src/client.c @@ -57,6 +57,7 @@ #include "acl.h" #include "listensocket.h" #include "fastevent.h" +#include "geoip.h" /* for ADMIN_COMMAND_ERROR, and ADMIN_ICESTATS_LEGACY_EXTENSION_APPLICATION */ #include "admin.h" @@ -189,6 +190,8 @@ int client_create(client_t **c_ptr, connection_t *con, http_parser_t *parser) fastevent_emit(FASTEVENT_TYPE_CLIENT_CREATE, FASTEVENT_FLAG_MODIFICATION_ALLOWED, FASTEVENT_DATATYPE_CLIENT, client); + geoip_lookup_client(global.geoip_db, client); + return ret; } diff --git a/src/connection.h b/src/connection.h index 3261d1ee..7c659024 100644 --- a/src/connection.h +++ b/src/connection.h @@ -64,6 +64,10 @@ struct connection_tag { /* IP Address of the client as seen by the server */ char *ip; + + struct { + char iso_3166_1_alpha_2[3]; /* 2 bytes plus \0 */ + } geoip; }; void connection_initialize(void); diff --git a/src/geoip.c b/src/geoip.c new file mode 100644 index 00000000..55ec2c9a --- /dev/null +++ b/src/geoip.c @@ -0,0 +1,115 @@ +/* Icecast + * + * This program is distributed under the GNU General Public License, version 2. + * A copy of this license is included with this source. + * + * Copyright 2023 , Philipp Schafft , + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "icecasttypes.h" + +#ifdef HAVE_MAXMINDDB +#include +#include +#include +#endif + +#include +#include + +#include "geoip.h" +#include "global.h" +#include "client.h" +#include "connection.h" +#include "util_string.h" +#include "logging.h" +#define CATMODULE "geoip" + +struct geoip_db_tag { + igloo_ro_full_t __parent; + +#ifdef HAVE_MAXMINDDB + MMDB_s mmdb; +#endif +}; + +static void geoip_db_free(igloo_ro_t self) +{ +#ifdef HAVE_MAXMINDDB + geoip_db_t *db = igloo_ro_to_type(self, geoip_db_t); + + MMDB_close(&(db->mmdb)); +#endif +} + +igloo_RO_PUBLIC_TYPE(geoip_db_t, igloo_ro_full_t, + igloo_RO_TYPEDECL_FREE(geoip_db_free) + ); + +#ifdef HAVE_MAXMINDDB +geoip_db_t * geoip_db_new(const char *filename) +{ + geoip_db_t *ret; + MMDB_s mmdb; + int status; + + status = MMDB_open(filename, MMDB_MODE_MMAP, &mmdb); + if (status != MMDB_SUCCESS) { + if (status == MMDB_IO_ERROR) { + ICECAST_LOG_ERROR("Cannot open geoip database: %s: %s", MMDB_strerror(status), strerror(errno)); + } else { + ICECAST_LOG_ERROR("Cannot open geoip database: %s", MMDB_strerror(status)); + } + return NULL; + } + + if (igloo_ro_new_raw(&ret, geoip_db_t, igloo_instance) != igloo_ERROR_NONE) + return NULL; + + ret->mmdb = mmdb; + + ICECAST_LOG_INFO("Loaded geoip database: %s", filename); + + return ret; +} + +void geoip_lookup_client(geoip_db_t *self, client_t * client) +{ + int gai_error, mmdb_error; + MMDB_lookup_result_s result; + connection_t *con; + + if (!self || !client) + return; + + if (!client->con && !client->con->ip) + return; + + con = client->con; + + result = MMDB_lookup_string(&(self->mmdb), client->con->ip, &gai_error, &mmdb_error); + + if (gai_error || mmdb_error != MMDB_SUCCESS) + return; + + if (result.found_entry) { + MMDB_entry_data_s entry_data; + int status = MMDB_get_value(&result.entry, &entry_data, "country", "iso_code", (const char*)NULL); + + if (status == MMDB_SUCCESS && entry_data.has_data) { + if (entry_data.type == MMDB_DATA_TYPE_UTF8_STRING) { + if (entry_data.data_size < sizeof(con->geoip.iso_3166_1_alpha_2)) { + memcpy(con->geoip.iso_3166_1_alpha_2, entry_data.utf8_string, entry_data.data_size); + con->geoip.iso_3166_1_alpha_2[entry_data.data_size] = 0; + util_strtolower(con->geoip.iso_3166_1_alpha_2); + ICECAST_LOG_DINFO("FOUND: <%zu> <%H>", (size_t)entry_data.data_size, con->geoip.iso_3166_1_alpha_2); + } + } + } + } +} +#endif diff --git a/src/geoip.h b/src/geoip.h new file mode 100644 index 00000000..083935e1 --- /dev/null +++ b/src/geoip.h @@ -0,0 +1,25 @@ +/* Icecast + * + * This program is distributed under the GNU General Public License, version 2. + * A copy of this license is included with this source. + * + * Copyright 2023-2023, Philipp Schafft , + */ + +#ifndef __GEOIP_H__ +#define __GEOIP_H__ + +#include "icecasttypes.h" +#include "client.h" + +igloo_RO_FORWARD_TYPE(geoip_db_t); + +#ifdef HAVE_MAXMINDDB +geoip_db_t * geoip_db_new(const char *filename); +void geoip_lookup_client(geoip_db_t *self, client_t * client); +#else +#define geoip_db_new(filename) NULL +#define geoip_lookup_client(self,client) +#endif + +#endif /* __GEOIP_H__ */ diff --git a/src/global.c b/src/global.c index de8be3f7..e07f8414 100644 --- a/src/global.c +++ b/src/global.c @@ -51,6 +51,7 @@ void global_shutdown(void) { thread_mutex_destroy(&_global_mutex); igloo_ro_unref(&global.modulecontainer); + igloo_ro_unref(&global.geoip_db); avl_tree_free(global.source_tree, NULL); igloo_sp_unref(&_instance_uuid, igloo_instance); } diff --git a/src/global.h b/src/global.h index ffb5336b..6d9d60a9 100644 --- a/src/global.h +++ b/src/global.h @@ -53,6 +53,7 @@ typedef struct ice_global_tag relay_t *master_relays; module_container_t *modulecontainer; + geoip_db_t *geoip_db; /* state */ diff --git a/src/icecasttypes.h b/src/icecasttypes.h index 87bd5400..dc30b8d8 100644 --- a/src/icecasttypes.h +++ b/src/icecasttypes.h @@ -134,6 +134,10 @@ typedef struct mount_identifier_tag mount_identifier_t; typedef struct string_renderer_tag string_renderer_t; +/* ---[ geoip.[ch] ]--- */ + +typedef struct geoip_db_tag geoip_db_t; + /* ---[ event.[ch] ]--- */ typedef struct event_tag event_t; @@ -160,6 +164,7 @@ typedef void * refobject_t; /* --- [ For libigloo ]--- */ #define igloo_RO_APPTYPES \ igloo_RO_TYPE(string_renderer_t) \ + igloo_RO_TYPE(geoip_db_t) \ igloo_RO_TYPE(event_t) \ igloo_RO_TYPE(event_registration_t) \ igloo_RO_TYPE(module_t) \ diff --git a/src/main.c b/src/main.c index c1d1a412..b6f146c0 100644 --- a/src/main.c +++ b/src/main.c @@ -91,6 +91,7 @@ #include "listensocket.h" #include "fastevent.h" #include "prng.h" +#include "geoip.h" #include "navigation.h" #include @@ -755,6 +756,10 @@ int main(int argc, char **argv) ICECAST_LOG_INFO("Server's PID is %lli", (long long int)getpid()); __log_system_name(); + config = config_get_config(); + global.geoip_db = geoip_db_new(config->geoipdbfile); + config_release_config(); + /* REM 3D Graphics */ /* let her rip */