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

Feature: Added per country geoip statistics

This commit is contained in:
Philipp Schafft 2023-09-23 17:26:38 +00:00
parent 1963562d58
commit 5e66d22e36
3 changed files with 91 additions and 1 deletions

View File

@ -150,6 +150,12 @@
#define DEFAULT_HTML_REQUEST ""
#define BUILDM3U_RAW_REQUEST "buildm3u"
typedef struct {
size_t listeners;
size_t tls;
size_t ipv6;
} country_t;
typedef struct {
const char *prefix;
size_t length;
@ -577,6 +583,8 @@ xmlDocPtr admin_build_sourcelist(const char *mount, client_t *client, admin_form
snprintf(buf, sizeof(buf), "%"PRIu64, source->dumpfile_written);
xmlNewTextChild(srcnode, NULL, XMLSTR("dumpfile_written"), XMLSTR(buf));
admin_add_geoip_to_mount(source, srcnode, client->mode);
}
node = avl_get_next(node);
}
@ -956,6 +964,80 @@ void admin_add_listeners_to_mount(source_t *source,
avl_tree_unlock(source->client_tree);
}
static void admin_add_geoip_to_mount__country(source_t *source,
xmlNodePtr parent,
operation_mode mode,
const char *code,
country_t *country)
{
if (country->listeners) {
xmlNodePtr node = xmlNewChild(parent, NULL, XMLSTR("country"), NULL);
char buf[22];
if (code)
xmlSetProp(node, XMLSTR("iso-alpha-2"), XMLSTR(code));
snprintf(buf, sizeof(buf), "%llu", (long long unsigned)country->listeners);
xmlNewTextChild(node, NULL, XMLSTR("listeners"), XMLSTR(buf));
snprintf(buf, sizeof(buf), "%llu", (long long unsigned)country->tls);
xmlNewTextChild(node, NULL, XMLSTR("tls"), XMLSTR(buf));
snprintf(buf, sizeof(buf), "%llu", (long long unsigned)country->ipv6);
xmlNewTextChild(node, NULL, XMLSTR("ipv6"), XMLSTR(buf));
}
}
void admin_add_geoip_to_mount(source_t *source,
xmlNodePtr parent,
operation_mode mode)
{
avl_node *client_node;
xmlNodePtr geoip = xmlNewChild(parent, NULL, XMLSTR("geoip"), NULL);
country_t countries[26][26];
country_t default_country;
memset(countries, 0, sizeof(countries));
memset(&default_country, 0, sizeof(default_country));
avl_tree_rlock(source->client_tree);
client_node = avl_get_first(source->client_tree);
while(client_node) {
client_t *client = client_node->key;
connection_t *con = client->con;
country_t *country = &default_country;
if (con && *con->geoip.iso_3166_1_alpha_2) {
const char *iso = client->con->geoip.iso_3166_1_alpha_2;
if ((iso[0] >= 'a' && iso[0] <= 'z') && (iso[1] >= 'a' && iso[1] <= 'z')) {
country = &(countries[iso[0] - 'a'][iso[1] - 'a']);
}
}
country->listeners++;
if (con) {
if (con->tls)
country->tls++;
if (con->ip && strchr(con->ip, ':'))
country->ipv6++;
}
client_node = avl_get_next(client_node);
}
avl_tree_unlock(source->client_tree);
for (size_t idx_a = 0; idx_a < 26; idx_a++) {
for (size_t idx_b = 0; idx_b < 26; idx_b++) {
const char code[3] = {idx_a + 'a', idx_b + 'a', 0};
admin_add_geoip_to_mount__country(source, geoip, mode, code, &(countries[idx_a][idx_b]));
}
}
admin_add_geoip_to_mount__country(source, geoip, mode, NULL, &default_country);
}
static void command_show_listeners(client_t *client,
source_t *source,
admin_format_t response)
@ -976,6 +1058,7 @@ static void command_show_listeners(client_t *client,
xmlNewTextChild(srcnode, NULL, XMLSTR(client->mode == OMODE_LEGACY ? "Listeners" : "listeners"), XMLSTR(buf));
admin_add_listeners_to_mount(source, srcnode, client->mode);
admin_add_geoip_to_mount(source, srcnode, client->mode);
admin_send_response(doc, client, response,
LISTCLIENTS_HTML_REQUEST);

View File

@ -61,6 +61,11 @@ void admin_add_listeners_to_mount(source_t *source,
xmlNodePtr parent,
operation_mode mode);
void admin_add_geoip_to_mount(source_t *source,
xmlNodePtr parent,
operation_mode mode);
xmlNodePtr admin_add_role_to_authentication(auth_t *auth, xmlNodePtr parent);
admin_command_id_t admin_get_command(const char *command);

View File

@ -934,8 +934,10 @@ static xmlNodePtr _dump_stats_to_doc (xmlNodePtr root, unsigned int flags, const
if (source_real->running)
xmlNewTextChild(xmlnode, NULL, XMLSTR("content-type"), XMLSTR(source_real->format->contenttype));
if (flags & STATS_XML_FLAG_SHOW_LISTENERS)
if (flags & STATS_XML_FLAG_SHOW_LISTENERS) {
admin_add_listeners_to_mount(source_real, xmlnode, client->mode);
admin_add_geoip_to_mount(source_real, xmlnode, client->mode);
}
}
avl_tree_unlock(global.source_tree);