Skip to content

Commit

Permalink
call listener on packet errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Oct 4, 2023
1 parent bae7bd7 commit 747391a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/zenith/Proxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public synchronized void connect() {
if (CONFIG.client.viaversion.autoProtocolVersion)
updateViaProtocolVersion();
ChannelInitializer<Channel> originalChannelInitializer = this.client.buildChannelInitializer();
final MCProxyViaServerProxy viaProxy = new MCProxyViaServerProxy();
final MCProxyViaServerProxy viaProxy = new MCProxyViaServerProxy(this.client);
viaProxy.init();
ChannelInitializer<Channel> viaChannelInitializer = viaProxy.inject(originalChannelInitializer);
Bootstrap bootstrap = this.client.buildBootstrap(viaChannelInitializer);
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/zenith/via/MCProxyViaServerProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.viaversion.viaversion.libs.gson.JsonObject;
import com.viaversion.viaversion.util.VersionInfo;
import com.viaversion.viaversion.velocity.util.LoggerWrapper;
import com.zenith.network.client.ClientSession;
import com.zenith.via.handler.MCProxyViaChannelInitializer;
import com.zenith.via.platform.*;
import io.netty.channel.Channel;
Expand All @@ -33,6 +34,11 @@ public class MCProxyViaServerProxy implements ViaServerProxyPlatform<MinecraftPr
private final MCProxyViaConfig config = new MCProxyViaConfig(Paths.get("via").resolve("via-config.yml").toFile());
private final MCProxyViaBackwardsPlatform backwardsPlatform = new MCProxyViaBackwardsPlatform();
private java.util.logging.Logger logger = new LoggerWrapper(LoggerFactory.getLogger("ViaVersion"));
private ClientSession client;

public MCProxyViaServerProxy(final ClientSession client) {
this.client = client;
}

public void init() {
config.reloadConfig();
Expand Down Expand Up @@ -60,7 +66,7 @@ public void init() {
}

public ChannelInitializer<Channel> inject(final ChannelInitializer<Channel> original) {
return new MCProxyViaChannelInitializer(original);
return new MCProxyViaChannelInitializer(original, this.client);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.viaversion.viaversion.connection.UserConnectionImpl;
import com.viaversion.viaversion.protocol.ProtocolPipelineImpl;
import com.zenith.network.client.ClientSession;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import lombok.NonNull;
Expand All @@ -21,9 +22,11 @@ public class MCProxyViaChannelInitializer extends ChannelInitializer<Channel> {
}

private final ChannelInitializer<Channel> original;
private final ClientSession client;

public MCProxyViaChannelInitializer(ChannelInitializer<Channel> original) {
public MCProxyViaChannelInitializer(ChannelInitializer<Channel> original, final ClientSession client) {
this.original = original;
this.client = client;
}

@Override
Expand All @@ -39,7 +42,7 @@ protected void initChannel(@NonNull Channel channel) throws Exception {

// pipeline order before readTimeout -> encryption -> sizer -> compression -> codec -> manager
// pipeline order after readTimeout -> encryption -> sizer -> compression -> via-encoder -> via-decoder -> codec -> manager
channel.pipeline().addBefore("codec", "via-encoder", new MCProxyViaEncodeHandler(userConnection));
channel.pipeline().addBefore("codec", "via-decoder", new MCProxyViaDecodeHandler(userConnection));
channel.pipeline().addBefore("codec", "via-encoder", new MCProxyViaEncodeHandler(userConnection, this.client));
channel.pipeline().addBefore("codec", "via-decoder", new MCProxyViaDecodeHandler(userConnection, this.client));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.exception.CancelCodecException;
import com.viaversion.viaversion.exception.CancelDecoderException;
import com.zenith.network.client.ClientSession;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
Expand All @@ -13,9 +14,11 @@
@ChannelHandler.Sharable
public class MCProxyViaDecodeHandler extends MessageToMessageDecoder<ByteBuf> {
private final UserConnection info;
private final ClientSession client;

public MCProxyViaDecodeHandler(UserConnection info) {
public MCProxyViaDecodeHandler(UserConnection info, final ClientSession client) {
this.info = info;
this.client = client;
}

@Override
Expand All @@ -38,6 +41,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> o
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (cause instanceof CancelCodecException) return;
super.exceptionCaught(ctx, cause);
if (!this.client.callPacketError(cause))
super.exceptionCaught(ctx, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.exception.CancelCodecException;
import com.viaversion.viaversion.exception.CancelEncoderException;
import com.zenith.network.client.ClientSession;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
Expand All @@ -13,9 +14,11 @@
@ChannelHandler.Sharable
public class MCProxyViaEncodeHandler extends MessageToMessageEncoder<ByteBuf> {
private final UserConnection info;
private final ClientSession client;

public MCProxyViaEncodeHandler(UserConnection info) {
public MCProxyViaEncodeHandler(UserConnection info, final ClientSession client) {
this.info = info;
this.client = client;
}

@Override
Expand All @@ -38,6 +41,7 @@ protected void encode(ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> o
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (cause instanceof CancelCodecException) return;
super.exceptionCaught(ctx, cause);
if (!this.client.callPacketError(cause))
super.exceptionCaught(ctx, cause);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/zenith/via/platform/MCProxyViaAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class MCProxyViaAPI extends ViaAPIBase<MinecraftProtocol> {
@Override
public int getPlayerVersion(MinecraftProtocol player) {
return ProtocolVersion.v1_12_2.getVersion();
return ProtocolVersion.v1_20.getVersion();
}

@Override
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/com/zenith/PingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ public void constPing() {
// testing ping srv dns resolver
assertEquals(ProtocolVersionDetector.getProtocolVersion("constantiam.net", 25565), 762);
}

// @Test
public void nineb9tPing() {
// testing ping srv dns resolver
assertEquals(ProtocolVersionDetector.getProtocolVersion("9b9t.com", 25565), 757);
}
}

0 comments on commit 747391a

Please sign in to comment.