diff --git a/src/config/config.c b/src/config/config.c index bc3578859..d6a890584 100644 --- a/src/config/config.c +++ b/src/config/config.c @@ -1004,7 +1004,7 @@ static void initConfig(struct config *conf) conf->webserver.port.a = cJSON_CreateStringReference("comma-separated list of <[ip_address:]port>"); conf->webserver.port.f = FLAG_RESTART_FTL; conf->webserver.port.t = CONF_STRING; - conf->webserver.port.d.s = (char*)"80,[::]:80,443s,[::]:443s"; + conf->webserver.port.d.s = (char*)"80o,[::]:80o,443so,[::]:443so"; conf->webserver.port.c = validate_stub; // Type-based checking + civetweb syntax checking conf->webserver.threads.k = "webserver.threads"; @@ -1640,13 +1640,13 @@ static void get_web_port(struct config *conf) return; } // Create the string - snprintf(ports, 32, "%d,%ds", http_port, https_port); + snprintf(ports, 32, "%do,%dos", http_port, https_port); // Append IPv6 ports if IPv6 is enabled const bool have_ipv6 = ipv6_enabled(); if(have_ipv6) snprintf(ports + strlen(ports), 32 - strlen(ports), - ",[::]:%d,[::]:%ds", http_port, https_port); + ",[::]:%do,[::]:%dos", http_port, https_port); // Set default values for webserver ports if(conf->webserver.port.t == CONF_STRING_ALLOCATED) diff --git a/src/webserver/webserver.c b/src/webserver/webserver.c index c3fa16a5b..791f09089 100644 --- a/src/webserver/webserver.c +++ b/src/webserver/webserver.c @@ -210,6 +210,19 @@ static struct serverports in_port_t port; } server_ports[MAXPORTS] = { 0 }; static in_port_t https_port = 0; +/** + * @brief Retrieves and logs the server ports configuration. + * + * This function checks if the server context is initialized and then retrieves + * the configured server ports. It logs the port information and stores the + * details in the `server_ports` array. It also identifies and stores the first + * HTTPS port if available. + * + * @note If no ports are configured, a warning is logged and the function returns. + * + * @param void This function does not take any parameters. + * @return void This function does not return any value. + */ static void get_server_ports(void) { if(ctx == NULL) @@ -217,30 +230,40 @@ static void get_server_ports(void) // Loop over all listening ports struct mg_server_port mgports[MAXPORTS] = { 0 }; - if(mg_get_server_ports(ctx, MAXPORTS, mgports) > 0) + const int ports = mg_get_server_ports(ctx, MAXPORTS, mgports); + + // Stop if no ports are configured + if(ports < 1) { - // 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].is_redirect = mgports[i].is_redirect; - 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")); - } + log_warn("No web server ports configured!"); + return; + } + + // Loop over all ports + for(unsigned int i = 0; i < (unsigned int)ports; 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].is_redirect = mgports[i].is_redirect; + server_ports[i].protocol = mgports[i].protocol; + + // Store (first) HTTPS port if not already set + if(mgports[i].is_ssl && https_port == 0) + https_port = mgports[i].port; + + // Print port information + if(i == 0) + log_info("Web server ports:"); + log_info(" - %d (HTTP%s, IPv%s%s)", + mgports[i].port, mgports[i].is_ssl ? "S" : "", + mgports[i].protocol == 1 ? "4" : (mgports[i].protocol == 3 ? "6" : "4+6"), + mgports[i].is_redirect ? ", redirecting" : ""); + } } @@ -532,6 +555,9 @@ void http_init(void) return; } + // Get server ports + get_server_ports(); + // Register API handler mg_set_request_handler(ctx, "/api", api_handler, NULL); @@ -561,9 +587,6 @@ void http_init(void) // Prepare prerequisites for Lua allocate_lua(); - // Get server ports - get_server_ports(); - // Restore sessions from database init_api(); diff --git a/test/pihole.toml b/test/pihole.toml index 644e58074..b41145125 100644 --- a/test/pihole.toml +++ b/test/pihole.toml @@ -657,7 +657,7 @@ # # Possible values are: # comma-separated list of <[ip_address:]port> - port = "80,[::]:80,443s,[::]:443s" + port = "80o,[::]:80o,443so,[::]:443so" # Maximum number of worker threads allowed. # The Pi-hole web server handles each incoming connection in a separate thread. diff --git a/test/test_suite.bats b/test/test_suite.bats index 42f2ab5d8..f98f12b52 100644 --- a/test/test_suite.bats +++ b/test/test_suite.bats @@ -2049,7 +2049,7 @@ [[ "${lines[0]}" == "[ 1.1.1.1 abc-custom.com def-custom.de, 2.2.2.2 äste.com steä.com ]" ]] run bash -c './pihole-FTL --config webserver.port' printf "%s\n" "${lines[@]}" - [[ "${lines[0]}" == "80,[::]:80,443s,[::]:443s" ]] + [[ "${lines[0]}" == "80o,[::]:80o,443so,[::]:443so" ]] } @test "Create, verify and re-import Teleporter file via CLI" {