Added option to suppress client protocol errors

This commit is contained in:
Lenni0451 2024-05-25 17:21:49 +02:00
parent ad518fa7c2
commit 350230dd4f
No known key found for this signature in database
GPG Key ID: 5D59B86635AD3F2F
8 changed files with 32 additions and 11 deletions

View File

@ -67,6 +67,7 @@ public class ViaProxyConfig extends Config implements com.viaversion.viaversion.
private final OptionSpec<Integer> optionCompressionThreshold;
private final OptionSpec<Boolean> optionAllowBetaPinging;
private final OptionSpec<Boolean> optionIgnoreProtocolTranslationErrors;
private final OptionSpec<Boolean> optionSuppressClientProtocolErrors;
private final OptionSpec<Boolean> optionAllowLegacyClientPassthrough;
private final OptionSpec<String> optionCustomMotd;
private final OptionSpec<String> optionResourcePackUrl;
@ -87,6 +88,7 @@ public class ViaProxyConfig extends Config implements com.viaversion.viaversion.
private int compressionThreshold = 256;
private boolean allowBetaPinging = false;
private boolean ignoreProtocolTranslationErrors = false;
private boolean suppressClientProtocolErrors = false;
private boolean allowLegacyClientPassthrough = false;
private String customMotd = "";
private String resourcePackUrl = "";
@ -112,6 +114,7 @@ public class ViaProxyConfig extends Config implements com.viaversion.viaversion.
this.optionCompressionThreshold = this.optionParser.accepts("compression-threshold").withRequiredArg().ofType(Integer.class).defaultsTo(this.compressionThreshold);
this.optionAllowBetaPinging = this.optionParser.accepts("allow-beta-pinging").withRequiredArg().ofType(Boolean.class).defaultsTo(this.allowBetaPinging);
this.optionIgnoreProtocolTranslationErrors = this.optionParser.accepts("ignore-protocol-translation-errors").withRequiredArg().ofType(Boolean.class).defaultsTo(this.ignoreProtocolTranslationErrors);
this.optionSuppressClientProtocolErrors = this.optionParser.accepts("suppress-client-protocol-errors").withRequiredArg().ofType(Boolean.class).defaultsTo(this.suppressClientProtocolErrors);
this.optionAllowLegacyClientPassthrough = this.optionParser.accepts("allow-legacy-client-passthrough").withRequiredArg().ofType(Boolean.class).defaultsTo(this.allowLegacyClientPassthrough);
this.optionCustomMotd = this.optionParser.accepts("custom-motd").withRequiredArg().ofType(String.class).defaultsTo(this.customMotd);
this.optionResourcePackUrl = this.optionParser.accepts("resource-pack-url").withRequiredArg().ofType(String.class).defaultsTo(this.resourcePackUrl);
@ -144,6 +147,7 @@ public class ViaProxyConfig extends Config implements com.viaversion.viaversion.
this.compressionThreshold = this.getInt("compression-threshold", this.compressionThreshold);
this.allowBetaPinging = this.getBoolean("allow-beta-pinging", this.allowBetaPinging);
this.ignoreProtocolTranslationErrors = this.getBoolean("ignore-protocol-translation-errors", this.ignoreProtocolTranslationErrors);
this.suppressClientProtocolErrors = this.getBoolean("suppress-client-protocol-errors", this.suppressClientProtocolErrors);
this.allowLegacyClientPassthrough = this.getBoolean("allow-legacy-client-passthrough", this.allowLegacyClientPassthrough);
this.customMotd = this.getString("custom-motd", this.customMotd);
this.resourcePackUrl = this.getString("resource-pack-url", this.resourcePackUrl);
@ -180,6 +184,7 @@ public class ViaProxyConfig extends Config implements com.viaversion.viaversion.
this.compressionThreshold = options.valueOf(this.optionCompressionThreshold);
this.allowBetaPinging = options.valueOf(this.optionAllowBetaPinging);
this.ignoreProtocolTranslationErrors = options.valueOf(this.optionIgnoreProtocolTranslationErrors);
this.suppressClientProtocolErrors = options.valueOf(this.optionSuppressClientProtocolErrors);
this.allowLegacyClientPassthrough = options.valueOf(this.optionAllowLegacyClientPassthrough);
this.customMotd = options.valueOf(this.optionCustomMotd);
this.resourcePackUrl = options.valueOf(this.optionResourcePackUrl);
@ -347,6 +352,15 @@ public class ViaProxyConfig extends Config implements com.viaversion.viaversion.
this.set("ignore-protocol-translation-errors", ignoreProtocolTranslationErrors);
}
public boolean shouldSuppressClientProtocolErrors() {
return this.suppressClientProtocolErrors;
}
public void setSuppressClientProtocolErrors(final boolean suppressClientProtocolErrors) {
this.suppressClientProtocolErrors = suppressClientProtocolErrors;
this.set("suppress-client-protocol-errors", suppressClientProtocolErrors);
}
public boolean shouldAllowLegacyClientPassthrough() {
return this.allowLegacyClientPassthrough;
}

View File

@ -107,7 +107,7 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler<IPacket> {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
ExceptionUtil.handleNettyException(ctx, cause, this.proxyConnection);
ExceptionUtil.handleNettyException(ctx, cause, this.proxyConnection, true);
}
private void handleHandshake(final C2SHandshakingClientIntentionPacket packet) {

View File

@ -53,7 +53,7 @@ public class LegacyPassthroughInitialHandler extends SimpleChannelInboundHandler
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
ExceptionUtil.handleNettyException(ctx, cause, null);
ExceptionUtil.handleNettyException(ctx, cause, null, true);
}
}

View File

@ -69,7 +69,7 @@ public class PassthroughClient2ProxyHandler extends SimpleChannelInboundHandler<
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
ExceptionUtil.handleNettyException(ctx, cause, null);
ExceptionUtil.handleNettyException(ctx, cause, null, true);
}
protected void connectToServer(final Channel c2pChannel) {

View File

@ -66,7 +66,7 @@ public class Proxy2ServerHandler extends SimpleChannelInboundHandler<IPacket> {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
ExceptionUtil.handleNettyException(ctx, cause, this.proxyConnection);
ExceptionUtil.handleNettyException(ctx, cause, this.proxyConnection, false);
}
}

View File

@ -52,7 +52,7 @@ public class PassthroughProxy2ServerHandler extends SimpleChannelInboundHandler<
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
ExceptionUtil.handleNettyException(ctx, cause, null);
ExceptionUtil.handleNettyException(ctx, cause, null, false);
}
}

View File

@ -21,6 +21,7 @@ import com.viaversion.viaversion.exception.InformativeException;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.EncoderException;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
import net.raphimc.viaproxy.util.logging.Logger;
@ -41,7 +42,7 @@ public class ExceptionUtil {
}
}
public static void handleNettyException(ChannelHandlerContext ctx, Throwable cause, ProxyConnection proxyConnection) {
public static void handleNettyException(ChannelHandlerContext ctx, Throwable cause, ProxyConnection proxyConnection, boolean client2Proxy) {
if (!ctx.channel().isOpen()) return;
if (cause instanceof ClosedChannelException) return;
if (cause instanceof IOException) return;
@ -49,12 +50,14 @@ public class ExceptionUtil {
ctx.channel().close();
return;
}
Logger.LOGGER.error("Caught unhandled netty exception", cause);
try {
if (proxyConnection != null) {
proxyConnection.kickClient("§cAn unhandled error occurred in your connection and it has been closed.\n§aError details for report:§f" + ExceptionUtil.prettyPrint(cause));
if (!client2Proxy || !ViaProxy.getConfig().shouldSuppressClientProtocolErrors()) {
Logger.LOGGER.error("Caught unhandled netty exception", cause);
try {
if (proxyConnection != null) {
proxyConnection.kickClient("§cAn unhandled error occurred in your connection and it has been closed.\n§aError details for report:§f" + ExceptionUtil.prettyPrint(cause));
}
} catch (Throwable ignored) {
}
} catch (Throwable ignored) {
}
ctx.channel().close();
}

View File

@ -52,6 +52,10 @@ allow-beta-pinging: false
# This may cause issues depending on the type of packet which failed to translate.
ignore-protocol-translation-errors: false
#
# Enabling this will suppress client protocol errors to prevent lag when ViaProxy is getting spammed with invalid packets.
# This may cause issues with debugging client connection issues because no error messages will be printed.
suppress-client-protocol-errors: false
#
# Allow <= 1.6.4 clients to connect through ViaProxy to the target server. (No protocol translation or packet handling)
allow-legacy-client-passthrough: false
#