Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect pihole blocking status #143

Merged
merged 2 commits into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions FTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ typedef struct {
const char* setupVars;
const char* wildcards;
const char* auditlist;
const char* dnsmasqconfig;
} logFileNamesStruct;

typedef struct {
Expand Down Expand Up @@ -209,6 +210,7 @@ bool debugGC;
bool debugDB;
bool threadwritelock;
bool threadreadlock;
unsigned char blockingstatus;

char ** wildcarddomains;

Expand Down
18 changes: 18 additions & 0 deletions grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ void read_gravity_files(void)
{
logg("No wildcard blocking list present");
}

// Get blocking status
check_blocking_status();
}

int countlines(const char* fname)
Expand Down Expand Up @@ -201,3 +204,18 @@ int countlineswith(const char* str, const char* fname)

return found;
}

void check_blocking_status(void)
{
int disabled = countlineswith("#addn-hosts=/etc/pihole/gravity.list",files.dnsmasqconfig);

if(disabled < 0)
// Failed to open file -> unknown status
blockingstatus = 2;
else if(disabled > 0)
// Disabled
blockingstatus = 0;
else
// Enabled
blockingstatus = 1;
}
32 changes: 30 additions & 2 deletions request.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,20 @@ void getStats(int *sock)
{
percentage = 1e2*blocked/total;
}
sprintf(server_message,"domains_being_blocked %i\ndns_queries_today %i\nads_blocked_today %i\nads_percentage_today %f\n", \
counters.gravity,total,blocked,percentage);

switch(blockingstatus)
{
case 0: // Blocking disabled
sprintf(server_message,"domains_being_blocked N/A\n");
swrite(server_message, *sock);
break;
default: // Either unknown or enabled
sprintf(server_message,"domains_being_blocked %i\n",counters.gravity);
swrite(server_message, *sock);
break;
}
sprintf(server_message,"dns_queries_today %i\nads_blocked_today %i\nads_percentage_today %f\n", \
total,blocked,percentage);
swrite(server_message, *sock);
sprintf(server_message,"unique_domains %i\nqueries_forwarded %i\nqueries_cached %i\n", \
counters.domains,counters.forwardedqueries,counters.cached);
Expand All @@ -253,6 +265,22 @@ void getStats(int *sock)
activeclients);
swrite(server_message, *sock);

switch(blockingstatus)
{
case 0: // Blocking disabled
sprintf(server_message,"status disabled\n");
swrite(server_message, *sock);
break;
case 1: // Blocking Enabled
sprintf(server_message,"status enabled\n");
swrite(server_message, *sock);
break;
default: // Unknown status
sprintf(server_message,"status unknown\n");
swrite(server_message, *sock);
break;
}

if(debugclients)
logg("Sent stats data to client, ID: %i", *sock);
}
Expand Down
1 change: 1 addition & 0 deletions routines.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ void formatNumber(bool raw, int n, char* buffer);
void read_gravity_files(void);
int countlines(const char* fname);
int countlineswith(const char* str, const char* fname);
void check_blocking_status(void);

void check_setupVarsconf(void);
char * read_setupVarsconf(const char * key);
Expand Down
3 changes: 2 additions & 1 deletion structs.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ logFileNamesStruct files = {
"/etc/pihole/blacklist.txt",
"/etc/pihole/setupVars.conf",
"/etc/dnsmasq.d/03-pihole-wildcard.conf",
"/etc/pihole/auditlog.list"
"/etc/pihole/auditlog.list",
"/etc/dnsmasq.d/01-pihole.conf"
};

countersStruct counters = { 0 };
Expand Down
3 changes: 2 additions & 1 deletion test/test_suite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ load 'libs/bats-support/load'
[[ ${lines[7]} =~ "queries_cached 2" ]]
[[ ${lines[8]} == "clients_ever_seen 3" ]]
[[ ${lines[9]} == "unique_clients 3" ]]
[[ ${lines[10]} == "---EOM---" ]]
[[ ${lines[10]} == "status unknown" ]]
[[ ${lines[11]} == "---EOM---" ]]
}

@test "Top Clients" {
Expand Down