Skip to content

Commit

Permalink
[REFACTOR] IDEA Code Inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
gravit0 committed Mar 21, 2024
1 parent f7decac commit 038af76
Show file tree
Hide file tree
Showing 26 changed files with 48 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.security.KeyStore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import pro.gravit.utils.launch.LaunchOptions;
import pro.gravit.utils.launch.ModuleLaunch;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import pro.gravit.utils.ProviderMap;
import pro.gravit.utils.command.Command;
import pro.gravit.utils.command.SubCommand;
import pro.gravit.utils.helper.VerifyHelper;

import java.io.IOException;
import java.lang.reflect.Type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pro.gravit.launcher.base.events.request.GetAvailabilityAuthRequestEvent;
import pro.gravit.launcher.base.request.auth.AuthRequest;
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.core.interfaces.provider.AuthSupportSudo;
import pro.gravit.launchserver.manangers.AuthManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pro.gravit.launchserver.auth.core;

import pro.gravit.launcher.base.request.auth.AuthRequest;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.auth.AuthException;
import pro.gravit.launchserver.manangers.AuthManager;
import pro.gravit.launchserver.socket.Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import pro.gravit.launchserver.command.hash.*;
import pro.gravit.launchserver.command.modules.LoadModuleCommand;
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.SaveProfilesCommand;
import pro.gravit.launchserver.command.service.*;
import pro.gravit.launchserver.command.sync.*;
import pro.gravit.launchserver.command.tools.SignDirCommand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ public void invoke(String... args) {
LaunchServerConfig config = server.config;
config.auth.forEach((name, pair) -> {
});
if (config.protectHandler instanceof NoProtectHandler) {
printCheckResult("protectHandler", "protectHandler none", false);
} else if (config.protectHandler instanceof AdvancedProtectHandler) {
printCheckResult("protectHandler", "", true);
if (!((AdvancedProtectHandler) config.protectHandler).enableHardwareFeature) {
printCheckResult("protectHandler.hardwareId", "you can improve security by using hwid provider", null);
} else {
printCheckResult("protectHandler.hardwareId", "", true);
switch (config.protectHandler) {
case NoProtectHandler noProtectHandler -> printCheckResult("protectHandler", "protectHandler none", false);
case AdvancedProtectHandler advancedProtectHandler -> {
printCheckResult("protectHandler", "", true);
if (!advancedProtectHandler.enableHardwareFeature) {
printCheckResult("protectHandler.hardwareId", "you can improve security by using hwid provider", null);
} else {
printCheckResult("protectHandler.hardwareId", "", true);
}
}
} else if (config.protectHandler instanceof StdProtectHandler) {
printCheckResult("protectHandler", "you can improve security by using advanced", null);
} else {
printCheckResult("protectHandler", "unknown protectHandler", null);
case StdProtectHandler stdProtectHandler ->
printCheckResult("protectHandler", "you can improve security by using advanced", null);
case null, default -> printCheckResult("protectHandler", "unknown protectHandler", null);
}
if (config.netty.address.startsWith("ws://")) {
if (config.netty.ipForwarding)
Expand Down Expand Up @@ -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);
Certificate[] certChainPlain = keyStore.getCertificateChain(config.sign.keyAlias);
List<X509Certificate> certChain = Arrays.stream(certChainPlain).map(e -> (X509Certificate) e).toList();
X509Certificate cert = certChain.get(0);
X509Certificate cert = certChain.getFirst();
cert.checkValidity();
if (certChain.size() == 1) {
printCheckResult("sign", "certificate chain contains <2 element(recommend 2 and more)", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public boolean check(T address) {
}
}

static class LimitEntry {
protected static class LimitEntry {
long time;
int trys;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import pro.gravit.utils.command.SubCommand;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.JVMHelper;
import pro.gravit.utils.helper.LogHelper;
import pro.gravit.utils.helper.SecurityHelper;
import pro.gravit.utils.helper.UnpackHelper;

Expand All @@ -20,7 +19,6 @@
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
import pro.gravit.launchserver.components.AuthLimiterComponent;
import pro.gravit.launchserver.components.Component;
import pro.gravit.launchserver.components.ProGuardComponent;
import pro.gravit.utils.helper.JVMHelper;

import java.io.File;
import java.util.*;

import static java.util.concurrent.TimeUnit.HOURS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pro.gravit.launchserver.socket.response.auth.JoinServerResponse;
import pro.gravit.launchserver.socket.response.auth.SetProfileResponse;
import pro.gravit.utils.BiHookSet;
import pro.gravit.utils.HookSet;

public class AuthHookManager {
public final BiHookSet<AuthResponse.AuthContext, Client> preHook = new BiHookSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ public void process(ChannelHandlerContext ctx, TextWebSocketFrame frame, Client
if(executors == null) {
process(safeStatus, client, ip, context, response);
} else {
executors.submit(() -> {
process(safeStatus, client, ip, context, response);
});
executors.submit(() -> process(safeStatus, client, ip, context, response));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,36 @@ protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) {
} catch (Throwable ex) {
logger.error("WebSocket frame handler hook error", ex);
}
if (frame instanceof TextWebSocketFrame textWebSocketFrame) {
if (logger.isTraceEnabled()) {
logger.trace("Message from {}: {}", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip, textWebSocketFrame.text());
}
try {
service.process(ctx, textWebSocketFrame, client, context.ip, connectUUID);
} catch (Throwable ex) {
logger.warn("Client {} send invalid request. Connection force closed.", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip);
switch (frame) {
case TextWebSocketFrame textWebSocketFrame -> {
if (logger.isTraceEnabled()) {
logger.trace("Client message: {}", textWebSocketFrame.text());
logger.error("Process websockets request failed", ex);
logger.trace("Message from {}: {}", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip, textWebSocketFrame.text());
}
try {
service.process(ctx, textWebSocketFrame, client, context.ip, connectUUID);
} catch (Throwable ex) {
logger.warn("Client {} send invalid request. Connection force closed.", context.ip == null ? IOHelper.getIP(ctx.channel().remoteAddress()) : context.ip);
if (logger.isTraceEnabled()) {
logger.trace("Client message: {}", textWebSocketFrame.text());
logger.error("Process websockets request failed", ex);
}
ctx.channel().close();
}
}
case PingWebSocketFrame pingWebSocketFrame -> {
frame.content().retain();
ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content()));
//return;
}
case PongWebSocketFrame pongWebSocketFrame -> logger.trace("WebSocket Client received pong");
case CloseWebSocketFrame closeWebSocketFrame -> {
int statusCode = closeWebSocketFrame.statusCode();
ctx.channel().close();
}
} else if ((frame instanceof PingWebSocketFrame)) {
frame.content().retain();
ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content()));
//return;
} else if (frame instanceof PongWebSocketFrame) {
logger.trace("WebSocket Client received pong");
} else if (frame instanceof CloseWebSocketFrame closeWebSocketFrame) {
int statusCode = closeWebSocketFrame.statusCode();
ctx.channel().close();
} else {
String message = "unsupported frame type: " + frame.getClass().getName();
logger.error(new UnsupportedOperationException(message)); // prevent strange crash here.
case null, default -> {
String message = "unsupported frame type: " + frame.getClass().getName();
logger.error(new UnsupportedOperationException(message)); // prevent strange crash here.
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private static void setDateAndCacheHeaders(HttpResponse response, File fileToCac
response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(time));

// 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.LAST_MODIFIED, dateFormatter.format(Instant.ofEpochMilli(fileToCache.lastModified())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void start(boolean pipeOutput) throws IOException, InterruptedException {
systemClassPath.addAll(ClientLauncherEntryPoint.resolveClassPath(workDir, params.actions, params.profile)
.filter(x -> !params.profile.getModulePath().contains(workDir.relativize(x).toString()))
.map(Path::toString)
.collect(Collectors.toList()));
.toList());
}
if (Launcher.getConfig().environment != LauncherConfig.LauncherEnvironment.PROD) {
processArgs.add(JVMHelper.jvmProperty(LogHelper.DEV_PROPERTY, String.valueOf(LogHelper.isDevEnabled())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import pro.gravit.utils.helper.LogHelper;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;
import java.io.IOException;
import java.net.URI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,7 @@ public void enable(OptionalFile file, boolean manual, BiConsumer<OptionalFile, B
if (enabled.contains(file)) return;
enabled.add(file);
if (callback != null) callback.accept(file, true);
OptionalFileInstallInfo installInfo = this.installInfo.get(file);
if (installInfo == null) {
installInfo = new OptionalFileInstallInfo();
this.installInfo.put(file, installInfo);
}
OptionalFileInstallInfo installInfo = this.installInfo.computeIfAbsent(file, k -> new OptionalFileInstallInfo());
installInfo.isManual = manual;
if (file.dependencies != null) {
for (OptionalFile dep : file.dependencies) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public String getType() {
return "batchProfileByUsername";
}

static class Entry {
public static class Entry {
@LauncherNetworkAPI
String username;
@LauncherNetworkAPI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.security.cert.CertificateException;
import java.time.Duration;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ public class BasicLauncherEventHandler implements RequestService.EventHandler {

@Override
public <T extends WebSocketEvent> boolean eventHandle(T event) {
if (event instanceof SecurityReportRequestEvent) {
SecurityReportRequestEvent event1 = (SecurityReportRequestEvent) event;
if (event instanceof SecurityReportRequestEvent event1) {
if (event1.action == SecurityReportRequestEvent.ReportAction.TOKEN_EXPIRED) {
try {
Request.restore();
} catch (Exception e) {
LogHelper.error(e);
}
}
} else if (event instanceof ExtendedTokenRequestEvent) {
ExtendedTokenRequestEvent event1 = (ExtendedTokenRequestEvent) event;
} else if (event instanceof ExtendedTokenRequestEvent event1) {
String token = event1.getExtendedToken();
if (token != null) {
Request.addExtendedToken(event1.getExtendedTokenName(), new Request.ExtendedToken(event1.getExtendedToken(), event1.getExtendedTokenExpire()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pro.gravit.launcher.base.modules.LauncherModule;
import pro.gravit.launcher.base.modules.impl.SimpleModuleManager;

import java.net.URL;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ private static CheckClassResultTypeApi fromType(LauncherTrustManager.CheckClassR
case UNTRUSTED -> CheckClassResultTypeApi.UNTRUSTED;
case UNVERIFED -> CheckClassResultTypeApi.UNVERIFED;
case UNCOMPAT -> CheckClassResultTypeApi.UNCOMPAT;
default -> CheckClassResultTypeApi.UNKNOWN;
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public HashedDir(HInput input) throws IOException {
entry = switch (type) {
case FILE -> new HashedFile(input);
case DIR -> new HashedDir(input);
default -> throw new AssertionError("Unsupported hashed entry type: " + type.name());
};

// Try add entry to map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public static String getNativeExtension(JVMHelper.OS OS_TYPE) {
case MUSTDIE -> ".dll";
case LINUX -> ".so";
case MACOSX -> ".dylib";
default -> throw new InternalError(String.format("Unsupported OS TYPE '%s'", OS_TYPE));
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void unpackZipNoCheck(String resource, Path target) throws IOExcep
IOHelper.transfer(input, target.resolve(IOHelper.toPath(entry.getName())));
}
}
} catch (NoSuchFileException e) {
} catch (NoSuchFileException ignored) {
}
}
}

0 comments on commit 038af76

Please sign in to comment.