Restore RemotePackSync plugin identity
Build and publish release / Build and publish RemotePackSync (push) Successful in 51s
Build and publish release / Build and publish RemotePackSync (push) Successful in 51s
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package dev.lexian.remotepacksync;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.util.HexFormat;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/** Downloads and validates a SHA-1 checksum without downloading the resource pack itself. */
|
||||
public final class RemoteSha1Fetcher {
|
||||
private static final Pattern SHA1_PATTERN = Pattern.compile("(?i)(?<![0-9a-f])[0-9a-f]{40}(?![0-9a-f])");
|
||||
private static final Duration REQUEST_TIMEOUT = Duration.ofSeconds(15);
|
||||
private static final int MAX_RESPONSE_BYTES = 64 * 1024;
|
||||
|
||||
private final HttpClient client = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(10))
|
||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.build();
|
||||
|
||||
public byte[] fetch(URI sha1Url) throws IOException, InterruptedException {
|
||||
HttpRequest request = HttpRequest.newBuilder(sha1Url)
|
||||
.GET()
|
||||
.timeout(REQUEST_TIMEOUT)
|
||||
.header("Accept", "text/plain, text/*;q=0.9, */*;q=0.1")
|
||||
.header("User-Agent", "RemotePackSync/v1.0.0")
|
||||
.build();
|
||||
|
||||
HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
throw new IOException("SHA-1 endpoint returned HTTP " + response.statusCode());
|
||||
}
|
||||
if (response.body().length > MAX_RESPONSE_BYTES) {
|
||||
throw new IOException("SHA-1 response exceeds " + MAX_RESPONSE_BYTES + " bytes");
|
||||
}
|
||||
|
||||
String responseText = new String(response.body(), java.nio.charset.StandardCharsets.UTF_8);
|
||||
Matcher matcher = SHA1_PATTERN.matcher(responseText);
|
||||
if (!matcher.find()) {
|
||||
throw new IOException("SHA-1 response does not contain a 40-character hexadecimal SHA-1");
|
||||
}
|
||||
return HexFormat.of().parseHex(matcher.group());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user