[FEATURE] Setup support env configuration in ServerWrapper, fix compile

This commit is contained in:
Gravita 2025-02-24 19:47:41 +07:00
parent 2e93102106
commit 9e29053afa
8 changed files with 47 additions and 21 deletions

View file

@ -154,7 +154,7 @@ public static String getRefreshToken() {
public static void reconnect() throws Exception { public static void reconnect() throws Exception {
getRequestService().open(); getRequestService().connect();
restore(); restore();
} }

View file

@ -6,7 +6,7 @@
public interface RequestService { public interface RequestService {
<T extends WebSocketEvent> CompletableFuture<T> request(Request<T> request) throws IOException; <T extends WebSocketEvent> CompletableFuture<T> request(Request<T> request) throws IOException;
void open(); void connect() throws Exception;
void registerEventHandler(EventHandler handler); void registerEventHandler(EventHandler handler);

View file

@ -9,6 +9,7 @@
import java.net.URI; import java.net.URI;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.WebSocket; import java.net.http.WebSocket;
import java.nio.ByteBuffer;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.KeyStoreException; import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -63,7 +64,7 @@ public ClientJSONPoint(URI uri) {
} }
} }
public void open() throws Exception { public void connect() throws Exception {
webSocket = webSocketBuilder.buildAsync(uri, this).get(); webSocket = webSocketBuilder.buildAsync(uri, this).get();
} }
@ -97,6 +98,17 @@ public CompletionStage<?> onClose(WebSocket webSocket, int statusCode, String re
return WebSocket.Listener.super.onClose(webSocket, statusCode, reason); return WebSocket.Listener.super.onClose(webSocket, statusCode, reason);
} }
@Override
public void onOpen(WebSocket webSocket) {
onOpen();
WebSocket.Listener.super.onOpen(webSocket);
}
@Override
public CompletionStage<?> onBinary(WebSocket webSocket, ByteBuffer data, boolean last) {
return WebSocket.Listener.super.onBinary(webSocket, data, last);
}
@Override @Override
public void onError(WebSocket webSocket, Throwable error) { public void onError(WebSocket webSocket, Throwable error) {
LogHelper.error(error); LogHelper.error(error);

View file

@ -45,7 +45,7 @@ public <T extends WebSocketEvent> CompletableFuture<T> request(Request<T> reques
} }
@Override @Override
public void open() { public void connect() {
} }

View file

@ -11,9 +11,12 @@
import javax.net.ssl.SSLException; import javax.net.ssl.SSLException;
import java.io.IOException; import java.io.IOException;
import java.net.http.WebSocket;
import java.nio.ByteBuffer;
import java.util.HashSet; import java.util.HashSet;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;

View file

@ -26,7 +26,7 @@ public <T extends WebSocketEvent> CompletableFuture<T> request(Request<T> reques
} }
@Override @Override
public void open() { public void connect() {
} }

View file

@ -308,13 +308,13 @@ public void apply() {
} }
public void applyEnv() { public void applyEnv() {
this.authId = applyEnvOrDefault("LAUNCHSERVER_AUTH_ID", this.authId); this.authId = applyEnvOrDefault("SERVERWRAPPER_AUTH_ID", this.authId);
this.address = applyEnvOrDefault("LAUNCHSERVER_ADDRESS", this.address); this.address = applyEnvOrDefault("SERVERWRAPPER_ADDRESS", this.address);
this.serverName = applyEnvOrDefault("LAUNCHSERVER_SERVER_NAME", this.serverName); this.serverName = applyEnvOrDefault("SERVERWRAPPER_SERVER_NAME", this.serverName);
this.encodedServerEcPublicKey = applyEnvOrDefault("LAUNCHSERVER_EC_PUBLIC_KEY", Base64.getUrlDecoder()::decode, null); this.encodedServerEcPublicKey = applyEnvOrDefault("SERVERWRAPPER_EC_PUBLIC_KEY", Base64.getUrlDecoder()::decode, null);
this.encodedServerRsaPublicKey = applyEnvOrDefault("LAUNCHSERVER_RSA_PUBLIC_KEY", Base64.getUrlDecoder()::decode, null); this.encodedServerRsaPublicKey = applyEnvOrDefault("SERVERWRAPPER_RSA_PUBLIC_KEY", Base64.getUrlDecoder()::decode, null);
{ {
String token = System.getenv("CHECK_SERVER_TOKEN"); String token = System.getenv("SERVERWRAPPER_CHECK_SERVER_TOKEN");
if(token != null) { if(token != null) {
if(extendedTokens == null) { if(extendedTokens == null) {
extendedTokens = new HashMap<>(); extendedTokens = new HashMap<>();

View file

@ -30,8 +30,11 @@ public ServerWrapperSetup() throws IOException {
public void run() throws Exception { public void run() throws Exception {
ServerWrapper wrapper = ServerWrapper.wrapper; ServerWrapper wrapper = ServerWrapper.wrapper;
System.out.println("Print server jar filename:"); String jarName = System.getenv("SERVERWRAPPER_JAR_NAME");
String jarName = commands.commandHandler.readLine(); if(jarName == null) {
System.out.println("Print server jar filename:");
jarName = commands.commandHandler.readLine();
}
Path jarPath = Paths.get(jarName); Path jarPath = Paths.get(jarName);
String mainClassName; String mainClassName;
String agentClassName; String agentClassName;
@ -56,14 +59,18 @@ public void run() throws Exception {
if (agentClassName != null) { if (agentClassName != null) {
LogHelper.info("Found PremainClass %s", agentClassName); LogHelper.info("Found PremainClass %s", agentClassName);
} }
System.out.println("Print your server name:"); if(wrapper.config.serverName == null || wrapper.config.serverName.isEmpty()) {
wrapper.config.serverName = commands.commandHandler.readLine(); System.out.println("Print your server name:");
wrapper.config.serverName = commands.commandHandler.readLine();
}
wrapper.config.mainclass = mainClassName; wrapper.config.mainclass = mainClassName;
boolean altMode = false; boolean altMode = false;
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
if(!Request.isAvailable() || Request.getRequestService().isClosed()) { if(!Request.isAvailable() || Request.getRequestService().isClosed()) {
System.out.println("Print launchserver websocket host( ws://host:port/api ):"); if(wrapper.config.address == null || wrapper.config.address.isEmpty()) {
wrapper.config.address = commands.commandHandler.readLine(); System.out.println("Print launchserver websocket host( ws://host:port/api ):");
wrapper.config.address = commands.commandHandler.readLine();
}
StdWebSocketService service; StdWebSocketService service;
try { try {
service = StdWebSocketService.initWebSockets(wrapper.config.address).get(); service = StdWebSocketService.initWebSockets(wrapper.config.address).get();
@ -73,9 +80,11 @@ public void run() throws Exception {
} }
Request.setRequestService(service); Request.setRequestService(service);
} }
System.out.println("Print server token:"); if(wrapper.config.extendedTokens == null || wrapper.config.extendedTokens.get("checkServer") == null) {
String checkServerToken = commands.commandHandler.readLine(); System.out.println("Print server token:");
wrapper.config.extendedTokens.put("checkServer", new Request.ExtendedToken(checkServerToken, 0)); String checkServerToken = commands.commandHandler.readLine();
wrapper.config.extendedTokens.put("checkServer", new Request.ExtendedToken(checkServerToken, 0));
}
wrapper.updateLauncherConfig(); wrapper.updateLauncherConfig();
try { try {
wrapper.restore(); wrapper.restore();
@ -93,7 +102,9 @@ public void run() throws Exception {
} }
if(wrapper.profile != null && wrapper.profile.getVersion().compareTo(ClientProfileVersions.MINECRAFT_1_18) >= 0) { if(wrapper.profile != null && wrapper.profile.getVersion().compareTo(ClientProfileVersions.MINECRAFT_1_18) >= 0) {
LogHelper.info("Switch to alternative start mode (1.18)"); LogHelper.info("Switch to alternative start mode (1.18)");
wrapper.config.classpath.add(jarName); if(!wrapper.config.classpath.contains(jarName)) {
wrapper.config.classpath.add(jarName);
}
wrapper.config.classLoaderConfig = ClientProfile.ClassLoaderConfig.LAUNCHER; wrapper.config.classLoaderConfig = ClientProfile.ClassLoaderConfig.LAUNCHER;
altMode = true; altMode = true;
} }