Skip to content

Commit

Permalink
Merge branch 'development-v6' into tweak/api_network_info
Browse files Browse the repository at this point in the history
Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Jul 19, 2024
2 parents 66b7fdf + 10e98d1 commit f07d902
Show file tree
Hide file tree
Showing 37 changed files with 668 additions and 423 deletions.
8 changes: 4 additions & 4 deletions .github/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ WORKDIR /app
COPY . /app

ARG CI_ARCH="linux/amd64"
ENV CI_ARCH ${CI_ARCH}
ENV CI_ARCH=${CI_ARCH}
ARG GIT_BRANCH="test"
ENV GIT_BRANCH ${GIT_BRANCH}
ENV GIT_BRANCH=${GIT_BRANCH}
ARG GIT_TAG="test"
ENV GIT_TAG ${GIT_TAG}
ENV GIT_TAG=${GIT_TAG}
ARG BUILD_OPTS=""
ENV BUILD_OPTS ${BUILD_OPTS}
ENV BUILD_OPTS=${BUILD_OPTS}

# Build FTL
# Remove possible old build files
Expand Down
6 changes: 6 additions & 0 deletions src/api/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ int api_stats_upstreams(struct ftl_conn *api);
int api_stats_top_domains(struct ftl_conn *api);
int api_stats_top_clients(struct ftl_conn *api);
int api_stats_recentblocked(struct ftl_conn *api);
cJSON *get_top_domains(struct ftl_conn *api, const int count,
const bool blocked, const bool domains_only);
cJSON *get_top_clients(struct ftl_conn *api, const int count,
const bool blocked, const bool clients_only,
const bool names_only);
cJSON *get_top_upstreams(struct ftl_conn *api, const bool upstreams_only);

// History methods
int api_history(struct ftl_conn *api);
Expand Down
2 changes: 1 addition & 1 deletion src/api/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static int set_blocking(struct ftl_conn *api)
// The blocking status does not need to be changed

// Delete a possibly running timer
set_blockingmode_timer(-1.0, true);
set_blockingmode_timer(timer, true);

log_debug(DEBUG_API, "No change in blocking mode, resetting timer");
}
Expand Down
29 changes: 16 additions & 13 deletions src/api/docs/content/specs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -348,21 +348,23 @@ components:
sync:
type: object
properties:
active:
type: boolean
server:
type: string
interval:
type: integer
count:
type: integer
rtc:
type: object
properties:
set:
type: boolean
device:
type: string
utc:
type: boolean
rtc:
type: object
properties:
set:
type: boolean
device:
type: string
utc:
type: boolean
resolver:
type: object
properties:
Expand Down Expand Up @@ -708,13 +710,14 @@ components:
active: true
address: ""
sync:
active: true
server: "pool.ntp.org"
interval: 3600
count: 8
rtc:
set: true
device: ""
utc: true
rtc:
set: true
device: ""
utc: true
resolver:
resolveIPv4: true
resolveIPv6: true
Expand Down
89 changes: 45 additions & 44 deletions src/api/queries.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
// dbopen(false, ), dbclose()
#include "database/common.h"

static int add_strings_to_array(struct ftl_conn *api, cJSON *array, const char *querystr, const int max_count)
#if 0
static int add_strings_to_array(struct ftl_conn *api, cJSON *array1, cJSON *array2, const char *querystr, const int max_count)
{

sqlite3 *memdb = get_memdb();
Expand All @@ -44,11 +45,24 @@ static int add_strings_to_array(struct ftl_conn *api, cJSON *array, const char *
sqlite3_errstr(rc));
}

// Loop through returned rows
// Loop through returned rows and add them to the array
int counter = 0;
while((rc = sqlite3_step(stmt)) == SQLITE_ROW &&
(max_count < 0 || ++counter < max_count))
JSON_COPY_STR_TO_ARRAY(array, (const char*)sqlite3_column_text(stmt, 0));
(max_count < 0 || ++counter <= max_count))
{
const char *array1_str = (const char*)sqlite3_column_text(stmt, 0);
if(array1_str != NULL && array1_str[0] != '\0')
// Only add non-empty strings
JSON_COPY_STR_TO_ARRAY(array1, array1_str);
if(array2 != NULL)
{
// We have a second array to fill (second column in the query)
const char *array2_str = (const char*)sqlite3_column_text(stmt, 1);
if(array2_str != NULL && array2_str[0] != '\0')
// Only add non-empty strings
JSON_COPY_STR_TO_ARRAY(array2, array2_str);
}
}

// Acceptable return codes are either
// - SQLITE_DONE: We read all lines, or
Expand All @@ -67,60 +81,47 @@ static int add_strings_to_array(struct ftl_conn *api, cJSON *array, const char *

return 0;
}
#endif

int api_queries_suggestions(struct ftl_conn *api)
{
int rc;
// Does the user request a custom number of records to be included?
int count = 30;
get_int_var(api->request->query_string, "count", &count);

// Get domains
cJSON *domain = JSON_NEW_ARRAY();
rc = add_strings_to_array(api, domain, "SELECT domain FROM domain_by_id", count);
if(rc != 0)
cJSON *domain = get_top_domains(api, count, false, true);
cJSON *blocked = get_top_domains(api, count, true, true);
// Add domains from both arrays, avoiding duplicates
cJSON *entry = NULL;
cJSON_ArrayForEach(entry, blocked)
{
log_err("Cannot read domains from database");
cJSON_Delete(domain);
return rc;
// Check if the domain is already in the list
bool found = false;
cJSON *entry2 = NULL;
cJSON_ArrayForEach(entry2, domain)
{
if(strcmp(cJSON_GetStringValue(entry), cJSON_GetStringValue(entry2)) == 0)
{
found = true;
break;
}
}
if(!found)
JSON_ADD_ITEM_TO_ARRAY(domain, cJSON_Duplicate(entry, true));
}
// Free the blocked list
cJSON_Delete(blocked);

// Get clients, both by IP and names
// We have to call DISTINCT() here as multiple IPs can map to and name and
// vice versa
cJSON *client_ip = JSON_NEW_ARRAY();
rc = add_strings_to_array(api, client_ip, "SELECT DISTINCT(ip) FROM client_by_id", count);
if(rc != 0)
{
log_err("Cannot read client IPs from database");
cJSON_Delete(domain);
cJSON_Delete(client_ip);
return rc;
}
cJSON *client_name = JSON_NEW_ARRAY();
rc = add_strings_to_array(api, client_name, "SELECT DISTINCT(name) FROM client_by_id", count);
if(rc != 0)
{
log_err("Cannot read client names from database");
cJSON_Delete(domain);
cJSON_Delete(client_ip);
cJSON_Delete(client_name);
return rc;
}
cJSON *client_ip = get_top_clients(api, count, false, true, false);
cJSON *client_name = get_top_clients(api, count, false, true, true);

// Get upstreams
cJSON *upstream = JSON_NEW_ARRAY();
rc = add_strings_to_array(api, upstream, "SELECT forward FROM forward_by_id", count);
if(rc != 0)
{
log_err("Cannot read forward from database");
cJSON_Delete(domain);
cJSON_Delete(client_ip);
cJSON_Delete(client_name);
cJSON_Delete(upstream);
return rc;
}
// Delete duplicate entries from client_name
cJSON_unique_array(client_name);

// Get upstreams
cJSON *upstream = get_top_upstreams(api, true);
// Get types
cJSON *type = JSON_NEW_ARRAY();
queriesData query = { 0 };
Expand Down
Loading

0 comments on commit f07d902

Please sign in to comment.