diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java index 553c1674..8ad75c78 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java @@ -14,10 +14,7 @@ import pro.gravit.launchserver.config.LaunchServerConfig; import pro.gravit.launchserver.config.LaunchServerRuntimeConfig; import pro.gravit.launchserver.launchermodules.LauncherModuleLoader; -import pro.gravit.launchserver.manangers.CertificateManager; -import pro.gravit.launchserver.manangers.MirrorManager; -import pro.gravit.launchserver.manangers.ReconfigurableManager; -import pro.gravit.launchserver.manangers.SessionManager; +import pro.gravit.launchserver.manangers.*; import pro.gravit.launchserver.manangers.hook.AuthHookManager; import pro.gravit.launchserver.modules.events.LaunchServerFullInitEvent; import pro.gravit.launchserver.modules.events.LaunchServerInitPhase; @@ -82,6 +79,7 @@ public final class LaunchServer implements Runnable, AutoCloseable, Reconfigurab public final MirrorManager mirrorManager; public final ReconfigurableManager reconfigurableManager; public final ConfigManager configManager; + public final PingServerManager pingServerManager; // HWID ban + anti-brutforce public final CertificateManager certificateManager; public final ProguardConf proguardConf; @@ -146,6 +144,7 @@ public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, La reconfigurableManager = new ReconfigurableManager(); authHookManager = new AuthHookManager(); configManager = new ConfigManager(); + pingServerManager = new PingServerManager(this); //Generate or set new Certificate API certificateManager.orgName = config.projectName; if (config.certificate != null && config.certificate.enabled) { @@ -389,6 +388,8 @@ public void syncProfilesDir() throws IOException { // Sort and set new profiles newProfies.sort(Comparator.comparing(a -> a)); profilesList = Collections.unmodifiableList(newProfies); + if(pingServerManager != null) + pingServerManager.syncServers(); } public void syncUpdatesDir(Collection dirs) throws IOException { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/PingServerManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/PingServerManager.java new file mode 100644 index 00000000..65a80829 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/PingServerManager.java @@ -0,0 +1,55 @@ +package pro.gravit.launchserver.manangers; + +import pro.gravit.launcher.profiles.ClientProfile; +import pro.gravit.launcher.request.management.PingServerReportRequest; +import pro.gravit.launchserver.LaunchServer; + +import java.util.HashMap; +import java.util.Map; + +public class PingServerManager { + public static class ServerInfoEntry + { + public PingServerReportRequest.PingServerReport lastReport; + public long lastReportTime; + + public ServerInfoEntry(PingServerReportRequest.PingServerReport lastReport) { + this.lastReport = lastReport; + this.lastReportTime = System.currentTimeMillis(); + } + + public ServerInfoEntry() { + } + } + public final Map map = new HashMap<>(); + private final LaunchServer server; + + public PingServerManager(LaunchServer server) { + this.server = server; + } + public void syncServers() + { + server.getProfiles().forEach((p) -> { + for(ClientProfile.ServerProfile sp : p.getServers()) + { + ServerInfoEntry entry = map.get(sp.name); + if(entry == null) + { + map.put(sp.name, new ServerInfoEntry()); + } + } + }); + } + public boolean updateServer(String name, PingServerReportRequest.PingServerReport report) + { + ServerInfoEntry entry = map.get(name); + if(entry == null) + return false; + else + { + entry.lastReportTime = System.currentTimeMillis(); + entry.lastReport = report; + return true; + } + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java index 53defc97..5a8b8001 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/WebSocketService.java @@ -16,6 +16,7 @@ import pro.gravit.launchserver.socket.response.SimpleResponse; import pro.gravit.launchserver.socket.response.WebSocketServerResponse; import pro.gravit.launchserver.socket.response.auth.*; +import pro.gravit.launchserver.socket.response.management.PingServerReportResponse; import pro.gravit.launchserver.socket.response.management.ServerStatusResponse; import pro.gravit.launchserver.socket.response.profile.BatchProfileByUsername; import pro.gravit.launchserver.socket.response.profile.ProfileByUUIDResponse; @@ -85,6 +86,7 @@ public static void registerResponses() { providers.register("securityReport", SecurityReportResponse.class); providers.register("hardwareReport", HardwareReportResponse.class); providers.register("serverStatus", ServerStatusResponse.class); + providers.register("pingServerReport", PingServerReportResponse.class); } public void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client, String ip) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/management/PingServerReportResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/management/PingServerReportResponse.java new file mode 100644 index 00000000..5474e83f --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/management/PingServerReportResponse.java @@ -0,0 +1,27 @@ +package pro.gravit.launchserver.socket.response.management; + +import io.netty.channel.ChannelHandlerContext; +import pro.gravit.launcher.ClientPermissions; +import pro.gravit.launcher.events.request.PingServerReportRequestEvent; +import pro.gravit.launcher.request.management.PingServerReportRequest; +import pro.gravit.launchserver.socket.Client; +import pro.gravit.launchserver.socket.response.SimpleResponse; + +public class PingServerReportResponse extends SimpleResponse { + public PingServerReportRequest.PingServerReport data; + public String name; + @Override + public String getType() { + return "pingServerReport"; + } + + @Override + public void execute(ChannelHandlerContext ctx, Client client) throws Exception { + if(!client.isAuth || client.permissions == null || !client.permissions.isPermission(ClientPermissions.PermissionConsts.MANAGEMENT)) + { + sendError("Access denied"); + } + server.pingServerManager.updateServer(name, data); + sendResult(new PingServerReportRequestEvent()); + } +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/PingServerReportRequestEvent.java b/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/PingServerReportRequestEvent.java new file mode 100644 index 00000000..ed45ae0f --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/events/request/PingServerReportRequestEvent.java @@ -0,0 +1,10 @@ +package pro.gravit.launcher.events.request; + +import pro.gravit.launcher.events.RequestEvent; + +public class PingServerReportRequestEvent extends RequestEvent { + @Override + public String getType() { + return "pingServerReport"; + } +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/ClientProfile.java b/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/ClientProfile.java index 81330fa7..8504697c 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/ClientProfile.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/profiles/ClientProfile.java @@ -60,8 +60,10 @@ public final class ClientProfile implements Comparable { private String title; @LauncherNetworkAPI private String info; + @Deprecated @LauncherNetworkAPI private String serverAddress; + @Deprecated @LauncherNetworkAPI private int serverPort; @LauncherNetworkAPI @@ -70,6 +72,24 @@ public final class ClientProfile implements Comparable { @LauncherNetworkAPI private String mainClass; + public static class ServerProfile + { + public String name; + public String serverAddress; + public int serverPort; + public boolean isDefault = true; + } + @LauncherNetworkAPI + private List servers = new ArrayList<>(1); + public ServerProfile getDefaultServerProfile() + { + for(ServerProfile profile : servers) + { + if(profile.isDefault) return profile; + } + return null; + } + @Override public int compareTo(ClientProfile o) { return Integer.compare(getSortIndex(), o.getSortIndex()); @@ -131,8 +151,13 @@ public String getMainClass() { return mainClass; } + public List getServers() { + return servers; + } + public String getServerAddress() { - return serverAddress; + ServerProfile profile = getDefaultServerProfile(); + return profile == null ? "localhost" : profile.serverAddress; } public Set getOptional() { @@ -245,9 +270,10 @@ public void pushOptionalClassPath(pushOptionalClassPathCallback callback) throws } public int getServerPort() { - return serverPort; + ServerProfile profile = getDefaultServerProfile(); + return profile == null ? 25565 : profile.serverPort; } - + @Deprecated public InetSocketAddress getServerSocketAddress() { return InetSocketAddress.createUnresolved(getServerAddress(), getServerPort()); } diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/management/PingServerReportRequest.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/management/PingServerReportRequest.java new file mode 100644 index 00000000..1b783bc2 --- /dev/null +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/management/PingServerReportRequest.java @@ -0,0 +1,44 @@ +package pro.gravit.launcher.request.management; + +import pro.gravit.launcher.events.request.PingServerReportRequestEvent; +import pro.gravit.launcher.request.Request; + +import java.util.List; + +public class PingServerReportRequest extends Request { + @Override + public String getType() { + return "pingServerReport"; + } + + public static class PingServerReport + { + public final String name; + public final int maxPlayers; // player slots + public final int playersOnline; + public static class UsernameInfo + { + public final String username; + + public UsernameInfo(String username) { + this.username = username; + } + } + //Server addional info + public double tps; //Server tps + public List users; + + public PingServerReport(String name, int maxPlayers, int playersOnline) { + this.name = name; + this.maxPlayers = maxPlayers; + this.playersOnline = playersOnline; + } + } + public final String name; + public final PingServerReport data; + + public PingServerReportRequest(String name, PingServerReport data) { + this.name = name; + this.data = data; + } +} diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java index 7360155d..0d98c46e 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/websockets/ClientWebSocketService.java @@ -101,6 +101,8 @@ public void registerResults() { results.register("verifySecureLevelKey", VerifySecureLevelKeyRequestEvent.class); results.register("securityReport", SecurityReportRequestEvent.class); results.register("hardwareReport", HardwareReportRequestEvent.class); + results.register("serverStatus", ServerStatusRequestEvent.class); + results.register("pingServerReport", PingServerReportRequestEvent.class); } public void waitIfNotConnected() {