From 63891cd743d440e08c1aaac9ea966b481f3b3a5f Mon Sep 17 00:00:00 2001 From: Gravita Date: Fri, 26 Mar 2021 21:56:17 +0700 Subject: [PATCH] [FEATURE] ProGuard Component --- .../pro/gravit/launchserver/LaunchServer.java | 48 +--- .../gravit/launchserver/Reconfigurable.java | 5 + .../binary/JARLauncherBinary.java | 5 +- .../launchserver/binary/ProguardConf.java | 100 --------- .../binary/tasks/ProGuardBuildTask.java | 65 ------ .../command/basic/ProguardCleanCommand.java | 29 --- .../basic/RegenProguardDictCommand.java | 29 --- .../basic/RemoveMappingsProguardCommand.java | 30 --- .../command/handler/CommandHandler.java | 3 - .../command/service/ComponentCommand.java | 3 +- .../components/AuthLimiterComponent.java | 11 +- .../components/CommandRemoverComponent.java | 11 +- .../launchserver/components/Component.java | 15 +- .../components/ProGuardComponent.java | 208 ++++++++++++++++++ .../components/RegLimiterComponent.java | 11 +- .../config/LaunchServerConfig.java | 9 +- .../pro/gravit/utils/command/SubCommand.java | 15 +- modules | 2 +- 18 files changed, 259 insertions(+), 340 deletions(-) delete mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/binary/ProguardConf.java delete mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/ProGuardBuildTask.java delete mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/ProguardCleanCommand.java delete mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/RegenProguardDictCommand.java delete mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/RemoveMappingsProguardCommand.java create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/components/ProGuardComponent.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java index 58dfb9c9..3c34c5b5 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java @@ -79,7 +79,6 @@ public final class LaunchServer implements Runnable, AutoCloseable, Reconfigurab public final FeaturesManager featuresManager; // HWID ban + anti-brutforce public final CertificateManager certificateManager; - public final ProguardConf proguardConf; // Server public final CommandHandler commandHandler; public final NettyServerSocketHandler nettyServerSocketHandler; @@ -126,17 +125,8 @@ public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, La runtime.verify(); config.verify(); if (config.sessions == null) config.sessions = new MemorySessionStorage(); - if (config.components != null) { - LogHelper.debug("PreInit components"); - config.components.forEach((k, v) -> { - LogHelper.subDebug("PreInit component %s", k); - v.preInit(this); - }); - LogHelper.debug("PreInit components successful"); - } // build hooks, anti-brutforce and other - proguardConf = new ProguardConf(this); sessionManager = new SessionManager(this); mirrorManager = new MirrorManager(); reconfigurableManager = new ReconfigurableManager(); @@ -154,14 +144,6 @@ public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, La // init modules modulesManager.invokeEvent(new LaunchServerInitPhase(this)); - if (config.components != null) { - LogHelper.debug("Init components"); - config.components.forEach((k, v) -> { - LogHelper.subDebug("Init component %s", k); - v.init(this); - }); - LogHelper.debug("Init components successful"); - } // Set launcher EXE binary launcherBinary = new JARLauncherBinary(this); @@ -171,6 +153,15 @@ public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, La launcherEXEBinary.init(); syncLauncherBinaries(); launcherModuleLoader = new LauncherModuleLoader(this); + if (config.components != null) { + LogHelper.debug("Init components"); + config.components.forEach((k, v) -> { + LogHelper.subDebug("Init component %s", k); + v.setComponentName(k); + v.init(this); + }); + LogHelper.debug("Init components successful"); + } // Sync updates dir if (!IOHelper.isDir(updatesDir)) Files.createDirectory(updatesDir); @@ -184,14 +175,6 @@ public LaunchServer(LaunchServerDirectories directories, LaunchServerEnv env, La nettyServerSocketHandler = new NettyServerSocketHandler(this); // post init modules modulesManager.invokeEvent(new LaunchServerPostInitPhase(this)); - if (config.components != null) { - LogHelper.debug("PostInit components"); - config.components.forEach((k, v) -> { - LogHelper.subDebug("PostInit component %s", k); - v.postInit(this); - }); - LogHelper.debug("PostInit components successful"); - } } public void reload(ReloadType type) throws Exception { @@ -209,24 +192,13 @@ public void reload(ReloadType type) throws Exception { config.verify(); config.init(type); if (type.equals(ReloadType.FULL) && config.components != null) { - LogHelper.debug("PreInit components"); - config.components.forEach((k, v) -> { - LogHelper.subDebug("PreInit component %s", k); - v.preInit(this); - }); - LogHelper.debug("PreInit components successful"); LogHelper.debug("Init components"); config.components.forEach((k, v) -> { LogHelper.subDebug("Init component %s", k); + v.setComponentName(k); v.init(this); }); LogHelper.debug("Init components successful"); - LogHelper.debug("PostInit components"); - config.components.forEach((k, v) -> { - LogHelper.subDebug("PostInit component %s", k); - v.postInit(this); - }); - LogHelper.debug("PostInit components successful"); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/Reconfigurable.java b/LaunchServer/src/main/java/pro/gravit/launchserver/Reconfigurable.java index 3b39c5f6..25da381d 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/Reconfigurable.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/Reconfigurable.java @@ -2,6 +2,7 @@ import pro.gravit.utils.command.Command; +import java.util.HashMap; import java.util.Map; /** @@ -15,4 +16,8 @@ public interface Reconfigurable { * Value is a command object */ Map getCommands(); + + default Map defaultCommandsMap() { + return new HashMap<>(); + } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/JARLauncherBinary.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/JARLauncherBinary.java index f6191d0a..c3a03985 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/JARLauncherBinary.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/JARLauncherBinary.java @@ -3,6 +3,7 @@ import pro.gravit.launcher.Launcher; import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.binary.tasks.*; +import pro.gravit.launchserver.components.ProGuardComponent; import java.io.IOException; import java.nio.file.Files; @@ -38,10 +39,8 @@ public void init() { tasks.add(new PrepareBuildTask(server)); if (!server.config.sign.enabled) tasks.add(new CertificateAutogenTask(server)); tasks.add(new MainBuildTask(server)); - if (server.config.launcher.attachLibraryBeforeProGuard) tasks.add(new AttachJarsTask(server)); - tasks.add(new ProGuardBuildTask(server)); + tasks.add(new AttachJarsTask(server)); tasks.add(new AdditionalFixesApplyTask(server)); - if (!server.config.launcher.attachLibraryBeforeProGuard) tasks.add(new AttachJarsTask(server)); if (server.config.launcher.compress) tasks.add(new CompressBuildTask(server)); tasks.add(new SignJarTask(server.config.sign, server)); } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/ProguardConf.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/ProguardConf.java deleted file mode 100644 index e955a30f..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/ProguardConf.java +++ /dev/null @@ -1,100 +0,0 @@ -package pro.gravit.launchserver.binary; - -import pro.gravit.launchserver.LaunchServer; -import pro.gravit.utils.helper.*; - -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.nio.file.Files; -import java.nio.file.Path; -import java.security.SecureRandom; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class ProguardConf { - public static final String[] JAVA9_OPTS = new String[]{ - "-libraryjars '/jmods/'" - }; - public static final String[] JAVA8_OPTS = new String[]{ - "-libraryjars '/lib/rt.jar'", - "-libraryjars '/lib/jce.jar'", - "-libraryjars '/lib/ext/nashorn.jar'", - "-libraryjars '/lib/ext/jfxrt.jar'" - }; - private static final char[] chars = "1aAbBcC2dDeEfF3gGhHiI4jJkKl5mMnNoO6pPqQrR7sStT8uUvV9wWxX0yYzZ".toCharArray(); - public final Path proguard; - public final Path config; - public final Path mappings; - public final Path words; - public transient final LaunchServer srv; - - public ProguardConf(LaunchServer srv) { - proguard = srv.dir.resolve("proguard"); - config = proguard.resolve("proguard.config"); - mappings = proguard.resolve("mappings.pro"); - words = proguard.resolve("random.pro"); - this.srv = srv; - } - - private static String generateString(SecureRandom rand, String lowString, String upString, int il) { - StringBuilder sb = new StringBuilder(Math.max(il, lowString.length())); - for (int i = 0; i < lowString.length(); ++i) { - sb.append(rand.nextBoolean() ? lowString.charAt(i) : upString.charAt(i)); - } - int toI = il - lowString.length(); - for (int i = 0; i < toI; i++) sb.append(chars[rand.nextInt(chars.length)]); - return sb.toString(); - } - - public String[] buildConfig(Path inputJar, Path outputJar) { - List confStrs = new ArrayList<>(); - prepare(false); - if (srv.config.launcher.proguardGenMappings) - confStrs.add("-printmapping '" + mappings.toFile().getName() + "'"); - confStrs.add("-obfuscationdictionary '" + words.toFile().getName() + "'"); - confStrs.add("-injar '" + inputJar.toAbsolutePath() + "'"); - confStrs.add("-outjar '" + outputJar.toAbsolutePath() + "'"); - Collections.addAll(confStrs, JVMHelper.JVM_VERSION >= 9 ? JAVA9_OPTS : JAVA8_OPTS); - srv.launcherBinary.coreLibs.stream() - .map(e -> "-libraryjars '" + e.toAbsolutePath().toString() + "'") - .forEach(confStrs::add); - - srv.launcherBinary.addonLibs.stream() - .map(e -> "-libraryjars '" + e.toAbsolutePath().toString() + "'") - .forEach(confStrs::add); - confStrs.add("-classobfuscationdictionary '" + words.toFile().getName() + "'"); - confStrs.add("@".concat(config.toFile().getName())); - return confStrs.toArray(new String[0]); - } - - private void genConfig(boolean force) throws IOException { - if (IOHelper.exists(config) && !force) return; - Files.deleteIfExists(config); - UnpackHelper.unpack(IOHelper.getResourceURL("pro/gravit/launchserver/defaults/proguard.cfg"), config); - } - - public void genWords(boolean force) throws IOException { - if (IOHelper.exists(words) && !force) return; - Files.deleteIfExists(words); - SecureRandom rand = SecurityHelper.newRandom(); - rand.setSeed(SecureRandom.getSeed(32)); - try (PrintWriter out = new PrintWriter(new OutputStreamWriter(IOHelper.newOutput(words), IOHelper.UNICODE_CHARSET))) { - String projectName = srv.config.projectName.replaceAll("\\W", ""); - String lowName = projectName.toLowerCase(); - String upName = projectName.toUpperCase(); - for (int i = 0; i < Short.MAX_VALUE; i++) out.println(generateString(rand, lowName, upName, 14)); - } - } - - public void prepare(boolean force) { - try { - IOHelper.createParentDirs(config); - genWords(force); - genConfig(force); - } catch (IOException e) { - LogHelper.error(e); - } - } -} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/ProGuardBuildTask.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/ProGuardBuildTask.java deleted file mode 100644 index 7fe6b43a..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/ProGuardBuildTask.java +++ /dev/null @@ -1,65 +0,0 @@ -package pro.gravit.launchserver.binary.tasks; - -import pro.gravit.launchserver.LaunchServer; -import pro.gravit.utils.helper.IOHelper; -import pro.gravit.utils.helper.JVMHelper; -import pro.gravit.utils.helper.LogHelper; -import proguard.Configuration; -import proguard.ConfigurationParser; -import proguard.ParseException; -import proguard.ProGuard; - -import java.io.IOException; -import java.nio.file.Path; -import java.nio.file.Paths; - -public class ProGuardBuildTask implements LauncherBuildTask { - private final LaunchServer server; - - public ProGuardBuildTask(LaunchServer server) { - this.server = server; - } - - @Override - public String getName() { - return "ProGuard"; - } - - @Override - public Path process(Path inputFile) throws IOException { - Path outputJar = server.launcherBinary.nextLowerPath(this); - if (server.config.launcher.enabledProGuard) { - Configuration proguard_cfg = new Configuration(); - ConfigurationParser parser = new ConfigurationParser(server.proguardConf.buildConfig(inputFile, outputJar), - server.proguardConf.proguard.toFile(), System.getProperties()); - if (JVMHelper.JVM_VERSION >= 9) { - Path javaJModsPath = Paths.get(System.getProperty("java.home")).resolve("jmods"); - if (!IOHelper.exists(javaJModsPath)) { - LogHelper.warning("Directory %s not found. It is not good", javaJModsPath); - } else { - //Find javaFX libraries - if (!IOHelper.exists(javaJModsPath.resolve("javafx.base.jmod"))) - LogHelper.error("javafx.base.jmod not found. Launcher can be assembled incorrectly. Maybe you need to install OpenJFX?"); - if (!IOHelper.exists(javaJModsPath.resolve("javafx.graphics.jmod"))) - LogHelper.error("javafx.graphics.jmod not found. Launcher can be assembled incorrectly. Maybe you need to install OpenJFX?"); - if (!IOHelper.exists(javaJModsPath.resolve("javafx.controls.jmod"))) - LogHelper.error("javafx.controls.jmod not found. Launcher can be assembled incorrectly. Maybe you need to install OpenJFX?"); - } - } - try { - parser.parse(proguard_cfg); - ProGuard proGuard = new ProGuard(proguard_cfg); - proGuard.execute(); - } catch (ParseException e) { - LogHelper.error(e); - } - } else - IOHelper.copy(inputFile, outputJar); - return outputJar; - } - - @Override - public boolean allowDelete() { - return true; - } -} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/ProguardCleanCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/ProguardCleanCommand.java deleted file mode 100644 index 7ec6460b..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/ProguardCleanCommand.java +++ /dev/null @@ -1,29 +0,0 @@ -package pro.gravit.launchserver.command.basic; - -import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.command.Command; - -import java.io.IOException; -import java.nio.file.Files; - -public class ProguardCleanCommand extends Command { - public ProguardCleanCommand(LaunchServer server) { - super(server); - } - - @Override - public String getArgsDescription() { - return null; - } - - @Override - public String getUsageDescription() { - return "Resets proguard config"; - } - - @Override - public void invoke(String... args) throws IOException { - server.proguardConf.prepare(true); - Files.deleteIfExists(server.proguardConf.mappings); - } -} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/RegenProguardDictCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/RegenProguardDictCommand.java deleted file mode 100644 index 214ec014..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/RegenProguardDictCommand.java +++ /dev/null @@ -1,29 +0,0 @@ -package pro.gravit.launchserver.command.basic; - -import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.command.Command; - -import java.io.IOException; - -public class RegenProguardDictCommand extends Command { - - public RegenProguardDictCommand(LaunchServer server) { - super(server); - } - - @Override - public String getArgsDescription() { - return null; - } - - @Override - public String getUsageDescription() { - return "Regenerates proguard dictonary"; - } - - @Override - public void invoke(String... args) throws IOException { - server.proguardConf.genWords(true); - } - -} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/RemoveMappingsProguardCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/RemoveMappingsProguardCommand.java deleted file mode 100644 index f16d5374..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/basic/RemoveMappingsProguardCommand.java +++ /dev/null @@ -1,30 +0,0 @@ -package pro.gravit.launchserver.command.basic; - -import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.command.Command; - -import java.io.IOException; -import java.nio.file.Files; - -public class RemoveMappingsProguardCommand extends Command { - - public RemoveMappingsProguardCommand(LaunchServer server) { - super(server); - } - - @Override - public String getArgsDescription() { - return null; - } - - @Override - public String getUsageDescription() { - return "Removes proguard mappings (if you want to gen new mappings)."; - } - - @Override - public void invoke(String... args) throws IOException { - Files.deleteIfExists(server.proguardConf.mappings); - } - -} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java index 8b0a7507..a54ce314 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/handler/CommandHandler.java @@ -27,9 +27,6 @@ public static void registerCommands(pro.gravit.utils.command.CommandHandler hand basic.registerCommand("debug", new DebugCommand()); basic.registerCommand("clear", new ClearCommand(handler)); basic.registerCommand("gc", new GCCommand()); - basic.registerCommand("proguardClean", new ProguardCleanCommand(server)); - basic.registerCommand("proguardDictRegen", new RegenProguardDictCommand(server)); - basic.registerCommand("proguardMappingsRemove", new RemoveMappingsProguardCommand(server)); basic.registerCommand("loadModule", new LoadModuleCommand(server)); basic.registerCommand("modules", new ModulesCommand(server)); basic.registerCommand("test", new TestCommand(server)); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ComponentCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ComponentCommand.java index 0e5ec89c..99ed0557 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ComponentCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/ComponentCommand.java @@ -75,9 +75,8 @@ public void invoke(String... args) throws Exception { String fileName = args[2]; try (Reader reader = IOHelper.newReader(Paths.get(fileName))) { Component component = Launcher.gsonManager.configGson.fromJson(reader, Component.class); - component.preInit(server); + component.setComponentName(componentName); component.init(server); - component.postInit(server); LogHelper.info("Component %s(%s) loaded", componentName, component.getClass().getName()); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/AuthLimiterComponent.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/AuthLimiterComponent.java index e14e0142..cda63163 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/AuthLimiterComponent.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/AuthLimiterComponent.java @@ -10,21 +10,12 @@ public class AuthLimiterComponent extends IPLimiter implements NeedGarbageCollec public String message; private transient LaunchServer srv; - @Override - public void preInit(LaunchServer launchServer) { - srv = launchServer; - } - @Override public void init(LaunchServer launchServer) { + srv = launchServer; launchServer.authHookManager.preHook.registerHook(this::preAuthHook); } - @Override - public void postInit(LaunchServer launchServer) { - - } - public boolean preAuthHook(AuthResponse.AuthContext context, Client client) { if (!check(context.ip)) { throw new HookException(message); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/CommandRemoverComponent.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/CommandRemoverComponent.java index a42b7151..93f9759a 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/CommandRemoverComponent.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/CommandRemoverComponent.java @@ -11,18 +11,9 @@ public class CommandRemoverComponent extends Component implements AutoCloseable public final transient Map commandsList = new HashMap<>(); private transient LaunchServer server = null; - @Override - public void preInit(LaunchServer launchServer) { - server = launchServer; - } - @Override public void init(LaunchServer launchServer) { - - } - - @Override - public void postInit(LaunchServer launchServer) { + server = launchServer; for (String cmd : removeList) { Command removedCmd = launchServer.commandHandler.unregisterCommand(cmd); if (removedCmd != null) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java index e1d61b26..508036c6 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java @@ -6,6 +6,7 @@ public abstract class Component { public static final ProviderMap providers = new ProviderMap<>(); private static boolean registredComp = false; + protected String componentName; public static void registerComponents() { if (!registredComp) { @@ -16,9 +17,19 @@ public static void registerComponents() { } } - public abstract void preInit(LaunchServer launchServer); + @Deprecated + public void preInit(LaunchServer launchServer) { + + } public abstract void init(LaunchServer launchServer); - public abstract void postInit(LaunchServer launchServer); + public final void setComponentName(String s) { + this.componentName = s; + } + + @Deprecated + public void postInit(LaunchServer launchServer) { + + } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/ProGuardComponent.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/ProGuardComponent.java new file mode 100644 index 00000000..2c64ea68 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/ProGuardComponent.java @@ -0,0 +1,208 @@ +package pro.gravit.launchserver.components; + +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.Reconfigurable; +import pro.gravit.launchserver.binary.tasks.LauncherBuildTask; +import pro.gravit.utils.command.Command; +import pro.gravit.utils.command.SubCommand; +import pro.gravit.utils.helper.*; +import proguard.Configuration; +import proguard.ConfigurationParser; +import proguard.ParseException; +import proguard.ProGuard; + +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.SecureRandom; +import java.util.*; + +public class ProGuardComponent extends Component implements AutoCloseable, Reconfigurable { + public String modeAfter = "MainBuild"; + public String dir = "proguard"; + public boolean enabled = true; + public boolean mappings = true; + public transient ProguardConf proguardConf; + @Override + public void init(LaunchServer launchServer) { + proguardConf = new ProguardConf(launchServer, this); + launchServer.launcherBinary.add((v) -> v.getName().startsWith(modeAfter), new ProGuardBuildTask(launchServer, proguardConf, this)); + } + + @Override + public void close() throws Exception { + + } + + @Override + public Map getCommands() { + Map commands = defaultCommandsMap(); + commands.put("reset", new SubCommand("[]", "reset proguard config") { + @Override + public void invoke(String... args) throws Exception { + proguardConf.prepare(true); + Files.deleteIfExists(proguardConf.mappings); + } + }); + commands.put("regen", new SubCommand("[]", "regenerate proguard dictionary") { + @Override + public void invoke(String... args) throws Exception { + proguardConf.genWords(true); + } + }); + commands.put("clean", new SubCommand("[]", "clean proguard mappings") { + @Override + public void invoke(String... args) throws Exception { + proguardConf.prepare(true); + Files.deleteIfExists(proguardConf.mappings); + } + }); + return null; + } + + public static class ProGuardBuildTask implements LauncherBuildTask { + private final LaunchServer server; + private final ProGuardComponent component; + private final ProguardConf proguardConf; + + public ProGuardBuildTask(LaunchServer server, ProguardConf conf, ProGuardComponent component) { + this.server = server; + this.component = component; + this.proguardConf = conf; + } + + @Override + public String getName() { + return "ProGuard.".concat(component.componentName); + } + + @Override + public Path process(Path inputFile) throws IOException { + Path outputJar = server.launcherBinary.nextLowerPath(this); + if (component.enabled) { + Configuration proguard_cfg = new Configuration(); + ConfigurationParser parser = new ConfigurationParser(proguardConf.buildConfig(inputFile, outputJar), + proguardConf.proguard.toFile(), System.getProperties()); + if (JVMHelper.JVM_VERSION >= 9) { + Path javaJModsPath = Paths.get(System.getProperty("java.home")).resolve("jmods"); + if (!IOHelper.exists(javaJModsPath)) { + LogHelper.warning("Directory %s not found. It is not good", javaJModsPath); + } else { + //Find javaFX libraries + if (!IOHelper.exists(javaJModsPath.resolve("javafx.base.jmod"))) + LogHelper.error("javafx.base.jmod not found. Launcher can be assembled incorrectly. Maybe you need to install OpenJFX?"); + if (!IOHelper.exists(javaJModsPath.resolve("javafx.graphics.jmod"))) + LogHelper.error("javafx.graphics.jmod not found. Launcher can be assembled incorrectly. Maybe you need to install OpenJFX?"); + if (!IOHelper.exists(javaJModsPath.resolve("javafx.controls.jmod"))) + LogHelper.error("javafx.controls.jmod not found. Launcher can be assembled incorrectly. Maybe you need to install OpenJFX?"); + } + } + try { + parser.parse(proguard_cfg); + ProGuard proGuard = new ProGuard(proguard_cfg); + proGuard.execute(); + } catch (ParseException e) { + LogHelper.error(e); + } + } else + IOHelper.copy(inputFile, outputJar); + return outputJar; + } + + @Override + public boolean allowDelete() { + return true; + } + } + + public static class ProguardConf { + public static final String[] JAVA9_OPTS = new String[]{ + "-libraryjars '/jmods/'" + }; + public static final String[] JAVA8_OPTS = new String[]{ + "-libraryjars '/lib/rt.jar'", + "-libraryjars '/lib/jce.jar'", + "-libraryjars '/lib/ext/nashorn.jar'", + "-libraryjars '/lib/ext/jfxrt.jar'" + }; + private static final char[] chars = "1aAbBcC2dDeEfF3gGhHiI4jJkKl5mMnNoO6pPqQrR7sStT8uUvV9wWxX0yYzZ".toCharArray(); + public final Path proguard; + public final Path config; + public final Path mappings; + public final Path words; + public transient final LaunchServer srv; + private transient final ProGuardComponent component; + + public ProguardConf(LaunchServer srv, ProGuardComponent component) { + this.component = component; + this.proguard = srv.dir.resolve(component.dir); + config = proguard.resolve("proguard.config"); + mappings = proguard.resolve("mappings.pro"); + words = proguard.resolve("random.pro"); + this.srv = srv; + } + + private static String generateString(SecureRandom rand, String lowString, String upString, int il) { + StringBuilder sb = new StringBuilder(Math.max(il, lowString.length())); + for (int i = 0; i < lowString.length(); ++i) { + sb.append(rand.nextBoolean() ? lowString.charAt(i) : upString.charAt(i)); + } + int toI = il - lowString.length(); + for (int i = 0; i < toI; i++) sb.append(chars[rand.nextInt(chars.length)]); + return sb.toString(); + } + + public String[] buildConfig(Path inputJar, Path outputJar) { + List confStrs = new ArrayList<>(); + prepare(false); + if (component.mappings) + confStrs.add("-printmapping '" + mappings.toFile().getName() + "'"); + confStrs.add("-obfuscationdictionary '" + words.toFile().getName() + "'"); + confStrs.add("-injar '" + inputJar.toAbsolutePath() + "'"); + confStrs.add("-outjar '" + outputJar.toAbsolutePath() + "'"); + Collections.addAll(confStrs, JVMHelper.JVM_VERSION >= 9 ? JAVA9_OPTS : JAVA8_OPTS); + srv.launcherBinary.coreLibs.stream() + .map(e -> "-libraryjars '" + e.toAbsolutePath().toString() + "'") + .forEach(confStrs::add); + + srv.launcherBinary.addonLibs.stream() + .map(e -> "-libraryjars '" + e.toAbsolutePath().toString() + "'") + .forEach(confStrs::add); + confStrs.add("-classobfuscationdictionary '" + words.toFile().getName() + "'"); + confStrs.add("@".concat(config.toFile().getName())); + return confStrs.toArray(new String[0]); + } + + private void genConfig(boolean force) throws IOException { + if (IOHelper.exists(config) && !force) return; + Files.deleteIfExists(config); + UnpackHelper.unpack(IOHelper.getResourceURL("pro/gravit/launchserver/defaults/proguard.cfg"), config); + } + + public void genWords(boolean force) throws IOException { + if (IOHelper.exists(words) && !force) return; + Files.deleteIfExists(words); + SecureRandom rand = SecurityHelper.newRandom(); + rand.setSeed(SecureRandom.getSeed(32)); + try (PrintWriter out = new PrintWriter(new OutputStreamWriter(IOHelper.newOutput(words), IOHelper.UNICODE_CHARSET))) { + String projectName = srv.config.projectName.replaceAll("\\W", ""); + String lowName = projectName.toLowerCase(); + String upName = projectName.toUpperCase(); + for (int i = 0; i < Short.MAX_VALUE; i++) out.println(generateString(rand, lowName, upName, 14)); + } + } + + public void prepare(boolean force) { + try { + IOHelper.createParentDirs(config); + genWords(force); + genConfig(force); + } catch (IOException e) { + LogHelper.error(e); + } + } + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/RegLimiterComponent.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/RegLimiterComponent.java index 426395b3..2ca25fd7 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/RegLimiterComponent.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/RegLimiterComponent.java @@ -15,18 +15,9 @@ public class RegLimiterComponent extends IPLimiter implements NeedGarbageCollect public List excludeIps = new ArrayList<>(); - @Override - public void preInit(LaunchServer launchServer) { - this.launchServer = launchServer; - } - @Override public void init(LaunchServer launchServer) { - - } - - @Override - public void postInit(LaunchServer launchServer) { + this.launchServer = launchServer; launchServer.authHookManager.registraion.registerHook(this::registerHook); } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java b/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java index 9c273026..af4f2208 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/config/LaunchServerConfig.java @@ -16,6 +16,7 @@ import pro.gravit.launchserver.binary.tasks.exe.Launch4JTask; import pro.gravit.launchserver.components.AuthLimiterComponent; import pro.gravit.launchserver.components.Component; +import pro.gravit.launchserver.components.ProGuardComponent; import pro.gravit.launchserver.components.RegLimiterComponent; import pro.gravit.launchserver.dao.provider.DaoProvider; import pro.gravit.utils.Version; @@ -93,11 +94,8 @@ public static LaunchServerConfig getDefault(LaunchServer.LaunchServerEnv env) { newConfig.launcher = new LauncherConf(); newConfig.launcher.guardType = "no"; newConfig.launcher.compress = true; - newConfig.launcher.attachLibraryBeforeProGuard = false; newConfig.launcher.deleteTempFiles = true; - newConfig.launcher.enabledProGuard = true; newConfig.launcher.stripLineNumbers = true; - newConfig.launcher.proguardGenMappings = true; newConfig.sign = new JarSignerConf(); @@ -112,6 +110,8 @@ public static LaunchServerConfig getDefault(LaunchServer.LaunchServerEnv env) { regLimiterComponent.rateLimitMillis = 1000 * 60 * 60 * 10; //Блок на 10 часов regLimiterComponent.message = "Превышен лимит регистраций"; newConfig.components.put("regLimiter", regLimiterComponent); + ProGuardComponent proGuardComponent = new ProGuardComponent(); + newConfig.components.put("proguard", proGuardComponent); return newConfig; } @@ -293,14 +293,11 @@ public static class NettyUpdatesBind { public static class LauncherConf { public String guardType; - public boolean attachLibraryBeforeProGuard; public boolean compress; @Deprecated public boolean warningMissArchJava; - public boolean enabledProGuard; public boolean stripLineNumbers; public boolean deleteTempFiles; - public boolean proguardGenMappings; public boolean certificatePinning; public int memoryLimit = 256; } diff --git a/LauncherCore/src/main/java/pro/gravit/utils/command/SubCommand.java b/LauncherCore/src/main/java/pro/gravit/utils/command/SubCommand.java index 54bd51fe..b4e001c7 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/command/SubCommand.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/command/SubCommand.java @@ -1,13 +1,24 @@ package pro.gravit.utils.command; public abstract class SubCommand extends Command { + private String defaultArgs; + private String defaultUsage; + + public SubCommand() { + } + + public SubCommand(String defaultArgs, String defaultUsage) { + this.defaultArgs = defaultArgs; + this.defaultUsage = defaultUsage; + } + @Override public String getArgsDescription() { - return null; + return defaultArgs; } @Override public String getUsageDescription() { - return null; + return defaultUsage; } } diff --git a/modules b/modules index 02ff0dc3..34cece4a 160000 --- a/modules +++ b/modules @@ -1 +1 @@ -Subproject commit 02ff0dc35355d47dc181d93acb1bf11d6eb57bc4 +Subproject commit 34cece4a9809b12ddc6c13d89c7bd7daabcb2068