2019-08-25 12:39:08 +03:00
|
|
|
package pro.gravit.launchserver;
|
|
|
|
|
2019-10-16 12:38:44 +03:00
|
|
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
2019-08-25 12:39:08 +03:00
|
|
|
import pro.gravit.launcher.Launcher;
|
2020-04-05 10:27:04 +03:00
|
|
|
import pro.gravit.launcher.LauncherTrustManager;
|
2019-08-26 13:24:19 +03:00
|
|
|
import pro.gravit.launcher.modules.events.PreConfigPhase;
|
2019-09-27 03:31:19 +03:00
|
|
|
import pro.gravit.launcher.request.auth.AuthRequest;
|
2019-08-25 12:39:08 +03:00
|
|
|
import pro.gravit.launchserver.auth.handler.AuthHandler;
|
|
|
|
import pro.gravit.launchserver.auth.protect.ProtectHandler;
|
|
|
|
import pro.gravit.launchserver.auth.provider.AuthProvider;
|
|
|
|
import pro.gravit.launchserver.auth.texture.TextureProvider;
|
|
|
|
import pro.gravit.launchserver.components.Component;
|
|
|
|
import pro.gravit.launchserver.config.LaunchServerConfig;
|
|
|
|
import pro.gravit.launchserver.config.LaunchServerRuntimeConfig;
|
|
|
|
import pro.gravit.launchserver.dao.provider.DaoProvider;
|
2019-10-17 16:22:24 +03:00
|
|
|
import pro.gravit.launchserver.manangers.CertificateManager;
|
2019-08-25 12:39:08 +03:00
|
|
|
import pro.gravit.launchserver.manangers.LaunchServerGsonManager;
|
2019-08-26 13:24:19 +03:00
|
|
|
import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager;
|
2019-08-25 12:39:08 +03:00
|
|
|
import pro.gravit.launchserver.socket.WebSocketService;
|
|
|
|
import pro.gravit.utils.command.CommandHandler;
|
|
|
|
import pro.gravit.utils.command.JLineCommandHandler;
|
|
|
|
import pro.gravit.utils.command.StdCommandHandler;
|
|
|
|
import pro.gravit.utils.helper.IOHelper;
|
|
|
|
import pro.gravit.utils.helper.JVMHelper;
|
|
|
|
import pro.gravit.utils.helper.LogHelper;
|
|
|
|
import pro.gravit.utils.helper.SecurityHelper;
|
|
|
|
|
2019-10-19 19:46:04 +03:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.Writer;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.security.KeyPair;
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.security.Security;
|
|
|
|
import java.security.cert.CertificateException;
|
|
|
|
import java.security.interfaces.ECPrivateKey;
|
|
|
|
import java.security.interfaces.ECPublicKey;
|
|
|
|
|
2019-08-25 12:39:08 +03:00
|
|
|
public class LaunchServerStarter {
|
2019-10-19 19:43:25 +03:00
|
|
|
public static final boolean allowUnsigned = Boolean.getBoolean("launchserver.allowUnsigned");
|
2019-11-28 19:14:06 +03:00
|
|
|
public static final boolean inDocker = Boolean.getBoolean("launchserver.dockered");
|
2019-10-19 19:46:04 +03:00
|
|
|
|
2019-08-25 12:39:08 +03:00
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
JVMHelper.checkStackTrace(LaunchServerStarter.class);
|
|
|
|
JVMHelper.verifySystemProperties(LaunchServer.class, true);
|
|
|
|
LogHelper.addOutput(IOHelper.WORKING_DIR.resolve("LaunchServer.log"));
|
|
|
|
LogHelper.printVersion("LaunchServer");
|
|
|
|
LogHelper.printLicense("LaunchServer");
|
|
|
|
if (!StarterAgent.isAgentStarted()) {
|
|
|
|
LogHelper.error("StarterAgent is not started!");
|
|
|
|
LogHelper.error("You should add to JVM options this option: `-javaagent:LaunchServer.jar`");
|
|
|
|
}
|
|
|
|
Path dir = IOHelper.WORKING_DIR;
|
|
|
|
Path configFile, runtimeConfigFile;
|
2019-10-19 19:46:04 +03:00
|
|
|
Path publicKeyFile = dir.resolve("public.key");
|
2019-08-25 12:39:08 +03:00
|
|
|
Path privateKeyFile = dir.resolve("private.key");
|
2019-10-16 12:38:44 +03:00
|
|
|
ECPublicKey publicKey;
|
|
|
|
ECPrivateKey privateKey;
|
2019-10-17 16:22:24 +03:00
|
|
|
Security.addProvider(new BouncyCastleProvider());
|
|
|
|
CertificateManager certificateManager = new CertificateManager();
|
|
|
|
try {
|
|
|
|
certificateManager.readTrustStore(dir.resolve("truststore"));
|
|
|
|
} catch (CertificateException e) {
|
|
|
|
throw new IOException(e);
|
|
|
|
}
|
2019-10-17 16:58:52 +03:00
|
|
|
{
|
2020-02-21 07:17:16 +03:00
|
|
|
//LauncherTrustManager.CheckMode mode = (Version.RELEASE == Version.Type.LTS || Version.RELEASE == Version.Type.STABLE) ?
|
|
|
|
// (allowUnsigned ? LauncherTrustManager.CheckMode.WARN_IN_NOT_SIGNED : LauncherTrustManager.CheckMode.EXCEPTION_IN_NOT_SIGNED) :
|
|
|
|
// (allowUnsigned ? LauncherTrustManager.CheckMode.NONE_IN_NOT_SIGNED : LauncherTrustManager.CheckMode.WARN_IN_NOT_SIGNED);
|
|
|
|
certificateManager.checkClass(LaunchServer.class, LauncherTrustManager.CheckMode.NONE_IN_NOT_SIGNED);
|
2019-10-17 16:58:52 +03:00
|
|
|
}
|
2019-08-25 12:39:08 +03:00
|
|
|
|
|
|
|
LaunchServerRuntimeConfig runtimeConfig;
|
|
|
|
LaunchServerConfig config;
|
|
|
|
LaunchServer.LaunchServerEnv env = LaunchServer.LaunchServerEnv.PRODUCTION;
|
2019-10-17 16:22:24 +03:00
|
|
|
LaunchServerModulesManager modulesManager = new LaunchServerModulesManager(dir.resolve("modules"), dir.resolve("config"), certificateManager.trustManager);
|
2019-08-26 13:24:19 +03:00
|
|
|
modulesManager.autoload();
|
|
|
|
modulesManager.initModules(null);
|
2019-08-25 12:39:08 +03:00
|
|
|
registerAll();
|
2019-08-26 13:24:19 +03:00
|
|
|
initGson(modulesManager);
|
2019-08-25 12:39:08 +03:00
|
|
|
if (IOHelper.exists(dir.resolve("LaunchServer.conf"))) {
|
|
|
|
configFile = dir.resolve("LaunchServer.conf");
|
|
|
|
} else {
|
|
|
|
configFile = dir.resolve("LaunchServer.json");
|
|
|
|
}
|
|
|
|
if (IOHelper.exists(dir.resolve("RuntimeLaunchServer.conf"))) {
|
|
|
|
runtimeConfigFile = dir.resolve("RuntimeLaunchServer.conf");
|
|
|
|
} else {
|
|
|
|
runtimeConfigFile = dir.resolve("RuntimeLaunchServer.json");
|
|
|
|
}
|
|
|
|
CommandHandler localCommandHandler;
|
|
|
|
try {
|
|
|
|
Class.forName("org.jline.terminal.Terminal");
|
|
|
|
|
|
|
|
// JLine2 available
|
|
|
|
localCommandHandler = new JLineCommandHandler();
|
|
|
|
LogHelper.info("JLine2 terminal enabled");
|
|
|
|
} catch (ClassNotFoundException ignored) {
|
|
|
|
localCommandHandler = new StdCommandHandler(true);
|
|
|
|
LogHelper.warning("JLine2 isn't in classpath, using std");
|
|
|
|
}
|
|
|
|
if (IOHelper.isFile(publicKeyFile) && IOHelper.isFile(privateKeyFile)) {
|
2019-10-16 12:38:44 +03:00
|
|
|
LogHelper.info("Reading EC keypair");
|
|
|
|
publicKey = SecurityHelper.toPublicECKey(IOHelper.read(publicKeyFile));
|
|
|
|
privateKey = SecurityHelper.toPrivateECKey(IOHelper.read(privateKeyFile));
|
2019-08-25 12:39:08 +03:00
|
|
|
} else {
|
2019-10-16 12:38:44 +03:00
|
|
|
LogHelper.info("Generating EC keypair");
|
|
|
|
KeyPair pair = SecurityHelper.genECKeyPair(new SecureRandom());
|
|
|
|
publicKey = (ECPublicKey) pair.getPublic();
|
|
|
|
privateKey = (ECPrivateKey) pair.getPrivate();
|
2019-08-25 12:39:08 +03:00
|
|
|
|
|
|
|
// Write key pair list
|
2019-10-16 12:38:44 +03:00
|
|
|
LogHelper.info("Writing EC keypair list");
|
2019-08-25 12:39:08 +03:00
|
|
|
IOHelper.write(publicKeyFile, publicKey.getEncoded());
|
|
|
|
IOHelper.write(privateKeyFile, privateKey.getEncoded());
|
|
|
|
}
|
2019-08-26 14:22:24 +03:00
|
|
|
modulesManager.invokeEvent(new PreConfigPhase());
|
|
|
|
generateConfigIfNotExists(configFile, localCommandHandler, env);
|
|
|
|
LogHelper.info("Reading LaunchServer config file");
|
|
|
|
try (BufferedReader reader = IOHelper.newReader(configFile)) {
|
|
|
|
config = Launcher.gsonManager.gson.fromJson(reader, LaunchServerConfig.class);
|
|
|
|
}
|
2019-08-25 12:39:08 +03:00
|
|
|
if (!Files.exists(runtimeConfigFile)) {
|
|
|
|
LogHelper.info("Reset LaunchServer runtime config file");
|
|
|
|
runtimeConfig = new LaunchServerRuntimeConfig();
|
|
|
|
runtimeConfig.reset();
|
|
|
|
} else {
|
|
|
|
LogHelper.info("Reading LaunchServer runtime config file");
|
|
|
|
try (BufferedReader reader = IOHelper.newReader(runtimeConfigFile)) {
|
|
|
|
runtimeConfig = Launcher.gsonManager.gson.fromJson(reader, LaunchServerRuntimeConfig.class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LaunchServer.LaunchServerConfigManager launchServerConfigManager = new LaunchServer.LaunchServerConfigManager() {
|
|
|
|
@Override
|
|
|
|
public LaunchServerConfig readConfig() throws IOException {
|
|
|
|
LaunchServerConfig config1;
|
|
|
|
try (BufferedReader reader = IOHelper.newReader(configFile)) {
|
|
|
|
config1 = Launcher.gsonManager.gson.fromJson(reader, LaunchServerConfig.class);
|
|
|
|
}
|
|
|
|
return config1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public LaunchServerRuntimeConfig readRuntimeConfig() throws IOException {
|
|
|
|
LaunchServerRuntimeConfig config1;
|
|
|
|
try (BufferedReader reader = IOHelper.newReader(runtimeConfigFile)) {
|
|
|
|
config1 = Launcher.gsonManager.gson.fromJson(reader, LaunchServerRuntimeConfig.class);
|
|
|
|
}
|
|
|
|
return config1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeConfig(LaunchServerConfig config) throws IOException {
|
|
|
|
try (Writer writer = IOHelper.newWriter(configFile)) {
|
|
|
|
if (Launcher.gsonManager.configGson != null) {
|
|
|
|
Launcher.gsonManager.configGson.toJson(config, writer);
|
|
|
|
} else {
|
|
|
|
LogHelper.error("Error writing LaunchServer runtime config file. Gson is null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeRuntimeConfig(LaunchServerRuntimeConfig config) throws IOException {
|
|
|
|
try (Writer writer = IOHelper.newWriter(runtimeConfigFile)) {
|
|
|
|
if (Launcher.gsonManager.configGson != null) {
|
|
|
|
Launcher.gsonManager.configGson.toJson(config, writer);
|
|
|
|
} else {
|
|
|
|
LogHelper.error("Error writing LaunchServer runtime config file. Gson is null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-11-28 19:14:06 +03:00
|
|
|
LaunchServer.LaunchServerDirectories directories = new LaunchServer.LaunchServerDirectories();
|
|
|
|
directories.dir = dir;
|
|
|
|
if (inDocker) {
|
2020-04-05 10:27:04 +03:00
|
|
|
Path parentLibraries = StarterAgent.libraries.toAbsolutePath().normalize().getParent();
|
|
|
|
directories.launcherLibrariesCompileDir = parentLibraries.resolve(LaunchServer.LaunchServerDirectories.LAUNCHERLIBRARIESCOMPILE_NAME);
|
|
|
|
directories.launcherLibrariesDir = parentLibraries.resolve(LaunchServer.LaunchServerDirectories.LAUNCHERLIBRARIES_NAME);
|
2019-11-28 19:14:06 +03:00
|
|
|
}
|
2019-08-25 12:39:08 +03:00
|
|
|
LaunchServer server = new LaunchServerBuilder()
|
2019-11-28 19:14:06 +03:00
|
|
|
.setDirectories(directories)
|
2019-08-25 12:39:08 +03:00
|
|
|
.setEnv(env)
|
|
|
|
.setCommandHandler(localCommandHandler)
|
|
|
|
.setPrivateKey(privateKey)
|
|
|
|
.setPublicKey(publicKey)
|
|
|
|
.setRuntimeConfig(runtimeConfig)
|
|
|
|
.setConfig(config)
|
|
|
|
.setModulesManager(modulesManager)
|
|
|
|
.setLaunchServerConfigManager(launchServerConfigManager)
|
2019-10-17 16:22:24 +03:00
|
|
|
.setCertificateManager(certificateManager)
|
2019-08-25 12:39:08 +03:00
|
|
|
.build();
|
|
|
|
server.run();
|
|
|
|
}
|
|
|
|
|
2019-08-26 13:24:19 +03:00
|
|
|
public static void initGson(LaunchServerModulesManager modulesManager) {
|
|
|
|
Launcher.gsonManager = new LaunchServerGsonManager(modulesManager);
|
2019-08-25 12:39:08 +03:00
|
|
|
Launcher.gsonManager.initGson();
|
|
|
|
}
|
|
|
|
|
2019-10-19 19:46:04 +03:00
|
|
|
public static void registerAll() {
|
2019-08-25 12:39:08 +03:00
|
|
|
|
|
|
|
AuthHandler.registerHandlers();
|
|
|
|
AuthProvider.registerProviders();
|
|
|
|
TextureProvider.registerProviders();
|
|
|
|
Component.registerComponents();
|
|
|
|
ProtectHandler.registerHandlers();
|
|
|
|
WebSocketService.registerResponses();
|
|
|
|
DaoProvider.registerProviders();
|
2019-09-27 03:31:19 +03:00
|
|
|
AuthRequest.registerProviders();
|
2019-08-25 12:39:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void generateConfigIfNotExists(Path configFile, CommandHandler commandHandler, LaunchServer.LaunchServerEnv env) throws IOException {
|
|
|
|
if (IOHelper.isFile(configFile))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create new config
|
|
|
|
LogHelper.info("Creating LaunchServer config");
|
|
|
|
|
|
|
|
|
|
|
|
LaunchServerConfig newConfig = LaunchServerConfig.getDefault(env);
|
|
|
|
// Set server address
|
|
|
|
String address;
|
|
|
|
if (env.equals(LaunchServer.LaunchServerEnv.TEST)) {
|
|
|
|
address = "localhost";
|
|
|
|
newConfig.setProjectName("test");
|
|
|
|
} else {
|
|
|
|
System.out.println("LaunchServer address(default: localhost): ");
|
|
|
|
address = commandHandler.readLine();
|
|
|
|
System.out.println("LaunchServer projectName: ");
|
|
|
|
newConfig.setProjectName(commandHandler.readLine());
|
|
|
|
}
|
|
|
|
if (address == null || address.isEmpty()) {
|
|
|
|
LogHelper.error("Address null. Using localhost");
|
|
|
|
address = "localhost";
|
|
|
|
}
|
|
|
|
if (newConfig.projectName == null || newConfig.projectName.isEmpty()) {
|
|
|
|
LogHelper.error("ProjectName null. Using MineCraft");
|
|
|
|
newConfig.projectName = "MineCraft";
|
|
|
|
}
|
|
|
|
|
|
|
|
newConfig.netty.address = "ws://" + address + ":9274/api";
|
|
|
|
newConfig.netty.downloadURL = "http://" + address + ":9274/%dirname%/";
|
|
|
|
newConfig.netty.launcherURL = "http://" + address + ":9274/Launcher.jar";
|
|
|
|
newConfig.netty.launcherEXEURL = "http://" + address + ":9274/Launcher.exe";
|
|
|
|
|
|
|
|
// Write LaunchServer config
|
|
|
|
LogHelper.info("Writing LaunchServer config file");
|
|
|
|
try (BufferedWriter writer = IOHelper.newWriter(configFile)) {
|
|
|
|
Launcher.gsonManager.configGson.toJson(newConfig, writer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|