feat: code
This commit is contained in:
@@ -5,6 +5,8 @@ import com.google.gson.JsonParser;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
@@ -41,18 +43,29 @@ public class BindRequest {
|
||||
* @return A future that completes with true if status is 'bound', false
|
||||
* otherwise
|
||||
*/
|
||||
public CompletableFuture<Boolean> checkBindStatus() {
|
||||
public CompletableFuture<Boolean> checkBindStatus(String hostname, int port) {
|
||||
// NetworkService is already configured with BaseURL, only need the path here
|
||||
String path = "/server/server-bind-status";
|
||||
|
||||
return networkService.getData(path, null)
|
||||
.thenApply(responseBody -> {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
|
||||
params.put("address", hostname);
|
||||
params.put("port", String.valueOf(port));
|
||||
|
||||
return networkService.getData(path, params)
|
||||
.thenApply(response -> {
|
||||
|
||||
if (response.getStatusCode() != 200) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String responseBody = response.getBody();
|
||||
try {
|
||||
JsonObject responseData = JsonParser.parseString(responseBody).getAsJsonObject();
|
||||
boolean bound = responseData.has("bound") && responseData.get("bound").getAsBoolean();
|
||||
|
||||
if (!bound) {
|
||||
logger.warn("Mineroo Bind: Server not bound. 'bound' field is false.");
|
||||
if (bound) {
|
||||
logger.warn("Mineroo Bind: Server already bound.");
|
||||
}
|
||||
return bound;
|
||||
} catch (Exception e) {
|
||||
@@ -73,7 +86,7 @@ public class BindRequest {
|
||||
* @param port The server port
|
||||
* @return A future that completes with true if tokens are received
|
||||
*/
|
||||
public CompletableFuture<Boolean> initialMotdVerifyRequest(String hostname, Integer port) {
|
||||
public CompletableFuture<Boolean> initialMotdVerifyRequest(String hostname, int port) {
|
||||
String path = "/server/motd-verify";
|
||||
|
||||
JsonObject json = new JsonObject();
|
||||
@@ -81,7 +94,14 @@ public class BindRequest {
|
||||
json.addProperty("server_port", port);
|
||||
|
||||
return networkService.postData(path, json)
|
||||
.thenApply(responseBody -> {
|
||||
.thenApply(response -> {
|
||||
String responseBody = response.getBody();
|
||||
|
||||
if (response.getStatusCode() != 200) {
|
||||
logger.error("Mineroo Bind: Api fetch failed.");
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
JsonObject responseData = JsonParser.parseString(responseBody).getAsJsonObject();
|
||||
|
||||
@@ -128,7 +148,8 @@ public class BindRequest {
|
||||
json.addProperty("description", description);
|
||||
|
||||
return networkService.postData(path, json)
|
||||
.thenApply(responseBody -> {
|
||||
.thenApply(response -> {
|
||||
String responseBody = response.getBody();
|
||||
// Assume as long as the request returns successfully and no exception is
|
||||
// thrown, it is considered successful (HTTP 200)
|
||||
// If the API returns a specific {"success": false}, you need to parse the JSON
|
||||
|
||||
@@ -19,7 +19,7 @@ public class HttpNetworkService implements NetworkServiceInterface {
|
||||
|
||||
/**
|
||||
* @param baseUrl Base URL of the API (e.g., "https://oapi.mineroo.online")
|
||||
* @param username Username for Basic Auth (usually "mbind" in your case)
|
||||
* @param username Username for Basic Auth (usually "mserver" in your case)
|
||||
* @param password Password for Basic Auth (the token)
|
||||
*/
|
||||
public HttpNetworkService(String baseUrl, String username, String password) {
|
||||
@@ -36,7 +36,7 @@ public class HttpNetworkService implements NetworkServiceInterface {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<String> getData(String endpoint, Map<String, String> params) {
|
||||
public CompletableFuture<NetworkResponse> getData(String endpoint, Map<String, String> params) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
// 1. Build URL and query parameters
|
||||
@@ -61,7 +61,8 @@ public class HttpNetworkService implements NetworkServiceInterface {
|
||||
|
||||
// You can do a simple status code check here, or leave it to the upper layer
|
||||
// To keep the interface pure, we just return the body
|
||||
return response.body();
|
||||
NetworkResponse resp = new NetworkResponse(response.statusCode(), response.body());
|
||||
return resp;
|
||||
|
||||
} catch (Exception e) {
|
||||
// Wrap checked exceptions as runtime exceptions, Future will catch them
|
||||
@@ -71,7 +72,7 @@ public class HttpNetworkService implements NetworkServiceInterface {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<String> postData(String endpoint, JsonObject body) {
|
||||
public CompletableFuture<NetworkResponse> postData(String endpoint, JsonObject body) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
@@ -83,7 +84,9 @@ public class HttpNetworkService implements NetworkServiceInterface {
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
return response.body();
|
||||
|
||||
NetworkResponse resp = new NetworkResponse(response.statusCode(), response.body());
|
||||
return resp;
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("HTTP POST failed: " + endpoint, e);
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package online.mineroo.common;
|
||||
|
||||
public class NetworkResponse {
|
||||
private final int statusCode;
|
||||
private final String body;
|
||||
|
||||
public NetworkResponse(int statusCode, String body) {
|
||||
this.statusCode = statusCode;
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public int getStatusCode() {
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import java.util.concurrent.CompletableFuture;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public interface NetworkServiceInterface {
|
||||
CompletableFuture<String> getData(String endpoint, Map<String, String> params);
|
||||
CompletableFuture<NetworkResponse> getData(String endpoint, Map<String, String> params);
|
||||
|
||||
CompletableFuture<String> postData(String endpoint, JsonObject body);
|
||||
CompletableFuture<NetworkResponse> postData(String endpoint, JsonObject body);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ public class ProtocolConstants {
|
||||
// Protocol channel name
|
||||
public static String PROTOCOL_CHANNEL = "mineroo:plugin:api";
|
||||
|
||||
// Event names
|
||||
public static String BIND_USER_REQUEST = "user-bind-request";
|
||||
// Sub-channel/event names
|
||||
public static String API_REQUEST = "API_REQUEST";
|
||||
public static String BIND_MOTD_TOKEN = "SET_MOTD_TOKEN";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user