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;
|
2019-01-04 18:45:11 +03:00
|
|
|
import ru.gravit.launcher.ClientPermissions;
|
2018-09-17 10:07:32 +03:00
|
|
|
import ru.gravit.launcher.Launcher;
|
|
|
|
import ru.gravit.launcher.LauncherConfig;
|
2019-02-10 11:38:48 +03:00
|
|
|
import ru.gravit.launcher.events.request.ProfilesRequestEvent;
|
2018-12-20 18:45:01 +03:00
|
|
|
import ru.gravit.launcher.profiles.ClientProfile;
|
2019-04-04 11:16:23 +03:00
|
|
|
import ru.gravit.launcher.request.auth.AuthRequest;
|
2018-12-20 18:45:01 +03:00
|
|
|
import ru.gravit.launcher.request.update.ProfilesRequest;
|
2019-04-04 11:59:41 +03:00
|
|
|
import ru.gravit.launcher.request.websockets.LegacyRequestBridge;
|
2019-04-03 11:46:18 +03:00
|
|
|
import ru.gravit.launcher.server.setup.ServerWrapperSetup;
|
2018-12-19 15:38:32 +03:00
|
|
|
import ru.gravit.utils.PublicURLClassLoader;
|
2019-04-03 13:30:58 +03:00
|
|
|
import ru.gravit.utils.config.JsonConfigurable;
|
2019-01-15 06:35:39 +03:00
|
|
|
import ru.gravit.utils.helper.CommonHelper;
|
|
|
|
import ru.gravit.utils.helper.IOHelper;
|
|
|
|
import ru.gravit.utils.helper.LogHelper;
|
|
|
|
import ru.gravit.utils.helper.SecurityHelper;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2019-01-15 06:35:39 +03:00
|
|
|
import java.io.IOException;
|
2018-12-20 18:45:01 +03:00
|
|
|
import java.lang.invoke.MethodHandle;
|
|
|
|
import java.lang.invoke.MethodHandles;
|
|
|
|
import java.lang.invoke.MethodType;
|
2019-04-03 13:30:58 +03:00
|
|
|
import java.lang.reflect.Type;
|
2018-12-20 18:45:01 +03:00
|
|
|
import java.net.URL;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
2019-04-03 13:30:58 +03:00
|
|
|
import java.security.spec.InvalidKeySpecException;
|
2018-12-20 18:45:01 +03:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
2019-04-03 13:30:58 +03:00
|
|
|
public class ServerWrapper extends JsonConfigurable<ServerWrapper.Config> {
|
|
|
|
public ModulesManager modulesManager;
|
|
|
|
public Config config;
|
|
|
|
public PublicURLClassLoader ucp;
|
|
|
|
public ClassLoader loader;
|
|
|
|
public ClientPermissions permissions;
|
2019-04-03 11:46:18 +03:00
|
|
|
public static ServerWrapper wrapper;
|
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"));
|
2018-12-26 17:18:10 +03:00
|
|
|
public static Path configFile = Paths.get(System.getProperty("serverwrapper.configFile", "ServerWrapperConfig.json"));
|
2018-12-20 18:45:01 +03:00
|
|
|
public static Path publicKeyFile = Paths.get(System.getProperty("serverwrapper.publicKeyFile", "public.key"));
|
2019-04-03 11:46:18 +03:00
|
|
|
public static boolean disableSetup = Boolean.valueOf(System.getProperty("serverwrapper.disableSetup", "false"));
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2019-04-03 13:30:58 +03:00
|
|
|
public ServerWrapper(Type type, Path configPath) {
|
|
|
|
super(type, configPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean auth() {
|
2018-10-09 14:37:46 +03:00
|
|
|
try {
|
|
|
|
LauncherConfig cfg = Launcher.getConfig();
|
2019-04-04 14:40:24 +03:00
|
|
|
AuthRequest request = new AuthRequest(config.login, SecurityHelper.newRSAEncryptCipher(cfg.publicKey).doFinal(IOHelper.encode(config.password)), config.auth_id, AuthRequest.ConnectTypes.SERVER);
|
|
|
|
permissions = request.request().permissions;
|
2019-02-10 11:38:48 +03:00
|
|
|
ProfilesRequestEvent result = new ProfilesRequest(cfg).request();
|
2018-12-23 18:57:40 +03:00
|
|
|
for (ClientProfile p : result.profiles) {
|
|
|
|
LogHelper.debug("Get profile: %s", p.getTitle());
|
|
|
|
if (p.getTitle().equals(config.title)) {
|
2019-04-03 13:30:58 +03:00
|
|
|
profile = p;
|
2018-12-23 18:57:40 +03:00
|
|
|
Launcher.profile = p;
|
2018-10-09 14:37:46 +03:00
|
|
|
LogHelper.debug("Found profile: %s", Launcher.profile.getTitle());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-04-03 13:30:58 +03:00
|
|
|
if (profile == null) {
|
2019-01-12 02:35:04 +03:00
|
|
|
LogHelper.error("Your profile not found");
|
2019-04-03 13:30:58 +03:00
|
|
|
if (config.stopOnError) System.exit(-1);
|
2019-01-12 02:35:04 +03:00
|
|
|
}
|
2018-10-09 14:37:46 +03:00
|
|
|
return true;
|
2018-11-08 15:30:16 +03:00
|
|
|
} catch (Throwable e) {
|
2018-10-09 14:37:46 +03:00
|
|
|
LogHelper.error(e);
|
2019-04-03 13:30:58 +03:00
|
|
|
if (config.stopOnError) System.exit(-1);
|
2018-10-09 14:37:46 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2019-04-03 13:30:58 +03:00
|
|
|
public boolean loopAuth(int count, int sleeptime) {
|
2018-11-08 15:30:16 +03:00
|
|
|
if (count == 0) {
|
|
|
|
while (true) {
|
2019-04-03 13:30:58 +03:00
|
|
|
if (auth()) 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) {
|
2019-04-03 13:30:58 +03:00
|
|
|
if (auth()) return true;
|
2018-10-09 14:37:46 +03:00
|
|
|
try {
|
|
|
|
Thread.sleep(sleeptime);
|
|
|
|
} catch (InterruptedException e) {
|
2019-04-03 16:27:40 +03:00
|
|
|
Thread.currentThread().interrupt();
|
|
|
|
LogHelper.error(e);
|
2018-10-09 14:37:46 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2019-01-15 06:35:39 +03:00
|
|
|
public static void initGson() {
|
|
|
|
if (Launcher.gson != null) return;
|
2019-01-04 14:19:34 +03:00
|
|
|
Launcher.gsonBuilder = new GsonBuilder();
|
|
|
|
Launcher.gson = Launcher.gsonBuilder.create();
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:27:40 +03:00
|
|
|
public void run(String... args) throws Throwable {
|
2019-04-03 11:46:18 +03:00
|
|
|
gsonBuiler = new GsonBuilder();
|
|
|
|
gsonBuiler.setPrettyPrinting();
|
|
|
|
gson = gsonBuiler.create();
|
|
|
|
initGson();
|
2019-04-03 16:27:40 +03:00
|
|
|
if (args.length > 0 && args[0].equals("setup") && !disableSetup) {
|
2019-04-03 11:46:18 +03:00
|
|
|
LogHelper.debug("Read ServerWrapperConfig.json");
|
2019-04-03 13:30:58 +03:00
|
|
|
loadConfig();
|
2019-04-03 11:46:18 +03:00
|
|
|
ServerWrapperSetup setup = new ServerWrapperSetup();
|
|
|
|
setup.run();
|
|
|
|
System.exit(0);
|
|
|
|
}
|
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-26 17:18:10 +03:00
|
|
|
LogHelper.debug("Read ServerWrapperConfig.json");
|
2019-04-03 13:30:58 +03:00
|
|
|
loadConfig();
|
|
|
|
updateLauncherConfig();
|
2019-04-03 16:27:40 +03:00
|
|
|
if (config.env != null) Launcher.applyLauncherEnv(config.env);
|
2019-01-15 06:48:20 +03:00
|
|
|
else Launcher.applyLauncherEnv(LauncherConfig.LauncherEnvironment.STD);
|
2019-01-15 06:35:39 +03:00
|
|
|
if (config.logFile != null) LogHelper.addOutput(IOHelper.newWriter(Paths.get(config.logFile), true));
|
2019-04-03 13:30:58 +03:00
|
|
|
if (config.syncAuth) auth();
|
2018-11-08 15:30:16 +03:00
|
|
|
else
|
2019-04-03 13:30:58 +03:00
|
|
|
CommonHelper.newThread("Server Auth Thread", true, () -> loopAuth(config.reconnectCount, config.reconnectSleep));
|
2018-09-17 10:07:32 +03:00
|
|
|
modulesManager.initModules();
|
2019-01-03 17:22:59 +03:00
|
|
|
String classname = (config.mainclass == null || 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");
|
2019-01-15 06:35:39 +03:00
|
|
|
if (config.stopOnError) System.exit(-1);
|
2018-11-27 16:33:41 +03:00
|
|
|
}
|
2018-09-24 14:55:39 +03:00
|
|
|
Class<?> mainClass;
|
2018-12-20 18:45:01 +03:00
|
|
|
if (config.customClassPath) {
|
2019-01-15 06:35:39 +03:00
|
|
|
if (config.classpath == null)
|
|
|
|
throw new UnsupportedOperationException("classpath is null, customClassPath not available");
|
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");
|
|
|
|
}
|
2019-01-15 06:35:39 +03:00
|
|
|
if (config.librariesDir == null)
|
|
|
|
throw new UnsupportedOperationException("librariesDir is null, autoloadLibraries not available");
|
2018-12-19 15:38:32 +03:00
|
|
|
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));
|
|
|
|
modulesManager.postInitModules();
|
2019-04-04 11:59:41 +03:00
|
|
|
if(config.websocket.enabled)
|
|
|
|
{
|
|
|
|
LegacyRequestBridge.service.reconnectCallback = () ->
|
|
|
|
{
|
|
|
|
LogHelper.debug("WebSocket connect closed. Try reconnect");
|
|
|
|
try {
|
|
|
|
if (!LegacyRequestBridge.service.reconnectBlocking()) LogHelper.error("Error connecting");
|
|
|
|
LogHelper.debug("Connect to %s", config.websocket.address);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
auth();
|
|
|
|
};
|
|
|
|
}
|
2019-04-04 14:40:24 +03:00
|
|
|
LogHelper.info("ServerWrapper: Project %s, LaunchServer address: %s port %d. Title: %s", config.projectname, config.websocket.address, config.title);
|
2019-01-03 18:10:23 +03:00
|
|
|
LogHelper.info("Minecraft Version (for profile): %s", wrapper.profile == null ? "unknown" : 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());
|
2019-04-03 16:27:40 +03:00
|
|
|
if (config.args == null) {
|
2019-04-03 11:46:18 +03:00
|
|
|
String[] real_args;
|
2019-04-03 16:27:40 +03:00
|
|
|
if (args.length > 0) {
|
2019-04-03 11:46:18 +03:00
|
|
|
real_args = new String[args.length - 1];
|
|
|
|
System.arraycopy(args, 1, real_args, 0, args.length - 1);
|
|
|
|
} else real_args = args;
|
|
|
|
|
2019-02-05 15:30:56 +03:00
|
|
|
mainMethod.invoke(real_args);
|
2019-04-03 16:27:40 +03:00
|
|
|
} else {
|
2019-02-05 15:30:56 +03:00
|
|
|
mainMethod.invoke(config.args);
|
2019-02-05 14:33:53 +03:00
|
|
|
}
|
2018-09-17 10:07:32 +03:00
|
|
|
}
|
2019-04-03 16:27:40 +03:00
|
|
|
|
|
|
|
public void updateLauncherConfig() {
|
2019-04-03 13:30:58 +03:00
|
|
|
|
|
|
|
LauncherConfig cfg = null;
|
|
|
|
try {
|
2019-04-04 14:40:24 +03:00
|
|
|
cfg = new LauncherConfig(config.websocket.address, SecurityHelper.toPublicRSAKey(IOHelper.read(publicKeyFile)), new HashMap<>(), config.projectname);
|
2019-04-04 11:16:23 +03:00
|
|
|
if(config.websocket != null && config.websocket.enabled)
|
|
|
|
{
|
|
|
|
cfg.isNettyEnabled = true;
|
|
|
|
cfg.nettyAddress = config.websocket.address;
|
|
|
|
cfg.nettyPort = 1111;
|
|
|
|
}
|
2019-04-03 13:30:58 +03:00
|
|
|
} catch (InvalidKeySpecException | IOException e) {
|
|
|
|
LogHelper.error(e);
|
|
|
|
}
|
|
|
|
Launcher.setConfig(cfg);
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2019-04-03 13:30:58 +03:00
|
|
|
public static void main(String... args) throws Throwable {
|
|
|
|
LogHelper.printVersion("ServerWrapper");
|
|
|
|
LogHelper.printLicense("ServerWrapper");
|
|
|
|
ServerWrapper.wrapper = new ServerWrapper(ServerWrapper.Config.class, configFile);
|
|
|
|
ServerWrapper.wrapper.run(args);
|
|
|
|
}
|
2018-09-24 14:30:42 +03:00
|
|
|
|
2019-04-03 13:30:58 +03:00
|
|
|
@Override
|
|
|
|
public Config getConfig() {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Config getDefaultConfig() {
|
2019-01-15 06:35:39 +03:00
|
|
|
Config newConfig = new Config();
|
2018-12-26 17:18:10 +03:00
|
|
|
newConfig.title = "Your profile title";
|
|
|
|
newConfig.projectname = "MineCraft";
|
|
|
|
newConfig.login = "login";
|
|
|
|
newConfig.password = "password";
|
2019-01-03 17:22:59 +03:00
|
|
|
newConfig.mainclass = "";
|
2019-01-03 18:23:11 +03:00
|
|
|
newConfig.syncAuth = true;
|
2019-01-12 02:35:04 +03:00
|
|
|
newConfig.stopOnError = true;
|
2019-01-03 18:23:11 +03:00
|
|
|
newConfig.reconnectCount = 10;
|
|
|
|
newConfig.reconnectSleep = 1000;
|
2019-04-04 11:16:23 +03:00
|
|
|
newConfig.websocket = new WebSocketConf();
|
|
|
|
newConfig.websocket.address = "ws://localhost:9274/api";
|
|
|
|
newConfig.websocket.enabled = false;
|
2019-01-15 06:48:20 +03:00
|
|
|
newConfig.env = LauncherConfig.LauncherEnvironment.STD;
|
2019-04-03 13:30:58 +03:00
|
|
|
return newConfig;
|
|
|
|
}
|
2018-09-24 14:30:42 +03:00
|
|
|
|
2019-04-03 13:30:58 +03:00
|
|
|
@Override
|
|
|
|
public void setConfig(Config config) {
|
|
|
|
this.config = config;
|
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;
|
2019-04-04 11:16:23 +03:00
|
|
|
public WebSocketConf websocket;
|
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;
|
2019-01-12 02:35:04 +03:00
|
|
|
public boolean stopOnError;
|
2018-10-09 14:37:46 +03:00
|
|
|
public boolean syncAuth;
|
2019-01-12 02:35:04 +03:00
|
|
|
public String logFile;
|
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;
|
2019-02-05 14:33:53 +03:00
|
|
|
public String[] args;
|
2018-09-27 00:43:35 +03:00
|
|
|
public String password;
|
2019-03-22 09:14:29 +03:00
|
|
|
public String auth_id = "";
|
2019-01-15 06:48:20 +03:00
|
|
|
public LauncherConfig.LauncherEnvironment env;
|
2018-09-24 14:30:42 +03:00
|
|
|
}
|
2019-04-04 11:16:23 +03:00
|
|
|
public static final class WebSocketConf
|
|
|
|
{
|
|
|
|
public boolean enabled;
|
|
|
|
public String address;
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2018-09-17 10:07:32 +03:00
|
|
|
public ClientProfile profile;
|
|
|
|
}
|