Launcher/ServerWrapper/src/main/java/ru/gravit/launcher/server/ServerWrapper.java

167 lines
7.5 KiB
Java
Raw Normal View History

2018-09-17 10:07:32 +03:00
package ru.gravit.launcher.server;
2018-09-24 14:30:42 +03:00
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
2018-09-17 10:07:32 +03:00
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
2018-09-24 14:30:42 +03:00
import java.nio.file.Path;
2018-09-17 10:07:32 +03:00
import java.nio.file.Paths;
import java.util.HashMap;
2018-09-17 10:07:32 +03:00
import ru.gravit.launcher.Launcher;
import ru.gravit.launcher.LauncherConfig;
import ru.gravit.launcher.request.auth.AuthServerRequest;
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;
import ru.gravit.launcher.serialize.config.entry.BooleanConfigEntry;
import ru.gravit.launcher.serialize.config.entry.IntegerConfigEntry;
2018-09-24 14:30:42 +03:00
import ru.gravit.launcher.serialize.config.entry.StringConfigEntry;
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-17 10:07:32 +03:00
import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.launcher.request.update.ProfilesRequest;
import ru.gravit.launcher.serialize.signed.SignedObjectHolder;
import ru.gravit.utils.helper.SecurityHelper;
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 Path configFile;
public static Config config;
2018-11-08 15:30:16 +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();
if (auth == null) throw new Exception("Non auth!"); // security check
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) {
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-11-08 15:30:16 +03:00
for (int i = 0; i < count; ++i) {
if (auth(wrapper)) return true;
try {
Thread.sleep(sleeptime);
} catch (InterruptedException e) {
return false;
}
}
return false;
}
2018-11-08 15:30:16 +03:00
2018-09-17 10:07:32 +03:00
public static void main(String[] args) throws Throwable {
ServerWrapper wrapper = new ServerWrapper();
LogHelper.printVersion("ServerWrapper");
LogHelper.printLicense("ServerWrapper");
2018-09-17 10:07:32 +03:00
modulesManager = new ModulesManager(wrapper);
2018-10-09 14:57:32 +03:00
modulesManager.autoload(Paths.get("srv_modules")); //BungeeCord using modules dir
2018-09-17 10:07:32 +03:00
Launcher.modulesManager = modulesManager;
configFile = Paths.get("ServerWrapper.cfg");
2018-09-17 10:07:32 +03:00
modulesManager.preInitModules();
2018-09-24 14:30:42 +03:00
generateConfigIfNotExists();
try (BufferedReader reader = IOHelper.newReader(configFile)) {
config = new Config(TextConfigReader.read(reader, true));
}
2018-11-08 15:30:16 +03:00
LauncherConfig cfg = new LauncherConfig(config.address, config.port, SecurityHelper.toPublicRSAKey(IOHelper.read(Paths.get("public.key"))), new HashMap<>(), config.projectname);
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();
String classname = config.mainclass.isEmpty() ? args[0] : config.mainclass;
Class<?> mainClass;
2018-11-08 15:30:16 +03:00
if (config.customClassLoader) {
@SuppressWarnings("unchecked")
2018-11-08 15:30:16 +03:00
Class<ClassLoader> classloader_class = (Class<ClassLoader>) Class.forName(config.classloader);
ClassLoader loader = classloader_class.getConstructor(ClassLoader.class).newInstance(ClassLoader.getSystemClassLoader());
Thread.currentThread().setContextClassLoader(loader);
2018-11-08 15:30:16 +03:00
mainClass = Class.forName(classname, false, loader);
} 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-09-25 17:06:13 +03:00
LogHelper.debug("Invoke main method");
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
LogHelper.info("Creating LaunchWrapper config");
2018-09-24 14:30:42 +03:00
Config newConfig;
2018-10-07 12:25:20 +03:00
try (BufferedReader reader = IOHelper.newReader(IOHelper.getResourceURL("ru/gravit/launcher/server/ServerWrapper.cfg"))) {
2018-09-24 14:30:42 +03:00
newConfig = new Config(TextConfigReader.read(reader, false));
}
LogHelper.warning("Title is not set. Please show ServerWrapper.cfg");
// Write LaunchServer config
LogHelper.info("Writing LaunchWrapper config file");
2018-09-24 14:30:42 +03:00
try (BufferedWriter writer = IOHelper.newWriter(configFile)) {
TextConfigWriter.write(newConfig.block, writer, true);
}
}
2018-11-08 15:30:16 +03:00
2018-09-24 14:30:42 +03:00
public static final class Config extends ConfigObject {
public String title;
public String projectname;
public String address;
public int port;
public int reconnectCount;
public int reconnectSleep;
public boolean customClassLoader;
public boolean syncAuth;
public String classloader;
public String mainclass;
public String login;
public String password;
2018-11-08 15:30:16 +03:00
2018-09-24 14:30:42 +03:00
protected Config(BlockConfigEntry block) {
super(block);
2018-11-08 15:30:16 +03:00
title = block.getEntryValue("title", StringConfigEntry.class);
address = block.getEntryValue("address", StringConfigEntry.class);
projectname = block.getEntryValue("projectName", StringConfigEntry.class);
login = block.getEntryValue("login", StringConfigEntry.class);
password = block.getEntryValue("password", StringConfigEntry.class);
port = block.getEntryValue("port", IntegerConfigEntry.class);
customClassLoader = block.getEntryValue("customClassLoader", BooleanConfigEntry.class);
2018-11-08 15:30:16 +03:00
if (customClassLoader)
classloader = block.getEntryValue("classloader", StringConfigEntry.class);
mainclass = block.getEntryValue("MainClass", StringConfigEntry.class);
reconnectCount = block.hasEntry("reconnectCount") ? block.getEntryValue("reconnectCount", IntegerConfigEntry.class) : 1;
reconnectSleep = block.hasEntry("reconnectSleep") ? block.getEntryValue("reconnectSleep", IntegerConfigEntry.class) : 30000;
syncAuth = block.hasEntry("syncAuth") ? block.getEntryValue("syncAuth", BooleanConfigEntry.class) : true;
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;
}