From 091eaaa8957c7e0e48c82d562ff00830c76df37c Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 14 Nov 2019 22:40:19 +0100 Subject: [PATCH] Add new /api/stats/recent_blocked Signed-off-by: DL6ER --- src/api/api.c | 39 ++++++++++++++++++++++++++++++--------- src/api/api.h | 1 + src/api/http.c | 4 ++++ src/api/json_macros.h | 11 +++++++++++ 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index 78bbd13b..f2c84a11 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -898,28 +898,44 @@ int api_stats_history(const char *client_message, struct mg_connection *conn) JSON_SENT_OBJECT(json); } -void getRecentBlocked(const char *client_message, struct mg_connection *conn) +int api_stats_recentblocked(struct mg_connection *conn) { - int num=1; + unsigned int num=1; - // Test for integer that specifies number of entries to be shown - if(sscanf(client_message, "%*[^(](%i)", &num) > 0) { - // User wants a different number of requests - if(num >= counters->queries) - num = 0; + const struct mg_request_info *request = mg_get_request_info(conn); + if(request->query_string != NULL) + { + char buffer[256] = { 0 }; + + // Test for integer that specifies number of entries to be shown + if(GET_VAR("show", buffer, request->query_string) > 0) + { + // User wants a different number of requests + sscanf(buffer, "%u", &num); + if(num >= (unsigned int)counters->queries) + { + num = 0; + } + } } // Find most recently blocked query - int found = 0; + unsigned int found = 0; + cJSON *blocked = JSON_NEW_ARRAY(); for(int queryID = counters->queries - 1; queryID > 0 ; queryID--) { const queriesData* query = getQuery(queryID, true); if(query == NULL) + { continue; + } if(query->status == QUERY_GRAVITY || query->status == QUERY_REGEX || query->status == QUERY_BLACKLIST || + query->status == QUERY_EXTERNAL_BLOCKED_IP || + query->status == QUERY_EXTERNAL_BLOCKED_NULL || + query->status == QUERY_EXTERNAL_BLOCKED_NXRA || query->status == QUERY_GRAVITY_CNAME || query->status == QUERY_REGEX_CNAME || query->status == QUERY_BLACKLIST_CNAME) @@ -930,14 +946,19 @@ void getRecentBlocked(const char *client_message, struct mg_connection *conn) // the privacy settings at the time the query was made const char *domain = getDomainString(query); if(domain == NULL) + { continue; + } - http_send(conn, false, "%s\n", domain); + JSON_ARRAY_REF_STR(blocked, domain); } if(found >= num) break; } + cJSON *json = JSON_NEW_OBJ(); + JSON_OBJ_ADD_ITEM(json, "blocked", blocked); + JSON_SENT_OBJECT(json); } int api_ftl_clientIP(struct mg_connection *conn) diff --git a/src/api/api.h b/src/api/api.h index 2cc1288b..9ff99278 100644 --- a/src/api/api.h +++ b/src/api/api.h @@ -26,6 +26,7 @@ int api_stats_upstreams(struct mg_connection *conn); int api_stats_top_domains(bool blocked, struct mg_connection *conn); int api_stats_top_clients(bool blocked, struct mg_connection *conn); int api_stats_history(const char *client_message, struct mg_connection *conn); +int api_stats_recentblocked(struct mg_connection *conn); void getRecentBlocked(const char *client_message, struct mg_connection *conn); void getQueryTypesOverTime(struct mg_connection *conn); diff --git a/src/api/http.c b/src/api/http.c index 49895bf1..1ec63a30 100644 --- a/src/api/http.c +++ b/src/api/http.c @@ -127,6 +127,10 @@ static int api_handler(struct mg_connection *conn, void *ignored) { ret = api_stats_history("", conn); } + else if(strcasecmp("/api/stats/recent_blocked", request->local_uri) == 0) + { + ret = api_stats_recentblocked(conn); + } /******************************** not found ******************************/ /* else { diff --git a/src/api/json_macros.h b/src/api/json_macros.h index fdd88b95..b8052c7c 100644 --- a/src/api/json_macros.h +++ b/src/api/json_macros.h @@ -71,6 +71,17 @@ cJSON_AddItemToArray(object, number_item); \ } +#define JSON_ARRAY_REF_STR(array, string){ \ + cJSON *string_item = cJSON_CreateStringReference((const char*)string); \ + if(string_item == NULL) \ + { \ + cJSON_Delete(array); \ + send_http_error(conn); \ + return 500; \ + } \ + cJSON_AddItemToArray(array, string_item); \ +} + // cJSON_AddItemToObject() does not return anything // Note that this operation transfers the ownership of the added item to the // new parent so that when that array or object is deleted, it gets deleted as well.