[FEATURE] Добавлен запрос ServerStatus. Удален execCommand и logListen

This commit is contained in:
Gravit 2020-05-30 04:06:15 +07:00
parent 39f7d4bbda
commit 81abc3fdc2
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
9 changed files with 118 additions and 113 deletions

View file

@ -15,9 +15,8 @@
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.socket.response.SimpleResponse;
import pro.gravit.launchserver.socket.response.WebSocketServerResponse;
import pro.gravit.launchserver.socket.response.admin.AddLogListenerResponse;
import pro.gravit.launchserver.socket.response.admin.ExecCommandResponse;
import pro.gravit.launchserver.socket.response.auth.*;
import pro.gravit.launchserver.socket.response.management.ServerStatusResponse;
import pro.gravit.launchserver.socket.response.profile.BatchProfileByUsername;
import pro.gravit.launchserver.socket.response.profile.ProfileByUUIDResponse;
import pro.gravit.launchserver.socket.response.profile.ProfileByUsername;
@ -34,6 +33,7 @@
import pro.gravit.utils.helper.LogHelper;
import java.lang.reflect.Type;
import java.util.concurrent.atomic.AtomicLong;
public class WebSocketService {
public static final ProviderMap<WebSocketServerResponse> providers = new ProviderMap<>();
@ -42,6 +42,18 @@ public class WebSocketService {
private final LaunchServer server;
private final Gson gson;
//Statistic data
public AtomicLong shortRequestLatency = new AtomicLong();
public AtomicLong shortRequestCounter = new AtomicLong();
public AtomicLong middleRequestLatency = new AtomicLong();
public AtomicLong middleRequestCounter = new AtomicLong();
public AtomicLong longRequestLatency = new AtomicLong();
public AtomicLong longRequestCounter = new AtomicLong();
public AtomicLong lastRequestTime = new AtomicLong();
public WebSocketService(ChannelGroup channels, LaunchServer server) {
this.channels = channels;
this.server = server;
@ -58,9 +70,7 @@ public static void registerResponses() {
providers.register("profiles", ProfilesResponse.class);
providers.register("launcher", LauncherResponse.class);
providers.register("updateList", UpdateListResponse.class);
providers.register("cmdExec", ExecCommandResponse.class);
providers.register("setProfile", SetProfileResponse.class);
providers.register("addLogListener", AddLogListenerResponse.class);
providers.register("update", UpdateResponse.class);
providers.register("restoreSession", RestoreSessionResponse.class);
providers.register("batchProfileByUsername", BatchProfileByUsername.class);
@ -74,16 +84,56 @@ public static void registerResponses() {
providers.register("verifySecureLevelKey", VerifySecureLevelKeyResponse.class);
providers.register("securityReport", SecurityReportResponse.class);
providers.register("hardwareReport", HardwareReportResponse.class);
providers.register("serverStatus", ServerStatusResponse.class);
}
public void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client client, String ip) {
long startTimeNanos = System.nanoTime();
String request = frame.text();
WebSocketServerResponse response = gson.fromJson(request, WebSocketServerResponse.class);
if (response == null) {
RequestEvent event = new ErrorRequestEvent("This type of request is not supported");
sendObject(ctx, event);
return;
}
process(ctx, response, client, ip);
long executeTime = System.nanoTime() - startTimeNanos;
if(executeTime > 0)
{
addRequestTimeToStats(executeTime);
}
}
public void addRequestTimeToStats(long nanos)
{
if(nanos < 100_000_000L) // < 100 millis
{
shortRequestCounter.getAndIncrement();
shortRequestLatency.getAndAdd(nanos);
}
else if(nanos < 1_000_000_000L) // > 100 millis and < 1 second
{
middleRequestCounter.getAndIncrement();
middleRequestLatency.getAndAdd(nanos);
}
else // > 1 second
{
longRequestCounter.getAndIncrement();
longRequestLatency.getAndAdd(nanos);
}
long lastTime = lastRequestTime.get();
long currentTime = System.currentTimeMillis();
if(currentTime - lastTime > 60*1000) //1 minute
{
lastRequestTime.set(currentTime);
shortRequestLatency.set(0);
shortRequestCounter.set(0);
middleRequestCounter.set(0);
middleRequestLatency.set(0);
longRequestCounter.set(0);
longRequestLatency.set(0);
}
}
void process(ChannelHandlerContext ctx, WebSocketServerResponse response, Client client, String ip) {

View file

@ -1,45 +0,0 @@
package pro.gravit.launchserver.socket.response.admin;
import io.netty.channel.ChannelHandlerContext;
import pro.gravit.launcher.ClientPermissions;
import pro.gravit.launcher.events.request.LogEvent;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.launchserver.socket.response.SimpleResponse;
import pro.gravit.utils.helper.LogHelper;
public class AddLogListenerResponse extends SimpleResponse {
public final LogHelper.OutputTypes outputType = LogHelper.OutputTypes.PLAIN;
@Override
public String getType() {
return "addLogListener";
}
@Override
public void execute(ChannelHandlerContext ctx, Client client) {
if (!client.isAuth) {
sendError("Access denied");
return;
}
if (!client.permissions.isPermission(ClientPermissions.PermissionConsts.ADMIN)) {
sendError("Access denied");
return;
}
if (client.logOutput != null) {
LogHelper.info("Client %s remove log listener", client.username);
LogHelper.removeOutput(client.logOutput);
} else {
LogHelper.info("Client %s add log listener", client.username);
LogHelper.Output output = (str) -> {
if (!ctx.isRemoved()) {
service.sendObject(ctx, new LogEvent(str));
} else {
LogHelper.removeOutput(client.logOutput);
LogHelper.info("Client %s remove log listener", client.username);
}
};
client.logOutput = new LogHelper.OutputEnity(output, outputType);
LogHelper.addOutput(client.logOutput);
}
}
}

View file

@ -1,26 +0,0 @@
package pro.gravit.launchserver.socket.response.admin;
import io.netty.channel.ChannelHandlerContext;
import pro.gravit.launcher.ClientPermissions;
import pro.gravit.launcher.events.request.ExecCommandRequestEvent;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.launchserver.socket.response.SimpleResponse;
public class ExecCommandResponse extends SimpleResponse {
public String cmd;
@Override
public String getType() {
return "cmdExec";
}
@Override
public void execute(ChannelHandlerContext ctx, Client client) {
if (!client.isAuth || !client.permissions.isPermission(ClientPermissions.PermissionConsts.ADMIN)) {
sendError("Access denied");
return;
}
server.commandHandler.eval(cmd, false);
sendResult(new ExecCommandRequestEvent(true));
}
}

View file

@ -0,0 +1,28 @@
package pro.gravit.launchserver.socket.response.management;
import io.netty.channel.ChannelHandlerContext;
import pro.gravit.launcher.events.request.ServerStatusRequestEvent;
import pro.gravit.launchserver.socket.Client;
import pro.gravit.launchserver.socket.WebSocketService;
import pro.gravit.launchserver.socket.response.SimpleResponse;
import pro.gravit.utils.helper.JVMHelper;
public class ServerStatusResponse extends SimpleResponse {
@Override
public String getType() {
return "serverStatus";
}
@Override
public void execute(ChannelHandlerContext ctx, Client client) throws Exception {
ServerStatusRequestEvent event = new ServerStatusRequestEvent(server.config.projectName);
event.totalJavaMemory = JVMHelper.RUNTIME.totalMemory();
event.freeJavaMemory = JVMHelper.RUNTIME.freeMemory();
event.shortLatency = ( service.shortRequestLatency.get() / service.shortRequestCounter.get() ) / 1_000_000;
event.middleLatency = ( service.middleRequestLatency.get() / service.middleRequestCounter.get() ) / 1_000_000;
event.longLatency = ( service.longRequestLatency.get() / service.longRequestCounter.get() ) / 1_000_000;
event.latency = ( ( service.shortRequestLatency.get() + service.middleRequestLatency.get() + service.longRequestLatency.get() ) /
( service.shortRequestCounter.get() + service.middleRequestCounter.get() + service.longRequestCounter.get() ) ) / 1_000_000;
sendResult(event);
}
}

View file

@ -1,18 +0,0 @@
package pro.gravit.launcher.events.request;
import pro.gravit.launcher.LauncherNetworkAPI;
import pro.gravit.launcher.events.RequestEvent;
public class ExecCommandRequestEvent extends RequestEvent {
@LauncherNetworkAPI
public final boolean success;
public ExecCommandRequestEvent(boolean success) {
this.success = success;
}
@Override
public String getType() {
return "cmdExec";
}
}

View file

@ -0,0 +1,25 @@
package pro.gravit.launcher.events.request;
import pro.gravit.launcher.events.RequestEvent;
import pro.gravit.utils.helper.JVMHelper;
public class ServerStatusRequestEvent extends RequestEvent {
public final String projectName;
public long totalJavaMemory;
public long freeJavaMemory;
//Latency
public long shortLatency; //Millis
public long middleLatency; //Millis
public long longLatency; //Millis
public long latency;
public ServerStatusRequestEvent(String projectName) {
this.projectName = projectName;
}
@Override
public String getType() {
return "serverStatus";
}
}

View file

@ -1,19 +0,0 @@
package pro.gravit.launcher.request.admin;
import pro.gravit.launcher.events.request.ExecCommandRequestEvent;
import pro.gravit.launcher.request.Request;
import pro.gravit.launcher.request.websockets.WebSocketRequest;
public class ExecCommandRequest extends Request<ExecCommandRequestEvent> implements WebSocketRequest {
public final String cmd;
public ExecCommandRequest(String cmd) {
this.cmd = cmd;
}
@Override
public String getType() {
return "cmdExec";
}
}

View file

@ -0,0 +1,11 @@
package pro.gravit.launcher.request.management;
import pro.gravit.launcher.events.request.ServerStatusRequestEvent;
import pro.gravit.launcher.request.Request;
public class ServerStatusRequest extends Request<ServerStatusRequestEvent> {
@Override
public String getType() {
return "serverStatus";
}
}

View file

@ -90,7 +90,6 @@ public void registerResults() {
results.register("update", UpdateRequestEvent.class);
results.register("restoreSession", RestoreSessionRequestEvent.class);
results.register("log", LogEvent.class);
results.register("cmdExec", ExecCommandRequestEvent.class);
results.register("getAvailabilityAuth", GetAvailabilityAuthRequestEvent.class);
results.register("exception", ExceptionEvent.class);
results.register("register", RegisterRequestEvent.class);