From f188378fd6551781daecf5f5b882bba4bb485647 Mon Sep 17 00:00:00 2001 From: Gravita Date: Tue, 4 May 2021 19:13:33 +0700 Subject: [PATCH] [FEATURE] Print launcher modules in `modules` command --- .../command/modules/ModulesCommand.java | 34 ++++++++++++++++++- .../launchermodules/LauncherModuleLoader.java | 23 ++++++++++--- .../impl/LaunchServerModulesManager.java | 7 ++++ 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/modules/ModulesCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/modules/ModulesCommand.java index 1b35aaf3..4b557a1d 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/modules/ModulesCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/modules/ModulesCommand.java @@ -1,9 +1,20 @@ package pro.gravit.launchserver.command.modules; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import pro.gravit.launcher.LauncherTrustManager; +import pro.gravit.launcher.modules.LauncherModule; +import pro.gravit.launcher.modules.LauncherModuleInfo; import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.command.Command; +import pro.gravit.launchserver.launchermodules.LauncherModuleLoader; +import pro.gravit.utils.helper.LogHelper; + +import java.security.cert.X509Certificate; +import java.util.Arrays; public class ModulesCommand extends Command { + private transient Logger logger = LogManager.getLogger(); public ModulesCommand(LaunchServer server) { super(server); } @@ -20,6 +31,27 @@ public String getUsageDescription() { @Override public void invoke(String... args) { - server.modulesManager.printModulesInfo(); + for (LauncherModule module : server.modulesManager.getModules()) { + LauncherModuleInfo info = module.getModuleInfo(); + LauncherTrustManager.CheckClassResult checkStatus = module.getCheckResult(); + logger.info("[MODULE] {} v: {} p: {} deps: {} sig: {}", info.name, info.version.getVersionString(), info.priority, Arrays.toString(info.dependencies), checkStatus == null ? "null" : checkStatus.type); + printCheckStatusInfo(checkStatus); + } + for(LauncherModuleLoader.ModuleEntity entity : server.launcherModuleLoader.launcherModules) { + LauncherTrustManager.CheckClassResult checkStatus = entity.checkResult; + logger.info("[LAUNCHER MODULE] {} sig: {}", entity.path.getFileName().toString(), checkStatus == null ? "null" : checkStatus.type); + printCheckStatusInfo(checkStatus); + } + } + + private void printCheckStatusInfo(LauncherTrustManager.CheckClassResult checkStatus) { + if (checkStatus != null && checkStatus.endCertificate != null) { + X509Certificate cert = checkStatus.endCertificate; + logger.info("[MODULE CERT] Module signer: {}", cert.getSubjectDN().getName()); + } + if (checkStatus != null && checkStatus.rootCertificate != null) { + X509Certificate cert = checkStatus.rootCertificate; + logger.info("[MODULE CERT] Module signer CA: {}", cert.getSubjectDN().getName()); + } } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/launchermodules/LauncherModuleLoader.java b/LaunchServer/src/main/java/pro/gravit/launchserver/launchermodules/LauncherModuleLoader.java index 472995bc..b2afe7fd 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/launchermodules/LauncherModuleLoader.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/launchermodules/LauncherModuleLoader.java @@ -1,6 +1,8 @@ package pro.gravit.launchserver.launchermodules; import pro.gravit.launcher.Launcher; +import pro.gravit.launcher.LauncherTrustManager; +import pro.gravit.launcher.modules.LauncherModule; import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.asm.InjectClassAcceptor; import pro.gravit.launchserver.binary.tasks.MainBuildTask; @@ -84,8 +86,9 @@ public void addClassFieldsToProperties(Map propertyMap, String p } } - static class ModuleEntity { + public static class ModuleEntity { public Path path; + public LauncherTrustManager.CheckClassResult checkResult; public String moduleMainClass; public String moduleConfigClass; public String moduleConfigName; @@ -99,6 +102,7 @@ private ModulesVisitor() { } @Override + @SuppressWarnings("unchecked") public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (file.toFile().getName().endsWith(".jar")) try (JarFile f = new JarFile(file.toFile())) { @@ -107,9 +111,23 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO if (mainClass == null) { LogHelper.error("In module %s MainClass not found", file.toString()); } else { + if (classLoader == null) + classLoader = new LauncherModuleClassLoader(server.modulesManager.getModuleClassLoader()); + classLoader.addURL(file.toUri().toURL()); ModuleEntity entity = new ModuleEntity(); entity.path = file; entity.moduleMainClass = mainClass; + try { + Class mainClazz = (Class) classLoader.loadClass(entity.moduleMainClass); + entity.checkResult = server.modulesManager.checkModuleClass(mainClazz); + } catch (Throwable e) { + if(e instanceof ClassNotFoundException || e instanceof NoClassDefFoundError) { + LogHelper.error("Module-MainClass in module %s incorrect", file.toString()); + } else { + LogHelper.error(e); + } + return super.visitFile(file, attrs); + } entity.moduleConfigClass = attributes.getValue("Module-Config-Class"); if (entity.moduleConfigClass != null) { entity.moduleConfigName = attributes.getValue("Module-Config-Name"); @@ -117,9 +135,6 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO LogHelper.warning("Module-Config-Name in module %s null. Module not configured", file.toString()); } else { try { - if (classLoader == null) - classLoader = new LauncherModuleClassLoader(server.modulesManager.getModuleClassLoader()); - classLoader.addURL(file.toUri().toURL()); Class clazz = classLoader.loadClass(entity.moduleConfigClass); Path configPath = server.modulesManager.getConfigManager().getModuleConfig(entity.moduleConfigName); Object defaultConfig = MethodHandles.publicLookup().findStatic(clazz, "getDefault", MethodType.methodType(Object.class)).invoke(); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/modules/impl/LaunchServerModulesManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/modules/impl/LaunchServerModulesManager.java index 13fe4482..c770a883 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/modules/impl/LaunchServerModulesManager.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/modules/impl/LaunchServerModulesManager.java @@ -10,6 +10,8 @@ import java.nio.file.Path; import java.security.cert.X509Certificate; import java.util.Arrays; +import java.util.Collections; +import java.util.List; public class LaunchServerModulesManager extends SimpleModuleManager { public final LaunchServerCoreModule coreModule; @@ -24,6 +26,7 @@ public void fullInitializedLaunchServer(LaunchServer server) { initContext = new LaunchServerInitContext(server); } + @Deprecated public void printModulesInfo() { for (LauncherModule module : modules) { LauncherModuleInfo info = module.getModuleInfo(); @@ -40,6 +43,10 @@ public void printModulesInfo() { } } + public List getModules() { + return Collections.unmodifiableList(modules); + } + @Override public final boolean verifyClassCheckResult(LauncherTrustManager.CheckClassResult result) { return true;