From 6541a84972b73f5048e2e705748af4983e42bd4c Mon Sep 17 00:00:00 2001 From: crschnick Date: Thu, 11 Jul 2024 05:39:05 +0000 Subject: [PATCH] Check for occupied beacon --- .../main/java/io/xpipe/app/launcher/LauncherCommand.java | 8 ++++++++ beacon/src/main/java/io/xpipe/beacon/BeaconClient.java | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/app/src/main/java/io/xpipe/app/launcher/LauncherCommand.java b/app/src/main/java/io/xpipe/app/launcher/LauncherCommand.java index 68912542..e587374b 100644 --- a/app/src/main/java/io/xpipe/app/launcher/LauncherCommand.java +++ b/app/src/main/java/io/xpipe/app/launcher/LauncherCommand.java @@ -132,6 +132,14 @@ public class LauncherCommand implements Callable { + " is already locked. Is another instance running?"); OperationMode.halt(1); } + + // If an instance is running as another user, we cannot connect to it as the xpipe_auth file is inaccessible + // Therefore the beacon client is not present. + // We still should check whether it is somehow occupied, otherwise beacon server startup will fail + if (BeaconClient.isOccupied(port)) { + TrackEvent.info("Another instance is already running on this port as another user. Quitting ..."); + OperationMode.halt(1); + } } private XPipeDaemonMode getEffectiveMode() { diff --git a/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java b/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java index be1e2dae..26867ef9 100644 --- a/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java +++ b/beacon/src/main/java/io/xpipe/beacon/BeaconClient.java @@ -24,6 +24,15 @@ public class BeaconClient { this.port = port; } + public static boolean isOccupied(int port) { + var file = XPipeInstallation.getLocalBeaconAuthFile(); + var reachable = BeaconServer.isReachable(port); + if (!Files.exists(file) && !reachable) { + return false; + } + return reachable; + } + public static BeaconClient establishConnection(int port, BeaconClientInformation information) throws Exception { var client = new BeaconClient(port); var auth = Files.readString(XPipeInstallation.getLocalBeaconAuthFile());