Restart command.

This commit is contained in:
Zaxar163 2019-01-03 10:44:38 +03:00 committed by zaxar163
parent 8d34ae76fc
commit 9a27f863eb
14 changed files with 94 additions and 30 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;
@ -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,6 +254,7 @@ public static void main(String... args) throws Throwable {
public final Path dir;
public final List<String> args;
public final Path configFile;
@ -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");
@ -374,7 +379,6 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException {
modulesManager.autoload(dir.resolve("modules"));
modulesManager.preInitModules();
initGson();
LogHelper.setStacktraceEnabled(true);
// Read LaunchServer config
generateConfigIfNotExists();
@ -399,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)
@ -553,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();
@ -673,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

@ -23,7 +23,7 @@ private class CheckSuperClassVisitor extends ClassVisitor {
String superClassName;
public CheckSuperClassVisitor() {
super(Opcodes.ASM5);
super(Opcodes.ASM7);
}
@Override
@ -57,10 +57,13 @@ public void acceptVisitor(String className, ClassVisitor visitor) throws IOExcep
public byte[] getClassData(String className) throws IOException {
for (JarFile f : cp) {
if (f.getEntry(className + ".class") != null)
if (f.getEntry(className + ".class") != null) {
byte[] bytes = null;
try (InputStream in = f.getInputStream(f.getEntry(className + ".class"))) {
return IOHelper.read(in);
bytes = IOHelper.read(in);
}
return bytes;
}
}
return IOHelper.read(IOHelper.getResourceURL(className + ".class"));
}

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

@ -11,22 +11,29 @@ public DebugCommand(LaunchServer server) {
@Override
public String getArgsDescription() {
return "[true/false]";
return "[true/false] (true/false)";
}
@Override
public String getUsageDescription() {
return "Enable or disable debug logging at runtime";
return "Enable or disable debug and stacktrace logging at runtime";
}
@Override
public void invoke(String... args) {
boolean newValue;
boolean newValue, newTraceValue;
if (args.length >= 1) {
newValue = Boolean.parseBoolean(args[0]);
if(args.length >= 2) newTraceValue = Boolean.parseBoolean(args[1]);
else newTraceValue = newValue;
LogHelper.setDebugEnabled(newValue);
LogHelper.setStacktraceEnabled(newTraceValue);
} else
{
newValue = LogHelper.isDebugEnabled();
newTraceValue = LogHelper.isStacktraceEnabled();
}
LogHelper.subInfo("Debug enabled: " + newValue);
LogHelper.subInfo("Stacktrace enabled: " + newTraceValue);
}
}

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,9 +32,9 @@ 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_FRAMES);
ClassWriter cw = new SafeClassWriter(data.reader, ClassWriter.COMPUTE_MAXS);
cn.accept(cw);
return cw.toByteArray();
}

View file

@ -27,7 +27,7 @@ public interface Listener {
void onDisconnect(Exception e);
boolean onHandshake(long session, Integer type);
boolean onHandshake(long session, int type);
}
private static final ThreadFactory THREAD_FACTORY = r -> CommonHelper.newThread("Network Thread", true, r);
@ -76,7 +76,7 @@ public void close() {
listener.onDisconnect(e);
}
/*package*/ boolean onHandshake(long session, Integer type) {
/*package*/ boolean onHandshake(long session, int type) {
return listener == null || listener.onHandshake(session, type);
}

View file

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

View file

@ -30,6 +30,7 @@ public static void main(String[] arguments) throws IOException, InterruptedExcep
args.add(javaBin.toString());
String pathLauncher = IOHelper.getCodeSource(ClientLauncher.class).toString();
args.add(JVMHelper.jvmProperty(LogHelper.DEBUG_PROPERTY, Boolean.toString(LogHelper.isDebugEnabled())));
args.add(JVMHelper.jvmProperty(LogHelper.STACKTRACE_PROPERTY, Boolean.toString(LogHelper.isStacktraceEnabled())));
Collections.addAll(args, "-javaagent:".concat(pathLauncher));
Collections.addAll(args, "-cp");
Collections.addAll(args, pathLauncher);

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;
@ -358,6 +356,7 @@ public static Process launch(
args.add("-Xmx" + params.ram + 'M');
}
args.add(JVMHelper.jvmProperty(LogHelper.DEBUG_PROPERTY, Boolean.toString(LogHelper.isDebugEnabled())));
args.add(JVMHelper.jvmProperty(LogHelper.STACKTRACE_PROPERTY, Boolean.toString(LogHelper.isStacktraceEnabled())));
if (LauncherConfig.ADDRESS_OVERRIDE != null)
args.add(JVMHelper.jvmProperty(LauncherConfig.ADDRESS_OVERRIDE_PROPERTY, LauncherConfig.ADDRESS_OVERRIDE));
if (JVMHelper.OS_TYPE == OS.MUSTDIE) {

View file

@ -87,6 +87,7 @@ public static void main(String[] args) throws Throwable {
modulesManager.preInitModules();
LogHelper.debug("Read ServerWrapperConfig.json");
gsonBuiler = new GsonBuilder();
gsonBuiler.setPrettyPrinting();
gson = gsonBuiler.create();
generateConfigIfNotExists();
try(Reader reader = IOHelper.newReader(configFile))
@ -99,7 +100,7 @@ public static void main(String[] args) throws Throwable {
else
CommonHelper.newThread("Server Auth Thread", true, () -> ServerWrapper.loopAuth(wrapper, config.reconnectCount, config.reconnectSleep));
modulesManager.initModules();
String classname = config.mainclass.isEmpty() ? args[0] : config.mainclass;
String classname = (config.mainclass == null || config.mainclass.isEmpty()) ? args[0] : config.mainclass;
if (classname.length() == 0) {
LogHelper.error("MainClass not found. Please set MainClass for ServerWrapper.cfg or first commandline argument");
}
@ -152,6 +153,7 @@ private static void generateConfigIfNotExists() throws IOException {
newConfig.port = 7240;
newConfig.login = "login";
newConfig.password = "password";
newConfig.mainclass = "";
//try(Reader reader = IOHelper.newReader(IOHelper.getResourceURL("ru/gravit/launcher/server/ServerWrapper.cfg")))
//{
// newConfig = gson.fromJson(reader,Config.class);

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;

@ -1 +1 @@
Subproject commit eec958f2c5edbc7c053686ab7cebaab8dbca2778
Subproject commit ec1431605c4951ace5cbd2ab392b67cea25bddd5