From 2f4870e96c7aa2f76d8c7878c40964e40070aaf1 Mon Sep 17 00:00:00 2001 From: zaxar163 Date: Sun, 6 Jan 2019 16:22:17 +0400 Subject: [PATCH] Working transformers. --- .../binary/JARLauncherBinary.java | 29 ------- .../binary/tasks/MainBuildTask.java | 19 ++--- .../manangers/hook/BuildHookManager.java | 78 ++----------------- 3 files changed, 16 insertions(+), 110 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 f1c75680..2e349a99 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/JARLauncherBinary.java @@ -48,34 +48,5 @@ public void build() throws IOException { long time_end = System.currentTimeMillis(); IOHelper.move(thisPath, syncBinaryFile); LogHelper.info("Build successful from %d millis",time_end - time_start); - - // ProGuard - - /*for (Runnable r : server.buildHookManager.getPostProguardRunHooks()) - r.run(); - try (ZipInputStream input = new ZipInputStream(IOHelper.newInput(obfJar)); - ZipOutputStream output = new ZipOutputStream(IOHelper.newOutput(obfOutJar))) { - ZipEntry e = input.getNextEntry(); - while (e != null) { - String filename = e.getName(); - output.putNextEntry(IOHelper.newZipEntry(e)); - if (filename.endsWith(".class")) { - String classname = filename.replace('/', '.').substring(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, this); - try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) { - IOHelper.transfer(inputStream, output); - } - } else - IOHelper.transfer(input, output); - e = input.getNextEntry(); - } - for (ZipBuildHook h : server.buildHookManager.getProguardBuildHooks()) - h.build(output); - }*/ } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/MainBuildTask.java b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/MainBuildTask.java index 8fceaab3..03e803b1 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/MainBuildTask.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/binary/tasks/MainBuildTask.java @@ -10,6 +10,7 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.HashMap; import java.util.Map; +import java.util.jar.JarFile; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipInputStream; @@ -22,6 +23,7 @@ import ru.gravit.launcher.LauncherConfig; import ru.gravit.launcher.serialize.HOutput; import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.asm.ClassMetadataReader; import ru.gravit.launchserver.binary.BuildContext; import ru.gravit.launchserver.binary.JAConfigurator; import ru.gravit.utils.helper.IOHelper; @@ -35,6 +37,7 @@ public class MainBuildTask implements LauncherBuildTask { public final Path binaryFile; public Path cleanJar; private final LaunchServer server; + public final ClassMetadataReader reader; private final class RuntimeDirVisitor extends SimpleFileVisitor { private final ZipOutputStream output; private final Map runtime; @@ -66,7 +69,6 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO } // TODO: new native security wrapper and library... - @SuppressWarnings("unused") private final class GuardDirVisitor extends SimpleFileVisitor { private final ZipOutputStream output; private final Map guard; @@ -109,6 +111,7 @@ public MainBuildTask(LaunchServer srv) { runtimeDir = server.dir.resolve(Launcher.RUNTIME_DIR); guardDir = server.dir.resolve(Launcher.GUARD_DIR); binaryFile = server.dir.resolve(server.config.binaryName + "-main.jar"); + reader = new ClassMetadataReader(); } @Override @@ -123,7 +126,7 @@ public Path process(Path cleanJar) throws IOException { JAConfigurator jaConfigurator = new JAConfigurator(AutogenConfig.class.getName(), this)) { jaConfigurator.pool.insertClassPath(cleanJar.toFile().getAbsolutePath()); BuildContext context = new BuildContext(output, jaConfigurator, this); - server.buildHookManager.preHook(context); + server.buildHookManager.hook(context); jaConfigurator.setAddress(server.config.getAddress()); jaConfigurator.setPort(server.config.port); jaConfigurator.setProjectName(server.config.projectName); @@ -133,6 +136,7 @@ public Path process(Path cleanJar) throws IOException { jaConfigurator.setDownloadJava(server.config.isDownloadJava); jaConfigurator.setEnv(server.config.env); server.buildHookManager.registerAllClientModuleClass(jaConfigurator); + reader.getCp().add(new JarFile(cleanJar.toFile())); try (ZipInputStream input = new ZipInputStream(IOHelper.newInput(cleanJar))) { ZipEntry e = input.getNextEntry(); while (e != null) { @@ -148,7 +152,7 @@ public Path process(Path cleanJar) throws IOException { e = input.getNextEntry(); continue; } - /*if (filename.endsWith(".class")) { + if (filename.endsWith(".class")) { String classname = filename.replace('/', '.').substring(0, filename.length() - ".class".length()); byte[] bytes; @@ -157,10 +161,8 @@ public Path process(Path cleanJar) throws IOException { bytes = outputStream.toByteArray(); } bytes = server.buildHookManager.classTransform(bytes, classname, this); - try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) { - IOHelper.transfer(inputStream, output); - } - } else */ + output.write(bytes); + } else IOHelper.transfer(input, output); context.fileList.add(filename); e = input.getNextEntry(); @@ -176,7 +178,7 @@ public Path process(Path cleanJar) throws IOException { if (server.buildHookManager.buildRuntime()) { // Write launcher guard dir IOHelper.walk(runtimeDir, new RuntimeDirVisitor(output, runtime), false); - // IOHelper.walk(guardDir, new GuardDirVisitor(output, runtime), false); + IOHelper.walk(guardDir, new GuardDirVisitor(output, runtime), false); } // Create launcher config file byte[] launcherConfigBytes; @@ -195,7 +197,6 @@ public Path process(Path cleanJar) throws IOException { output.putNextEntry(e); jaConfigurator.compile(); output.write(jaConfigurator.getBytecode()); - server.buildHookManager.postHook(context); } catch (CannotCompileException | NotFoundException e) { LogHelper.error(e); } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/hook/BuildHookManager.java b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/hook/BuildHookManager.java index 78b79bc3..00bd4e05 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/hook/BuildHookManager.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/manangers/hook/BuildHookManager.java @@ -4,20 +4,14 @@ 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; import ru.gravit.launchserver.binary.BuildContext; import ru.gravit.launchserver.binary.JAConfigurator; import ru.gravit.launchserver.binary.tasks.MainBuildTask; -import ru.gravit.launchserver.manangers.NodeTransformer; public class BuildHookManager { - @FunctionalInterface - public interface ZipBuildHook { - void build(ZipOutputStream context); - } @FunctionalInterface public interface BuildHook { @@ -30,24 +24,14 @@ public interface Transformer { } private boolean BUILDRUNTIME; - private final Set POST_HOOKS; - private final Set POST_PROGUARDRUN_HOOKS; - private final Set POST_PROGUARD_HOOKS; - private final Set PRE_HOOKS; - private final Set POST_PROGUARD_BUILDHOOKS; + private final Set HOOKS; private final Set CLASS_TRANSFORMER; private final Set CLASS_BLACKLIST; private final Set MODULE_CLASS; private final Map INCLUDE_CLASS; - private final NodeTransformer noder; - private final NodeTransformer proguardNoder; public BuildHookManager() { - POST_HOOKS = new HashSet<>(4); - POST_PROGUARDRUN_HOOKS = new HashSet<>(4); - POST_PROGUARD_HOOKS = new HashSet<>(4); - PRE_HOOKS = new HashSet<>(4); - POST_PROGUARD_BUILDHOOKS = new HashSet<>(4); + HOOKS = new HashSet<>(4); CLASS_BLACKLIST = new HashSet<>(4); MODULE_CLASS = new HashSet<>(4); INCLUDE_CLASS = new HashMap<>(4); @@ -58,34 +42,6 @@ public BuildHookManager() { registerIgnoredClass("META-INF/LICENSE"); registerIgnoredClass("META-INF/NOTICE"); registerClientModuleClass(TestClientModule.class.getName()); - noder = new NodeTransformer(); - registerClassTransformer(noder); - proguardNoder = new NodeTransformer(); - registerProGuardHook(proguardNoder); - } - - public NodeTransformer getProguardNoder() { - return proguardNoder; - } - - public NodeTransformer getNoder() { - return noder; - } - - public Set getProguardBuildHooks() { - return POST_PROGUARD_BUILDHOOKS; - } - - public Set getPostProguardRunHooks() { - return POST_PROGUARDRUN_HOOKS; - } - - public void addPostProguardRunHook(Runnable hook) { - POST_PROGUARDRUN_HOOKS.add(hook); - } - - public void addPostProguardRunHook(ZipBuildHook hook) { - POST_PROGUARD_BUILDHOOKS.add(hook); } public void autoRegisterIgnoredClass(String clazz) { @@ -102,12 +58,6 @@ public byte[] classTransform(byte[] clazz, String classname, MainBuildTask reade return result; } - public byte[] proGuardClassTransform(byte[] clazz, String classname, MainBuildTask reader) { - byte[] result = clazz; - for (Transformer transformer : POST_PROGUARD_HOOKS) result = transformer.transform(result, classname, reader); - return result; - } - public void registerIncludeClass(String classname, byte[] classdata) { INCLUDE_CLASS.put(classname, classdata); } @@ -123,12 +73,8 @@ public boolean isContainsBlacklist(String clazz) { return false; } - public void postHook(BuildContext context) { - for (BuildHook hook : POST_HOOKS) hook.build(context); - } - - public void preHook(BuildContext context) { - for (BuildHook hook : PRE_HOOKS) hook.build(context); + public void hook(BuildContext context) { + for (BuildHook hook : HOOKS) hook.build(context); } public void registerAllClientModuleClass(JAConfigurator cfg) { @@ -147,20 +93,8 @@ public void registerIgnoredClass(String clazz) { CLASS_BLACKLIST.add(clazz); } - public void registerPostHook(BuildHook hook) { - POST_HOOKS.add(hook); - } - - public void registerProGuardHook(Transformer hook) { - POST_PROGUARD_HOOKS.add(hook); - } - - public boolean isNeedPostProguardHook() { - return POST_PROGUARD_HOOKS.size() > 1 || !POST_PROGUARDRUN_HOOKS.isEmpty() || !POST_PROGUARD_BUILDHOOKS.isEmpty() || !proguardNoder.getTransLst().isEmpty(); - } - - public void registerPreHook(BuildHook hook) { - PRE_HOOKS.add(hook); + public void registerHook(BuildHook hook) { + HOOKS.add(hook); } public void setBuildRuntime(boolean runtime) {