From 7a78cbb8789234ea5c5d9e9af860d9c4cd861be1 Mon Sep 17 00:00:00 2001 From: Gravita Date: Sat, 25 Sep 2021 18:23:23 +0700 Subject: [PATCH] [FIX] ServerWrapper improvements --- .../pro/gravit/launcher/request/Request.java | 49 ++++++++++++++----- .../gravit/launcher/server/ServerWrapper.java | 43 +++++++--------- .../server/setup/ServerWrapperSetup.java | 14 +++--- 3 files changed, 61 insertions(+), 45 deletions(-) diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/Request.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/Request.java index 4eb13f47..10153950 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/Request.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/Request.java @@ -20,7 +20,7 @@ public abstract class Request implements WebSocketRequ private static final List extendedTokenCallbacks = new ArrayList<>(4); private static final List> oauthChangeHandlers = new ArrayList<>(4); public static StdWebSocketService service; - private static UUID session = UUID.randomUUID(); + private static UUID session; private static AuthRequestEvent.OAuthRequestEvent oauth; private static Map extendedTokens; private static String authId; @@ -110,20 +110,44 @@ public static String getRefreshToken() { return oauth == null ? null : oauth.refreshToken; } - public static void reconnect() throws Exception { + public static RequestRestoreReport reconnect() throws Exception { service.open(); - restore(); + return restore(); } - public static void restore() throws Exception { - if (oauth != null) { - if (isTokenExpired() || oauth.accessToken == null) { - RefreshTokenRequest request = new RefreshTokenRequest(authId, oauth.refreshToken); - RefreshTokenRequestEvent event = request.request(); - setOAuth(authId, event.oauth); + public static class RequestRestoreReport { + public final boolean legacySession; + public final boolean refreshed; + public final List invalidExtendedTokens; + + public RequestRestoreReport(boolean legacySession, boolean refreshed, List invalidExtendedTokens) { + this.legacySession = legacySession; + this.refreshed = refreshed; + this.invalidExtendedTokens = invalidExtendedTokens; + } + } + + public static RequestRestoreReport restore() throws Exception { + if (session != null) { + RestoreSessionRequest request = new RestoreSessionRequest(session); + request.request(); + return new RequestRestoreReport(true, false, null); + } else { + boolean refreshed = false; + RestoreRequest request; + if(oauth != null) { + if (isTokenExpired() || oauth.accessToken == null) { + RefreshTokenRequest refreshRequest = new RefreshTokenRequest(authId, oauth.refreshToken); + RefreshTokenRequestEvent event = refreshRequest.request(); + setOAuth(authId, event.oauth); + refreshed = true; + } + request = new RestoreRequest(authId, oauth.accessToken, extendedTokens, false); + } else { + request = new RestoreRequest(authId, null, extendedTokens, false); } - RestoreRequest request = new RestoreRequest(authId, oauth.accessToken, extendedTokens, false); RestoreRequestEvent event = request.request(); + List invalidTokens = null; if (event.invalidTokens != null && event.invalidTokens.size() > 0) { boolean needRequest = false; Map tokens = new HashMap<>(); @@ -144,10 +168,9 @@ public static void restore() throws Exception { LogHelper.warning("Tokens %s not restored", String.join(",", event.invalidTokens)); } } + invalidTokens = event.invalidTokens; } - } else if (session != null) { - RestoreSessionRequest request = new RestoreSessionRequest(session); - request.request(); + return new RequestRestoreReport(false, refreshed, invalidTokens); } } diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java index 2cd8069d..0390f1eb 100644 --- a/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java +++ b/ServerWrapper/src/main/java/pro/gravit/launcher/server/ServerWrapper.java @@ -30,10 +30,7 @@ import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; +import java.util.*; public class ServerWrapper extends JsonConfigurable { public static final Path configFile = Paths.get(System.getProperty("serverwrapper.configFile", "ServerWrapperConfig.json")); @@ -121,23 +118,24 @@ public void run(String... args) throws Throwable { } String classname = (config.mainclass == null || config.mainclass.isEmpty()) ? args[0] : config.mainclass; if (classname.length() == 0) { - LogHelper.error("MainClass not found. Please set MainClass for ServerWrapper.cfg or first commandline argument"); - if (config.stopOnError) System.exit(-1); + 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); } Class mainClass; - if (config.customClassPath) { - if (config.classpath == null) - throw new UnsupportedOperationException("classpath is null, customClassPath not available"); - String[] cp = config.classpath.split(":"); + if (config.classpath != null && !config.classpath.isEmpty()) { if (!ServerAgent.isAgentStarted()) { LogHelper.warning("JavaAgent not found. Using URLClassLoader"); - URL[] urls = Arrays.stream(cp).map(Paths::get).map(IOHelper::toURL).toArray(URL[]::new); + URL[] urls = config.classpath.stream().map(Paths::get).map(IOHelper::toURL).toArray(URL[]::new); ucp = new PublicURLClassLoader(urls); Thread.currentThread().setContextClassLoader(ucp); loader = ucp; } else { - LogHelper.info("Found %d custom classpath elements", cp.length); - for (String c : cp) + LogHelper.info("Found %d custom classpath elements", config.classpath.size()); + for (String c : config.classpath) ServerAgent.addJVMClassPath(c); } } @@ -175,9 +173,9 @@ public void run(String... args) throws Throwable { System.arraycopy(args, 1, real_args, 0, args.length - 1); } else real_args = args; - mainMethod.invoke(real_args); + mainMethod.invoke((Object) real_args); } else { - mainMethod.invoke(config.args); + mainMethod.invoke((Object) config.args.toArray(new String[0])); } } @@ -204,10 +202,8 @@ public Config getDefaultConfig() { newConfig.projectname = "MineCraft"; newConfig.mainclass = ""; newConfig.extendedTokens = new HashMap<>(); - newConfig.syncAuth = true; - newConfig.stopOnError = true; - newConfig.reconnectCount = 10; - newConfig.reconnectSleep = 1000; + newConfig.args = new ArrayList<>(); + newConfig.classpath = new ArrayList<>(); newConfig.address = "ws://localhost:9274/api"; newConfig.env = LauncherConfig.LauncherEnvironment.STD; return newConfig; @@ -217,17 +213,12 @@ public static final class Config { public String projectname; public String address; public String serverName; - public int reconnectCount; - public int reconnectSleep; - public boolean customClassPath; public boolean autoloadLibraries; - public boolean stopOnError; - public boolean syncAuth; public String logFile; - public String classpath; + public List classpath; public String librariesDir; public String mainclass; - public String[] args; + public List args; public String authId; public AuthRequestEvent.OAuthRequestEvent oauth; public long oauthExpireTime; diff --git a/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperSetup.java b/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperSetup.java index 7b06b58d..3e08610d 100644 --- a/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperSetup.java +++ b/ServerWrapper/src/main/java/pro/gravit/launcher/server/setup/ServerWrapperSetup.java @@ -55,24 +55,26 @@ public void run() throws Exception { System.out.println("Print your server name:"); wrapper.config.serverName = commands.commandHandler.readLine(); wrapper.config.mainclass = mainClassName; - boolean stopOnError = wrapper.config.stopOnError; for (int i = 0; i < 10; ++i) { - System.out.println("Print launchserver websocket host( ws://host:port/api ):"); - wrapper.config.address = commands.commandHandler.readLine(); + if(Request.service == null || Request.service.isClosed) { + System.out.println("Print launchserver websocket host( ws://host:port/api ):"); + wrapper.config.address = commands.commandHandler.readLine(); + } System.out.println("Print server token:"); String checkServerToken = commands.commandHandler.readLine(); wrapper.config.extendedTokens.put("checkServer", checkServerToken); - wrapper.config.stopOnError = false; wrapper.updateLauncherConfig(); try { wrapper.restore(); wrapper.getProfiles(); + break; } catch (Throwable e) { LogHelper.error(e); - Request.service.close(); + if(!Request.service.isClosed) { + Request.service.close(); + } } } - wrapper.config.stopOnError = stopOnError; wrapper.saveConfig(); LogHelper.info("Generate start script"); Path startScript;