From ebe0631e5cab2a0f1f91398aaa1bc0694cd3019c Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Tue, 8 Aug 2017 23:02:59 -0400 Subject: [PATCH] Add dummy POST handling for whitelist and blacklist Signed-off-by: Mcat12 --- api.h | 1 + api_dns.c | 6 ++++++ request.c | 34 +++++++++++++++++++++++++++++++++- routines.h | 3 +-- socket.c | 4 ++-- 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/api.h b/api.h index 2cd69861..a5468780 100644 --- a/api.h +++ b/api.h @@ -27,6 +27,7 @@ void getDBstats(int *sock, char type); // Endpoints under /dns/ void getList(int *sock, char type, char list_type); +void addList(int *sock, char type, char list_type, char *data); void getPiholeStatus(int *sock, char type); // General API commands diff --git a/api_dns.c b/api_dns.c index 2449a0ac..f1135d58 100644 --- a/api_dns.c +++ b/api_dns.c @@ -56,3 +56,9 @@ void getPiholeStatus(int *sock, char type) sendAPIResponse(*sock, type); ssend(*sock, "\"status\":%i", status == 1 ? 0 : 1); } + +void addList(int *sock, char type, char list_type, char *data) +{ + sendAPIResponse(*sock, type); + ssend(*sock, "\"status\": \"success\""); +} diff --git a/request.c b/request.c index 17dcbd1e..5d3875e0 100644 --- a/request.c +++ b/request.c @@ -126,7 +126,26 @@ void process_socket_request(char *client_message, int *sock) } } -void process_api_request(char *client_message, int *sock, bool header) +char* getPayload(char *http_message) +{ + char *data_start; + char *unix_newline = strstr(http_message, "\n\n"); + char *win_newline = strstr(http_message, "\r\n\r\n"); + + if(unix_newline != NULL) + data_start = unix_newline + 2; + else if(win_newline != NULL) + data_start = win_newline + 4; + else + return NULL; + + if(strlen(data_start) == 0) + return NULL; + + return data_start; +} + +void process_api_request(char *client_message, char *full_message, int *sock, bool header) { char type; if(header) @@ -134,6 +153,11 @@ void process_api_request(char *client_message, int *sock, bool header) else type = API; + if(debug) + logg("Received API request: %s", full_message); + + char *data = getPayload(full_message); + if(command(client_message, "GET /stats/summary")) { getStats(sock, type); @@ -191,10 +215,18 @@ void process_api_request(char *client_message, int *sock, bool header) { getList(sock, type, WHITELIST); } + else if(command(client_message, "POST /dns/whitelist")) + { + addList(sock, type, WHITELIST, data); + } else if(command(client_message, "GET /dns/blacklist")) { getList(sock, type, BLACKLIST); } + else if(command(client_message, "POST /dns/blacklist")) + { + addList(sock, type, BLACKLIST, data); + } else if(command(client_message, "GET /dns/status")) { getPiholeStatus(sock, type); diff --git a/routines.h b/routines.h index 38007f9e..d6baa5f5 100644 --- a/routines.h +++ b/routines.h @@ -43,9 +43,8 @@ void *socket_listening_thread(void *args); void *api_listening_thread(void *args); void process_socket_request(char *client_message, int *sock); -void process_api_request(char *client_message, int *sock, bool header); +void process_api_request(char *client_message, char *full_message, int *sock, bool header); bool command(char *client_message, const char* cmd); -void formatNumber(bool raw, int n, char* buffer); void read_gravity_files(void); int countlines(const char* fname); diff --git a/socket.c b/socket.c index 04e9a8bd..7b63cacb 100644 --- a/socket.c +++ b/socket.c @@ -312,7 +312,7 @@ void *api_connection_handler_thread(void *socket_desc) // Clear client message receive buffer memset(client_message, 0, sizeof client_message); - if(strncmp(message, "GET ", 4) == 0) + if(strncmp(message, "GET ", 4) == 0 || strncmp(message, "POST ", 5) == 0) { // HTTP requests can be simple or full. // A simple request contains one line only, and looks like this: @@ -352,7 +352,7 @@ void *api_connection_handler_thread(void *socket_desc) else { enable_thread_lock(threadname); - process_api_request(request, &sock, header); + process_api_request(request, message, &sock, header); disable_thread_lock(threadname); }