1
0
mirror of https://gitlab.xiph.org/xiph/icecast-server.git synced 2024-06-02 06:01:10 +00:00

Feature: Include geocoordinates in geoip lookup

This commit is contained in:
Philipp Schafft 2023-10-02 13:07:00 +00:00
parent 5e66d22e36
commit f9fccf24ce
3 changed files with 41 additions and 5 deletions

View File

@ -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 {

View File

@ -16,6 +16,7 @@
#include <sys/types.h>
#include <time.h>
#include <stdbool.h>
#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;
};

View File

@ -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