Фиксы FileNotFoundException и распаковки guard/runtime

This commit is contained in:
Gravit 2018-11-01 20:45:11 +07:00
parent ef840af7a0
commit c3a040ec25
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
4 changed files with 22 additions and 17 deletions

View file

@ -5,7 +5,6 @@
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.file.*; import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap; import java.util.HashMap;
@ -20,7 +19,6 @@
import javassist.NotFoundException; import javassist.NotFoundException;
import ru.gravit.launcher.AutogenConfig; import ru.gravit.launcher.AutogenConfig;
import ru.gravit.launcher.Launcher; import ru.gravit.launcher.Launcher;
import ru.gravit.launcher.LauncherAPI;
import ru.gravit.launcher.LauncherConfig; import ru.gravit.launcher.LauncherConfig;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
@ -69,27 +67,27 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
private final class GuardDirVisitor extends SimpleFileVisitor<Path> { private final class GuardDirVisitor extends SimpleFileVisitor<Path> {
private final ZipOutputStream output; private final ZipOutputStream output;
private final Map<String, byte[]> runtime; private final Map<String, byte[]> guard;
private GuardDirVisitor(ZipOutputStream output, Map<String, byte[]> runtime) { private GuardDirVisitor(ZipOutputStream output, Map<String, byte[]> guard) {
this.output = output; this.output = output;
this.runtime = runtime; this.guard = guard;
} }
@Override @Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
String dirName = IOHelper.toString(guardDir.relativize(dir)); String dirName = IOHelper.toString(guardDir.relativize(dir));
output.putNextEntry(newEntry(dirName + '/')); output.putNextEntry(newGuardEntry(dirName + '/'));
return super.preVisitDirectory(dir, attrs); return super.preVisitDirectory(dir, attrs);
} }
@Override @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String fileName = IOHelper.toString(guardDir.relativize(file)); String fileName = IOHelper.toString(guardDir.relativize(file));
runtime.put(fileName, SecurityHelper.digest(DigestAlgorithm.MD5, file)); guard.put(fileName, SecurityHelper.digest(DigestAlgorithm.MD5, file));
// Create zip entry and transfer contents // Create zip entry and transfer contents
output.putNextEntry(newEntry(fileName)); output.putNextEntry(newGuardEntry(fileName));
IOHelper.transfer(file, output); IOHelper.transfer(file, output);
// Return result // Return result
@ -100,6 +98,9 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
private static ZipEntry newEntry(String fileName) { private static ZipEntry newEntry(String fileName) {
return newZipEntry(Launcher.RUNTIME_DIR + IOHelper.CROSS_SEPARATOR + fileName); return newZipEntry(Launcher.RUNTIME_DIR + IOHelper.CROSS_SEPARATOR + fileName);
} }
private static ZipEntry newGuardEntry(String fileName) {
return newZipEntry(Launcher.GUARD_DIR + IOHelper.CROSS_SEPARATOR + fileName);
}
public final Path runtimeDir; public final Path runtimeDir;
@ -246,13 +247,13 @@ private void stdBuild() throws IOException {
output.putNextEntry(newZipEntry(ent.getKey().replace('.', '/').concat(".class"))); output.putNextEntry(newZipEntry(ent.getKey().replace('.', '/').concat(".class")));
output.write(server.buildHookManager.classTransform(ent.getValue(), ent.getKey())); output.write(server.buildHookManager.classTransform(ent.getValue(), ent.getKey()));
} }
// map for runtime // map for guard
Map<String, byte[]> runtime = new HashMap<>(256); Map<String, byte[]> runtime = new HashMap<>(256);
if (server.buildHookManager.buildRuntime()) { if (server.buildHookManager.buildRuntime()) {
// Verify has init script file // Verify has init script file
if (!IOHelper.isFile(initScriptFile)) if (!IOHelper.isFile(initScriptFile))
throw new IOException(String.format("Missing init script file ('%s')", Launcher.INIT_SCRIPT_FILE)); throw new IOException(String.format("Missing init script file ('%s')", Launcher.INIT_SCRIPT_FILE));
// Write launcher runtime dir // Write launcher guard dir
IOHelper.walk(runtimeDir, new RuntimeDirVisitor(output, runtime), false); IOHelper.walk(runtimeDir, new RuntimeDirVisitor(output, runtime), false);
IOHelper.walk(guardDir, new GuardDirVisitor(output, runtime), false); IOHelper.walk(guardDir, new GuardDirVisitor(output, runtime), false);
} }
@ -281,11 +282,11 @@ private void stdBuild() throws IOException {
public void tryUnpackRuntime() throws IOException { public void tryUnpackRuntime() throws IOException {
// Verify is runtime dir unpacked // Verify is guard dir unpacked
if (IOHelper.isDir(runtimeDir)) if (IOHelper.isDir(runtimeDir))
return; // Already unpacked return; // Already unpacked
// Unpack launcher runtime files // Unpack launcher guard files
Files.createDirectory(runtimeDir); Files.createDirectory(runtimeDir);
LogHelper.info("Unpacking launcher runtime files"); LogHelper.info("Unpacking launcher runtime files");
try (ZipInputStream input = IOHelper.newZipInput(IOHelper.getResourceURL("runtime.zip"))) { try (ZipInputStream input = IOHelper.newZipInput(IOHelper.getResourceURL("runtime.zip"))) {
@ -293,18 +294,18 @@ public void tryUnpackRuntime() throws IOException {
if (entry.isDirectory()) if (entry.isDirectory())
continue; // Skip dirs continue; // Skip dirs
// Unpack runtime file // Unpack guard file
IOHelper.transfer(input, runtimeDir.resolve(IOHelper.toPath(entry.getName()))); IOHelper.transfer(input, runtimeDir.resolve(IOHelper.toPath(entry.getName())));
} }
} }
} }
public void tryUnpackGuard() throws IOException { public void tryUnpackGuard() throws IOException {
// Verify is runtime dir unpacked // Verify is guard dir unpacked
if (IOHelper.isDir(guardDir)) if (IOHelper.isDir(guardDir))
return; // Already unpacked return; // Already unpacked
// Unpack launcher runtime files // Unpack launcher guard files
Files.createDirectory(guardDir); Files.createDirectory(guardDir);
LogHelper.info("Unpacking launcher native guard files"); LogHelper.info("Unpacking launcher native guard files");
try (ZipInputStream input = IOHelper.newZipInput(IOHelper.getResourceURL("guard.zip"))) { try (ZipInputStream input = IOHelper.newZipInput(IOHelper.getResourceURL("guard.zip"))) {
@ -312,7 +313,7 @@ public void tryUnpackGuard() throws IOException {
if (entry.isDirectory()) if (entry.isDirectory())
continue; // Skip dirs continue; // Skip dirs
// Unpack runtime file // Unpack guard file
IOHelper.transfer(input, guardDir.resolve(IOHelper.toPath(entry.getName()))); IOHelper.transfer(input, guardDir.resolve(IOHelper.toPath(entry.getName())));
} }
} }

View file

@ -29,6 +29,8 @@ public static void main(String[] arguments) throws IOException, InterruptedExcep
String pathLauncher = IOHelper.getCodeSource(ClientLauncher.class).toString(); String pathLauncher = IOHelper.getCodeSource(ClientLauncher.class).toString();
args.add(JVMHelper.jvmProperty(LogHelper.DEBUG_PROPERTY, Boolean.toString(LogHelper.isDebugEnabled()))); args.add(JVMHelper.jvmProperty(LogHelper.DEBUG_PROPERTY, Boolean.toString(LogHelper.isDebugEnabled())));
Collections.addAll(args, "-javaagent:".concat(pathLauncher)); Collections.addAll(args, "-javaagent:".concat(pathLauncher));
Collections.addAll(args, "-cp");
Collections.addAll(args, pathLauncher);
Collections.addAll(args, LauncherEngine.class.getName()); Collections.addAll(args, LauncherEngine.class.getName());
EnvHelper.addEnv(processBuilder); EnvHelper.addEnv(processBuilder);
LogHelper.debug("Commandline: " + args); LogHelper.debug("Commandline: " + args);

View file

@ -151,7 +151,7 @@ public static void main(String... args) throws Throwable {
JVMHelper.checkStackTrace(LauncherEngine.class); JVMHelper.checkStackTrace(LauncherEngine.class);
JVMHelper.verifySystemProperties(Launcher.class, true); JVMHelper.verifySystemProperties(Launcher.class, true);
EnvHelper.checkDangerousParams(); EnvHelper.checkDangerousParams();
if(!LauncherAgent.isStarted()) throw new SecurityException("JavaAgent not set"); //if(!LauncherAgent.isStarted()) throw new SecurityException("JavaAgent not set");
LogHelper.printVersion("Launcher"); LogHelper.printVersion("Launcher");
// Start Launcher // Start Launcher
Instant start = Instant.now(); Instant start = Instant.now();

View file

@ -52,6 +52,8 @@ static int readBuildNumber() {
@LauncherAPI @LauncherAPI
public static final String RUNTIME_DIR = "runtime"; public static final String RUNTIME_DIR = "runtime";
@LauncherAPI @LauncherAPI
public static final String GUARD_DIR = "guard";
@LauncherAPI
public static final String CONFIG_FILE = "config.bin"; public static final String CONFIG_FILE = "config.bin";
@LauncherAPI @LauncherAPI
public static ClientProfile profile; public static ClientProfile profile;