Implemented ViaBedrock TransferProvider

This commit is contained in:
RaphiMC 2023-07-03 19:14:30 +02:00
parent 79f488747e
commit 286ac0a269
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
4 changed files with 100 additions and 5 deletions

View File

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

View File

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

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<InetAddress, InetSocketAddress> 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();
}
}

View File

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