Build shared resource folder with two-version retention and dedicated HTTP hosting
This commit is contained in:
@@ -2,10 +2,8 @@ package online.astradev.packs;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -15,7 +13,6 @@ import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
@@ -28,7 +25,6 @@ import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.channel.nio.NioIoHandler;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
import io.netty.handler.codec.http.DefaultFullHttpResponse;
|
||||
import io.netty.handler.codec.http.DefaultHttpResponse;
|
||||
import io.netty.handler.codec.http.FullHttpRequest;
|
||||
@@ -66,9 +62,6 @@ public final class PackHost implements AutoCloseable {
|
||||
}
|
||||
|
||||
public void start() throws InterruptedException {
|
||||
if (!config.mode.equals("self_host")) {
|
||||
return;
|
||||
}
|
||||
eventLoops = new MultiThreadIoEventLoopGroup(2, NioIoHandler.newFactory());
|
||||
ServerBootstrap bootstrap = new ServerBootstrap()
|
||||
.group(eventLoops)
|
||||
@@ -89,6 +82,8 @@ public final class PackHost implements AutoCloseable {
|
||||
|
||||
public void publish(PackBuilder.Pack pack) {
|
||||
published.put(pack.sha1(), pack.path());
|
||||
published.entrySet().removeIf(entry -> !Files.isRegularFile(entry.getValue()));
|
||||
grants.entrySet().removeIf(entry -> !published.containsKey(entry.getValue().hash()));
|
||||
}
|
||||
|
||||
public String issuePath(PackBuilder.Pack pack) {
|
||||
@@ -137,10 +132,6 @@ public final class PackHost implements AutoCloseable {
|
||||
activeDownloads--;
|
||||
}
|
||||
|
||||
public void installSniffer(ChannelPipeline pipeline) {
|
||||
pipeline.addFirst("astrapacks_detect", new Sniffer());
|
||||
}
|
||||
|
||||
private void installHttp(ChannelPipeline pipeline) {
|
||||
long bytesPerSecond = config.maxSpeedMiBPerSecond * 1024L * 1024L;
|
||||
pipeline.addLast("astrapacks_timeout", new ReadTimeoutHandler(30));
|
||||
@@ -151,27 +142,6 @@ public final class PackHost implements AutoCloseable {
|
||||
pipeline.addLast("astrapacks_download", new Download());
|
||||
}
|
||||
|
||||
private final class Sniffer extends ByteToMessageDecoder {
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext context, ByteBuf input, List<Object> output) {
|
||||
if (input.readableBytes() < 4) {
|
||||
return;
|
||||
}
|
||||
String prefix = input.toString(input.readerIndex(), 4, StandardCharsets.US_ASCII);
|
||||
ChannelPipeline pipeline = context.pipeline();
|
||||
if (prefix.equals("GET ") || prefix.equals("HEAD")) {
|
||||
for (String name : new ArrayList<>(pipeline.names())) {
|
||||
if (pipeline.get(name) != null && pipeline.get(name) != this) {
|
||||
pipeline.remove(name);
|
||||
}
|
||||
}
|
||||
installHttp(pipeline);
|
||||
}
|
||||
// Removing the decoder forwards the buffered bytes to HTTP or Minecraft unchanged.
|
||||
pipeline.remove(this);
|
||||
}
|
||||
}
|
||||
|
||||
private final class Download extends SimpleChannelInboundHandler<FullHttpRequest> {
|
||||
private boolean handled;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user