feat(start): let docker decide on a low port the user cannot bind

A failed bind below 1024 can mean the user lacks the right to bind it, while the Docker daemon can publish it. Such a port now counts as taken only when something answers on it.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura committed 2026-10-10 18:02:51 -05:00
1 parent dcf78f2f7f
commit f23c47c488
2 files changed
+54

No files matched your search

@@ -6,8 +6,12 @@ import io.floci.cli.doctor.Check;
import io.floci.cli.doctor.CheckResult;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URI;
import java.util.function.BooleanSupplier;
public class PortAvailableCheck implements Check {
@@ -46,9 +50,32 @@ public class PortAvailableCheck implements Check {
"Run 'lsof -i :" + port + "' to identify the process, or start Floci with --port <other>");
}
/**
* False only when {@code port} is known to be taken. A bind that fails on a privileged port
* may just be this user lacking the right to bind it (Linux below 1024), while the Docker
* daemon can publish it fine, so there the port counts as taken only if something answers on it.
*/
public static boolean isPortFree(int port) {
try (ServerSocket s = new ServerSocket(port)) {
return true;
} catch (IOException e) {
return !takenAfterFailedBind(port, () -> listensOnLoopback(port));
}
}
/** The ports a user may be refused for lack of privilege rather than because they are taken. */
static final int FIRST_UNPRIVILEGED_PORT = 1024;
/** What a failed bind of {@code port} means; {@code listening} is asked only when the bind alone cannot tell. */
public static boolean takenAfterFailedBind(int port, BooleanSupplier listening) {
return port >= FIRST_UNPRIVILEGED_PORT || listening.getAsBoolean();
}
/** Whether something accepts a connection on {@code port} on this machine. */
public static boolean listensOnLoopback(int port) {
try (Socket socket = new Socket()) {
socket.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), port), 500);
return true;
} catch (IOException e) {
return false;
}
@@ -6,6 +6,7 @@ import io.floci.cli.docker.DockerClient;
import io.floci.cli.docker.DockerClient.DockerHost;
import io.floci.cli.docker.DockerClient.Kind;
import io.floci.cli.docker.DockerException;
import io.floci.cli.doctor.checks.PortAvailableCheck;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
@@ -170,6 +171,32 @@ class StartPreflightTest {
assertEquals(List.of(), refused.calls);
}
/**
* A user who may not bind a privileged port (Linux below 1024) cannot tell "taken" from "not
* allowed" by binding, while the Docker daemon can publish it: only an answering listener
* makes such a port taken. Above that range a failed bind is the answer.
*/
@Test
void aFailedBindOnAPrivilegedPortIsNotProofItIsTaken() {
assertFalse(PortAvailableCheck.takenAfterFailedBind(80, () -> false));
assertFalse(PortAvailableCheck.takenAfterFailedBind(443, () -> false));
assertTrue(PortAvailableCheck.takenAfterFailedBind(80, () -> true));
assertTrue(PortAvailableCheck.takenAfterFailedBind(1024, () -> false));
assertTrue(PortAvailableCheck.takenAfterFailedBind(4566, () -> false));
}
@Test
void aListenerIsFoundOnLoopbackAndAClosedPortIsNot() throws Exception {
int port;
try (ServerSocket listening = new ServerSocket(0)) {
port = listening.getLocalPort();
assertTrue(PortAvailableCheck.listensOnLoopback(port));
assertFalse(PortAvailableCheck.isPortFree(port));
}
assertFalse(PortAvailableCheck.listensOnLoopback(port));
assertTrue(PortAvailableCheck.isPortFree(port));
}
@Test
void onlyARealPortOnALocalDaemonIsProbed() {
assertTrue(StartCommand.probesPortLocally(LOCAL, 4588));