From 8574db690cdd70de9acb821a1f73502265626c88 Mon Sep 17 00:00:00 2001 From: RaphiMC <50594595+RaphiMC@users.noreply.github.com> Date: Sun, 1 Oct 2023 22:03:36 +0200 Subject: [PATCH] Added LegacyProxyConnection for legacy client passthrough --- .../PassthroughClient2ProxyHandler.java | 48 ++++--------- .../PassthroughProxy2ServerHandler.java | 15 ++-- .../proxy/session/LegacyProxyConnection.java | 69 +++++++++++++++++++ .../proxy/session/ProxyConnection.java | 2 +- 4 files changed, 94 insertions(+), 40 deletions(-) create mode 100644 src/main/java/net/raphimc/viaproxy/proxy/session/LegacyProxyConnection.java diff --git a/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/passthrough/PassthroughClient2ProxyHandler.java b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/passthrough/PassthroughClient2ProxyHandler.java index 1feb6af..09bc648 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/passthrough/PassthroughClient2ProxyHandler.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/passthrough/PassthroughClient2ProxyHandler.java @@ -17,16 +17,15 @@ */ package net.raphimc.viaproxy.proxy.client2proxy.passthrough; -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.plugins.PluginManager; import net.raphimc.viaproxy.plugins.events.Proxy2ServerHandlerCreationEvent; import net.raphimc.viaproxy.proxy.proxy2server.passthrough.PassthroughProxy2ServerChannelInitializer; import net.raphimc.viaproxy.proxy.proxy2server.passthrough.PassthroughProxy2ServerHandler; +import net.raphimc.viaproxy.proxy.session.LegacyProxyConnection; import net.raphimc.viaproxy.proxy.util.ExceptionUtil; import net.raphimc.viaproxy.proxy.util.HAProxyUtil; import net.raphimc.viaproxy.util.logging.Logger; @@ -35,23 +34,15 @@ import java.util.function.Supplier; public class PassthroughClient2ProxyHandler extends SimpleChannelInboundHandler { - protected Channel c2pChannel; - protected NetClient p2sConnection; - - @Override - public void channelActive(ChannelHandlerContext ctx) throws Exception { - super.channelActive(ctx); - - this.c2pChannel = ctx.channel(); - } + private LegacyProxyConnection proxyConnection; @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); try { - if (this.p2sConnection != null) { - this.p2sConnection.getChannel().close(); + if (this.proxyConnection != null) { + this.proxyConnection.getChannel().close(); } } catch (Throwable ignored) { } @@ -62,11 +53,11 @@ public class PassthroughClient2ProxyHandler extends SimpleChannelInboundHandler< if (!ctx.channel().isOpen()) return; if (!msg.isReadable()) return; - if (this.p2sConnection == null) { - this.connectToServer(); + if (this.proxyConnection == null) { + this.connectToServer(ctx.channel()); } - this.p2sConnection.getChannel().writeAndFlush(msg.retain()).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); + this.proxyConnection.getChannel().writeAndFlush(msg.retain()).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } @Override @@ -74,26 +65,21 @@ public class PassthroughClient2ProxyHandler extends SimpleChannelInboundHandler< ExceptionUtil.handleNettyException(ctx, cause, null); } - protected void connectToServer() { - final Supplier handlerSupplier = () -> PluginManager.EVENT_MANAGER.call(new Proxy2ServerHandlerCreationEvent(new PassthroughProxy2ServerHandler(this), true)).getHandler(); - this.p2sConnection = new NetClient(handlerSupplier, PassthroughProxy2ServerChannelInitializer::new) { - @Override - public void initialize(Bootstrap bootstrap) { - bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4_000); - super.initialize(bootstrap); - } - }; + protected void connectToServer(final Channel c2pChannel) { + final Supplier handlerSupplier = () -> PluginManager.EVENT_MANAGER.call(new Proxy2ServerHandlerCreationEvent(new PassthroughProxy2ServerHandler(), true)).getHandler(); + this.proxyConnection = new LegacyProxyConnection(handlerSupplier, PassthroughProxy2ServerChannelInitializer::new, c2pChannel); + this.proxyConnection.getC2P().attr(LegacyProxyConnection.LEGACY_PROXY_CONNECTION_ATTRIBUTE_KEY).set(this.proxyConnection); try { - this.p2sConnection.connect(this.getServerAddress()); + this.proxyConnection.connect(this.getServerAddress()); } catch (Throwable e) { Logger.LOGGER.error("Failed to connect to target server", e); - this.p2sConnection = null; - this.c2pChannel.close(); + this.proxyConnection = null; + c2pChannel.close(); } if (Options.SERVER_HAPROXY_PROTOCOL) { - this.p2sConnection.getChannel().writeAndFlush(HAProxyUtil.createMessage(this.c2pChannel, this.p2sConnection.getChannel(), null)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); + this.proxyConnection.getChannel().writeAndFlush(HAProxyUtil.createMessage(c2pChannel, this.proxyConnection.getChannel(), null)).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } } @@ -101,8 +87,4 @@ public class PassthroughClient2ProxyHandler extends SimpleChannelInboundHandler< return new ServerAddress(Options.CONNECT_ADDRESS, Options.CONNECT_PORT); } - public Channel getC2pChannel() { - return this.c2pChannel; - } - } diff --git a/src/main/java/net/raphimc/viaproxy/proxy/proxy2server/passthrough/PassthroughProxy2ServerHandler.java b/src/main/java/net/raphimc/viaproxy/proxy/proxy2server/passthrough/PassthroughProxy2ServerHandler.java index f1d2866..7280b4d 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/proxy2server/passthrough/PassthroughProxy2ServerHandler.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/proxy2server/passthrough/PassthroughProxy2ServerHandler.java @@ -21,15 +21,18 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; -import net.raphimc.viaproxy.proxy.client2proxy.passthrough.PassthroughClient2ProxyHandler; +import net.raphimc.viaproxy.proxy.session.LegacyProxyConnection; import net.raphimc.viaproxy.proxy.util.ExceptionUtil; public class PassthroughProxy2ServerHandler extends SimpleChannelInboundHandler { - private final PassthroughClient2ProxyHandler c2pHandler; + private LegacyProxyConnection proxyConnection; - public PassthroughProxy2ServerHandler(final PassthroughClient2ProxyHandler c2pHandler) { - this.c2pHandler = c2pHandler; + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + super.channelActive(ctx); + + this.proxyConnection = LegacyProxyConnection.fromChannel(ctx.channel()); } @Override @@ -37,14 +40,14 @@ public class PassthroughProxy2ServerHandler extends SimpleChannelInboundHandler< super.channelInactive(ctx); try { - this.c2pHandler.getC2pChannel().close(); + this.proxyConnection.getC2P().close(); } catch (Throwable ignored) { } } @Override protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) { - this.c2pHandler.getC2pChannel().writeAndFlush(msg.retain()).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); + this.proxyConnection.getC2P().writeAndFlush(msg.retain()).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } @Override diff --git a/src/main/java/net/raphimc/viaproxy/proxy/session/LegacyProxyConnection.java b/src/main/java/net/raphimc/viaproxy/proxy/session/LegacyProxyConnection.java new file mode 100644 index 0000000..f1dfb6b --- /dev/null +++ b/src/main/java/net/raphimc/viaproxy/proxy/session/LegacyProxyConnection.java @@ -0,0 +1,69 @@ +/* + * 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.session; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.util.AttributeKey; +import net.raphimc.netminecraft.netty.connection.NetClient; +import net.raphimc.netminecraft.util.ServerAddress; + +import java.util.function.Function; +import java.util.function.Supplier; + +public class LegacyProxyConnection extends NetClient { + + public static final AttributeKey LEGACY_PROXY_CONNECTION_ATTRIBUTE_KEY = AttributeKey.valueOf("legacy_proxy_connection"); + + private final Channel c2p; + private ServerAddress serverAddress; + + public LegacyProxyConnection(final Supplier handlerSupplier, final Function, ChannelInitializer> channelInitializerSupplier, final Channel c2p) { + super(handlerSupplier, channelInitializerSupplier); + this.c2p = c2p; + } + + public static LegacyProxyConnection fromChannel(final Channel channel) { + return channel.attr(LEGACY_PROXY_CONNECTION_ATTRIBUTE_KEY).get(); + } + + @Override + public void connect(final ServerAddress serverAddress) { + this.serverAddress = serverAddress; + super.connect(serverAddress); + } + + @Override + public void initialize(final Bootstrap bootstrap) { + bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4_000); + bootstrap.attr(LEGACY_PROXY_CONNECTION_ATTRIBUTE_KEY, this); + super.initialize(bootstrap); + } + + public Channel getC2P() { + return this.c2p; + } + + public ServerAddress getServerAddress() { + return this.serverAddress; + } + +} diff --git a/src/main/java/net/raphimc/viaproxy/proxy/session/ProxyConnection.java b/src/main/java/net/raphimc/viaproxy/proxy/session/ProxyConnection.java index e14a119..c852fd0 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/session/ProxyConnection.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/session/ProxyConnection.java @@ -104,7 +104,7 @@ public class ProxyConnection extends NetClient { @Override public void initialize(final Bootstrap bootstrap) { bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 4_000); - bootstrap.attr(ProxyConnection.PROXY_CONNECTION_ATTRIBUTE_KEY, this); + bootstrap.attr(PROXY_CONNECTION_ATTRIBUTE_KEY, this); super.initialize(bootstrap); }