2018-12-26 16:17:47 +03:00
|
|
|
package ru.gravit.launchserver.binary;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2018-12-26 15:33:49 +03:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
import java.io.PrintWriter;
|
2018-09-17 10:07:32 +03:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.security.SecureRandom;
|
2018-09-21 14:38:16 +03:00
|
|
|
import java.util.ArrayList;
|
2019-01-08 16:36:05 +03:00
|
|
|
import java.util.List;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2018-12-26 16:17:47 +03:00
|
|
|
import ru.gravit.launchserver.LaunchServer;
|
2018-12-26 15:33:49 +03:00
|
|
|
import ru.gravit.utils.helper.IOHelper;
|
|
|
|
import ru.gravit.utils.helper.LogHelper;
|
|
|
|
import ru.gravit.utils.helper.SecurityHelper;
|
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
public class ProguardConf {
|
2018-09-22 17:33:00 +03:00
|
|
|
private static final String charsFirst = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
|
|
|
|
private static final String chars = "1aAbBcC2dDeEfF3gGhHiI4jJkKl5mMnNoO6pPqQrR7sStT8uUvV9wWxX0yYzZ";
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2018-09-22 17:33:00 +03:00
|
|
|
private static String generateString(SecureRandom rand, int il) {
|
|
|
|
StringBuilder sb = new StringBuilder(il);
|
|
|
|
sb.append(charsFirst.charAt(rand.nextInt(charsFirst.length())));
|
|
|
|
for (int i = 0; i < il - 1; i++) sb.append(chars.charAt(rand.nextInt(chars.length())));
|
|
|
|
return sb.toString();
|
|
|
|
}
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2018-09-22 17:33:00 +03:00
|
|
|
public final Path proguard;
|
|
|
|
public final Path config;
|
|
|
|
public final Path mappings;
|
|
|
|
public final Path words;
|
2019-01-05 18:15:19 +03:00
|
|
|
public transient final LaunchServer srv;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2018-09-22 17:33:00 +03:00
|
|
|
public ProguardConf(LaunchServer srv) {
|
2018-11-26 11:00:58 +03:00
|
|
|
proguard = srv.dir.resolve("proguard");
|
2018-09-22 17:33:00 +03:00
|
|
|
config = proguard.resolve("proguard.config");
|
|
|
|
mappings = proguard.resolve("mappings.pro");
|
|
|
|
words = proguard.resolve("random.pro");
|
2019-01-05 18:15:19 +03:00
|
|
|
this.srv = srv;
|
|
|
|
}
|
2019-01-08 16:36:05 +03:00
|
|
|
public String[] buildConfig(Path inputJar, Path outputJar)
|
2019-01-05 18:15:19 +03:00
|
|
|
{
|
2019-01-08 16:36:05 +03:00
|
|
|
List<String> confStrs = new ArrayList<>();
|
2018-09-22 17:33:00 +03:00
|
|
|
prepare(false);
|
2018-11-26 11:00:58 +03:00
|
|
|
if (srv.config.genMappings) confStrs.add("-printmapping \'" + mappings.toFile().getName() + "\'");
|
2018-09-22 17:33:00 +03:00
|
|
|
confStrs.add("-obfuscationdictionary \'" + words.toFile().getName() + "\'");
|
2019-01-05 18:15:19 +03:00
|
|
|
confStrs.add("-injar \'" + inputJar.toAbsolutePath() + "\'");
|
|
|
|
confStrs.add("-outjar \'" + outputJar.toAbsolutePath() + "\'");
|
2019-01-09 12:08:43 +03:00
|
|
|
srv.launcherBinary.coreLibs.stream()
|
|
|
|
.map(e -> "-libraryjars \'" + e.toAbsolutePath().toString() + "\'")
|
|
|
|
.forEach(confStrs::add);
|
2018-09-22 17:33:00 +03:00
|
|
|
confStrs.add("-classobfuscationdictionary \'" + words.toFile().getName() + "\'");
|
|
|
|
confStrs.add(readConf());
|
2019-01-08 16:36:05 +03:00
|
|
|
return confStrs.toArray(new String[0]);
|
2018-09-22 17:33:00 +03:00
|
|
|
}
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2018-09-22 17:33:00 +03:00
|
|
|
private void genConfig(boolean force) throws IOException {
|
|
|
|
if (IOHelper.exists(config) && !force) return;
|
|
|
|
Files.deleteIfExists(config);
|
|
|
|
try (OutputStream out = IOHelper.newOutput(config); InputStream in = IOHelper.newInput(IOHelper.getResourceURL("ru/gravit/launchserver/defaults/proguard.cfg"))) {
|
|
|
|
IOHelper.transfer(in, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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))) {
|
|
|
|
for (int i = 0; i < Short.MAX_VALUE; i++) out.println(generateString(rand, 24));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void prepare(boolean force) {
|
|
|
|
try {
|
|
|
|
IOHelper.createParentDirs(config);
|
|
|
|
genWords(force);
|
|
|
|
genConfig(force);
|
|
|
|
} catch (IOException e) {
|
|
|
|
LogHelper.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String readConf() {
|
|
|
|
return "@".concat(config.toFile().getName());
|
|
|
|
}
|
2018-09-17 10:07:32 +03:00
|
|
|
}
|