From 9f026b041a1039e1903e84fd855a91e859606566 Mon Sep 17 00:00:00 2001 From: RaphiMC <50594595+RaphiMC@users.noreply.github.com> Date: Wed, 5 Apr 2023 19:03:52 +0200 Subject: [PATCH] Added support for passing <= 1.6.4 clients through --- .../raphimc/viaproxy/cli/options/Options.java | 3 + .../Client2ProxyChannelInitializer.java | 4 + .../client2proxy/Client2ProxyHandler.java | 4 +- .../LegacyClientPassthroughHandler.java | 124 ++++++++++++++++++ .../viaproxy/proxy/util/ExceptionUtil.java | 4 +- 5 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/raphimc/viaproxy/proxy/client2proxy/LegacyClientPassthroughHandler.java diff --git a/src/main/java/net/raphimc/viaproxy/cli/options/Options.java b/src/main/java/net/raphimc/viaproxy/cli/options/Options.java index ec756b3..23b0882 100644 --- a/src/main/java/net/raphimc/viaproxy/cli/options/Options.java +++ b/src/main/java/net/raphimc/viaproxy/cli/options/Options.java @@ -58,6 +58,7 @@ public class Options { public static boolean LOCAL_SOCKET_AUTH; public static String RESOURCE_PACK_URL; // Example: http://example.com/resourcepack.zip public static boolean HAPROXY_PROTOCOL; + public static boolean LEGACY_CLIENT_PASSTHROUGH; public static void parse(final String[] args) throws IOException { final OptionParser parser = new OptionParser(); @@ -78,6 +79,7 @@ public class Options { final OptionSpec resourcePackUrl = parser.acceptsAll(asList("resource_pack_url", "resource_pack", "rpu", "rp"), "URL of a resource pack which all connecting clients can optionally download").withRequiredArg().ofType(String.class); final OptionSpec proxyUrl = parser.acceptsAll(asList("proxy_url", "proxy"), "URL of a SOCKS(4/5)/HTTP(S) proxy which will be used for TCP connections").withRequiredArg().ofType(String.class); final OptionSpec haProxyProtocol = parser.acceptsAll(asList("haproxy-protocol", "haproxy"), "Send HAProxy protocol messages to the backend server"); + final OptionSpec legacyClientPassthrough = parser.acceptsAll(asList("legacy_client_passthrough", "legacy_passthrough"), "Allow <= 1.6.4 clients to connect to the backend server instead of being kicked"); PluginManager.EVENT_MANAGER.call(new PreOptionsParseEvent(parser)); final OptionSet options = parser.parse(args); @@ -116,6 +118,7 @@ public class Options { } } HAPROXY_PROTOCOL = options.has(haProxyProtocol); + LEGACY_CLIENT_PASSTHROUGH = options.has(legacyClientPassthrough); PluginManager.EVENT_MANAGER.call(new PostOptionsParseEvent(options)); } diff --git a/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyChannelInitializer.java b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyChannelInitializer.java index c6cf81a..8a4bae9 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyChannelInitializer.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyChannelInitializer.java @@ -22,6 +22,7 @@ import io.netty.channel.ChannelHandler; import net.raphimc.netminecraft.constants.MCPipeline; import net.raphimc.netminecraft.netty.connection.MinecraftChannelInitializer; import net.raphimc.netminecraft.packet.registry.PacketRegistryUtil; +import net.raphimc.viaproxy.cli.options.Options; import net.raphimc.viaproxy.plugins.PluginManager; import net.raphimc.viaproxy.plugins.events.Client2ProxyChannelInitializeEvent; import net.raphimc.viaproxy.plugins.events.types.ITyped; @@ -41,6 +42,9 @@ public class Client2ProxyChannelInitializer extends MinecraftChannelInitializer return; } + if (Options.LEGACY_CLIENT_PASSTHROUGH) { + channel.pipeline().addLast("legacy-passthrough-handler", new LegacyClientPassthroughHandler()); + } super.initChannel(channel); channel.attr(MCPipeline.PACKET_REGISTRY_ATTRIBUTE_KEY).set(PacketRegistryUtil.getHandshakeRegistry(false)); diff --git a/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java index 727d937..85f26c9 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java @@ -121,14 +121,14 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler { switch (this.proxyConnection.getConnectionState()) { case HANDSHAKING: if (packet instanceof C2SHandshakePacket) this.handleHandshake((C2SHandshakePacket) packet); - else break; + else throw new IllegalStateException("Unexpected packet in HANDSHAKING state"); return; case LOGIN: if (packet instanceof C2SLoginHelloPacket1_7) this.handleLoginHello((C2SLoginHelloPacket1_7) packet); else if (packet instanceof C2SLoginKeyPacket1_7) this.handleLoginKey((C2SLoginKeyPacket1_7) packet); else if (packet instanceof C2SLoginCustomPayloadPacket) this.handleLoginCustomPayload((C2SLoginCustomPayloadPacket) packet); - else break; + else throw new IllegalStateException("Unexpected packet in LOGIN state"); return; case PLAY: diff --git a/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/LegacyClientPassthroughHandler.java b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/LegacyClientPassthroughHandler.java new file mode 100644 index 0000000..3def886 --- /dev/null +++ b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/LegacyClientPassthroughHandler.java @@ -0,0 +1,124 @@ +/* + * This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy + * Copyright (C) 2023 RK_01/RaphiMC and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.raphimc.viaproxy.proxy.client2proxy; + +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.*; +import net.raphimc.netminecraft.netty.connection.NetClient; +import net.raphimc.netminecraft.util.ServerAddress; +import net.raphimc.viaproxy.cli.options.Options; +import net.raphimc.viaproxy.proxy.util.ExceptionUtil; +import net.raphimc.viaproxy.util.logging.Logger; + +public class LegacyClientPassthroughHandler extends SimpleChannelInboundHandler { + + private Channel c2pChannel; + private NetClient p2sConnection; + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + super.channelActive(ctx); + + this.c2pChannel = ctx.channel(); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + super.channelInactive(ctx); + + try { + if (this.p2sConnection != null) { + this.p2sConnection.getChannel().close(); + } + } catch (Throwable ignored) { + } + } + + @Override + protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) { + if (!ctx.channel().isOpen()) return; + if (!msg.isReadable()) return; + + if (this.p2sConnection == null) { + final int length = msg.getUnsignedByte(0); + if (length == 0/*classic*/ || length == 1/*a1.0.15*/ || length == 2/*<= 1.6.4*/ || length == 254/*<= 1.6.4 (ping)*/) { + while (ctx.pipeline().last() != this) { + ctx.pipeline().removeLast(); + } + + this.connectToServer(); + } else { + ctx.pipeline().remove(this); + ctx.pipeline().fireChannelRead(msg.retain()); + return; + } + } + + this.p2sConnection.getChannel().writeAndFlush(msg.retain()).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); + } + + private void connectToServer() { + this.p2sConnection = new NetClient(() -> new SimpleChannelInboundHandler() { + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + super.channelInactive(ctx); + + try { + LegacyClientPassthroughHandler.this.c2pChannel.close(); + } catch (Throwable ignored) { + } + } + + @Override + protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) { + LegacyClientPassthroughHandler.this.c2pChannel.writeAndFlush(msg.retain()).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + ExceptionUtil.handleNettyException(ctx, cause, null); + } + }, s -> new ChannelInitializer() { + @Override + protected void initChannel(Channel channel) { + channel.pipeline().addLast(s.get()); + } + }) { + @Override + public void initialize(Bootstrap bootstrap) { + bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4_000); + super.initialize(bootstrap); + } + }; + + try { + this.p2sConnection.connect(new ServerAddress(Options.CONNECT_ADDRESS, Options.CONNECT_PORT)); + } catch (Throwable e) { + Logger.LOGGER.error("Failed to connect to target server", e); + this.p2sConnection = null; + this.c2pChannel.close(); + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + ExceptionUtil.handleNettyException(ctx, cause, null); + } + +} diff --git a/src/main/java/net/raphimc/viaproxy/proxy/util/ExceptionUtil.java b/src/main/java/net/raphimc/viaproxy/proxy/util/ExceptionUtil.java index 51b7384..6829bb9 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/util/ExceptionUtil.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/util/ExceptionUtil.java @@ -52,7 +52,9 @@ public class ExceptionUtil { } Logger.LOGGER.error("Caught unhandled netty exception", cause); try { - proxyConnection.kickClient("§cAn unhandled error occurred in your connection and it has been closed.\n§aError details for report:§f" + ExceptionUtil.prettyPrint(cause)); + 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) { } ctx.channel().close();