Added LegacyProxyConnection for legacy client passthrough

This commit is contained in:
RaphiMC 2023-10-01 22:03:36 +02:00
parent 006f359d69
commit 8574db690c
No known key found for this signature in database
GPG key ID: 0F6BB0657A03AC94
4 changed files with 94 additions and 40 deletions

View file

@ -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<ByteBuf> {
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<ChannelHandler> 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<ChannelHandler> 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;
}
}

View file

@ -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<ByteBuf> {
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

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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<LegacyProxyConnection> LEGACY_PROXY_CONNECTION_ATTRIBUTE_KEY = AttributeKey.valueOf("legacy_proxy_connection");
private final Channel c2p;
private ServerAddress serverAddress;
public LegacyProxyConnection(final Supplier<ChannelHandler> handlerSupplier, final Function<Supplier<ChannelHandler>, ChannelInitializer<Channel>> 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;
}
}

View file

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