9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2026-01-06 15:52:03 +00:00

添加token选项

This commit is contained in:
XiaoMoMi
2025-04-18 21:09:03 +08:00
parent 9b5d20f212
commit fa875b6946
2 changed files with 15 additions and 6 deletions

View File

@@ -67,6 +67,7 @@ public class SelfHost implements ResourcePackHost {
if (port < 0 || port > 65535) {
throw new IllegalArgumentException("Illegal port: '" + port + "' for self host");
}
boolean oneTimeToken = (boolean) arguments.getOrDefault("one-time-token", true);
String protocol = (String) arguments.getOrDefault("protocol", "http");
boolean denyNonMinecraftRequest = (boolean) arguments.getOrDefault("deny-non-minecraft-request", true);
Map<String, Object> rateMap = MiscUtils.castToMap(arguments.get("rate-map"), true);
@@ -76,7 +77,7 @@ public class SelfHost implements ResourcePackHost {
maxRequests = (int) rateMap.getOrDefault("max-requests", 5);
resetInterval = (int) rateMap.getOrDefault("reset-interval", 20) * 1000;
}
selfHostHttpServer.updateProperties(ip, port, denyNonMinecraftRequest, protocol, maxRequests, resetInterval);
selfHostHttpServer.updateProperties(ip, port, denyNonMinecraftRequest, protocol, maxRequests, resetInterval, oneTimeToken);
return INSTANCE;
}
}

View File

@@ -49,6 +49,7 @@ public class SelfHostHttpServer {
private int port = -1;
private String protocol = "http";
private boolean denyNonMinecraft = true;
private boolean useToken;
private volatile byte[] resourcePackBytes;
private String packHash;
@@ -59,12 +60,14 @@ public class SelfHostHttpServer {
boolean denyNonMinecraft,
String protocol,
int maxRequests,
int resetInternal) {
int resetInternal,
boolean token) {
this.ip = ip;
this.denyNonMinecraft = denyNonMinecraft;
this.protocol = protocol;
this.rateLimit = maxRequests;
this.rateLimitInterval = resetInternal;
this.useToken = token;
if (port <= 0 || port > 65535) {
throw new IllegalArgumentException("Invalid port number: " + port);
}
@@ -96,6 +99,9 @@ public class SelfHostHttpServer {
if (this.resourcePackBytes == null) {
return null;
}
if (!this.useToken) {
return new ResourcePackDownloadData(url(), packUUID, packHash);
}
String token = UUID.randomUUID().toString();
this.oneTimePackUrls.put(token, true);
return new ResourcePackDownloadData(
@@ -172,10 +178,12 @@ public class SelfHostHttpServer {
handleBlockedRequest(exchange, 429, "Rate limit exceeded");
return;
}
String token = parseToken(exchange);
if (!validateToken(token)) {
handleBlockedRequest(exchange, 403, "Invalid token");
return;
if (useToken) {
String token = parseToken(exchange);
if (!validateToken(token)) {
handleBlockedRequest(exchange, 403, "Invalid token");
return;
}
}
if (!validateClient(exchange)) {
handleBlockedRequest(exchange, 403, "Invalid client");