[FEATURE][EXPERIMENTAL] PingServerReportRequest

This commit is contained in:
Gravit 2020-06-24 11:06:08 +07:00
parent 4502f978fb
commit 3ad7002da7
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
8 changed files with 174 additions and 7 deletions

View file

@ -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<String> dirs) throws IOException {

View file

@ -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<String, ServerInfoEntry> 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;
}
}
}

View file

@ -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) {

View file

@ -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());
}
}

View file

@ -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";
}
}

View file

@ -60,8 +60,10 @@ public final class ClientProfile implements Comparable<ClientProfile> {
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<ClientProfile> {
@LauncherNetworkAPI
private String mainClass;
public static class ServerProfile
{
public String name;
public String serverAddress;
public int serverPort;
public boolean isDefault = true;
}
@LauncherNetworkAPI
private List<ServerProfile> 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<ServerProfile> getServers() {
return servers;
}
public String getServerAddress() {
return serverAddress;
ServerProfile profile = getDefaultServerProfile();
return profile == null ? "localhost" : profile.serverAddress;
}
public Set<OptionalFile> 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());
}

View file

@ -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<PingServerReportRequestEvent> {
@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<UsernameInfo> 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;
}
}

View file

@ -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() {