From a754427707a68c8e01c138660389e0f46c8b5638 Mon Sep 17 00:00:00 2001 From: creeper123123321 <7974274+creeper123123321@users.noreply.github.com> Date: Sat, 17 Oct 2020 16:10:51 -0300 Subject: [PATCH] Hold messages for auto protocol detector, fixes #65, 0.2.14 --- README.md | 4 +- build.gradle.kts | 2 +- .../clientside/ProtocolDetectionHandler.java | 103 ++++++++++++++++++ .../client/MixinClientConnectionChInit.java | 13 +-- .../providers/VRVersionProvider.java | 9 +- 5 files changed, 115 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/github/creeper123123321/viafabric/handler/clientside/ProtocolDetectionHandler.java diff --git a/README.md b/README.md index 88b9f90..89e3849 100644 --- a/README.md +++ b/README.md @@ -94,8 +94,8 @@ Adding [ViaBackwards](https://viaversion.com/backwards) (and optionally [ViaRewi **How to use protocol auto detector?:** - For using globally, set the protocol to AUTO or -2. For using in a specific server: ddns.example.com._v-2.viafabric - The protocol auto detector will try to ping with the client native protocol version so if you have ViaVersion or similar in the server it will use the translator, differently than multiconnect which uses -1 version, which may get the native server version. -- It may block the network thread up to 1 second. -- The versions are cached for 100 seconds. +- It may hold your handshake for up to 10 seconds. +- The results are cached for 100 seconds. **How can I set the version for specific servers?:** diff --git a/build.gradle.kts b/build.gradle.kts index cf3ee6b..d63ddd1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -21,7 +21,7 @@ val branch = if (!travisBranch.isNullOrBlank()) travisBranch else try { "unknown" } -version = "0.2.13-SNAPSHOT+" + try { +version = "0.2.14-SNAPSHOT+" + try { gitVersion() + "-" + branch } catch (e: Exception) { "unknown" diff --git a/src/main/java/com/github/creeper123123321/viafabric/handler/clientside/ProtocolDetectionHandler.java b/src/main/java/com/github/creeper123123321/viafabric/handler/clientside/ProtocolDetectionHandler.java new file mode 100644 index 0000000..c11e7d7 --- /dev/null +++ b/src/main/java/com/github/creeper123123321/viafabric/handler/clientside/ProtocolDetectionHandler.java @@ -0,0 +1,103 @@ +/* + * MIT License + * + * Copyright (c) 2018- creeper123123321 + * Copyright (c) 2019- contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.creeper123123321.viafabric.handler.clientside; + +import com.github.creeper123123321.viafabric.ViaFabric; +import com.github.creeper123123321.viafabric.service.ProtocolAutoDetector; +import io.netty.channel.ChannelDuplexHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelPromise; +import us.myles.ViaVersion.api.Pair; + +import java.net.InetSocketAddress; +import java.util.ArrayDeque; +import java.util.Queue; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +public class ProtocolDetectionHandler extends ChannelDuplexHandler { + private final Queue> queuedMessages = new ArrayDeque<>(); + private boolean hold = true; + private boolean pendentFlush; + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + super.channelActive(ctx); + if (ctx.channel().remoteAddress() instanceof InetSocketAddress) { + try { + ScheduledFuture timeoutRun = ctx.executor().schedule(() -> { + ViaFabric.JLOGGER.warning("Timeout for protocol auto-detection in " + + ctx.channel().remoteAddress() + " server"); + hold = false; + ctx.pipeline().remove(this); + }, 10, TimeUnit.SECONDS); + ProtocolAutoDetector.SERVER_VER.get(((InetSocketAddress) ctx.channel().remoteAddress())) + .whenComplete((obj, ex) -> { + hold = false; + ctx.pipeline().remove(this); + timeoutRun.cancel(false); + }); + // Let's cache it before we need it + } catch (ExecutionException e) { + ViaFabric.JLOGGER.warning("Protocol auto detector error: " + e); + } + } + } + + @Override + public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { + if (!hold) { + drainQueue(ctx); + super.write(ctx, msg, promise); + } else { + queuedMessages.add(new Pair<>(msg, promise)); + } + } + + @Override + public void flush(ChannelHandlerContext ctx) throws Exception { + if (!hold) { + drainQueue(ctx); + super.flush(ctx); + } else { + pendentFlush = true; + } + } + + private void drainQueue(ChannelHandlerContext ctx) { + queuedMessages.forEach(it -> ctx.write(it.getKey(), it.getValue())); + queuedMessages.clear(); + if (pendentFlush) ctx.flush(); + pendentFlush = false; + } + + @Override + public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { + drainQueue(ctx); + super.handlerRemoved(ctx); + } +} diff --git a/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinClientConnectionChInit.java b/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinClientConnectionChInit.java index 03c267d..2ce2905 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinClientConnectionChInit.java +++ b/src/main/java/com/github/creeper123123321/viafabric/mixin/client/MixinClientConnectionChInit.java @@ -27,11 +27,11 @@ package com.github.creeper123123321.viafabric.mixin.client; import com.github.creeper123123321.viafabric.ViaFabric; import com.github.creeper123123321.viafabric.handler.CommonTransformer; +import com.github.creeper123123321.viafabric.handler.clientside.ProtocolDetectionHandler; import com.github.creeper123123321.viafabric.handler.clientside.VRDecodeHandler; import com.github.creeper123123321.viafabric.handler.clientside.VREncodeHandler; import com.github.creeper123123321.viafabric.platform.VRClientSideUserConnection; import com.github.creeper123123321.viafabric.protocol.ViaFabricHostnameProtocol; -import com.github.creeper123123321.viafabric.service.ProtocolAutoDetector; import io.netty.channel.Channel; import io.netty.channel.socket.SocketChannel; import org.spongepowered.asm.mixin.Mixin; @@ -41,8 +41,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.ProtocolPipeline; -import java.util.concurrent.ExecutionException; - @Mixin(targets = "net.minecraft.network.ClientConnection$5") public class MixinClientConnectionChInit { @Inject(method = "initChannel", at = @At(value = "TAIL"), remap = false) @@ -54,13 +52,8 @@ public class MixinClientConnectionChInit { channel.pipeline() .addBefore("encoder", CommonTransformer.HANDLER_ENCODER_NAME, new VREncodeHandler(user)) .addBefore("decoder", CommonTransformer.HANDLER_DECODER_NAME, new VRDecodeHandler(user)); - - if (channel.remoteAddress() != null) { - try { - ProtocolAutoDetector.SERVER_VER.get(((SocketChannel) channel).remoteAddress()); // Let's cache it before we need it, and hope we'll not block netty thread - } catch (ExecutionException e) { - ViaFabric.JLOGGER.warning("Protocol auto detector error: " + e); - } + if (ViaFabric.config.isClientSideEnabled()) { + channel.pipeline().addAfter(CommonTransformer.HANDLER_ENCODER_NAME, "via-autoprotocol", new ProtocolDetectionHandler()); } } } diff --git a/src/main/java/com/github/creeper123123321/viafabric/providers/VRVersionProvider.java b/src/main/java/com/github/creeper123123321/viafabric/providers/VRVersionProvider.java index 00394e7..2f3473b 100644 --- a/src/main/java/com/github/creeper123123321/viafabric/providers/VRVersionProvider.java +++ b/src/main/java/com/github/creeper123123321/viafabric/providers/VRVersionProvider.java @@ -49,7 +49,6 @@ import java.lang.reflect.Method; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.util.*; -import java.util.concurrent.TimeUnit; import java.util.stream.IntStream; public class VRVersionProvider extends VersionProvider { @@ -102,8 +101,12 @@ public class VRVersionProvider extends VersionProvider { try { if (serverVer == -2) { - // sadly we'll need to block netty thread, we'll need to be fast - serverVer = ProtocolAutoDetector.SERVER_VER.get((InetSocketAddress) addr).get(1, TimeUnit.SECONDS).getId(); + // Hope protocol was autodetected + ProtocolVersion autoVer = + ProtocolAutoDetector.SERVER_VER.get((InetSocketAddress) addr).getNow(null); + if (autoVer != null) { + serverVer = autoVer.getId(); + } } } catch (Exception e) { ViaFabric.JLOGGER.warning("Couldn't auto detect: " + e);