diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/SignJarTask.java b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/SignJarTask.java index 9dd608c8..c06ee91c 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/SignJarTask.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/binary/tasks/SignJarTask.java @@ -41,24 +41,28 @@ public String getName() { @Override public Path process(Path inputFile) throws IOException { Path toRet = srv.launcherBinary.nextPath("signed"); + sign(config, inputFile, toRet); + return toRet; + } + + public static void sign(LaunchServerConfig.JarSignerConf config, Path inputFile, Path signedFile) throws IOException { KeyStore c = SignHelper.getStore(new File(config.keyStore).toPath(), config.keyStorePass, config.keyStoreType); - try (SignerJar output = new SignerJar(new ZipOutputStream(IOHelper.newOutput(toRet)), () -> this.gen(c), + try (SignerJar output = new SignerJar(new ZipOutputStream(IOHelper.newOutput(signedFile)), () -> SignJarTask.gen(config, c), config.metaInfSfName, config.metaInfKeyName); ZipInputStream input = new ZipInputStream(IOHelper.newInput(inputFile))) { //input.getManifest().getMainAttributes().forEach((a, b) -> output.addManifestAttribute(a.toString(), b.toString())); // may not work such as after Radon. ZipEntry e = input.getNextEntry(); while (e != null) { - if ("META-INF/MANIFEST.MF".equals(e.getName()) || "/META-INF/MANIFEST.MF".equals(e.getName())) { - Manifest m = new Manifest(input); - m.getMainAttributes().forEach((a, b) -> output.addManifestAttribute(a.toString(), b.toString())); - e = input.getNextEntry(); - continue; - } + if ("META-INF/MANIFEST.MF".equals(e.getName()) || "/META-INF/MANIFEST.MF".equals(e.getName())) { + Manifest m = new Manifest(input); + m.getMainAttributes().forEach((a, b) -> output.addManifestAttribute(a.toString(), b.toString())); + e = input.getNextEntry(); + continue; + } output.addFileContents(IOHelper.newZipEntry(e), input); e = input.getNextEntry(); } } - return toRet; } @Override @@ -66,7 +70,7 @@ public boolean allowDelete() { return true; } - public CMSSignedDataGenerator gen(KeyStore c) { + public static CMSSignedDataGenerator gen(LaunchServerConfig.JarSignerConf config, KeyStore c) { try { return SignHelper.createSignedDataGenerator(c, config.keyAlias, config.signAlgo, config.keyPass); 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 7f096924..8402b32b 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 @@ -89,6 +89,8 @@ public static void registerCommands(pro.gravit.utils.command.CommandHandler hand service.registerCommand("givePermission", new GivePermissionsCommand(server)); service.registerCommand("getPermissions", new GetPermissionsCommand(server)); service.registerCommand("clients", new ClientsCommand(server)); + service.registerCommand("signJar", new SignJarCommand(server)); + service.registerCommand("signDir", new SignDirCommand(server)); Category serviceCategory = new Category(service, "service", "Managing LaunchServer Components"); handler.registerCategory(serviceCategory); } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/SignDirCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/SignDirCommand.java new file mode 100644 index 00000000..87ef1c9e --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/SignDirCommand.java @@ -0,0 +1,52 @@ +package pro.gravit.launchserver.command.service; + +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.binary.tasks.SignJarTask; +import pro.gravit.launchserver.command.Command; +import pro.gravit.utils.helper.IOHelper; +import pro.gravit.utils.helper.LogHelper; + +import java.io.IOException; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; + +public class SignDirCommand extends Command { + private class SignJarVisitor extends SimpleFileVisitor + { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + if (file.toFile().getName().endsWith(".jar")) + { + Path tmpSign = server.dir.resolve("build").resolve(file.toFile().getName()); + LogHelper.info("Signing jar %s", file.toString()); + SignJarTask.sign(server.config.sign, file, tmpSign); + Files.deleteIfExists(file); + Files.move(tmpSign, file); + } + return super.visitFile(file, attrs); + } + } + public SignDirCommand(LaunchServer server) { + super(server); + } + + @Override + public String getArgsDescription() { + return "[path to dir]"; + } + + @Override + public String getUsageDescription() { + return "sign all jar files into dir"; + } + + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 1); + Path targetDir = Paths.get(args[0]); + if(!IOHelper.isDir(targetDir)) + throw new IllegalArgumentException(String.format("%s not directory", targetDir.toString())); + IOHelper.walk(targetDir, new SignJarVisitor(), true); + LogHelper.info("Success signed"); + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/SignJarCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/SignJarCommand.java new file mode 100644 index 00000000..6056597c --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/service/SignJarCommand.java @@ -0,0 +1,47 @@ +package pro.gravit.launchserver.command.service; + +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.binary.tasks.SignJarTask; +import pro.gravit.launchserver.command.Command; +import pro.gravit.utils.helper.IOHelper; +import pro.gravit.utils.helper.LogHelper; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class SignJarCommand extends Command { + public SignJarCommand(LaunchServer server) { + super(server); + } + + @Override + public String getArgsDescription() { + return "[path to file] (path to signed file)"; + } + + @Override + public String getUsageDescription() { + return "sign custom jar"; + } + + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 1); + Path target = Paths.get(args[0]); + Path tmpSign; + if(args.length > 1) + tmpSign = Paths.get(args[1]); + else + tmpSign = server.dir.resolve("build").resolve(target.toFile().getName()); + LogHelper.info("Signing jar %s to %s", target.toString(), tmpSign.toString()); + SignJarTask.sign(server.config.sign, target, tmpSign); + if(args.length <= 1) + { + LogHelper.info("Move temp jar %s to %s", tmpSign.toString(), target.toString()); + Files.deleteIfExists(target); + Files.move(tmpSign, target); + } + LogHelper.info("Success signed"); + } +}