Restart command. (#121)

This commit is contained in:
Zaxar163 2019-01-04 14:32:16 +03:00 committed by Gravit
parent 3fe5a34029
commit 34711eea80
8 changed files with 69 additions and 18 deletions

View file

@ -2,7 +2,9 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.lang.ProcessBuilder.Redirect;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.SocketAddress;
@ -18,6 +20,7 @@
import java.security.spec.InvalidKeySpecException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@ -88,7 +91,7 @@ public static final class Config {
public String projectName;
public String[] mirrors;
public String binaryName;
public LauncherConfig.LauncherEnvironment env;
@ -133,6 +136,8 @@ public static final class Config {
public boolean isWarningMissArchJava;
public String startScript;
public String getAddress() {
return address;
@ -236,7 +241,7 @@ public static void main(String... args) throws Throwable {
// Start LaunchServer
Instant start = Instant.now();
try {
new LaunchServer(IOHelper.WORKING_DIR).run();
new LaunchServer(IOHelper.WORKING_DIR, args).run();
} catch (Throwable exc) {
LogHelper.error(exc);
return;
@ -249,7 +254,8 @@ public static void main(String... args) throws Throwable {
public final Path dir;
public final List<String> args;
public final Path configFile;
public final Path publicKeyFile;
@ -300,7 +306,6 @@ public static void main(String... args) throws Throwable {
public final CommandHandler commandHandler;
public final ServerSocketHandler serverSocketHandler;
private final AtomicBoolean started = new AtomicBoolean(false);
@ -313,9 +318,9 @@ public static void main(String... args) throws Throwable {
public static Gson gson;
public static GsonBuilder gsonBuilder;
public LaunchServer(Path dir) throws IOException, InvalidKeySpecException {
// Setup config locations
public LaunchServer(Path dir, String[] args) throws IOException, InvalidKeySpecException {
this.dir = dir;
this.args = Arrays.asList(args);
configFile = dir.resolve("LaunchServer.conf");
publicKeyFile = dir.resolve("public.key");
privateKeyFile = dir.resolve("private.key");
@ -398,7 +403,6 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException {
reconfigurableManager = new ReconfigurableManager();
socketHookManager = new SocketHookManager();
authHookManager = new AuthHookManager();
GarbageManager.registerNeedGC(sessionManager);
GarbageManager.registerNeedGC(limiter);
if(config.permissionsHandler instanceof Reloadable)
@ -552,6 +556,7 @@ private void generateConfigIfNotExists() throws IOException {
newConfig.launch4j.productVer = newConfig.launch4j.fileVer;
newConfig.buildPostTransform = new PostBuildTransformConf();
newConfig.env = LauncherConfig.LauncherEnvironment.STD;
newConfig.startScript = "." + File.separator + "start.sh";
newConfig.authHandler = new MemoryAuthHandler();
newConfig.hwidHandler = new AcceptHWIDHandler();
@ -672,4 +677,25 @@ public void syncUpdatesDir(Collection<String> dirs) throws IOException {
}
updatesDirMap = Collections.unmodifiableMap(newUpdatesDirMap);
}
public void restart() {
ProcessBuilder builder = new ProcessBuilder();
List<String> args = new ArrayList<>();
if (config.startScript != null) args.add(config.startScript);
else throw new IllegalArgumentException ("Please create start script and link it as startScript in config.");
builder.directory(this.dir.toFile());
builder.inheritIO();
builder.redirectErrorStream(true);
builder.redirectOutput(Redirect.PIPE);
try {
builder.start();
} catch (IOException e) {
LogHelper.error(e);
}
}
public void fullyRestart() {
restart();
JVMHelper.RUNTIME.exit(0);
}
}

View file

@ -6,7 +6,6 @@
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.AuthException;
import ru.gravit.launchserver.auth.provider.AuthProviderResult;
import ru.gravit.utils.helper.VerifyHelper;
@ -14,7 +13,6 @@
public abstract class AuthHandler implements AutoCloseable {
private static final Map<String, Class<? extends AuthHandler>> AUTH_HANDLERS = new ConcurrentHashMap<>(4);
private static boolean registredHandl = false;
private transient LaunchServer server = LaunchServer.server;
public static UUID authError(String message) throws AuthException {

View file

@ -0,0 +1,26 @@
package ru.gravit.launchserver.command.basic;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.command.Command;
public final class RestartCommand extends Command {
public RestartCommand(LaunchServer server) {
super(server);
}
@Override
public String getArgsDescription() {
return null;
}
@Override
public String getUsageDescription() {
return "Restart LaunchServer";
}
@Override
@SuppressWarnings("CallToSystemExit")
public void invoke(String... args) {
server.fullyRestart();
}
}

View file

@ -29,6 +29,7 @@
import ru.gravit.launchserver.command.basic.RebindCommand;
import ru.gravit.launchserver.command.basic.RegenProguardDictCommand;
import ru.gravit.launchserver.command.basic.RemoveMappingsProguardCommand;
import ru.gravit.launchserver.command.basic.RestartCommand;
import ru.gravit.launchserver.command.basic.StopCommand;
import ru.gravit.launchserver.command.basic.TestCommand;
import ru.gravit.launchserver.command.basic.VersionCommand;
@ -105,6 +106,7 @@ protected CommandHandler(LaunchServer server) {
registerCommand("version", new VersionCommand(server));
registerCommand("build", new BuildCommand(server));
registerCommand("stop", new StopCommand(server));
registerCommand("restart", new RestartCommand(server));
registerCommand("rebind", new RebindCommand(server));
registerCommand("debug", new DebugCommand(server));
registerCommand("clear", new ClearCommand(server));

View file

@ -7,6 +7,7 @@
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
import ru.gravit.launcher.LauncherConfig;
import ru.gravit.launchserver.asm.SafeClassWriter;
import ru.gravit.launchserver.binary.JARLauncherBinary;
import ru.gravit.launchserver.manangers.hook.BuildHookManager.Transformer;
@ -31,7 +32,7 @@ public NodeTransformer() {
public byte[] transform(byte[] input, String classname, JARLauncherBinary data) {
ClassReader cr = new ClassReader(input);
ClassNode cn = new ClassNode();
cr.accept(cn, ClassReader.SKIP_DEBUG);
cr.accept(cn, data.server.config.env.equals(LauncherConfig.LauncherEnvironment.PROD) || data.server.config.env.equals(LauncherConfig.LauncherEnvironment.STD) ? ClassReader.SKIP_DEBUG : 0);
for (ClassNodeTransformer tr : transLst) tr.transform(cn, classname, data);
ClassWriter cw = new SafeClassWriter(data.reader, ClassWriter.COMPUTE_MAXS);
cn.accept(cw);

View file

@ -1,5 +1,5 @@
{
"port": 7420,
"port": 7240,
"authHandler": {
"type": "memory"
},

View file

@ -2,8 +2,6 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import ru.gravit.launcher.*;
import ru.gravit.launcher.hasher.DirWatcher;
import ru.gravit.launcher.hasher.FileNameMatcher;

View file

@ -58,11 +58,11 @@ public final class Launcher {
public static final String CONFIG_SCRIPT_FILE = "config.js";
private static final Pattern UUID_PATTERN = Pattern.compile("-", Pattern.LITERAL);
public static int MAJOR = 4;
public static int MINOR = 1;
public static int PATCH = 0;
public static int BUILD = 7;
public static Version.Type RELEASE = Version.Type.BETA;
public static final int MAJOR = 4;
public static final int MINOR = 1;
public static final int PATCH = 0;
public static final int BUILD = 7;
public static final Version.Type RELEASE = Version.Type.BETA;
public static GsonBuilder gsonBuilder;
public static Gson gson;