diff --git a/src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyVLLoader.java b/src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyVLLoader.java index 418ec5e..64e38e7 100644 --- a/src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyVLLoader.java +++ b/src/main/java/net/raphimc/viaproxy/protocolhack/impl/ViaProxyVLLoader.java @@ -22,6 +22,7 @@ import com.viaversion.viaversion.api.protocol.version.VersionProvider; import com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.CompressionProvider; import com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.HandItemProvider; import net.raphimc.viabedrock.protocol.providers.NettyPipelineProvider; +import net.raphimc.viabedrock.protocol.providers.TransferProvider; import net.raphimc.vialegacy.protocols.classic.protocola1_0_15toc0_28_30.providers.ClassicCustomCommandProvider; import net.raphimc.vialegacy.protocols.classic.protocola1_0_15toc0_28_30.providers.ClassicMPPassProvider; import net.raphimc.vialegacy.protocols.classic.protocola1_0_15toc0_28_30.providers.ClassicWorldHeightProvider; @@ -54,6 +55,7 @@ public class ViaProxyVLLoader extends VLLoader { // ViaBedrock Via.getManager().getProviders().use(NettyPipelineProvider.class, new ViaProxyNettyPipelineProvider()); + Via.getManager().getProviders().use(TransferProvider.class, new ViaProxyTransferProvider()); // ViaProxy plugins PluginManager.EVENT_MANAGER.call(new ViaLoadingEvent()); diff --git a/src/main/java/net/raphimc/viaproxy/protocolhack/providers/ViaProxyTransferProvider.java b/src/main/java/net/raphimc/viaproxy/protocolhack/providers/ViaProxyTransferProvider.java new file mode 100644 index 0000000..2f7f17d --- /dev/null +++ b/src/main/java/net/raphimc/viaproxy/protocolhack/providers/ViaProxyTransferProvider.java @@ -0,0 +1,39 @@ +/* + * 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.protocolhack.providers; + +import com.viaversion.viaversion.api.connection.UserConnection; +import net.raphimc.viabedrock.protocol.providers.TransferProvider; +import net.raphimc.viaproxy.protocolhack.viaproxy.ViaBedrockTransferHolder; +import net.raphimc.viaproxy.proxy.session.ProxyConnection; +import net.raphimc.viaproxy.proxy.util.CloseAndReturn; + +import java.net.InetSocketAddress; + +public class ViaProxyTransferProvider extends TransferProvider { + + @Override + public void connectToServer(UserConnection user, InetSocketAddress newAddress) { + ViaBedrockTransferHolder.addTempRedirect(user.getChannel(), newAddress); + try { + ProxyConnection.fromUserConnection(user).kickClient("§aThe server transferred you to another server §7(§e"+ newAddress.getHostName() + ":" + newAddress.getPort() + "§7)§a. Please reconnect to ViaProxy."); + } catch (CloseAndReturn ignored) { + } + } + +} diff --git a/src/main/java/net/raphimc/viaproxy/protocolhack/viaproxy/ViaBedrockTransferHolder.java b/src/main/java/net/raphimc/viaproxy/protocolhack/viaproxy/ViaBedrockTransferHolder.java new file mode 100644 index 0000000..862ef7b --- /dev/null +++ b/src/main/java/net/raphimc/viaproxy/protocolhack/viaproxy/ViaBedrockTransferHolder.java @@ -0,0 +1,51 @@ +/* + * 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.protocolhack.viaproxy; + +import io.netty.channel.Channel; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class ViaBedrockTransferHolder { + + private static final Map TEMP_REDIRECTS = new ConcurrentHashMap<>(); + + public static void addTempRedirect(final Channel channel, final InetSocketAddress redirect) { + TEMP_REDIRECTS.put(getChannelAddress(channel), redirect); + } + + public static InetSocketAddress removeTempRedirect(final Channel channel) { + return TEMP_REDIRECTS.remove(getChannelAddress(channel)); + } + + public static boolean hasTempRedirect(final Channel channel) { + return TEMP_REDIRECTS.containsKey(getChannelAddress(channel)); + } + + public static InetSocketAddress getTempRedirect(final Channel channel) { + return TEMP_REDIRECTS.get(getChannelAddress(channel)); + } + + private static InetAddress getChannelAddress(final Channel channel) { + return ((InetSocketAddress) channel.remoteAddress()).getAddress(); + } + +} 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 f6f2720..26c87ca 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/client2proxy/Client2ProxyHandler.java @@ -44,6 +44,7 @@ import net.raphimc.viaproxy.plugins.PluginManager; import net.raphimc.viaproxy.plugins.events.PreConnectEvent; import net.raphimc.viaproxy.plugins.events.Proxy2ServerHandlerCreationEvent; import net.raphimc.viaproxy.plugins.events.ResolveSrvEvent; +import net.raphimc.viaproxy.protocolhack.viaproxy.ViaBedrockTransferHolder; import net.raphimc.viaproxy.proxy.LoginState; import net.raphimc.viaproxy.proxy.external_interface.AuthLibServices; import net.raphimc.viaproxy.proxy.external_interface.ExternalInterface; @@ -76,13 +77,9 @@ import java.util.regex.Pattern; public class Client2ProxyHandler extends SimpleChannelInboundHandler { - private static final KeyPair KEY_PAIR; + private static final KeyPair KEY_PAIR = CryptUtil.generateKeyPair(); private static final Random RANDOM = new Random(); - static { - KEY_PAIR = CryptUtil.generateKeyPair(); - } - private ProxyConnection proxyConnection; private LoginState loginState = LoginState.FIRST_PACKET; @@ -227,6 +224,12 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler { } } + if (serverVersion.equals(VersionEnum.bedrockLatest) && packet.intendedState == ConnectionState.LOGIN && ViaBedrockTransferHolder.hasTempRedirect(this.proxyConnection.getC2P())) { + final InetSocketAddress newAddress = ViaBedrockTransferHolder.removeTempRedirect(this.proxyConnection.getC2P()); + connectIP = newAddress.getHostString(); + connectPort = newAddress.getPort(); + } + final ResolveSrvEvent resolveSrvEvent = PluginManager.EVENT_MANAGER.call(new ResolveSrvEvent(serverVersion, connectIP, connectPort)); connectIP = resolveSrvEvent.getHost(); connectPort = resolveSrvEvent.getPort();