[FIX] ServerWrapper improvements

This commit is contained in:
Gravita 2021-09-25 18:23:23 +07:00
parent 958686a032
commit 7a78cbb878
3 changed files with 61 additions and 45 deletions

View file

@ -20,7 +20,7 @@ public abstract class Request<R extends WebSocketEvent> implements WebSocketRequ
private static final List<ExtendedTokenCallback> extendedTokenCallbacks = new ArrayList<>(4);
private static final List<BiConsumer<String, AuthRequestEvent.OAuthRequestEvent>> 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<String, String> 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<String> invalidExtendedTokens;
public RequestRestoreReport(boolean legacySession, boolean refreshed, List<String> 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<String> invalidTokens = null;
if (event.invalidTokens != null && event.invalidTokens.size() > 0) {
boolean needRequest = false;
Map<String, String> 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);
}
}

View file

@ -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<ServerWrapper.Config> {
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<String> classpath;
public String librariesDir;
public String mainclass;
public String[] args;
public List<String> args;
public String authId;
public AuthRequestEvent.OAuthRequestEvent oauth;
public long oauthExpireTime;

View file

@ -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;