2019-06-02 05:03:08 +03:00
|
|
|
package pro.gravit.launcher.server;
|
2018-09-17 10:07:32 +03:00
|
|
|
|
2019-06-02 05:03:08 +03:00
|
|
|
import pro.gravit.launcher.ClientPermissions;
|
|
|
|
import pro.gravit.launcher.Launcher;
|
|
|
|
import pro.gravit.launcher.LauncherConfig;
|
2023-11-27 18:41:49 +03:00
|
|
|
import pro.gravit.launcher.api.AuthService;
|
|
|
|
import pro.gravit.launcher.api.ClientService;
|
2023-11-28 14:21:32 +03:00
|
|
|
import pro.gravit.launcher.api.ConfigService;
|
2022-07-24 12:24:25 +03:00
|
|
|
import pro.gravit.launcher.api.KeyService;
|
2019-06-02 05:03:08 +03:00
|
|
|
import pro.gravit.launcher.config.JsonConfigurable;
|
2020-12-17 13:52:25 +03:00
|
|
|
import pro.gravit.launcher.events.request.AuthRequestEvent;
|
2019-06-02 05:03:08 +03:00
|
|
|
import pro.gravit.launcher.events.request.ProfilesRequestEvent;
|
2019-06-03 10:58:10 +03:00
|
|
|
import pro.gravit.launcher.profiles.ClientProfile;
|
2020-12-17 13:52:25 +03:00
|
|
|
import pro.gravit.launcher.profiles.PlayerProfile;
|
2020-09-12 09:41:58 +03:00
|
|
|
import pro.gravit.launcher.profiles.optional.actions.OptionalAction;
|
2021-06-18 07:34:32 +03:00
|
|
|
import pro.gravit.launcher.profiles.optional.triggers.OptionalTrigger;
|
2019-06-02 05:03:08 +03:00
|
|
|
import pro.gravit.launcher.request.Request;
|
|
|
|
import pro.gravit.launcher.request.auth.AuthRequest;
|
2021-04-13 15:30:06 +03:00
|
|
|
import pro.gravit.launcher.request.auth.GetAvailabilityAuthRequest;
|
2019-06-02 05:03:08 +03:00
|
|
|
import pro.gravit.launcher.request.update.ProfilesRequest;
|
2021-11-16 14:32:52 +03:00
|
|
|
import pro.gravit.launcher.request.websockets.StdWebSocketService;
|
2022-06-13 15:37:40 +03:00
|
|
|
import pro.gravit.launcher.server.authlib.InstallAuthlib;
|
2019-06-03 10:58:10 +03:00
|
|
|
import pro.gravit.launcher.server.setup.ServerWrapperSetup;
|
|
|
|
import pro.gravit.utils.PublicURLClassLoader;
|
2019-06-02 05:03:08 +03:00
|
|
|
import pro.gravit.utils.helper.IOHelper;
|
|
|
|
import pro.gravit.utils.helper.LogHelper;
|
2022-07-24 12:24:25 +03:00
|
|
|
import pro.gravit.utils.helper.SecurityHelper;
|
2023-10-31 18:18:10 +03:00
|
|
|
import pro.gravit.utils.launch.*;
|
2018-12-20 18:45:01 +03:00
|
|
|
|
2023-10-31 18:57:37 +03:00
|
|
|
import java.lang.invoke.MethodHandle;
|
|
|
|
import java.lang.invoke.MethodHandles;
|
|
|
|
import java.lang.invoke.MethodType;
|
2019-10-19 19:46:04 +03:00
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
2021-09-25 14:23:23 +03:00
|
|
|
import java.util.*;
|
2023-10-31 18:18:10 +03:00
|
|
|
import java.util.stream.Collectors;
|
2019-10-19 19:46:04 +03:00
|
|
|
|
2019-04-03 13:30:58 +03:00
|
|
|
public class ServerWrapper extends JsonConfigurable<ServerWrapper.Config> {
|
2019-10-19 19:43:25 +03:00
|
|
|
public static final Path configFile = Paths.get(System.getProperty("serverwrapper.configFile", "ServerWrapperConfig.json"));
|
|
|
|
public static final boolean disableSetup = Boolean.parseBoolean(System.getProperty("serverwrapper.disableSetup", "false"));
|
2020-04-05 10:27:04 +03:00
|
|
|
public static ServerWrapper wrapper;
|
2023-10-31 18:18:10 +03:00
|
|
|
public static ClassLoaderControl classLoaderControl;
|
2020-04-05 10:27:04 +03:00
|
|
|
public Config config;
|
|
|
|
public ClientProfile profile;
|
2020-12-17 13:52:25 +03:00
|
|
|
public ClientProfile.ServerProfile serverProfile;
|
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);
|
|
|
|
}
|
|
|
|
|
2021-09-25 13:40:08 +03:00
|
|
|
public static void initGson() {
|
|
|
|
Launcher.gsonManager = new ServerWrapperGsonManager();
|
2020-04-05 10:27:04 +03:00
|
|
|
Launcher.gsonManager.initGson();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-09-25 13:40:08 +03:00
|
|
|
public void restore() throws Exception {
|
|
|
|
if(config.oauth != null) {
|
|
|
|
Request.setOAuth(config.authId, config.oauth, config.oauthExpireTime);
|
|
|
|
}
|
|
|
|
if(config.extendedTokens != null) {
|
|
|
|
Request.addAllExtendedToken(config.extendedTokens);
|
2021-05-23 14:13:30 +03:00
|
|
|
}
|
2023-11-27 18:41:49 +03:00
|
|
|
Request.RequestRestoreReport report = Request.restore(config.oauth != null, false, false);
|
2023-11-28 14:21:32 +03:00
|
|
|
if(report.userInfo != null) {
|
|
|
|
if(report.userInfo.playerProfile != null) {
|
|
|
|
AuthService.username = report.userInfo.playerProfile.username;
|
|
|
|
AuthService.uuid = report.userInfo.playerProfile.uuid;
|
|
|
|
}
|
|
|
|
AuthService.permissions = report.userInfo.permissions;
|
|
|
|
}
|
2021-05-23 14:13:30 +03:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:04:12 +03:00
|
|
|
public void getProfiles() throws Exception {
|
2021-09-25 13:40:08 +03:00
|
|
|
ProfilesRequestEvent result = new ProfilesRequest().request();
|
|
|
|
for (ClientProfile p : result.profiles) {
|
|
|
|
LogHelper.debug("Get profile: %s", p.getTitle());
|
|
|
|
boolean isFound = false;
|
|
|
|
for (ClientProfile.ServerProfile srv : p.getServers()) {
|
|
|
|
if (srv != null && srv.name.equals(config.serverName)) {
|
|
|
|
this.serverProfile = srv;
|
|
|
|
this.profile = p;
|
|
|
|
Launcher.profile = p;
|
2023-11-28 14:21:32 +03:00
|
|
|
AuthService.profile = p;
|
2021-09-25 13:40:08 +03:00
|
|
|
LogHelper.debug("Found profile: %s", Launcher.profile.getTitle());
|
|
|
|
isFound = true;
|
|
|
|
break;
|
2018-10-09 14:37:46 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-25 13:40:08 +03:00
|
|
|
if (isFound) break;
|
|
|
|
}
|
|
|
|
if (profile == null) {
|
|
|
|
LogHelper.warning("Not connected to ServerProfile. May be serverName incorrect?");
|
2018-10-09 14:37:46 +03:00
|
|
|
}
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2019-04-03 16:27:40 +03:00
|
|
|
public void run(String... args) throws Throwable {
|
2021-09-25 13:40:08 +03:00
|
|
|
initGson();
|
2020-01-20 08:21:59 +03:00
|
|
|
AuthRequest.registerProviders();
|
2021-04-13 15:30:06 +03:00
|
|
|
GetAvailabilityAuthRequest.registerProviders();
|
2020-09-12 09:41:58 +03:00
|
|
|
OptionalAction.registerProviders();
|
2021-06-18 07:34:32 +03:00
|
|
|
OptionalTrigger.registerProviders();
|
2023-06-30 16:22:22 +03:00
|
|
|
if (args.length > 0 && args[0].equalsIgnoreCase("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();
|
2019-08-16 12:49:22 +03:00
|
|
|
System.exit(0);
|
2019-04-03 11:46:18 +03:00
|
|
|
}
|
2023-06-30 16:22:22 +03:00
|
|
|
if (args.length > 1 && args[0].equalsIgnoreCase("installAuthlib") && !disableSetup) {
|
2022-06-13 15:37:40 +03:00
|
|
|
LogHelper.debug("Read ServerWrapperConfig.json");
|
|
|
|
loadConfig();
|
|
|
|
InstallAuthlib command = new InstallAuthlib();
|
|
|
|
command. run(args[1]);
|
|
|
|
System.exit(0);
|
|
|
|
}
|
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);
|
2021-11-16 19:00:41 +03:00
|
|
|
StdWebSocketService service = StdWebSocketService.initWebSockets(config.address).get();
|
2021-11-16 14:32:52 +03:00
|
|
|
service.reconnectCallback = () ->
|
|
|
|
{
|
|
|
|
LogHelper.debug("WebSocket connect closed. Try reconnect");
|
|
|
|
try {
|
|
|
|
Request.reconnect();
|
|
|
|
getProfiles();
|
|
|
|
} catch (Exception e) {
|
|
|
|
LogHelper.error(e);
|
|
|
|
}
|
|
|
|
};
|
2023-09-21 18:08:03 +03:00
|
|
|
if(config.properties != null) {
|
|
|
|
for(Map.Entry<String, String> e : config.properties.entrySet()) {
|
|
|
|
System.setProperty(e.getKey(), e.getValue());
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 14:32:52 +03:00
|
|
|
Request.setRequestService(service);
|
2019-01-15 06:35:39 +03:00
|
|
|
if (config.logFile != null) LogHelper.addOutput(IOHelper.newWriter(Paths.get(config.logFile), true));
|
2021-05-23 14:13:30 +03:00
|
|
|
{
|
2021-09-25 13:40:08 +03:00
|
|
|
restore();
|
2021-05-23 14:13:30 +03:00
|
|
|
getProfiles();
|
|
|
|
}
|
2022-07-24 12:24:25 +03:00
|
|
|
if(config.encodedServerRsaPublicKey != null) {
|
|
|
|
KeyService.serverRsaPublicKey = SecurityHelper.toPublicRSAKey(config.encodedServerRsaPublicKey);
|
|
|
|
}
|
2023-11-27 18:41:49 +03:00
|
|
|
if(config.encodedServerEcPublicKey != null) {
|
|
|
|
KeyService.serverEcPublicKey = SecurityHelper.toPublicECDSAKey(config.encodedServerEcPublicKey);
|
|
|
|
}
|
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) {
|
2021-09-25 14:23:23 +03:00
|
|
|
LogHelper.error("MainClass not found. Please set MainClass for ServerWrapper.json or first commandline argument");
|
|
|
|
System.exit(-1);
|
|
|
|
}
|
|
|
|
if(config.oauth == null && ( config.extendedTokens == null || config.extendedTokens.isEmpty())) {
|
|
|
|
LogHelper.error("Auth not configured. Please use 'java -jar ServerWrapper.jar setup'");
|
|
|
|
System.exit(-1);
|
2018-11-27 16:33:41 +03:00
|
|
|
}
|
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);
|
|
|
|
}
|
2021-10-17 12:53:33 +03:00
|
|
|
LogHelper.info("ServerWrapper: LaunchServer address: %s. Title: %s", config.address, Launcher.profile != null ? Launcher.profile.getTitle() : "unknown");
|
2023-04-20 15:06:12 +03:00
|
|
|
LogHelper.info("Minecraft Version (for profile): %s", wrapper.profile == null ? "unknown" : wrapper.profile.getVersion().toString());
|
2022-04-06 21:34:58 +03:00
|
|
|
String[] real_args;
|
2022-05-11 18:05:34 +03:00
|
|
|
if(config.args != null && config.args.size() > 0) {
|
|
|
|
real_args = config.args.toArray(new String[0]);
|
|
|
|
} else if (args.length > 0) {
|
2022-04-06 21:34:58 +03:00
|
|
|
real_args = new String[args.length - 1];
|
|
|
|
System.arraycopy(args, 1, real_args, 0, args.length - 1);
|
|
|
|
} else real_args = args;
|
|
|
|
Launch launch;
|
|
|
|
switch (config.classLoaderConfig) {
|
|
|
|
case LAUNCHER:
|
2023-10-31 18:18:10 +03:00
|
|
|
launch = new LegacyLaunch();
|
2022-04-06 21:34:58 +03:00
|
|
|
break;
|
|
|
|
case MODULE:
|
|
|
|
launch = new ModuleLaunch();
|
|
|
|
break;
|
|
|
|
default:
|
2023-10-31 18:18:10 +03:00
|
|
|
if(ServerAgent.isAgentStarted()) {
|
|
|
|
launch = new BasicLaunch(ServerAgent.inst);
|
|
|
|
} else {
|
|
|
|
launch = new BasicLaunch();
|
|
|
|
}
|
2022-04-06 21:34:58 +03:00
|
|
|
break;
|
|
|
|
}
|
2023-10-31 18:18:10 +03:00
|
|
|
LaunchOptions options = new LaunchOptions();
|
2023-11-01 00:05:14 +03:00
|
|
|
options.enableHacks = config.enableHacks;
|
2023-10-31 18:18:10 +03:00
|
|
|
options.moduleConf = config.moduleConf;
|
|
|
|
classLoaderControl = launch.init(config.classpath.stream().map(Paths::get).collect(Collectors.toCollection(ArrayList::new)), config.nativesDir, options);
|
2023-11-27 18:41:49 +03:00
|
|
|
if(ServerAgent.isAgentStarted()) {
|
|
|
|
ClientService.instrumentation = ServerAgent.inst;
|
|
|
|
}
|
|
|
|
ClientService.classLoaderControl = classLoaderControl;
|
|
|
|
ClientService.baseURLs = classLoaderControl.getURLs();
|
|
|
|
ClientService.nativePath = config.nativesDir;
|
2023-11-28 14:21:32 +03:00
|
|
|
ConfigService.serverName = config.serverName;
|
|
|
|
if(config.configServiceSettings != null) {
|
|
|
|
config.configServiceSettings.apply();
|
|
|
|
}
|
2018-12-19 16:02:08 +03:00
|
|
|
LogHelper.info("Start Minecraft Server");
|
2022-11-24 15:52:28 +03:00
|
|
|
LogHelper.debug("Invoke main method %s with %s", classname, launch.getClass().getName());
|
2022-04-06 21:34:58 +03:00
|
|
|
try {
|
2023-10-31 18:57:37 +03:00
|
|
|
if(config.compatClasses != null) {
|
|
|
|
for (String e : config.compatClasses) {
|
|
|
|
Class<?> clazz = classLoaderControl.getClass(e);
|
|
|
|
MethodHandle runMethod = MethodHandles.lookup().findStatic(clazz, "run", MethodType.methodType(void.class, ClassLoaderControl.class));
|
|
|
|
runMethod.invoke(classLoaderControl);
|
|
|
|
}
|
|
|
|
}
|
2023-10-31 18:18:10 +03:00
|
|
|
launch.launch(config.mainclass, config.mainmodule, Arrays.asList(real_args));
|
2022-04-06 21:34:58 +03:00
|
|
|
} catch (Throwable e) {
|
|
|
|
LogHelper.error(e);
|
|
|
|
System.exit(-1);
|
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() {
|
2021-10-17 12:53:33 +03:00
|
|
|
LauncherConfig cfg = new LauncherConfig(config.address, null, null, new HashMap<>(), "ServerWrapper");
|
2019-04-03 13:30:58 +03:00
|
|
|
Launcher.setConfig(cfg);
|
|
|
|
}
|
2018-11-08 15:30:16 +03:00
|
|
|
|
2019-04-03 13:30:58 +03:00
|
|
|
@Override
|
|
|
|
public Config getConfig() {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2020-04-05 10:27:04 +03:00
|
|
|
@Override
|
|
|
|
public void setConfig(Config config) {
|
|
|
|
this.config = config;
|
|
|
|
}
|
|
|
|
|
2019-04-03 13:30:58 +03:00
|
|
|
@Override
|
|
|
|
public Config getDefaultConfig() {
|
2019-01-15 06:35:39 +03:00
|
|
|
Config newConfig = new Config();
|
2020-12-17 13:52:25 +03:00
|
|
|
newConfig.serverName = "your server name";
|
2019-01-03 17:22:59 +03:00
|
|
|
newConfig.mainclass = "";
|
2021-09-25 13:40:08 +03:00
|
|
|
newConfig.extendedTokens = new HashMap<>();
|
2021-09-25 14:23:23 +03:00
|
|
|
newConfig.args = new ArrayList<>();
|
|
|
|
newConfig.classpath = new ArrayList<>();
|
2019-07-10 16:02:46 +03:00
|
|
|
newConfig.address = "ws://localhost:9274/api";
|
2021-12-02 12:45:36 +03:00
|
|
|
newConfig.classLoaderConfig = ClientProfile.ClassLoaderConfig.SYSTEM_ARGS;
|
2019-01-15 06:48:20 +03:00
|
|
|
newConfig.env = LauncherConfig.LauncherEnvironment.STD;
|
2023-09-21 18:08:03 +03:00
|
|
|
newConfig.properties = new HashMap<>();
|
2019-04-03 13:30:58 +03:00
|
|
|
return newConfig;
|
|
|
|
}
|
2018-09-24 14:30:42 +03:00
|
|
|
|
2018-12-23 18:50:31 +03:00
|
|
|
public static final class Config {
|
2021-10-17 12:53:33 +03:00
|
|
|
@Deprecated
|
2019-05-03 12:23:04 +03:00
|
|
|
public String projectname;
|
2019-07-10 16:02:46 +03:00
|
|
|
public String address;
|
2020-12-17 13:52:25 +03:00
|
|
|
public String serverName;
|
2018-12-19 15:38:32 +03:00
|
|
|
public boolean autoloadLibraries;
|
2019-01-12 02:35:04 +03:00
|
|
|
public String logFile;
|
2021-09-25 14:23:23 +03:00
|
|
|
public List<String> classpath;
|
2021-12-02 12:45:36 +03:00
|
|
|
public ClientProfile.ClassLoaderConfig classLoaderConfig;
|
2018-12-19 15:38:32 +03:00
|
|
|
public String librariesDir;
|
2018-09-24 14:55:39 +03:00
|
|
|
public String mainclass;
|
2023-10-31 18:18:10 +03:00
|
|
|
public String mainmodule;
|
|
|
|
public String nativesDir = "natives";
|
2021-09-25 14:23:23 +03:00
|
|
|
public List<String> args;
|
2023-10-31 18:57:37 +03:00
|
|
|
public List<String> compatClasses;
|
2021-09-25 13:40:08 +03:00
|
|
|
public String authId;
|
2021-05-23 14:13:30 +03:00
|
|
|
public AuthRequestEvent.OAuthRequestEvent oauth;
|
|
|
|
public long oauthExpireTime;
|
2023-08-13 13:57:08 +03:00
|
|
|
public Map<String, Request.ExtendedToken> extendedTokens;
|
2019-01-15 06:48:20 +03:00
|
|
|
public LauncherConfig.LauncherEnvironment env;
|
2023-10-31 18:18:10 +03:00
|
|
|
public LaunchOptions.ModuleConf moduleConf = new LaunchOptions.ModuleConf();
|
2022-07-24 12:24:25 +03:00
|
|
|
|
|
|
|
public byte[] encodedServerRsaPublicKey;
|
|
|
|
|
|
|
|
public byte[] encodedServerEcPublicKey;
|
2023-11-01 00:05:14 +03:00
|
|
|
public boolean enableHacks;
|
2023-09-21 18:08:03 +03:00
|
|
|
|
|
|
|
public Map<String, String> properties;
|
2023-11-28 14:21:32 +03:00
|
|
|
public ConfigServiceSettings configServiceSettings = new ConfigServiceSettings();
|
|
|
|
|
|
|
|
public static class ConfigServiceSettings {
|
|
|
|
public boolean disableLogging = false;
|
|
|
|
public boolean checkServerNeedProperties = false;
|
|
|
|
public boolean checkServerNeedHardware = false;
|
|
|
|
public void apply() {
|
|
|
|
ConfigService.disableLogging = disableLogging;
|
|
|
|
ConfigService.checkServerConfig.needHardware = checkServerNeedHardware;
|
|
|
|
ConfigService.checkServerConfig.needProperties = checkServerNeedProperties;
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 14:30:42 +03:00
|
|
|
}
|
2018-09-17 10:07:32 +03:00
|
|
|
}
|