Refactored legacy client passthrough to make use of events

This commit is contained in:
RaphiMC 2023-10-01 21:27:50 +02:00
parent 2471f36976
commit 006f359d69
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
13 changed files with 285 additions and 71 deletions

View File

@ -177,7 +177,7 @@ public class ViaProxy {
}
try {
Logger.LOGGER.info("Starting proxy server");
currentProxyServer = new NetServer(() -> PluginManager.EVENT_MANAGER.call(new Client2ProxyHandlerCreationEvent(new Client2ProxyHandler())).getHandler(), Client2ProxyChannelInitializer::new);
currentProxyServer = new NetServer(() -> PluginManager.EVENT_MANAGER.call(new Client2ProxyHandlerCreationEvent(new Client2ProxyHandler(), false)).getHandler(), Client2ProxyChannelInitializer::new);
PluginManager.EVENT_MANAGER.call(new ProxyStartEvent());
Logger.LOGGER.info("Binding proxy server to " + Options.BIND_ADDRESS + ":" + Options.BIND_PORT);
currentProxyServer.bind(Options.BIND_ADDRESS, Options.BIND_PORT, false);

View File

@ -25,16 +25,22 @@ public class Client2ProxyChannelInitializeEvent extends EventCancellable impleme
private final Type type;
private final Channel channel;
private final boolean isLegacyPassthrough;
public Client2ProxyChannelInitializeEvent(final Type type, final Channel channel) {
public Client2ProxyChannelInitializeEvent(final Type type, final Channel channel, final boolean isLegacyPassthrough) {
this.type = type;
this.channel = channel;
this.isLegacyPassthrough = isLegacyPassthrough;
}
public Channel getChannel() {
return this.channel;
}
public boolean isLegacyPassthrough() {
return this.isLegacyPassthrough;
}
@Override
public Type getType() {

View File

@ -26,16 +26,26 @@ public class Client2ProxyHandlerCreationEvent {
*/
private ChannelHandler handler;
public Client2ProxyHandlerCreationEvent(final ChannelHandler handler) {
this.handler = handler;
}
/**
* Whether the handler is a legacy passthrough handler.
*/
private final boolean isLegacyPassthrough;
public void setHandler(final ChannelHandler handler) {
public Client2ProxyHandlerCreationEvent(final ChannelHandler handler, final boolean isLegacyPassthrough) {
this.handler = handler;
this.isLegacyPassthrough = isLegacyPassthrough;
}
public ChannelHandler getHandler() {
return this.handler;
}
public void setHandler(final ChannelHandler handler) {
this.handler = handler;
}
public boolean isLegacyPassthrough() {
return this.isLegacyPassthrough;
}
}

View File

@ -25,16 +25,22 @@ public class Proxy2ServerChannelInitializeEvent extends EventCancellable impleme
private final Type type;
private final Channel channel;
private final boolean isLegacyPassthrough;
public Proxy2ServerChannelInitializeEvent(final Type type, final Channel channel) {
public Proxy2ServerChannelInitializeEvent(final Type type, final Channel channel, final boolean isLegacyPassthrough) {
this.type = type;
this.channel = channel;
this.isLegacyPassthrough = isLegacyPassthrough;
}
public Channel getChannel() {
return this.channel;
}
public boolean isLegacyPassthrough() {
return this.isLegacyPassthrough;
}
@Override
public Type getType() {

View File

@ -26,16 +26,26 @@ public class Proxy2ServerHandlerCreationEvent {
*/
private ChannelHandler handler;
public Proxy2ServerHandlerCreationEvent(final ChannelHandler handler) {
this.handler = handler;
}
/**
* Whether the handler is a legacy passthrough handler.
*/
private final boolean isLegacyPassthrough;
public void setHandler(final ChannelHandler handler) {
public Proxy2ServerHandlerCreationEvent(final ChannelHandler handler, final boolean isLegacyPassthrough) {
this.handler = handler;
this.isLegacyPassthrough = isLegacyPassthrough;
}
public ChannelHandler getHandler() {
return this.handler;
}
public void setHandler(final ChannelHandler handler) {
this.handler = handler;
}
public boolean isLegacyPassthrough() {
return this.isLegacyPassthrough;
}
}

View File

@ -26,13 +26,13 @@ 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;
import net.raphimc.viaproxy.proxy.client2proxy.passthrough.LegacyClientPassthroughHandler;
import net.raphimc.viaproxy.proxy.client2proxy.passthrough.PassthroughInitialHandler;
import java.util.function.Supplier;
public class Client2ProxyChannelInitializer extends MinecraftChannelInitializer {
public static final String LEGACY_PASSTHROUGH_HANDLER_NAME = "legacy-passthrough-handler";
public static final String LEGACY_PASSTHROUGH_INITIAL_HANDLER_NAME = "legacy-passthrough-initial-handler";
public Client2ProxyChannelInitializer(final Supplier<ChannelHandler> handlerSupplier) {
super(handlerSupplier);
@ -40,19 +40,19 @@ public class Client2ProxyChannelInitializer extends MinecraftChannelInitializer
@Override
protected void initChannel(Channel channel) {
if (PluginManager.EVENT_MANAGER.call(new Client2ProxyChannelInitializeEvent(ITyped.Type.PRE, channel)).isCancelled()) {
if (PluginManager.EVENT_MANAGER.call(new Client2ProxyChannelInitializeEvent(ITyped.Type.PRE, channel, false)).isCancelled()) {
channel.close();
return;
}
if (Options.LEGACY_CLIENT_PASSTHROUGH) {
channel.pipeline().addLast(LEGACY_PASSTHROUGH_HANDLER_NAME, new LegacyClientPassthroughHandler());
channel.pipeline().addLast(LEGACY_PASSTHROUGH_INITIAL_HANDLER_NAME, new PassthroughInitialHandler());
}
super.initChannel(channel);
channel.attr(MCPipeline.PACKET_REGISTRY_ATTRIBUTE_KEY).set(PacketRegistryUtil.getHandshakeRegistry(false));
if (PluginManager.EVENT_MANAGER.call(new Client2ProxyChannelInitializeEvent(ITyped.Type.POST, channel)).isCancelled()) {
if (PluginManager.EVENT_MANAGER.call(new Client2ProxyChannelInitializeEvent(ITyped.Type.POST, channel, false)).isCancelled()) {
channel.close();
}
}

View File

@ -203,7 +203,7 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler<IPacket> {
this.proxyConnection.kickClient(preConnectEvent.getCancelMessage());
}
final Supplier<ChannelHandler> handlerSupplier = () -> PluginManager.EVENT_MANAGER.call(new Proxy2ServerHandlerCreationEvent(new Proxy2ServerHandler())).getHandler();
final Supplier<ChannelHandler> handlerSupplier = () -> PluginManager.EVENT_MANAGER.call(new Proxy2ServerHandlerCreationEvent(new Proxy2ServerHandler(), false)).getHandler();
if (serverVersion.equals(VersionEnum.bedrockLatest)) {
this.proxyConnection = new BedrockProxyConnection(handlerSupplier, Proxy2ServerChannelInitializer::new, this.proxyConnection.getC2P());
} else {

View File

@ -0,0 +1,50 @@
/*
* 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.passthrough;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import net.raphimc.netminecraft.constants.MCPipeline;
import net.raphimc.viaproxy.plugins.PluginManager;
import net.raphimc.viaproxy.plugins.events.Client2ProxyChannelInitializeEvent;
import net.raphimc.viaproxy.plugins.events.types.ITyped;
import net.raphimc.viaproxy.proxy.client2proxy.Client2ProxyChannelInitializer;
import java.util.function.Supplier;
public class PassthroughClient2ProxyChannelInitializer extends Client2ProxyChannelInitializer {
public PassthroughClient2ProxyChannelInitializer(final Supplier<ChannelHandler> handlerSupplier) {
super(handlerSupplier);
}
@Override
protected void initChannel(Channel channel) {
if (PluginManager.EVENT_MANAGER.call(new Client2ProxyChannelInitializeEvent(ITyped.Type.PRE, channel, true)).isCancelled()) {
channel.close();
return;
}
channel.pipeline().addLast(MCPipeline.HANDLER_HANDLER_NAME, this.handlerSupplier.get());
if (PluginManager.EVENT_MANAGER.call(new Client2ProxyChannelInitializeEvent(ITyped.Type.POST, channel, true)).isCancelled()) {
channel.close();
}
}
}

View File

@ -20,20 +20,20 @@ package net.raphimc.viaproxy.proxy.client2proxy.passthrough;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.handler.codec.haproxy.HAProxyMessageEncoder;
import net.raphimc.netminecraft.constants.MCPipeline;
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.proxy2server.Proxy2ServerChannelInitializer;
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.util.ExceptionUtil;
import net.raphimc.viaproxy.proxy.util.HAProxyUtil;
import net.raphimc.viaproxy.util.logging.Logger;
import java.util.function.Function;
import java.util.function.Supplier;
public class LegacyClientPassthroughHandler extends SimpleChannelInboundHandler<ByteBuf> {
public class PassthroughClient2ProxyHandler extends SimpleChannelInboundHandler<ByteBuf> {
protected Channel c2pChannel;
protected NetClient p2sConnection;
@ -63,18 +63,7 @@ public class LegacyClientPassthroughHandler extends SimpleChannelInboundHandler<
if (!msg.isReadable()) return;
if (this.p2sConnection == null) {
final int lengthOrPacketId = msg.getUnsignedByte(0);
if (lengthOrPacketId == 0/*classic*/ || lengthOrPacketId == 1/*a1.0.15*/ || lengthOrPacketId == 2/*<= 1.6.4*/ || lengthOrPacketId == 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.connectToServer();
}
this.p2sConnection.getChannel().writeAndFlush(msg.retain()).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
@ -86,7 +75,8 @@ public class LegacyClientPassthroughHandler extends SimpleChannelInboundHandler<
}
protected void connectToServer() {
this.p2sConnection = new NetClient(this.getHandlerSupplier(), this.getChannelInitializerSupplier()) {
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);
@ -111,40 +101,8 @@ public class LegacyClientPassthroughHandler extends SimpleChannelInboundHandler<
return new ServerAddress(Options.CONNECT_ADDRESS, Options.CONNECT_PORT);
}
protected Function<Supplier<ChannelHandler>, ChannelInitializer<Channel>> getChannelInitializerSupplier() {
return s -> new ChannelInitializer<>() {
@Override
protected void initChannel(Channel channel) {
if (Options.SERVER_HAPROXY_PROTOCOL) {
channel.pipeline().addLast(Proxy2ServerChannelInitializer.VIAPROXY_HAPROXY_ENCODER_NAME, HAProxyMessageEncoder.INSTANCE);
}
channel.pipeline().addLast(MCPipeline.HANDLER_HANDLER_NAME, s.get());
}
};
}
protected Supplier<ChannelHandler> getHandlerSupplier() {
return () -> 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);
}
};
public Channel getC2pChannel() {
return this.c2pChannel;
}
}

View File

@ -0,0 +1,59 @@
/*
* 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.passthrough;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import net.raphimc.viaproxy.plugins.PluginManager;
import net.raphimc.viaproxy.plugins.events.Client2ProxyHandlerCreationEvent;
import net.raphimc.viaproxy.proxy.util.ExceptionUtil;
import java.util.function.Supplier;
public class PassthroughInitialHandler extends SimpleChannelInboundHandler<ByteBuf> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
if (!ctx.channel().isOpen()) return;
if (!msg.isReadable()) return;
final int lengthOrPacketId = msg.getUnsignedByte(0);
ctx.pipeline().remove(this);
if (lengthOrPacketId == 0/*classic*/ || lengthOrPacketId == 1/*a1.0.15*/ || lengthOrPacketId == 2/*<= 1.6.4*/ || lengthOrPacketId == 254/*<= 1.6.4 (ping)*/) {
while (ctx.pipeline().last() != null) {
ctx.pipeline().removeLast();
}
final Supplier<ChannelHandler> handlerSupplier = () -> PluginManager.EVENT_MANAGER.call(new Client2ProxyHandlerCreationEvent(new PassthroughClient2ProxyHandler(), true)).getHandler();
final PassthroughClient2ProxyChannelInitializer channelInitializer = new PassthroughClient2ProxyChannelInitializer(handlerSupplier);
channelInitializer.initChannel(ctx.channel());
ctx.channel().pipeline().fireChannelActive();
}
ctx.pipeline().fireChannelRead(msg.retain());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
ExceptionUtil.handleNettyException(ctx, cause, null);
}
}

View File

@ -56,10 +56,11 @@ public class Proxy2ServerChannelInitializer extends MinecraftChannelInitializer
@Override
protected void initChannel(Channel channel) {
if (PluginManager.EVENT_MANAGER.call(new Proxy2ServerChannelInitializeEvent(ITyped.Type.PRE, channel)).isCancelled()) {
if (PluginManager.EVENT_MANAGER.call(new Proxy2ServerChannelInitializeEvent(ITyped.Type.PRE, channel, false)).isCancelled()) {
channel.close();
return;
}
final ProxyConnection proxyConnection = ProxyConnection.fromChannel(channel);
final UserConnection user = new UserConnectionImpl(channel, true);
@ -78,7 +79,7 @@ public class Proxy2ServerChannelInitializer extends MinecraftChannelInitializer
channel.pipeline().addLast(new ViaProxyVLPipeline(user, proxyConnection.getServerVersion()));
channel.pipeline().addAfter(VLPipeline.VIA_CODEC_NAME, "via-" + MCPipeline.FLOW_CONTROL_HANDLER_NAME, new FlowControlHandler());
if (PluginManager.EVENT_MANAGER.call(new Proxy2ServerChannelInitializeEvent(ITyped.Type.POST, channel)).isCancelled()) {
if (PluginManager.EVENT_MANAGER.call(new Proxy2ServerChannelInitializeEvent(ITyped.Type.POST, channel, false)).isCancelled()) {
channel.close();
}
}

View File

@ -0,0 +1,59 @@
/*
* 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.proxy2server.passthrough;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.handler.codec.haproxy.HAProxyMessageEncoder;
import net.raphimc.netminecraft.constants.MCPipeline;
import net.raphimc.viaproxy.cli.options.Options;
import net.raphimc.viaproxy.plugins.PluginManager;
import net.raphimc.viaproxy.plugins.events.Proxy2ServerChannelInitializeEvent;
import net.raphimc.viaproxy.plugins.events.types.ITyped;
import net.raphimc.viaproxy.proxy.proxy2server.Proxy2ServerChannelInitializer;
import java.util.function.Supplier;
public class PassthroughProxy2ServerChannelInitializer extends Proxy2ServerChannelInitializer {
public PassthroughProxy2ServerChannelInitializer(final Supplier<ChannelHandler> handlerSupplier) {
super(handlerSupplier);
}
@Override
protected void initChannel(Channel channel) {
if (PluginManager.EVENT_MANAGER.call(new Proxy2ServerChannelInitializeEvent(ITyped.Type.PRE, channel, true)).isCancelled()) {
channel.close();
return;
}
if (Options.PROXY_URL != null) {
channel.pipeline().addLast(VIAPROXY_PROXY_HANDLER_NAME, this.getProxyHandler());
}
if (Options.SERVER_HAPROXY_PROTOCOL) {
channel.pipeline().addLast(VIAPROXY_HAPROXY_ENCODER_NAME, HAProxyMessageEncoder.INSTANCE);
}
channel.pipeline().addLast(MCPipeline.HANDLER_HANDLER_NAME, this.handlerSupplier.get());
if (PluginManager.EVENT_MANAGER.call(new Proxy2ServerChannelInitializeEvent(ITyped.Type.POST, channel, true)).isCancelled()) {
channel.close();
}
}
}

View File

@ -0,0 +1,55 @@
/*
* 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.proxy2server.passthrough;
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.util.ExceptionUtil;
public class PassthroughProxy2ServerHandler extends SimpleChannelInboundHandler<ByteBuf> {
private final PassthroughClient2ProxyHandler c2pHandler;
public PassthroughProxy2ServerHandler(final PassthroughClient2ProxyHandler c2pHandler) {
this.c2pHandler = c2pHandler;
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
try {
this.c2pHandler.getC2pChannel().close();
} catch (Throwable ignored) {
}
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
this.c2pHandler.getC2pChannel().writeAndFlush(msg.retain()).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
ExceptionUtil.handleNettyException(ctx, cause, null);
}
}