1
0
Fork 0
forked from mc/VTools

Compare commits

..

No commits in common. "5a975a1195859972891dae4292b1656450e41d52" and "7581ef4595071ce75c352328299afcb9e4c880b4" have entirely different histories.

View file

@ -6,6 +6,7 @@ import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ProxyServer;
import de.strifel.VTools.VTools; import de.strifel.VTools.VTools;
import de.strifel.VTools.listeners.TGBridge; import de.strifel.VTools.listeners.TGBridge;
import okhttp3.*;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
@ -13,7 +14,11 @@ import java.util.concurrent.LinkedBlockingQueue;
public class ServerCloser { public class ServerCloser {
private static final long MINUTE = 60L * 1000; private static final long MINUTE = 60L * 1000;
private static final OkHttpClient CLIENT = new OkHttpClient();
private static final String ACTION_STOP_JSON = "{\"action\":\"stop\"}";
@SuppressWarnings("java:S1068")
private static final String ACTION_START_JSON = "{\"action\":\"start\"}";
private final VTools plugin; private final VTools plugin;
private final ProxyServer server; private final ProxyServer server;
@ -26,17 +31,52 @@ public class ServerCloser {
this.server = plugin.getServer(); this.server = plugin.getServer();
} }
private boolean executeAzure() {
return executeAzure(ACTION_STOP_JSON);
}
private boolean executeAzure(String action) {
RequestBody requestBody = RequestBody.create(action, MediaType.parse("application/json"));
Request request = new Request.Builder()
.url(plugin.getConfigOrDefault("azure_api_url", "https://example.com/"))
.post(requestBody)
.addHeader("Content-Type", "application/json")
.build();
try (Response response = CLIENT.newCall(request).execute()) {
String body = response.body() == null ? "null" : response.body().string();
plugin.logger.info("ServerCloser: http request response: {} ({}).", body, response.code());
return (response.code() >= 200 && response.code() < 300);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private void close() { private void close() {
if (!INSTANCE.server.getAllPlayers().isEmpty()) { if (!INSTANCE.server.getAllPlayers().isEmpty()) {
plugin.logger.error("ServerCloser: 定时器到点时发现服务器有人。这不应发生,因为定时器本应该被打断。"); plugin.logger.error("ServerCloser: 定时器到点时发现服务器有人。这不应发生,因为定时器本应该被打断。");
TGBridge.error("ServerCloser: #bug @NaAlOH4 定时器到点时发现服务器有人。这不应发生,因为定时器本应该被打断。"); TGBridge.error("ServerCloser: #bug @NaAlOH4 定时器到点时发现服务器有人。这不应发生,因为定时器本应该被打断。");
return;
} }
String apiUrl = plugin.getConfigOrDefault("azure_api_url", "https://example.com/"); boolean httpSuccess = false;
for (int i = 0; i < 3; i++) {
httpSuccess = executeAzure();
if (httpSuccess) {
TGBridge.log("ServerCloser: 向 azure 发送关机命令成功,正在关闭 pymcd.");
TGBridge.setShuttingDown(0);
break;
}
}
if (!httpSuccess) {
TGBridge.error("服务器关机 http 请求失效,服务器可能没有正常关闭。");
} else {
try { try {
Runtime.getRuntime().exec(new String[]{"systemd-run", "--description=pymcd_stop", "--quiet", "--user", "--collect", "-p", "StandardInput=data", "-p", "StandardInputData=c2V0IC1lCnA9IiQocGdyZXAgLWYgcHltY2QucHkgLUFvKSIKa2lsbCAtSU5UICIkcCIKdGFpbCAtLXBpZD0iJHAiIC1mIC9kZXYvbnVsbApjdXJsIC1zIC1IICJDb250ZW50LVR5cGU6IGFwcGxpY2F0aW9uL2pzb24iIC1kICJ7XCJhY3Rpb25cIjpcInN0b3BcIn0iICIkMSIK", "bash", "-s", apiUrl}); Runtime.getRuntime().exec(new String[]{"pkill", "pymcd"});
} catch (IOException ignored) {} } catch (IOException e) {
TGBridge.error("关闭 pymcd 时出现问题:" + e.getMessage());
}
}
} }
private boolean firstInit; private boolean firstInit;