This commit is contained in:
1 parent
4c7f145f97
commit
b1d44fe961
2 files changed
+39
No files matched your search
@@ -25,6 +25,8 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
@@ -198,6 +200,23 @@ public final class AsminDiscordBridge extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleStatusRequest(HttpExchange exchange) throws IOException {
|
private void handleStatusRequest(HttpExchange exchange) throws IOException {
|
||||||
|
String origin = exchange.getRequestHeaders().getFirst("Origin");
|
||||||
|
String allowedOrigin = getAllowedOrigin(origin);
|
||||||
|
if (allowedOrigin != null) {
|
||||||
|
exchange.getResponseHeaders().set("Access-Control-Allow-Origin", allowedOrigin);
|
||||||
|
exchange.getResponseHeaders().set("Vary", "Origin");
|
||||||
|
}
|
||||||
|
if ("OPTIONS".equalsIgnoreCase(exchange.getRequestMethod())) {
|
||||||
|
if (allowedOrigin == null) {
|
||||||
|
exchange.sendResponseHeaders(403, -1);
|
||||||
|
} else {
|
||||||
|
exchange.getResponseHeaders().set("Access-Control-Allow-Methods", "GET, OPTIONS");
|
||||||
|
exchange.getResponseHeaders().set("Access-Control-Allow-Headers", "Content-Type");
|
||||||
|
exchange.sendResponseHeaders(204, -1);
|
||||||
|
}
|
||||||
|
exchange.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!"GET".equalsIgnoreCase(exchange.getRequestMethod()) || !"/status".equals(exchange.getRequestURI().getPath())) {
|
if (!"GET".equalsIgnoreCase(exchange.getRequestMethod()) || !"/status".equals(exchange.getRequestURI().getPath())) {
|
||||||
exchange.getResponseHeaders().set("Allow", "GET");
|
exchange.getResponseHeaders().set("Allow", "GET");
|
||||||
exchange.sendResponseHeaders(405, -1);
|
exchange.sendResponseHeaders(405, -1);
|
||||||
@@ -211,6 +230,24 @@ public final class AsminDiscordBridge extends JavaPlugin {
|
|||||||
try (OutputStream output = exchange.getResponseBody()) { output.write(body); }
|
try (OutputStream output = exchange.getResponseBody()) { output.write(body); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getAllowedOrigin(String origin) {
|
||||||
|
if (origin == null || origin.isBlank()) return null;
|
||||||
|
try {
|
||||||
|
URI uri = new URI(origin);
|
||||||
|
String host = uri.getHost();
|
||||||
|
if (host == null || !("http".equalsIgnoreCase(uri.getScheme()) || "https".equalsIgnoreCase(uri.getScheme()))) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
for (String domain : getConfig().getStringList("web.alloweddomains")) {
|
||||||
|
String allowed = domain == null ? "" : domain.trim().toLowerCase();
|
||||||
|
if (!allowed.isEmpty() && host.equals(allowed)) return origin;
|
||||||
|
}
|
||||||
|
} catch (URISyntaxException ignored) {
|
||||||
|
// Niepoprawny Origin nie dostaje nagłówków CORS.
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private void updateWebStatus(boolean online) {
|
private void updateWebStatus(boolean online) {
|
||||||
if (webServer == null) return;
|
if (webServer == null) return;
|
||||||
List<String> names = online ? getServer().getOnlinePlayers().stream()
|
List<String> names = online ? getServer().getOnlinePlayers().stream()
|
||||||
|
|||||||
@@ -12,3 +12,5 @@ web:
|
|||||||
enabled: true
|
enabled: true
|
||||||
host: "0.0.0.0"
|
host: "0.0.0.0"
|
||||||
port: 25568
|
port: 25568
|
||||||
|
alloweddomains:
|
||||||
|
- "classicmc.top"
|
||||||
Reference in new issue
Block a user