Added support for passing <= 1.6.4 clients through

This commit is contained in:
RaphiMC 2023-04-05 19:03:52 +02:00
parent 14d2ad1f36
commit 9f026b041a
5 changed files with 136 additions and 3 deletions

View File

@ -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<String> 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<String> 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<Void> haProxyProtocol = parser.acceptsAll(asList("haproxy-protocol", "haproxy"), "Send HAProxy protocol messages to the backend server");
final OptionSpec<Void> 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));
}

View File

@ -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));

View File

@ -121,14 +121,14 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler<IPacket> {
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:

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<ByteBuf> {
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<ByteBuf>() {
@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<Channel>() {
@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);
}
}

View File

@ -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();