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

Make webserver ports optional by default #2138

Merged
merged 5 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions src/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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)
Expand Down
78 changes: 52 additions & 26 deletions src/webserver/webserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,37 +210,63 @@ 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)
return;

// Loop over all listening ports
struct mg_server_port mgports[MAXPORTS] = { 0 };
if(mg_get_server_ports(ctx, MAXPORTS, mgports) > 0)
unsigned int ports_avail = 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(ports_avail == 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" : "");

// Increase number of available ports
ports_avail++;
}
}

Expand Down Expand Up @@ -532,6 +558,9 @@ void http_init(void)
return;
}

// Get server ports
get_server_ports();

// Register API handler
mg_set_request_handler(ctx, "/api", api_handler, NULL);

Expand Down Expand Up @@ -561,9 +590,6 @@ void http_init(void)
// Prepare prerequisites for Lua
allocate_lua();

// Get server ports
get_server_ports();

// Restore sessions from database
init_api();

Expand Down
2 changes: 1 addition & 1 deletion test/pihole.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion test/test_suite.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
Loading