From c4237f1846bbc34bb2e78e7afe3f62fdb768bc15 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Sun, 8 Oct 2023 18:32:59 +0200 Subject: [PATCH] Include HTTPS port (if any) in /api/auth response Signed-off-by: DL6ER --- src/api/auth.c | 5 +++ src/api/docs/content/specs/auth.yaml | 3 ++ src/webserver/webserver.c | 49 ++++++++++++++++++++++++++++ src/webserver/webserver.h | 2 ++ 4 files changed, 59 insertions(+) diff --git a/src/api/auth.c b/src/api/auth.c index aa8fb9af..3b531476 100644 --- a/src/api/auth.c +++ b/src/api/auth.c @@ -22,6 +22,8 @@ #include "daemon.h" // sha256_raw_to_hex() #include "config/password.h" +// get_https_port() +#include "webserver/webserver.h" // crypto library #include @@ -312,6 +314,7 @@ static int get_session_object(struct ftl_conn *api, cJSON *json, const int user_ JSON_ADD_NULL_TO_OBJECT(session, "sid"); JSON_ADD_NUMBER_TO_OBJECT(session, "validity", -1); JSON_ADD_ITEM_TO_OBJECT(json, "session", session); + JSON_ADD_NUMBER_TO_OBJECT(json, "https_port", get_https_port()); JSON_ADD_BOOL_TO_OBJECT(json, "dns", dns); return 0; } @@ -325,6 +328,7 @@ static int get_session_object(struct ftl_conn *api, cJSON *json, const int user_ JSON_REF_STR_IN_OBJECT(session, "csrf", auth_data[user_id].csrf); JSON_ADD_NUMBER_TO_OBJECT(session, "validity", auth_data[user_id].valid_until - now); JSON_ADD_ITEM_TO_OBJECT(json, "session", session); + JSON_ADD_NUMBER_TO_OBJECT(json, "https_port", get_https_port()); JSON_ADD_BOOL_TO_OBJECT(json, "dns", dns); return 0; } @@ -335,6 +339,7 @@ static int get_session_object(struct ftl_conn *api, cJSON *json, const int user_ JSON_ADD_NULL_TO_OBJECT(session, "sid"); JSON_ADD_NUMBER_TO_OBJECT(session, "validity", -1); JSON_ADD_ITEM_TO_OBJECT(json, "session", session); + JSON_ADD_NUMBER_TO_OBJECT(json, "https_port", get_https_port()); JSON_ADD_BOOL_TO_OBJECT(json, "dns", dns); return 0; } diff --git a/src/api/docs/content/specs/auth.yaml b/src/api/docs/content/specs/auth.yaml index 2c314181..2f24ade8 100644 --- a/src/api/docs/content/specs/auth.yaml +++ b/src/api/docs/content/specs/auth.yaml @@ -245,6 +245,9 @@ components: validity: type: integer description: Remaining lifetime of this session unless refreshed (seconds) + https_port: + type: integer + description: HTTPS port of the Pi-hole webserver (0 if disabled) dns: type: boolean description: Whether the DNS server is up and running. False only in failed state diff --git a/src/webserver/webserver.c b/src/webserver/webserver.c index 26cf4f9e..962a31f3 100644 --- a/src/webserver/webserver.c +++ b/src/webserver/webserver.c @@ -204,6 +204,52 @@ void FTL_mbed_debug(void *user_param, int level, const char *file, int line, con log_web("mbedTLS(%s:%d, %d): %.*s", file, line, level, (int)len, message); } +#define MAXPORTS 8 +static struct serverports +{ + bool is_secure; + unsigned char protocol; // 1 = IPv4, 2 = IPv4+IPv6, 3 = IPv6 + in_port_t port; +} server_ports[MAXPORTS] = { 0 }; +static in_port_t https_port = 0; +static void get_server_ports(void) +{ + if(ctx == NULL) + return; + + // Loop over all listening ports + struct mg_server_port mgports[MAXPORTS] = { 0 }; + if(mg_get_server_ports(ctx, MAXPORTS, mgports) > 0) + { + // Loop over all ports + for(unsigned int i = 0; i < MAXPORTS; i++) + { + // Stop if no more ports are configured + if(mgports[i].protocol == 0) + break; + + // Store port information + server_ports[i].port = mgports[i].port; + server_ports[i].is_secure = mgports[i].is_ssl; + server_ports[i].protocol = mgports[i].protocol; + + // Store HTTPS port if not already set + if(mgports[i].is_ssl && https_port == 0) + https_port = mgports[i].port; + + // Print port information + log_debug(DEBUG_API, "Listening on port %d (HTTP%s, IPv%s)", + mgports[i].port, mgports[i].is_ssl ? "S" : "", + mgports[i].protocol == 1 ? "4" : (mgports[i].protocol == 3 ? "6" : "4+6")); + } + } +} + +in_port_t __attribute__((pure)) get_https_port(void) +{ + return https_port; +} + void http_init(void) { log_web("Initializing HTTP server on port %s", config.webserver.port.v.s); @@ -338,6 +384,9 @@ void http_init(void) // Prepare prerequisites for Lua allocate_lua(); + + // Get server ports + get_server_ports(); } static char *append_to_path(char *path, const char *append) diff --git a/src/webserver/webserver.h b/src/webserver/webserver.h index e551498d..d90b4494 100644 --- a/src/webserver/webserver.h +++ b/src/webserver/webserver.h @@ -13,4 +13,6 @@ void http_init(void); void http_terminate(void); +in_port_t get_https_port(void) __attribute__((pure)); + #endif // WEBSERVER_H \ No newline at end of file