Include HTTPS port (if any) in /api/auth response

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER 2023-10-08 18:32:59 +02:00
parent d75599c7ab
commit c4237f1846
No known key found for this signature in database
GPG Key ID: 00135ACBD90B28DD
4 changed files with 59 additions and 0 deletions

View File

@ -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 <nettle/sha2.h>
@ -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;
}

View File

@ -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

View File

@ -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)

View File

@ -13,4 +13,6 @@
void http_init(void);
void http_terminate(void);
in_port_t get_https_port(void) __attribute__((pure));
#endif // WEBSERVER_H