2026-09-06 18:22:00 +03:00
|
|
|
package online.astradev.packs;
|
|
|
|
|
|
2026-09-06 18:36:18 +03:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.InetSocketAddress;
|
2026-09-06 19:32:43 +03:00
|
|
|
import java.net.URI;
|
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2026-09-06 18:36:18 +03:00
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2026-09-06 19:32:43 +03:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
import java.util.concurrent.Executors;
|
2026-09-06 18:36:18 +03:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
2026-09-06 19:32:43 +03:00
|
|
|
import com.sun.net.httpserver.HttpExchange;
|
|
|
|
|
import com.sun.net.httpserver.HttpServer;
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 19:32:43 +03:00
|
|
|
/** Serves only published archives, with expiring links and bounded downloads. */
|
2026-09-06 18:22:00 +03:00
|
|
|
public final class PackHost implements AutoCloseable {
|
|
|
|
|
private record Grant(String hash, long expires) {}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
private final PackConfig.Hosting config;
|
2026-09-06 19:32:43 +03:00
|
|
|
private final Map<String, Path> published = new ConcurrentHashMap<>();
|
|
|
|
|
private final Map<String, Grant> grants = new ConcurrentHashMap<>();
|
2026-09-06 18:36:18 +03:00
|
|
|
private final Map<String, Integer> downloadsByIp = new HashMap<>();
|
2026-09-06 19:32:43 +03:00
|
|
|
private final ExecutorService workers = Executors.newVirtualThreadPerTaskExecutor();
|
|
|
|
|
private HttpServer server;
|
2026-09-06 18:36:18 +03:00
|
|
|
private int activeDownloads;
|
2026-09-06 18:28:23 +03:00
|
|
|
|
|
|
|
|
public PackHost(PackConfig.Hosting config) {
|
|
|
|
|
this.config = config;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-06 19:32:43 +03:00
|
|
|
public void start() throws IOException {
|
|
|
|
|
server = HttpServer.create(new InetSocketAddress(config.bindAddress, config.port), 0);
|
|
|
|
|
server.createContext("/", this::handle);
|
|
|
|
|
server.setExecutor(workers);
|
|
|
|
|
server.start();
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-06 18:28:23 +03:00
|
|
|
public void publish(PackBuilder.Pack pack) {
|
|
|
|
|
published.put(pack.sha1(), pack.path());
|
2026-09-06 19:20:55 +03:00
|
|
|
published.entrySet().removeIf(entry -> !Files.isRegularFile(entry.getValue()));
|
|
|
|
|
grants.entrySet().removeIf(entry -> !published.containsKey(entry.getValue().hash()));
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public String issuePath(PackBuilder.Pack pack) {
|
|
|
|
|
String path = "/astrapacks/" + pack.sha1() + ".zip";
|
2026-09-06 18:36:18 +03:00
|
|
|
if (!config.blockNonGameRequests) {
|
|
|
|
|
return path;
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
long now = System.currentTimeMillis();
|
2026-09-06 18:36:18 +03:00
|
|
|
grants.entrySet().removeIf(entry -> entry.getValue().expires() < now);
|
|
|
|
|
if (grants.size() >= 10000) {
|
2026-09-06 18:28:23 +03:00
|
|
|
throw new IllegalStateException("Too many resource-pack download grants");
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
String token = UUID.randomUUID().toString();
|
2026-09-06 19:32:43 +03:00
|
|
|
grants.put(token, new Grant(pack.sha1(), now + config.tokenLifetimeSeconds * 1000L));
|
2026-09-06 18:22:00 +03:00
|
|
|
return path + "?token=" + token;
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 19:32:43 +03:00
|
|
|
private void handle(HttpExchange exchange) throws IOException {
|
|
|
|
|
try (exchange) {
|
|
|
|
|
exchange.getResponseHeaders().set("Connection", "close");
|
|
|
|
|
String method = exchange.getRequestMethod();
|
|
|
|
|
if (!method.equals("GET") && !method.equals("HEAD")) {
|
|
|
|
|
exchange.sendResponseHeaders(405, -1);
|
2026-09-06 18:28:23 +03:00
|
|
|
return;
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 19:32:43 +03:00
|
|
|
String path = exchange.getRequestURI().getPath();
|
2026-09-06 18:28:23 +03:00
|
|
|
if (!path.matches("/astrapacks/[a-f0-9]{40}\\.zip")) {
|
2026-09-06 19:32:43 +03:00
|
|
|
exchange.sendResponseHeaders(404, -1);
|
2026-09-06 18:28:23 +03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String hash = path.substring("/astrapacks/".length(), path.length() - ".zip".length());
|
2026-09-06 18:22:00 +03:00
|
|
|
Path file = published.get(hash);
|
2026-09-06 18:28:23 +03:00
|
|
|
if (file == null || !Files.isRegularFile(file)) {
|
2026-09-06 19:32:43 +03:00
|
|
|
exchange.sendResponseHeaders(404, -1);
|
2026-09-06 18:28:23 +03:00
|
|
|
return;
|
|
|
|
|
}
|
2026-09-06 19:32:43 +03:00
|
|
|
if (!authorized(exchange.getRequestURI(), hash)) {
|
|
|
|
|
exchange.sendResponseHeaders(403, -1);
|
2026-09-06 18:28:23 +03:00
|
|
|
return;
|
|
|
|
|
}
|
2026-09-06 18:36:18 +03:00
|
|
|
|
2026-09-06 19:32:43 +03:00
|
|
|
String ip = exchange.getRemoteAddress().getAddress().getHostAddress();
|
2026-09-06 18:36:18 +03:00
|
|
|
if (!acquireDownload(ip)) {
|
2026-09-06 19:32:43 +03:00
|
|
|
exchange.sendResponseHeaders(429, -1);
|
2026-09-06 18:36:18 +03:00
|
|
|
return;
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 19:32:43 +03:00
|
|
|
try {
|
|
|
|
|
sendFile(exchange, file, hash);
|
|
|
|
|
} finally {
|
|
|
|
|
releaseDownload(ip);
|
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
Thread.currentThread().interrupt();
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-06 19:32:43 +03:00
|
|
|
private boolean authorized(URI uri, String hash) {
|
|
|
|
|
if (!config.blockNonGameRequests) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
String query = uri.getRawQuery();
|
|
|
|
|
if (query == null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
for (String parameter : query.split("&")) {
|
|
|
|
|
if (parameter.startsWith("token=")) {
|
|
|
|
|
try {
|
|
|
|
|
String token = URLDecoder.decode(parameter.substring(6), StandardCharsets.UTF_8);
|
|
|
|
|
Grant grant = grants.get(token);
|
|
|
|
|
return grant != null && grant.hash().equals(hash) && grant.expires() >= System.currentTimeMillis();
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
2026-09-06 19:32:43 +03:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-09-06 18:36:18 +03:00
|
|
|
|
2026-09-06 19:32:43 +03:00
|
|
|
private void sendFile(HttpExchange exchange, Path file, String hash) throws IOException, InterruptedException {
|
|
|
|
|
var headers = exchange.getResponseHeaders();
|
|
|
|
|
headers.set("Content-Type", "application/zip");
|
|
|
|
|
headers.set("ETag", '"' + hash + '"');
|
|
|
|
|
headers.set("Cache-Control", "private, max-age=31536000, immutable");
|
|
|
|
|
long size = Files.size(file);
|
|
|
|
|
if (exchange.getRequestMethod().equals("HEAD")) {
|
|
|
|
|
headers.set("Content-Length", Long.toString(size));
|
|
|
|
|
exchange.sendResponseHeaders(200, -1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try (var input = Files.newInputStream(file)) {
|
|
|
|
|
exchange.sendResponseHeaders(200, size);
|
|
|
|
|
var output = exchange.getResponseBody();
|
|
|
|
|
byte[] buffer = new byte[65536];
|
|
|
|
|
long transferred = 0;
|
|
|
|
|
long started = System.nanoTime();
|
|
|
|
|
long bytesPerSecond = config.maxSpeedMiBPerSecond * 1024L * 1024L;
|
|
|
|
|
int read;
|
|
|
|
|
while ((read = input.read(buffer)) != -1) {
|
|
|
|
|
transferred += read;
|
|
|
|
|
long delay = transferred * 1_000_000_000L / bytesPerSecond - (System.nanoTime() - started);
|
|
|
|
|
if (delay > 0) {
|
|
|
|
|
TimeUnit.NANOSECONDS.sleep(delay);
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
2026-09-06 19:32:43 +03:00
|
|
|
output.write(buffer, 0, read);
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 19:32:43 +03:00
|
|
|
private synchronized boolean acquireDownload(String ip) {
|
|
|
|
|
int count = downloadsByIp.getOrDefault(ip, 0);
|
|
|
|
|
if (count >= config.maxDownloadsPerIp || activeDownloads >= config.maxConcurrentDownloads) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
downloadsByIp.put(ip, count + 1);
|
|
|
|
|
activeDownloads++;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private synchronized void releaseDownload(String ip) {
|
|
|
|
|
int remaining = downloadsByIp.get(ip) - 1;
|
|
|
|
|
if (remaining == 0) {
|
|
|
|
|
downloadsByIp.remove(ip);
|
|
|
|
|
} else {
|
|
|
|
|
downloadsByIp.put(ip, remaining);
|
|
|
|
|
}
|
|
|
|
|
activeDownloads--;
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void close() {
|
2026-09-06 19:32:43 +03:00
|
|
|
if (server != null) {
|
|
|
|
|
server.stop(0);
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
2026-09-06 19:32:43 +03:00
|
|
|
workers.shutdownNow();
|
2026-09-06 18:22:00 +03:00
|
|
|
grants.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|