From 5d2def141406fad66b368ee57473959436e65523 Mon Sep 17 00:00:00 2001 From: Gravit Date: Sun, 23 Sep 2018 18:25:02 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A2=D1=80=D0=B0=D0=BD=D1=81=D1=84=D0=BE?= =?UTF-8?q?=D1=80=D0=BC=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BA=D0=BB=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D0=BE=D0=B2=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20ProGuard?= =?UTF-8?q?=20(BuildHook=20API)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../binary/JARLauncherBinary.java | 39 +++++++++++++++++-- .../manangers/BuildHookManager.java | 20 ++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java index aa73ed52..b6f12a51 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java @@ -5,10 +5,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; +import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; import java.util.HashMap; import java.util.Map; @@ -24,6 +21,7 @@ import ru.gravit.launcher.Launcher; import ru.gravit.launcher.LauncherAPI; import ru.gravit.launcher.LauncherConfig; +import ru.gravit.launchserver.manangers.BuildHookManager; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.SecurityHelper; @@ -110,6 +108,38 @@ public void build() throws IOException { } catch (ParseException e1) { e1.printStackTrace(); } + if(server.buildHookManager.isNeedPostProguardHook()) + { + Path obfPath = Paths.get(server.config.binaryName + "-obf.jar"); + Path tmpPath = Paths.get(server.config.binaryName + "-tmp.jar"); + IOHelper.move(obfPath,tmpPath); + try (ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(obfPath))) + { + try (ZipInputStream input = new ZipInputStream( + IOHelper.newInput(tmpPath))) { + ZipEntry e = input.getNextEntry(); + while (e != null) { + String filename = e.getName(); + output.putNextEntry(e); + if (filename.endsWith(".class")) { + CharSequence classname = filename.replace('/', '.').subSequence(0, + filename.length() - ".class".length()); + byte[] bytes; + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(2048)) { + IOHelper.transfer(input, outputStream); + bytes = outputStream.toByteArray(); + } + bytes = server.buildHookManager.proGuardClassTransform(bytes, classname); + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) { + IOHelper.transfer(inputStream, output); + } + } else + IOHelper.transfer(input, output); + e = input.getNextEntry(); + } + } + } + } if (server.config.sign.enabled) signBuild(); } @@ -123,6 +153,7 @@ private void signBuild() throws IOException { while (e != null) { output.addFileContents(e, input); e = input.getNextEntry(); + } } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/BuildHookManager.java b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/BuildHookManager.java index ce5e6181..5b923204 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/BuildHookManager.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/BuildHookManager.java @@ -1,9 +1,11 @@ package ru.gravit.launchserver.manangers; +import java.io.InputStream; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.zip.ZipOutputStream; import ru.gravit.launcher.AutogenConfig; import ru.gravit.launcher.modules.TestClientModule; @@ -20,6 +22,10 @@ public interface PostBuildHook { public interface PreBuildHook { void build(BuildContext context); } + @FunctionalInterface + public interface ProGuardTransformHook { + byte[] transform(byte[] input, CharSequence classname); + } @FunctionalInterface public interface Transformer { @@ -28,6 +34,7 @@ public interface Transformer { private boolean BUILDRUNTIME; private final Set POST_HOOKS; + private final Set POST_PROGUARD_HOOKS; private final Set PRE_HOOKS; private final Set CLASS_TRANSFORMER; private final Set CLASS_BLACKLIST; @@ -36,6 +43,7 @@ public interface Transformer { public BuildHookManager() { POST_HOOKS = new HashSet<>(4); + POST_PROGUARD_HOOKS = new HashSet<>(4); PRE_HOOKS = new HashSet<>(4); CLASS_BLACKLIST = new HashSet<>(4); MODULE_CLASS = new HashSet<>(4); @@ -62,6 +70,11 @@ public byte[] classTransform(byte[] clazz, CharSequence classname) { for (Transformer transformer : CLASS_TRANSFORMER) result = transformer.transform(result, classname); return result; } + public byte[] proGuardClassTransform(byte[] clazz, CharSequence classname) { + byte[] result = clazz; + for (Transformer transformer : CLASS_TRANSFORMER) result = transformer.transform(result, classname); + return result; + } public void registerIncludeClass(String classname, byte[] classdata) { INCLUDE_CLASS.put(classname, classdata); @@ -102,6 +115,13 @@ public void registerIgnoredClass(String clazz) { public void registerPostHook(PostBuildHook hook) { POST_HOOKS.add(hook); } + public void registerProGuardHook(ProGuardTransformHook hook) { + POST_PROGUARD_HOOKS.add(hook); + } + public boolean isNeedPostProguardHook() + { + return !POST_PROGUARD_HOOKS.isEmpty(); + } public void registerPreHook(PreBuildHook hook) { PRE_HOOKS.add(hook);