From f9fccf24ce07a6d2d67aa72e2b05e417f5f996ce Mon Sep 17 00:00:00 2001 From: Philipp Schafft Date: Mon, 2 Oct 2023 13:07:00 +0000 Subject: [PATCH] Feature: Include geocoordinates in geoip lookup --- src/admin.c | 26 ++++++++++++++++++++++---- src/connection.h | 5 +++++ src/geoip.c | 15 ++++++++++++++- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/admin.c b/src/admin.c index 3ac05184..825d3bd5 100644 --- a/src/admin.c +++ b/src/admin.c @@ -930,10 +930,28 @@ 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)); + if (client->con) { + connection_t *con = client->con; + + if (*con->geoip.iso_3166_1_alpha_2 || con->geoip.have_latitude || con->geoip.have_longitude) { + xmlNodePtr geoip = xmlNewChild(node, NULL, XMLSTR("geoip"), NULL); + + if (*con->geoip.iso_3166_1_alpha_2) { + xmlNodePtr country = xmlNewChild(geoip, NULL, XMLSTR("country"), NULL); + xmlSetProp(country, XMLSTR("iso-alpha-2"), XMLSTR(con->geoip.iso_3166_1_alpha_2)); + } + if (con->geoip.have_latitude || con->geoip.have_longitude) { + xmlNodePtr location = xmlNewChild(geoip, NULL, XMLSTR("location"), NULL); + if (con->geoip.have_latitude) { + snprintf(buf, sizeof(buf), "%f", con->geoip.latitude); + xmlSetProp(location, XMLSTR("latitude"), XMLSTR(buf)); + } + if (con->geoip.have_longitude) { + snprintf(buf, sizeof(buf), "%f", con->geoip.longitude); + xmlSetProp(location, XMLSTR("longitude"), XMLSTR(buf)); + } + } + } } do { diff --git a/src/connection.h b/src/connection.h index 7c659024..254160cf 100644 --- a/src/connection.h +++ b/src/connection.h @@ -16,6 +16,7 @@ #include #include +#include #include "tls.h" @@ -66,6 +67,10 @@ struct connection_tag { char *ip; struct { + double latitude; + double longitude; + bool have_latitude; + bool have_longitude; char iso_3166_1_alpha_2[3]; /* 2 bytes plus \0 */ } geoip; }; diff --git a/src/geoip.c b/src/geoip.c index 55ec2c9a..6d197d5f 100644 --- a/src/geoip.c +++ b/src/geoip.c @@ -98,8 +98,9 @@ void geoip_lookup_client(geoip_db_t *self, client_t * client) 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); + int status; + 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)) { @@ -110,6 +111,18 @@ void geoip_lookup_client(geoip_db_t *self, client_t * client) } } } + + status = MMDB_get_value(&result.entry, &entry_data, "location", "latitude", (const char*)NULL); + if (status == MMDB_SUCCESS && entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_DOUBLE) { + con->geoip.latitude = entry_data.double_value; + con->geoip.have_latitude = true; + } + + status = MMDB_get_value(&result.entry, &entry_data, "location", "longitude", (const char*)NULL); + if (status == MMDB_SUCCESS && entry_data.has_data && entry_data.type == MMDB_DATA_TYPE_DOUBLE) { + con->geoip.longitude = entry_data.double_value; + con->geoip.have_longitude = true; + } } } #endif