2018-09-17 10:07:32 +03:00
|
|
|
package ru.gravit.launcher.server;
|
|
|
|
|
|
|
|
|
2018-12-23 18:50:31 +03:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
2018-09-17 10:07:32 +03:00
|
|
|
import ru.gravit.launcher.Launcher;
|
|
|
|
import ru.gravit.launcher.LauncherConfig;
|
2018-12-20 18:45:01 +03:00
|
|
|
import ru.gravit.launcher.profiles.ClientProfile;
|
2018-09-27 00:43:35 +03:00
|
|
|
import ru.gravit.launcher.request.auth.AuthServerRequest;
|
2018-12-20 18:45:01 +03:00
|
|
|
import ru.gravit.launcher.request.update.ProfilesRequest;
|
2018-09-24 14:30:42 +03:00
|
|
|
import ru.gravit.launcher.serialize.config.ConfigObject;
|
|
|
|
import ru.gravit.launcher.serialize.config.TextConfigReader;
|
|
|
|
import ru.gravit.launcher.serialize.config.TextConfigWriter;
|
|
|
|
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
|
2018-09-24 14:55:39 +03:00
|
|
|
import ru.gravit.launcher.serialize.config.entry.BooleanConfigEntry;
|
2018-09-25 16:43:12 +03:00
|
|
|
import ru.gravit.launcher.serialize.config.entry.IntegerConfigEntry;
|
2018-09-24 14:30:42 +03:00
|
|
|
import ru.gravit.launcher.serialize.config.entry.StringConfigEntry;
|
2018-12-20 18:45:01 +03:00
|
|
|
import ru.gravit.launcher.serialize.signed.SignedObjectHolder;
|
2018-12-19 15:38:32 +03:00
|
|
|
import ru.gravit.utils.PublicURLClassLoader;
|
2018-10-09 14:37:46 +03:00
|
|
|
import ru.gravit.utils.helper.CommonHelper;
|
2018-09-17 10:20:34 +03:00
|
|
|
import ru.gravit.utils.helper.IOHelper;
|
|
|
|
import ru.gravit.utils.helper.LogHelper;
|
2018-09-25 16:43:12 +03:00
|
|
|
import ru.gravit.utils.helper.SecurityHelper;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2018-12-23 18:50:31 +03:00
|
|
|
import java.io.*;
|
2018-12-20 18:45:01 +03:00
|
|
|
import java.lang.invoke.MethodHandle;
|
|
|
|
import java.lang.invoke.MethodHandles;
|
|
|
|
import java.lang.invoke.MethodType;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
public class ServerWrapper {
|
|
|
|
public static ModulesManager modulesManager;
|
2018-09-24 14:30:42 +03:00
|
|
|
public static Config config;
|
2018-12-19 15:38:32 +03:00
|
|
|
public static PublicURLClassLoader ucp;
|
|
|
|
public static ClassLoader loader;
|
2018-12-23 18:50:31 +03:00
|
|
|
private static Gson gson;
|
|
|
|
private static GsonBuilder gsonBuiler;
|
2018-12-19 15:38:32 +03:00
|
|
|
|
2018-12-20 18:45:01 +03:00
|
|
|
public static Path modulesDir = Paths.get(System.getProperty("serverwrapper.modulesDir", "modules"));
|
|
|
|
public static Path configFile = Paths.get(System.getProperty("serverwrapper.configFile", "ServerWrapper.cfg"));
|
|
|
|
public static Path publicKeyFile = Paths.get(System.getProperty("serverwrapper.publicKeyFile", "public.key"));
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2018-10-09 14:37:46 +03:00
|
|
|
public static boolean auth(ServerWrapper wrapper) {
|
|
|
|
try {
|
|
|
|
LauncherConfig cfg = Launcher.getConfig();
|
2018-11-08 15:30:16 +03:00
|
|
|
Boolean auth = new AuthServerRequest(cfg, config.login, SecurityHelper.newRSAEncryptCipher(cfg.publicKey).doFinal(IOHelper.encode(config.password)), 0, config.title).request();
|
2018-11-11 09:01:19 +03:00
|
|
|
if (auth == null) throw new Exception("Non auth!"); // security check
|
2018-10-09 14:37:46 +03:00
|
|
|
ProfilesRequest.Result result = new ProfilesRequest(cfg).request();
|
|
|
|
for (SignedObjectHolder<ClientProfile> p : result.profiles) {
|
|
|
|
LogHelper.debug("Get profile: %s", p.object.getTitle());
|
|
|
|
if (p.object.getTitle().equals(config.title)) {
|
|
|
|
wrapper.profile = p.object;
|
|
|
|
Launcher.profile = p.object;
|
|
|
|
LogHelper.debug("Found profile: %s", Launcher.profile.getTitle());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2018-11-08 15:30:16 +03:00
|
|
|
} catch (Throwable e) {
|
2018-10-09 14:37:46 +03:00
|
|
|
LogHelper.error(e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
|
|
|
public static boolean loopAuth(ServerWrapper wrapper, int count, int sleeptime) {
|
|
|
|
if (count == 0) {
|
|
|
|
while (true) {
|
|
|
|
if (auth(wrapper)) return true;
|
2018-10-09 14:37:46 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
if (auth(wrapper)) return true;
|
2018-10-09 14:37:46 +03:00
|
|
|
try {
|
|
|
|
Thread.sleep(sleeptime);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2018-12-20 18:52:09 +03:00
|
|
|
@SuppressWarnings("ConfusingArgumentToVarargsMethod")
|
2018-09-17 10:07:32 +03:00
|
|
|
public static void main(String[] args) throws Throwable {
|
|
|
|
ServerWrapper wrapper = new ServerWrapper();
|
2018-11-27 14:34:39 +03:00
|
|
|
LogHelper.printVersion("ServerWrapper");
|
|
|
|
LogHelper.printLicense("ServerWrapper");
|
2018-09-17 10:07:32 +03:00
|
|
|
modulesManager = new ModulesManager(wrapper);
|
2018-12-19 15:38:32 +03:00
|
|
|
modulesManager.autoload(modulesDir);
|
2018-09-17 10:07:32 +03:00
|
|
|
Launcher.modulesManager = modulesManager;
|
|
|
|
modulesManager.preInitModules();
|
2018-12-23 18:50:31 +03:00
|
|
|
LogHelper.debug("Read LaunchWrapper.cfg");
|
|
|
|
gsonBuiler = new GsonBuilder();
|
|
|
|
gson = gsonBuiler.create();
|
2018-09-24 14:30:42 +03:00
|
|
|
generateConfigIfNotExists();
|
2018-12-23 18:50:31 +03:00
|
|
|
try(Reader reader = IOHelper.newReader(configFile))
|
|
|
|
{
|
|
|
|
config = gson.fromJson(reader,Config.class);
|
2018-09-24 14:30:42 +03:00
|
|
|
}
|
2018-12-19 15:38:32 +03:00
|
|
|
LauncherConfig cfg = new LauncherConfig(config.address, config.port, SecurityHelper.toPublicRSAKey(IOHelper.read(publicKeyFile)), new HashMap<>(), config.projectname);
|
2018-09-26 11:54:14 +03:00
|
|
|
Launcher.setConfig(cfg);
|
2018-11-08 15:30:16 +03:00
|
|
|
if (config.syncAuth) auth(wrapper);
|
|
|
|
else
|
|
|
|
CommonHelper.newThread("Server Auth Thread", true, () -> ServerWrapper.loopAuth(wrapper, config.reconnectCount, config.reconnectSleep));
|
2018-09-17 10:07:32 +03:00
|
|
|
modulesManager.initModules();
|
2018-09-24 14:55:39 +03:00
|
|
|
String classname = config.mainclass.isEmpty() ? args[0] : config.mainclass;
|
2018-12-20 18:45:01 +03:00
|
|
|
if (classname.length() == 0) {
|
2018-11-27 16:33:41 +03:00
|
|
|
LogHelper.error("MainClass not found. Please set MainClass for ServerWrapper.cfg or first commandline argument");
|
|
|
|
}
|
2018-09-24 14:55:39 +03:00
|
|
|
Class<?> mainClass;
|
2018-12-20 18:45:01 +03:00
|
|
|
if (config.customClassPath) {
|
2018-12-19 15:38:32 +03:00
|
|
|
String[] cp = config.classpath.split(":");
|
2018-12-20 18:45:01 +03:00
|
|
|
if (!ServerAgent.isAgentStarted()) {
|
2018-12-19 15:38:32 +03:00
|
|
|
LogHelper.warning("JavaAgent not found. Using URLClassLoader");
|
|
|
|
URL[] urls = Arrays.stream(cp).map(Paths::get).map(IOHelper::toURL).toArray(URL[]::new);
|
|
|
|
ucp = new PublicURLClassLoader(urls);
|
|
|
|
Thread.currentThread().setContextClassLoader(ucp);
|
|
|
|
loader = ucp;
|
2018-12-20 18:45:01 +03:00
|
|
|
} else {
|
|
|
|
LogHelper.info("Found %d custom classpath elements", cp.length);
|
|
|
|
for (String c : cp)
|
2018-12-19 15:38:32 +03:00
|
|
|
ServerAgent.addJVMClassPath(c);
|
|
|
|
}
|
|
|
|
}
|
2018-12-20 18:45:01 +03:00
|
|
|
if (config.autoloadLibraries) {
|
|
|
|
if (!ServerAgent.isAgentStarted()) {
|
2018-12-19 15:38:32 +03:00
|
|
|
throw new UnsupportedOperationException("JavaAgent not found, autoloadLibraries not available");
|
|
|
|
}
|
|
|
|
Path librariesDir = Paths.get(config.librariesDir);
|
|
|
|
LogHelper.info("Load libraries");
|
|
|
|
ServerAgent.loadLibraries(librariesDir);
|
|
|
|
}
|
2018-12-20 18:45:01 +03:00
|
|
|
if (loader != null) mainClass = Class.forName(classname, true, loader);
|
2018-12-19 15:38:32 +03:00
|
|
|
else mainClass = Class.forName(classname);
|
2018-09-17 10:07:32 +03:00
|
|
|
MethodHandle mainMethod = MethodHandles.publicLookup().findStatic(mainClass, "main", MethodType.methodType(void.class, String[].class));
|
|
|
|
String[] real_args = new String[args.length - 1];
|
2018-09-22 17:33:00 +03:00
|
|
|
System.arraycopy(args, 1, real_args, 0, args.length - 1);
|
2018-09-17 10:07:32 +03:00
|
|
|
modulesManager.postInitModules();
|
2018-12-20 18:45:01 +03:00
|
|
|
LogHelper.info("ServerWrapper: Project %s, LaunchServer address: %s port %d. Title: %s", config.projectname, config.address, config.port, config.title);
|
|
|
|
LogHelper.info("Minecraft Version (for profile): %s", wrapper.profile.getVersion().name);
|
2018-12-19 16:02:08 +03:00
|
|
|
LogHelper.info("Start Minecraft Server");
|
2018-11-27 16:33:41 +03:00
|
|
|
LogHelper.debug("Invoke main method %s", mainClass.getName());
|
2018-09-17 10:07:32 +03:00
|
|
|
mainMethod.invoke(real_args);
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2018-09-24 14:30:42 +03:00
|
|
|
private static void generateConfigIfNotExists() throws IOException {
|
|
|
|
if (IOHelper.isFile(configFile))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create new config
|
2018-09-24 15:59:42 +03:00
|
|
|
LogHelper.info("Creating LaunchWrapper config");
|
2018-09-24 14:30:42 +03:00
|
|
|
Config newConfig;
|
2018-12-23 18:50:31 +03:00
|
|
|
try(Reader reader = IOHelper.newReader(IOHelper.getResourceURL("ru/gravit/launcher/server/ServerWrapper.cfg")))
|
|
|
|
{
|
|
|
|
newConfig = gson.fromJson(reader,Config.class);
|
2018-09-24 14:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
LogHelper.warning("Title is not set. Please show ServerWrapper.cfg");
|
|
|
|
|
|
|
|
// Write LaunchServer config
|
2018-09-24 15:59:42 +03:00
|
|
|
LogHelper.info("Writing LaunchWrapper config file");
|
2018-12-23 18:50:31 +03:00
|
|
|
try(Writer writer = IOHelper.newWriter(configFile))
|
|
|
|
{
|
|
|
|
gson.toJson(newConfig,writer);
|
2018-09-24 14:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2018-12-23 18:50:31 +03:00
|
|
|
public static final class Config {
|
2018-09-24 14:30:42 +03:00
|
|
|
public String title;
|
2018-09-25 16:43:12 +03:00
|
|
|
public String projectname;
|
|
|
|
public String address;
|
|
|
|
public int port;
|
2018-10-09 14:37:46 +03:00
|
|
|
public int reconnectCount;
|
|
|
|
public int reconnectSleep;
|
2018-12-19 15:38:32 +03:00
|
|
|
public boolean customClassPath;
|
|
|
|
public boolean autoloadLibraries;
|
2018-10-09 14:37:46 +03:00
|
|
|
public boolean syncAuth;
|
2018-12-19 15:38:32 +03:00
|
|
|
public String classpath;
|
|
|
|
public String librariesDir;
|
2018-09-24 14:55:39 +03:00
|
|
|
public String mainclass;
|
2018-09-27 00:43:35 +03:00
|
|
|
public String login;
|
|
|
|
public String password;
|
2018-09-24 14:30:42 +03:00
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
public ClientProfile profile;
|
|
|
|
}
|