From fc04f65875dbdb8b9d07333b2e0a6fe8dbc7107a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20Kr=C3=BCger?= Date: Wed, 30 Oct 2024 22:50:07 +0100 Subject: [PATCH] feat: add HttpInjector#isRelevant - style: reformat some classes - doc: improve some jd comments - fix: HttpRequest#parse throwing Exception instead of IOException - feat: actual error handling of HttpRequest#parse --- .gitignore | 3 +- README.md | 5 -- .../mcbrawls/inject/api/InjectPlatform.java | 1 + .../net/mcbrawls/inject/api/Injector.java | 16 +++--- .../mcbrawls/inject/api/InjectorContext.java | 6 ++- .../mcbrawls/inject/api/http/HttpByteBuf.java | 2 + .../inject/api/http/HttpInjector.java | 50 ++++++++++++++----- .../mcbrawls/inject/api/http/HttpRequest.java | 7 +-- .../fabric/mixin/ClientConnectionMixin.java | 2 + gradle.properties | 2 +- .../paper/example/InjectPaperExample.java | 6 --- 11 files changed, 62 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index 87014de..6508aa6 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,5 @@ replay_*.log *.hprof *.jfr -**/run*/ \ No newline at end of file +**/run*/ +.kotlin/ \ No newline at end of file diff --git a/README.md b/README.md index 36055fa..5aa8405 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,6 @@ server. ```java class MyEpicHttpInjector extends HttpInjector { - @Override - public boolean isRelevant(InjectorContext ctx, HttpRequest request) { - return true; - } - @Override public HttpByteBuf intercept(ChannelHandlerContext ctx, HttpRequest request) { HttpByteBuf buf = HttpByteBuf.httpBuf(ctx); diff --git a/api/src/main/java/net/mcbrawls/inject/api/InjectPlatform.java b/api/src/main/java/net/mcbrawls/inject/api/InjectPlatform.java index bf0e67e..7a93516 100644 --- a/api/src/main/java/net/mcbrawls/inject/api/InjectPlatform.java +++ b/api/src/main/java/net/mcbrawls/inject/api/InjectPlatform.java @@ -6,6 +6,7 @@ public interface InjectPlatform { /** * Registers the injector. + * * @param injector The injector. */ void registerInjector(Injector injector); diff --git a/api/src/main/java/net/mcbrawls/inject/api/Injector.java b/api/src/main/java/net/mcbrawls/inject/api/Injector.java index d95cbb6..8c8d434 100644 --- a/api/src/main/java/net/mcbrawls/inject/api/Injector.java +++ b/api/src/main/java/net/mcbrawls/inject/api/Injector.java @@ -12,11 +12,11 @@ @Sharable public abstract class Injector extends ChannelDuplexHandler { /** - * Predicate for matching if this injector is relevant - * to the given context. - * @param ctx The context. - * @param direction The direction in which this packet goes. - * @return true if it's relevant and should be handled by this injector, false if not. + * Predicate for matching if this injector is relevant to the given context. + * + * @param ctx the context. + * @param direction the direction in which this packet goes. + * @return true if it is relevant and should be handled by this injector, false if not. */ public boolean isRelevant(InjectorContext ctx, PacketDirection direction) { return false; @@ -24,6 +24,7 @@ public boolean isRelevant(InjectorContext ctx, PacketDirection direction) { /** * Gets executed on every channel read. + * * @param ctx The context. * @param buf The read byte buffer. * @return true if the channel read was handled and should not get @@ -35,8 +36,9 @@ public boolean onRead(ChannelHandlerContext ctx, ByteBuf buf) throws Exception { /** * Gets executed on every channel write. - * @param ctx The context. - * @param buf The written byte buffer. + * + * @param ctx The context. + * @param buf The written byte buffer. * @param promise The mutable channel future. * @return true if the channel write was handled and should not get * delegated to the superclass, false if it should be delegated to the superclass. diff --git a/api/src/main/java/net/mcbrawls/inject/api/InjectorContext.java b/api/src/main/java/net/mcbrawls/inject/api/InjectorContext.java index 2db8cf1..479e0d0 100644 --- a/api/src/main/java/net/mcbrawls/inject/api/InjectorContext.java +++ b/api/src/main/java/net/mcbrawls/inject/api/InjectorContext.java @@ -5,7 +5,9 @@ /** * Context of an injector. + * * @param pipeline The channel pipeline. - * @param message The read byte buffer. + * @param message The read byte buffer. */ -public record InjectorContext(ChannelPipeline pipeline, ByteBuf message) {} \ No newline at end of file +public record InjectorContext(ChannelPipeline pipeline, ByteBuf message) { +} \ No newline at end of file diff --git a/api/src/main/java/net/mcbrawls/inject/api/http/HttpByteBuf.java b/api/src/main/java/net/mcbrawls/inject/api/http/HttpByteBuf.java index 88e5174..2d40cc0 100644 --- a/api/src/main/java/net/mcbrawls/inject/api/http/HttpByteBuf.java +++ b/api/src/main/java/net/mcbrawls/inject/api/http/HttpByteBuf.java @@ -2,6 +2,7 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; + import java.nio.charset.StandardCharsets; /** @@ -54,6 +55,7 @@ public ByteBuf inner() { /** * Creates a new {@link HttpByteBuf} based off a {@link ChannelHandlerContext}'s allocator. + * * @param ctx the context * @return the HTTP byte buf */ diff --git a/api/src/main/java/net/mcbrawls/inject/api/http/HttpInjector.java b/api/src/main/java/net/mcbrawls/inject/api/http/HttpInjector.java index b1708fa..fca6b5c 100644 --- a/api/src/main/java/net/mcbrawls/inject/api/http/HttpInjector.java +++ b/api/src/main/java/net/mcbrawls/inject/api/http/HttpInjector.java @@ -10,15 +10,39 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; + @Sharable public abstract class HttpInjector extends Injector { private final Logger logger = LoggerFactory.getLogger("HttpInjector " + hashCode()); public abstract HttpByteBuf intercept(ChannelHandlerContext ctx, HttpRequest request); + /** + * Predicate for matching if this injector is relevant to the given HTTP request. + * Should only need to be overridden once you are dealing with multiple HTTP injectors. + * + * @param ctx the context. + * @param request the HTTP request. + * @return true if it is relevant and should be handled by this injector, false if not. + */ + public boolean isRelevant(InjectorContext ctx, HttpRequest request) { + return true; + } + @Override - public boolean isRelevant(InjectorContext ctx, PacketDirection direction) { - return isRequestGet(ctx.message()); + public final boolean isRelevant(InjectorContext ctx, PacketDirection direction) { + if (!isRequestGet(ctx.message())) return false; + + HttpRequest request; + try { + request = HttpRequest.parse(ctx.message()); + } catch (IOException exception) { + logger.error("failed parsing HTTP request: {}", exception.getMessage()); + return false; + } + + return isRelevant(ctx, request); } @Override @@ -27,17 +51,17 @@ public boolean onRead(ChannelHandlerContext ctx, ByteBuf buf) throws Exception { HttpByteBuf response = intercept(ctx, request); ctx.writeAndFlush(response.inner()) - .addListener(ChannelFutureListener.CLOSE) - .addListener(future -> { - Throwable cause = future.cause(); - if (cause == null) { - logger.debug("Write successful"); - } else { - logger.error("Write failed: {}", String.valueOf(cause)); - //noinspection CallToPrintStackTrace - cause.printStackTrace(); - } - }); + .addListener(ChannelFutureListener.CLOSE) + .addListener(future -> { + Throwable cause = future.cause(); + if (cause == null) { + logger.debug("Write successful"); + } else { + logger.error("Write failed: {}", String.valueOf(cause)); + //noinspection CallToPrintStackTrace + cause.printStackTrace(); + } + }); return true; } diff --git a/api/src/main/java/net/mcbrawls/inject/api/http/HttpRequest.java b/api/src/main/java/net/mcbrawls/inject/api/http/HttpRequest.java index 054e83c..113c821 100644 --- a/api/src/main/java/net/mcbrawls/inject/api/http/HttpRequest.java +++ b/api/src/main/java/net/mcbrawls/inject/api/http/HttpRequest.java @@ -4,6 +4,7 @@ import io.netty.buffer.ByteBufInputStream; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; @@ -36,13 +37,13 @@ public String getHeader(String header) { return headers.get(header); } - public static HttpRequest parse(ByteBuf buf) throws Exception { + public static HttpRequest parse(ByteBuf buf) throws IOException { try (ByteBufInputStream stream = new ByteBufInputStream(buf)) { return parse(stream); } } - private static HttpRequest parse(InputStream stream) throws Exception { + private static HttpRequest parse(InputStream stream) throws IOException { try (InputStreamReader reader = new InputStreamReader(stream); BufferedReader bufferedReader = new BufferedReader(reader)) { String request = bufferedReader.readLine(); @@ -51,7 +52,7 @@ private static HttpRequest parse(InputStream stream) throws Exception { } } - private static Map readHeaders(BufferedReader reader) throws Exception { + private static Map readHeaders(BufferedReader reader) throws IOException { Map headers = new HashMap<>(); String header = reader.readLine(); while (header != null && !header.isEmpty()) { diff --git a/fabric/src/main/java/net/mcbrawls/inject/fabric/mixin/ClientConnectionMixin.java b/fabric/src/main/java/net/mcbrawls/inject/fabric/mixin/ClientConnectionMixin.java index 9be566f..0677067 100644 --- a/fabric/src/main/java/net/mcbrawls/inject/fabric/mixin/ClientConnectionMixin.java +++ b/fabric/src/main/java/net/mcbrawls/inject/fabric/mixin/ClientConnectionMixin.java @@ -5,11 +5,13 @@ import net.minecraft.network.ClientConnection; import net.minecraft.network.NetworkSide; import net.minecraft.network.handler.PacketSizeLogger; +import org.spongepowered.asm.mixin.Debug; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +@Debug(export = true) @Mixin(ClientConnection.class) public class ClientConnectionMixin { @Inject(method = "addHandlers", at = @At("TAIL")) diff --git a/gradle.properties b/gradle.properties index ee26635..12b56e6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ yarn_mappings=1.21.1+build.3 loader_version=0.16.7 # Mod Properties -version=2.0.1 +version=2.0.2 group=net.mcbrawls.inject id=inject diff --git a/paper/src/example/java/net/mcbrawls/inject/paper/example/InjectPaperExample.java b/paper/src/example/java/net/mcbrawls/inject/paper/example/InjectPaperExample.java index 99bbaea..f5d03c1 100644 --- a/paper/src/example/java/net/mcbrawls/inject/paper/example/InjectPaperExample.java +++ b/paper/src/example/java/net/mcbrawls/inject/paper/example/InjectPaperExample.java @@ -1,7 +1,6 @@ package net.mcbrawls.inject.paper.example; import io.netty.channel.ChannelHandlerContext; -import net.mcbrawls.inject.api.InjectorContext; import net.mcbrawls.inject.api.http.HttpByteBuf; import net.mcbrawls.inject.api.http.HttpInjector; import net.mcbrawls.inject.api.http.HttpRequest; @@ -10,11 +9,6 @@ public class InjectPaperExample extends JavaPlugin { static class MyEpicHttpInjector extends HttpInjector { - @Override - public boolean isRelevant(InjectorContext ctx, HttpRequest request) { - return true; - } - @Override public HttpByteBuf intercept(ChannelHandlerContext ctx, HttpRequest request) { HttpByteBuf buf = HttpByteBuf.httpBuf(ctx);