Add new /api/stats/recent_blocked

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER
2019-11-14 22:40:19 +01:00
parent 834688ccaf
commit 091eaaa895
4 changed files with 46 additions and 9 deletions
+30 -9
View File
@@ -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)
+1
View File
@@ -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);
+4
View File
@@ -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
{
+11
View File
@@ -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.