mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 11:39:11 +03:00
[FEATURE] Подписывание любого файла и любой диретории
This commit is contained in:
parent
ff30ba2670
commit
fb21aeb888
4 changed files with 114 additions and 9 deletions
|
@ -41,24 +41,28 @@ public String getName() {
|
||||||
@Override
|
@Override
|
||||||
public Path process(Path inputFile) throws IOException {
|
public Path process(Path inputFile) throws IOException {
|
||||||
Path toRet = srv.launcherBinary.nextPath("signed");
|
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);
|
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);
|
config.metaInfSfName, config.metaInfKeyName);
|
||||||
ZipInputStream input = new ZipInputStream(IOHelper.newInput(inputFile))) {
|
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.
|
//input.getManifest().getMainAttributes().forEach((a, b) -> output.addManifestAttribute(a.toString(), b.toString())); // may not work such as after Radon.
|
||||||
ZipEntry e = input.getNextEntry();
|
ZipEntry e = input.getNextEntry();
|
||||||
while (e != null) {
|
while (e != null) {
|
||||||
if ("META-INF/MANIFEST.MF".equals(e.getName()) || "/META-INF/MANIFEST.MF".equals(e.getName())) {
|
if ("META-INF/MANIFEST.MF".equals(e.getName()) || "/META-INF/MANIFEST.MF".equals(e.getName())) {
|
||||||
Manifest m = new Manifest(input);
|
Manifest m = new Manifest(input);
|
||||||
m.getMainAttributes().forEach((a, b) -> output.addManifestAttribute(a.toString(), b.toString()));
|
m.getMainAttributes().forEach((a, b) -> output.addManifestAttribute(a.toString(), b.toString()));
|
||||||
e = input.getNextEntry();
|
e = input.getNextEntry();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
output.addFileContents(IOHelper.newZipEntry(e), input);
|
output.addFileContents(IOHelper.newZipEntry(e), input);
|
||||||
e = input.getNextEntry();
|
e = input.getNextEntry();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return toRet;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -66,7 +70,7 @@ public boolean allowDelete() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CMSSignedDataGenerator gen(KeyStore c) {
|
public static CMSSignedDataGenerator gen(LaunchServerConfig.JarSignerConf config, KeyStore c) {
|
||||||
try {
|
try {
|
||||||
return SignHelper.createSignedDataGenerator(c,
|
return SignHelper.createSignedDataGenerator(c,
|
||||||
config.keyAlias, config.signAlgo, config.keyPass);
|
config.keyAlias, config.signAlgo, config.keyPass);
|
||||||
|
|
|
@ -89,6 +89,8 @@ public static void registerCommands(pro.gravit.utils.command.CommandHandler hand
|
||||||
service.registerCommand("givePermission", new GivePermissionsCommand(server));
|
service.registerCommand("givePermission", new GivePermissionsCommand(server));
|
||||||
service.registerCommand("getPermissions", new GetPermissionsCommand(server));
|
service.registerCommand("getPermissions", new GetPermissionsCommand(server));
|
||||||
service.registerCommand("clients", new ClientsCommand(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");
|
Category serviceCategory = new Category(service, "service", "Managing LaunchServer Components");
|
||||||
handler.registerCategory(serviceCategory);
|
handler.registerCategory(serviceCategory);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<Path>
|
||||||
|
{
|
||||||
|
@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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue