Launcher/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServerStarter.java

267 lines
12 KiB
Java
Raw Normal View History

package pro.gravit.launchserver;
2021-04-13 12:47:42 +03:00
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import pro.gravit.launcher.Launcher;
2020-04-05 10:27:04 +03:00
import pro.gravit.launcher.LauncherTrustManager;
import pro.gravit.launcher.modules.events.PreConfigPhase;
import pro.gravit.launcher.profiles.optional.actions.OptionalAction;
2021-06-18 07:34:32 +03:00
import pro.gravit.launcher.profiles.optional.triggers.OptionalTrigger;
import pro.gravit.launcher.request.auth.AuthRequest;
2021-04-13 15:30:06 +03:00
import pro.gravit.launcher.request.auth.GetAvailabilityAuthRequest;
2021-05-16 16:07:44 +03:00
import pro.gravit.launchserver.auth.core.AuthCoreProvider;
import pro.gravit.launchserver.auth.password.PasswordVerifier;
import pro.gravit.launchserver.auth.protect.ProtectHandler;
2020-05-18 15:26:38 +03:00
import pro.gravit.launchserver.auth.protect.hwid.HWIDProvider;
2020-12-16 21:39:24 +03:00
import pro.gravit.launchserver.auth.session.SessionStorage;
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.manangers.CertificateManager;
import pro.gravit.launchserver.manangers.LaunchServerGsonManager;
import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager;
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;
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.Security;
import java.security.cert.CertificateException;
public class LaunchServerStarter {
2019-10-19 19:43:25 +03:00
public static final boolean allowUnsigned = Boolean.getBoolean("launchserver.allowUnsigned");
2021-03-30 12:13:41 +03:00
public static final boolean prepareMode = Boolean.getBoolean("launchserver.prepareMode");
2021-04-13 12:47:42 +03:00
private static final Logger logger = LogManager.getLogger();
2019-10-19 19:46:04 +03:00
public static void main(String[] args) throws Exception {
JVMHelper.checkStackTrace(LaunchServerStarter.class);
JVMHelper.verifySystemProperties(LaunchServer.class, true);
2021-04-13 12:47:42 +03:00
//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;
try {
Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
Security.addProvider(new BouncyCastleProvider());
} catch (ClassNotFoundException ex) {
LogHelper.error("Library BouncyCastle not found! Is directory 'libraries' empty?");
return;
}
CertificateManager certificateManager = new CertificateManager();
try {
certificateManager.readTrustStore(dir.resolve("truststore"));
} catch (CertificateException e) {
throw new IOException(e);
}
{
LauncherTrustManager.CheckClassResult result = certificateManager.checkClass(LaunchServer.class);
2021-03-20 11:53:22 +03:00
if (result.type == LauncherTrustManager.CheckClassResultType.SUCCESS) {
2021-04-13 12:47:42 +03:00
logger.info("LaunchServer signed by {}", result.endCertificate.getSubjectDN().getName());
2021-03-20 11:53:22 +03:00
} else if (result.type == LauncherTrustManager.CheckClassResultType.NOT_SIGNED) {
// None
2021-03-20 11:53:22 +03:00
} else {
if (result.exception != null) {
2021-04-13 12:47:42 +03:00
logger.error(result.exception);
}
2021-04-13 12:47:42 +03:00
logger.warn("LaunchServer signed incorrectly. Status: {}", result.type.name());
}
}
LaunchServerRuntimeConfig runtimeConfig;
LaunchServerConfig config;
LaunchServer.LaunchServerEnv env = LaunchServer.LaunchServerEnv.PRODUCTION;
LaunchServerModulesManager modulesManager = new LaunchServerModulesManager(dir.resolve("modules"), dir.resolve("config"), certificateManager.trustManager);
modulesManager.autoload();
modulesManager.initModules(null);
registerAll();
initGson(modulesManager);
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();
2021-04-13 12:47:42 +03:00
logger.info("JLine2 terminal enabled");
} catch (ClassNotFoundException ignored) {
localCommandHandler = new StdCommandHandler(true);
2021-04-13 12:47:42 +03:00
logger.warn("JLine2 isn't in classpath, using std");
}
modulesManager.invokeEvent(new PreConfigPhase());
generateConfigIfNotExists(configFile, localCommandHandler, env);
2021-04-13 12:47:42 +03:00
logger.info("Reading LaunchServer config file");
try (BufferedReader reader = IOHelper.newReader(configFile)) {
config = Launcher.gsonManager.gson.fromJson(reader, LaunchServerConfig.class);
}
if (!Files.exists(runtimeConfigFile)) {
2021-04-13 12:47:42 +03:00
logger.info("Reset LaunchServer runtime config file");
runtimeConfig = new LaunchServerRuntimeConfig();
runtimeConfig.reset();
} else {
2021-04-13 12:47:42 +03:00
logger.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 {
2021-04-13 12:47:42 +03:00
logger.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 {
2021-04-13 12:47:42 +03:00
logger.error("Error writing LaunchServer runtime config file. Gson is null");
}
}
}
};
LaunchServer.LaunchServerDirectories directories = new LaunchServer.LaunchServerDirectories();
directories.dir = dir;
LaunchServer server = new LaunchServerBuilder()
.setDirectories(directories)
.setEnv(env)
.setCommandHandler(localCommandHandler)
.setRuntimeConfig(runtimeConfig)
.setConfig(config)
.setModulesManager(modulesManager)
.setLaunchServerConfigManager(launchServerConfigManager)
.setCertificateManager(certificateManager)
.build();
2021-05-25 12:17:29 +03:00
if (!prepareMode) {
2021-03-30 12:13:41 +03:00
server.run();
} else {
server.close();
}
}
public static void initGson(LaunchServerModulesManager modulesManager) {
Launcher.gsonManager = new LaunchServerGsonManager(modulesManager);
Launcher.gsonManager.initGson();
}
2021-05-16 16:07:44 +03:00
@SuppressWarnings("deprecation")
2019-10-19 19:46:04 +03:00
public static void registerAll() {
2021-05-16 16:07:44 +03:00
AuthCoreProvider.registerProviders();
PasswordVerifier.registerProviders();
TextureProvider.registerProviders();
Component.registerComponents();
ProtectHandler.registerHandlers();
WebSocketService.registerResponses();
AuthRequest.registerProviders();
2021-04-13 15:30:06 +03:00
GetAvailabilityAuthRequest.registerProviders();
2020-05-18 15:26:38 +03:00
HWIDProvider.registerProviders();
OptionalAction.registerProviders();
2021-06-18 07:34:32 +03:00
OptionalTrigger.registerProviders();
2020-12-16 21:39:24 +03:00
SessionStorage.registerProviders();
}
public static void generateConfigIfNotExists(Path configFile, CommandHandler commandHandler, LaunchServer.LaunchServerEnv env) throws IOException {
if (IOHelper.isFile(configFile))
return;
// Create new config
2021-04-13 12:47:42 +03:00
logger.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 {
address = System.getenv("ADDRESS");
2021-05-25 12:17:29 +03:00
if (address == null) {
address = System.getProperty("launchserver.address", null);
}
2021-05-25 12:17:29 +03:00
if (address == null) {
System.out.println("LaunchServer address(default: localhost): ");
address = commandHandler.readLine();
}
String projectName = System.getenv("PROJECTNAME");
2021-05-25 12:17:29 +03:00
if (projectName == null) {
projectName = System.getProperty("launchserver.projectname", null);
}
2021-05-25 12:17:29 +03:00
if (projectName == null) {
System.out.println("LaunchServer projectName: ");
projectName = commandHandler.readLine();
}
newConfig.setProjectName(projectName);
}
if (address == null || address.isEmpty()) {
2021-04-13 12:47:42 +03:00
logger.error("Address null. Using localhost");
address = "localhost";
}
if (newConfig.projectName == null || newConfig.projectName.isEmpty()) {
2021-04-13 12:47:42 +03:00
logger.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
2021-04-13 12:47:42 +03:00
logger.info("Writing LaunchServer config file");
try (BufferedWriter writer = IOHelper.newWriter(configFile)) {
Launcher.gsonManager.configGson.toJson(newConfig, writer);
}
}
}