diff --git a/src/main/java/io/floci/cli/doctor/checks/PortAvailableCheck.java b/src/main/java/io/floci/cli/doctor/checks/PortAvailableCheck.java index d6cfe38..cdab33e 100644 --- a/src/main/java/io/floci/cli/doctor/checks/PortAvailableCheck.java +++ b/src/main/java/io/floci/cli/doctor/checks/PortAvailableCheck.java @@ -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 "); } + /** + * 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; } diff --git a/src/test/java/io/floci/cli/unit/StartPreflightTest.java b/src/test/java/io/floci/cli/unit/StartPreflightTest.java index 5618ae0..37ea49c 100644 --- a/src/test/java/io/floci/cli/unit/StartPreflightTest.java +++ b/src/test/java/io/floci/cli/unit/StartPreflightTest.java @@ -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));