[FEATURE] Print launcher modules in modules command

This commit is contained in:
Gravita 2021-05-04 19:13:33 +07:00
parent ae82964a24
commit f188378fd6
3 changed files with 59 additions and 5 deletions

View file

@ -1,9 +1,20 @@
package pro.gravit.launchserver.command.modules; 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.LaunchServer;
import pro.gravit.launchserver.command.Command; 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 { public class ModulesCommand extends Command {
private transient Logger logger = LogManager.getLogger();
public ModulesCommand(LaunchServer server) { public ModulesCommand(LaunchServer server) {
super(server); super(server);
} }
@ -20,6 +31,27 @@ public String getUsageDescription() {
@Override @Override
public void invoke(String... args) { 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());
}
} }
} }

View file

@ -1,6 +1,8 @@
package pro.gravit.launchserver.launchermodules; package pro.gravit.launchserver.launchermodules;
import pro.gravit.launcher.Launcher; 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.LaunchServer;
import pro.gravit.launchserver.asm.InjectClassAcceptor; import pro.gravit.launchserver.asm.InjectClassAcceptor;
import pro.gravit.launchserver.binary.tasks.MainBuildTask; import pro.gravit.launchserver.binary.tasks.MainBuildTask;
@ -84,8 +86,9 @@ public void addClassFieldsToProperties(Map<String, Object> propertyMap, String p
} }
} }
static class ModuleEntity { public static class ModuleEntity {
public Path path; public Path path;
public LauncherTrustManager.CheckClassResult checkResult;
public String moduleMainClass; public String moduleMainClass;
public String moduleConfigClass; public String moduleConfigClass;
public String moduleConfigName; public String moduleConfigName;
@ -99,6 +102,7 @@ private ModulesVisitor() {
} }
@Override @Override
@SuppressWarnings("unchecked")
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toFile().getName().endsWith(".jar")) if (file.toFile().getName().endsWith(".jar"))
try (JarFile f = new JarFile(file.toFile())) { try (JarFile f = new JarFile(file.toFile())) {
@ -107,9 +111,23 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
if (mainClass == null) { if (mainClass == null) {
LogHelper.error("In module %s MainClass not found", file.toString()); LogHelper.error("In module %s MainClass not found", file.toString());
} else { } else {
if (classLoader == null)
classLoader = new LauncherModuleClassLoader(server.modulesManager.getModuleClassLoader());
classLoader.addURL(file.toUri().toURL());
ModuleEntity entity = new ModuleEntity(); ModuleEntity entity = new ModuleEntity();
entity.path = file; entity.path = file;
entity.moduleMainClass = mainClass; entity.moduleMainClass = mainClass;
try {
Class<? extends LauncherModule> mainClazz = (Class<? extends LauncherModule>) 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"); entity.moduleConfigClass = attributes.getValue("Module-Config-Class");
if (entity.moduleConfigClass != null) { if (entity.moduleConfigClass != null) {
entity.moduleConfigName = attributes.getValue("Module-Config-Name"); 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()); LogHelper.warning("Module-Config-Name in module %s null. Module not configured", file.toString());
} else { } else {
try { try {
if (classLoader == null)
classLoader = new LauncherModuleClassLoader(server.modulesManager.getModuleClassLoader());
classLoader.addURL(file.toUri().toURL());
Class<?> clazz = classLoader.loadClass(entity.moduleConfigClass); Class<?> clazz = classLoader.loadClass(entity.moduleConfigClass);
Path configPath = server.modulesManager.getConfigManager().getModuleConfig(entity.moduleConfigName); Path configPath = server.modulesManager.getConfigManager().getModuleConfig(entity.moduleConfigName);
Object defaultConfig = MethodHandles.publicLookup().findStatic(clazz, "getDefault", MethodType.methodType(Object.class)).invoke(); Object defaultConfig = MethodHandles.publicLookup().findStatic(clazz, "getDefault", MethodType.methodType(Object.class)).invoke();

View file

@ -10,6 +10,8 @@
import java.nio.file.Path; import java.nio.file.Path;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class LaunchServerModulesManager extends SimpleModuleManager { public class LaunchServerModulesManager extends SimpleModuleManager {
public final LaunchServerCoreModule coreModule; public final LaunchServerCoreModule coreModule;
@ -24,6 +26,7 @@ public void fullInitializedLaunchServer(LaunchServer server) {
initContext = new LaunchServerInitContext(server); initContext = new LaunchServerInitContext(server);
} }
@Deprecated
public void printModulesInfo() { public void printModulesInfo() {
for (LauncherModule module : modules) { for (LauncherModule module : modules) {
LauncherModuleInfo info = module.getModuleInfo(); LauncherModuleInfo info = module.getModuleInfo();
@ -40,6 +43,10 @@ public void printModulesInfo() {
} }
} }
public List<LauncherModule> getModules() {
return Collections.unmodifiableList(modules);
}
@Override @Override
public final boolean verifyClassCheckResult(LauncherTrustManager.CheckClassResult result) { public final boolean verifyClassCheckResult(LauncherTrustManager.CheckClassResult result) {
return true; return true;