Skip to content

Commit

Permalink
feat: add HttpInjector#isRelevant
Browse files Browse the repository at this point in the history
- style: reformat some classes
- doc: improve some jd comments
- fix: HttpRequest#parse throwing Exception instead of IOException
- feat: actual error handling of HttpRequest#parse
  • Loading branch information
radstevee committed Oct 30, 2024
1 parent 0a97786 commit fc04f65
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ replay_*.log
*.hprof
*.jfr

**/run*/
**/run*/
.kotlin/
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public interface InjectPlatform {
/**
* Registers the injector.
*
* @param injector The injector.
*/
void registerInjector(Injector injector);
Expand Down
16 changes: 9 additions & 7 deletions api/src/main/java/net/mcbrawls/inject/api/Injector.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
@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;
}

/**
* 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
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
public record InjectorContext(ChannelPipeline pipeline, ByteBuf message) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;

import java.nio.charset.StandardCharsets;

/**
Expand Down Expand Up @@ -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
*/
Expand Down
50 changes: 37 additions & 13 deletions api/src/main/java/net/mcbrawls/inject/api/http/HttpInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -51,7 +52,7 @@ private static HttpRequest parse(InputStream stream) throws Exception {
}
}

private static Map<String, String> readHeaders(BufferedReader reader) throws Exception {
private static Map<String, String> readHeaders(BufferedReader reader) throws IOException {
Map<String, String> headers = new HashMap<>();
String header = reader.readLine();
while (header != null && !header.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit fc04f65

Please sign in to comment.