[REFACTOR] IDEA Code Inspect

This commit is contained in:
Gravita 2024-03-21 19:40:45 +07:00
parent f7decac23d
commit 038af764a1
26 changed files with 48 additions and 71 deletions

View file

@ -34,7 +34,6 @@
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.nio.file.*; import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.security.KeyStore; import java.security.KeyStore;

View file

@ -6,7 +6,6 @@
import pro.gravit.utils.launch.LaunchOptions; import pro.gravit.utils.launch.LaunchOptions;
import pro.gravit.utils.launch.ModuleLaunch; import pro.gravit.utils.launch.ModuleLaunch;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;

View file

@ -27,7 +27,6 @@
import pro.gravit.utils.ProviderMap; import pro.gravit.utils.ProviderMap;
import pro.gravit.utils.command.Command; import pro.gravit.utils.command.Command;
import pro.gravit.utils.command.SubCommand; import pro.gravit.utils.command.SubCommand;
import pro.gravit.utils.helper.VerifyHelper;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;

View file

@ -4,7 +4,6 @@
import pro.gravit.launcher.base.events.request.GetAvailabilityAuthRequestEvent; import pro.gravit.launcher.base.events.request.GetAvailabilityAuthRequestEvent;
import pro.gravit.launcher.base.request.auth.AuthRequest; import pro.gravit.launcher.base.request.auth.AuthRequest;
import pro.gravit.launcher.base.request.auth.details.AuthLoginOnlyDetails; import pro.gravit.launcher.base.request.auth.details.AuthLoginOnlyDetails;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.auth.AuthException; import pro.gravit.launchserver.auth.AuthException;
import pro.gravit.launchserver.auth.core.interfaces.provider.AuthSupportSudo; import pro.gravit.launchserver.auth.core.interfaces.provider.AuthSupportSudo;
import pro.gravit.launchserver.manangers.AuthManager; import pro.gravit.launchserver.manangers.AuthManager;

View file

@ -1,7 +1,6 @@
package pro.gravit.launchserver.auth.core; package pro.gravit.launchserver.auth.core;
import pro.gravit.launcher.base.request.auth.AuthRequest; import pro.gravit.launcher.base.request.auth.AuthRequest;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.auth.AuthException; import pro.gravit.launchserver.auth.AuthException;
import pro.gravit.launchserver.manangers.AuthManager; import pro.gravit.launchserver.manangers.AuthManager;
import pro.gravit.launchserver.socket.Client; import pro.gravit.launchserver.socket.Client;

View file

@ -5,9 +5,7 @@
import pro.gravit.launchserver.command.hash.*; import pro.gravit.launchserver.command.hash.*;
import pro.gravit.launchserver.command.modules.LoadModuleCommand; import pro.gravit.launchserver.command.modules.LoadModuleCommand;
import pro.gravit.launchserver.command.modules.ModulesCommand; import pro.gravit.launchserver.command.modules.ModulesCommand;
import pro.gravit.launchserver.command.profiles.MakeProfileCommand;
import pro.gravit.launchserver.command.profiles.ProfilesCommand; import pro.gravit.launchserver.command.profiles.ProfilesCommand;
import pro.gravit.launchserver.command.profiles.SaveProfilesCommand;
import pro.gravit.launchserver.command.service.*; import pro.gravit.launchserver.command.service.*;
import pro.gravit.launchserver.command.sync.*; import pro.gravit.launchserver.command.sync.*;
import pro.gravit.launchserver.command.tools.SignDirCommand; import pro.gravit.launchserver.command.tools.SignDirCommand;

View file

@ -60,19 +60,19 @@ public void invoke(String... args) {
LaunchServerConfig config = server.config; LaunchServerConfig config = server.config;
config.auth.forEach((name, pair) -> { config.auth.forEach((name, pair) -> {
}); });
if (config.protectHandler instanceof NoProtectHandler) { switch (config.protectHandler) {
printCheckResult("protectHandler", "protectHandler none", false); case NoProtectHandler noProtectHandler -> printCheckResult("protectHandler", "protectHandler none", false);
} else if (config.protectHandler instanceof AdvancedProtectHandler) { case AdvancedProtectHandler advancedProtectHandler -> {
printCheckResult("protectHandler", "", true); printCheckResult("protectHandler", "", true);
if (!((AdvancedProtectHandler) config.protectHandler).enableHardwareFeature) { if (!advancedProtectHandler.enableHardwareFeature) {
printCheckResult("protectHandler.hardwareId", "you can improve security by using hwid provider", null); printCheckResult("protectHandler.hardwareId", "you can improve security by using hwid provider", null);
} else { } else {
printCheckResult("protectHandler.hardwareId", "", true); printCheckResult("protectHandler.hardwareId", "", true);
} }
} else if (config.protectHandler instanceof StdProtectHandler) { }
case StdProtectHandler stdProtectHandler ->
printCheckResult("protectHandler", "you can improve security by using advanced", null); printCheckResult("protectHandler", "you can improve security by using advanced", null);
} else { case null, default -> printCheckResult("protectHandler", "unknown protectHandler", null);
printCheckResult("protectHandler", "unknown protectHandler", null);
} }
if (config.netty.address.startsWith("ws://")) { if (config.netty.address.startsWith("ws://")) {
if (config.netty.ipForwarding) if (config.netty.ipForwarding)
@ -110,7 +110,7 @@ public void invoke(String... args) {
KeyStore keyStore = SignHelper.getStore(new File(config.sign.keyStore).toPath(), config.sign.keyStorePass, config.sign.keyStoreType); KeyStore keyStore = SignHelper.getStore(new File(config.sign.keyStore).toPath(), config.sign.keyStorePass, config.sign.keyStoreType);
Certificate[] certChainPlain = keyStore.getCertificateChain(config.sign.keyAlias); Certificate[] certChainPlain = keyStore.getCertificateChain(config.sign.keyAlias);
List<X509Certificate> certChain = Arrays.stream(certChainPlain).map(e -> (X509Certificate) e).toList(); List<X509Certificate> certChain = Arrays.stream(certChainPlain).map(e -> (X509Certificate) e).toList();
X509Certificate cert = certChain.get(0); X509Certificate cert = certChain.getFirst();
cert.checkValidity(); cert.checkValidity();
if (certChain.size() == 1) { if (certChain.size() == 1) {
printCheckResult("sign", "certificate chain contains <2 element(recommend 2 and more)", false); printCheckResult("sign", "certificate chain contains <2 element(recommend 2 and more)", false);

View file

@ -90,7 +90,7 @@ public boolean check(T address) {
} }
} }
static class LimitEntry { protected static class LimitEntry {
long time; long time;
int trys; int trys;

View file

@ -9,7 +9,6 @@
import pro.gravit.utils.command.SubCommand; import pro.gravit.utils.command.SubCommand;
import pro.gravit.utils.helper.IOHelper; import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.JVMHelper; import pro.gravit.utils.helper.JVMHelper;
import pro.gravit.utils.helper.LogHelper;
import pro.gravit.utils.helper.SecurityHelper; import pro.gravit.utils.helper.SecurityHelper;
import pro.gravit.utils.helper.UnpackHelper; import pro.gravit.utils.helper.UnpackHelper;
@ -20,7 +19,6 @@
import java.nio.file.Paths; import java.nio.file.Paths;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;

View file

@ -15,9 +15,7 @@
import pro.gravit.launchserver.components.AuthLimiterComponent; import pro.gravit.launchserver.components.AuthLimiterComponent;
import pro.gravit.launchserver.components.Component; import pro.gravit.launchserver.components.Component;
import pro.gravit.launchserver.components.ProGuardComponent; import pro.gravit.launchserver.components.ProGuardComponent;
import pro.gravit.utils.helper.JVMHelper;
import java.io.File;
import java.util.*; import java.util.*;
import static java.util.concurrent.TimeUnit.HOURS; import static java.util.concurrent.TimeUnit.HOURS;

View file

@ -7,7 +7,6 @@
import pro.gravit.launchserver.socket.response.auth.JoinServerResponse; import pro.gravit.launchserver.socket.response.auth.JoinServerResponse;
import pro.gravit.launchserver.socket.response.auth.SetProfileResponse; import pro.gravit.launchserver.socket.response.auth.SetProfileResponse;
import pro.gravit.utils.BiHookSet; import pro.gravit.utils.BiHookSet;
import pro.gravit.utils.HookSet;
public class AuthHookManager { public class AuthHookManager {
public final BiHookSet<AuthResponse.AuthContext, Client> preHook = new BiHookSet<>(); public final BiHookSet<AuthResponse.AuthContext, Client> preHook = new BiHookSet<>();

View file

@ -142,9 +142,7 @@ public void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client
if(executors == null) { if(executors == null) {
process(safeStatus, client, ip, context, response); process(safeStatus, client, ip, context, response);
} else { } else {
executors.submit(() -> { executors.submit(() -> process(safeStatus, client, ip, context, response));
process(safeStatus, client, ip, context, response);
});
} }
} }

View file

@ -63,7 +63,8 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
} catch (Throwable ex) { } catch (Throwable ex) {
logger.error("WebSocket frame handler hook error", ex); logger.error("WebSocket frame handler hook error", ex);
} }
if (frame instanceof TextWebSocketFrame textWebSocketFrame) { switch (frame) {
case TextWebSocketFrame textWebSocketFrame -> {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Message from {}: {}", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip, textWebSocketFrame.text()); logger.trace("Message from {}: {}", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip, textWebSocketFrame.text());
} }
@ -77,20 +78,23 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
} }
ctx.channel().close(); ctx.channel().close();
} }
} else if ((frame instanceof PingWebSocketFrame)) { }
case PingWebSocketFrame pingWebSocketFrame -> {
frame.content().retain(); frame.content().retain();
ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content())); ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content()));
//return; //return;
} else if (frame instanceof PongWebSocketFrame) { }
logger.trace("WebSocket Client received pong"); case PongWebSocketFrame pongWebSocketFrame -> logger.trace("WebSocket Client received pong");
} else if (frame instanceof CloseWebSocketFrame closeWebSocketFrame) { case CloseWebSocketFrame closeWebSocketFrame -> {
int statusCode = closeWebSocketFrame.statusCode(); int statusCode = closeWebSocketFrame.statusCode();
ctx.channel().close(); ctx.channel().close();
} else { }
case null, default -> {
String message = "unsupported frame type: " + frame.getClass().getName(); String message = "unsupported frame type: " + frame.getClass().getName();
logger.error(new UnsupportedOperationException(message)); // prevent strange crash here. logger.error(new UnsupportedOperationException(message)); // prevent strange crash here.
} }
} }
}
@Override @Override
public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception { public void channelInactive(ChannelHandlerContext channelHandlerContext) throws Exception {

View file

@ -152,7 +152,7 @@ private static void setDateAndCacheHeaders(HttpResponse response, File fileToCac
response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(time)); response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(time));
// Add cache headers // Add cache headers
response.headers().set(HttpHeaderNames.EXPIRES, dateFormatter.format(time.plus(HTTP_CACHE_SECONDS, ChronoUnit.SECONDS))); response.headers().set(HttpHeaderNames.EXPIRES, dateFormatter.format(time.plusSeconds(HTTP_CACHE_SECONDS)));
response.headers().set(HttpHeaderNames.CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS); response.headers().set(HttpHeaderNames.CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS);
response.headers().set( response.headers().set(
HttpHeaderNames.LAST_MODIFIED, dateFormatter.format(Instant.ofEpochMilli(fileToCache.lastModified()))); HttpHeaderNames.LAST_MODIFIED, dateFormatter.format(Instant.ofEpochMilli(fileToCache.lastModified())));

View file

@ -150,7 +150,7 @@ public void start(boolean pipeOutput) throws IOException, InterruptedException {
systemClassPath.addAll(ClientLauncherEntryPoint.resolveClassPath(workDir, params.actions, params.profile) systemClassPath.addAll(ClientLauncherEntryPoint.resolveClassPath(workDir, params.actions, params.profile)
.filter(x -> !params.profile.getModulePath().contains(workDir.relativize(x).toString())) .filter(x -> !params.profile.getModulePath().contains(workDir.relativize(x).toString()))
.map(Path::toString) .map(Path::toString)
.collect(Collectors.toList())); .toList());
} }
if (Launcher.getConfig().environment != LauncherConfig.LauncherEnvironment.PROD) { if (Launcher.getConfig().environment != LauncherConfig.LauncherEnvironment.PROD) {
processArgs.add(JVMHelper.jvmProperty(LogHelper.DEV_PROPERTY, String.valueOf(LogHelper.isDevEnabled()))); processArgs.add(JVMHelper.jvmProperty(LogHelper.DEV_PROPERTY, String.valueOf(LogHelper.isDevEnabled())));

View file

@ -6,7 +6,6 @@
import pro.gravit.utils.helper.LogHelper; import pro.gravit.utils.helper.LogHelper;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;

View file

@ -10,7 +10,6 @@
import java.io.InputStream; import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map; import java.util.Map;

View file

@ -126,11 +126,7 @@ public void enable(OptionalFile file, boolean manual, BiConsumer<OptionalFile, B
if (enabled.contains(file)) return; if (enabled.contains(file)) return;
enabled.add(file); enabled.add(file);
if (callback != null) callback.accept(file, true); if (callback != null) callback.accept(file, true);
OptionalFileInstallInfo installInfo = this.installInfo.get(file); OptionalFileInstallInfo installInfo = this.installInfo.computeIfAbsent(file, k -> new OptionalFileInstallInfo());
if (installInfo == null) {
installInfo = new OptionalFileInstallInfo();
this.installInfo.put(file, installInfo);
}
installInfo.isManual = manual; installInfo.isManual = manual;
if (file.dependencies != null) { if (file.dependencies != null) {
for (OptionalFile dep : file.dependencies) { for (OptionalFile dep : file.dependencies) {

View file

@ -27,7 +27,7 @@ public String getType() {
return "batchProfileByUsername"; return "batchProfileByUsername";
} }
static class Entry { public static class Entry {
@LauncherNetworkAPI @LauncherNetworkAPI
String username; String username;
@LauncherNetworkAPI @LauncherNetworkAPI

View file

@ -15,7 +15,6 @@
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.time.Duration; import java.time.Duration;
import java.util.concurrent.CompletionStage; import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer; import java.util.function.Consumer;

View file

@ -13,8 +13,7 @@ public class BasicLauncherEventHandler implements RequestService.EventHandler {
@Override @Override
public <T extends WebSocketEvent> boolean eventHandle(T event) { public <T extends WebSocketEvent> boolean eventHandle(T event) {
if (event instanceof SecurityReportRequestEvent) { if (event instanceof SecurityReportRequestEvent event1) {
SecurityReportRequestEvent event1 = (SecurityReportRequestEvent) event;
if (event1.action == SecurityReportRequestEvent.ReportAction.TOKEN_EXPIRED) { if (event1.action == SecurityReportRequestEvent.ReportAction.TOKEN_EXPIRED) {
try { try {
Request.restore(); Request.restore();
@ -22,8 +21,7 @@ public <T extends WebSocketEvent> boolean eventHandle(T event) {
LogHelper.error(e); LogHelper.error(e);
} }
} }
} else if (event instanceof ExtendedTokenRequestEvent) { } else if (event instanceof ExtendedTokenRequestEvent event1) {
ExtendedTokenRequestEvent event1 = (ExtendedTokenRequestEvent) event;
String token = event1.getExtendedToken(); String token = event1.getExtendedToken();
if (token != null) { if (token != null) {
Request.addExtendedToken(event1.getExtendedTokenName(), new Request.ExtendedToken(event1.getExtendedToken(), event1.getExtendedTokenExpire())); Request.addExtendedToken(event1.getExtendedTokenName(), new Request.ExtendedToken(event1.getExtendedToken(), event1.getExtendedTokenExpire()));

View file

@ -5,7 +5,6 @@
import pro.gravit.launcher.base.modules.LauncherModule; import pro.gravit.launcher.base.modules.LauncherModule;
import pro.gravit.launcher.base.modules.impl.SimpleModuleManager; import pro.gravit.launcher.base.modules.impl.SimpleModuleManager;
import java.net.URL;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;

View file

@ -84,7 +84,6 @@ private static CheckClassResultTypeApi fromType(LauncherTrustManager.CheckClassR
case UNTRUSTED -> CheckClassResultTypeApi.UNTRUSTED; case UNTRUSTED -> CheckClassResultTypeApi.UNTRUSTED;
case UNVERIFED -> CheckClassResultTypeApi.UNVERIFED; case UNVERIFED -> CheckClassResultTypeApi.UNVERIFED;
case UNCOMPAT -> CheckClassResultTypeApi.UNCOMPAT; case UNCOMPAT -> CheckClassResultTypeApi.UNCOMPAT;
default -> CheckClassResultTypeApi.UNKNOWN;
}; };
} }
} }

View file

@ -33,7 +33,6 @@ public HashedDir(HInput input) throws IOException {
entry = switch (type) { entry = switch (type) {
case FILE -> new HashedFile(input); case FILE -> new HashedFile(input);
case DIR -> new HashedDir(input); case DIR -> new HashedDir(input);
default -> throw new AssertionError("Unsupported hashed entry type: " + type.name());
}; };
// Try add entry to map // Try add entry to map

View file

@ -61,7 +61,6 @@ public static String getNativeExtension(JVMHelper.OS OS_TYPE) {
case MUSTDIE -> ".dll"; case MUSTDIE -> ".dll";
case LINUX -> ".so"; case LINUX -> ".so";
case MACOSX -> ".dylib"; case MACOSX -> ".dylib";
default -> throw new InternalError(String.format("Unsupported OS TYPE '%s'", OS_TYPE));
}; };
} }

View file

@ -61,7 +61,7 @@ public static void unpackZipNoCheck(String resource, Path target) throws IOExcep
IOHelper.transfer(input, target.resolve(IOHelper.toPath(entry.getName()))); IOHelper.transfer(input, target.resolve(IOHelper.toPath(entry.getName())));
} }
} }
} catch (NoSuchFileException e) { } catch (NoSuchFileException ignored) {
} }
} }
} }