Added JoinServerRequest event

This commit is contained in:
RaphiMC 2024-11-30 00:10:49 +01:00
parent 7c70cfa481
commit e6dc79ff61
No known key found for this signature in database
GPG Key ID: 0F6BB0657A03AC94
2 changed files with 68 additions and 16 deletions

View File

@ -0,0 +1,41 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2021-2024 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.plugins.events;
import net.raphimc.viaproxy.plugins.events.types.EventCancellable;
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
public class JoinServerRequestEvent extends EventCancellable {
private final ProxyConnection proxyConnection;
private final String serverIdHash;
public JoinServerRequestEvent(final ProxyConnection proxyConnection, final String serverIdHash) {
this.proxyConnection = proxyConnection;
this.serverIdHash = serverIdHash;
}
public ProxyConnection getProxyConnection() {
return this.proxyConnection;
}
public String getServerIdHash() {
return this.serverIdHash;
}
}

View File

@ -35,9 +35,11 @@ import net.raphimc.viabedrock.api.BedrockProtocolVersion;
import net.raphimc.viabedrock.protocol.storage.AuthChainData;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.plugins.events.FillPlayerDataEvent;
import net.raphimc.viaproxy.plugins.events.JoinServerRequestEvent;
import net.raphimc.viaproxy.protocoltranslator.viaproxy.ViaProxyConfig;
import net.raphimc.viaproxy.proxy.packethandler.OpenAuthModPacketHandler;
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
import net.raphimc.viaproxy.proxy.util.CloseAndReturn;
import net.raphimc.viaproxy.saves.impl.accounts.Account;
import net.raphimc.viaproxy.saves.impl.accounts.BedrockAccount;
import net.raphimc.viaproxy.saves.impl.accounts.MicrosoftAccount;
@ -94,6 +96,8 @@ public class ExternalInterface {
}
ViaProxy.EVENT_MANAGER.call(new FillPlayerDataEvent(proxyConnection));
} catch (CloseAndReturn e) {
throw e;
} catch (Throwable e) {
Logger.LOGGER.error("Failed to fill player data", e);
proxyConnection.kickClient("§cFailed to fill player data. This might be caused by outdated account tokens or rate limits. Wait a couple of seconds and try again. If the problem persists, remove and re-add your account.");
@ -103,24 +107,31 @@ public class ExternalInterface {
proxyConnection.getLoginHelloPacket().uuid = proxyConnection.getGameProfile().getId();
}
public static void joinServer(final String serverIdHash, final ProxyConnection proxyConnection) throws InterruptedException, ExecutionException {
public static void joinServer(final String serverIdHash, final ProxyConnection proxyConnection) {
Logger.u_info("auth", proxyConnection, "Trying to join online mode server");
if (ViaProxy.getConfig().getAuthMethod() == ViaProxyConfig.AuthMethod.OPENAUTHMOD) {
try {
final ByteBuf response = proxyConnection.getPacketHandler(OpenAuthModPacketHandler.class).sendCustomPayload(OpenAuthModConstants.JOIN_CHANNEL, PacketTypes.writeString(Unpooled.buffer(), serverIdHash)).get(6, TimeUnit.SECONDS);
if (response == null) throw new TimeoutException();
if (response.isReadable() && !response.readBoolean()) throw new TimeoutException();
} catch (TimeoutException e) {
proxyConnection.kickClient("§cAuthentication cancelled! You need to install OpenAuthMod in order to join this server.");
try {
if (ViaProxy.getConfig().getAuthMethod() == ViaProxyConfig.AuthMethod.OPENAUTHMOD) {
try {
final ByteBuf response = proxyConnection.getPacketHandler(OpenAuthModPacketHandler.class).sendCustomPayload(OpenAuthModConstants.JOIN_CHANNEL, PacketTypes.writeString(Unpooled.buffer(), serverIdHash)).get(6, TimeUnit.SECONDS);
if (response == null) throw new TimeoutException();
if (response.isReadable() && !response.readBoolean()) throw new TimeoutException();
} catch (TimeoutException e) {
proxyConnection.kickClient("§cAuthentication cancelled! You need to install OpenAuthMod in order to join this server.");
}
} else if (proxyConnection.getUserOptions().account() instanceof MicrosoftAccount microsoftAccount) {
try {
AuthLibServices.SESSION_SERVICE.joinServer(microsoftAccount.getGameProfile(), microsoftAccount.getMcProfile().getMcToken().getAccessToken(), serverIdHash);
} catch (Throwable e) {
proxyConnection.kickClient("§cFailed to authenticate with Mojang servers! Please try again in a couple of seconds.");
}
} else if (!ViaProxy.EVENT_MANAGER.call(new JoinServerRequestEvent(proxyConnection, serverIdHash)).isCancelled()) {
proxyConnection.kickClient("§cThis server is in online mode and requires a valid authentication mode.");
}
} else if (proxyConnection.getUserOptions().account() instanceof MicrosoftAccount microsoftAccount) {
try {
AuthLibServices.SESSION_SERVICE.joinServer(microsoftAccount.getGameProfile(), microsoftAccount.getMcProfile().getMcToken().getAccessToken(), serverIdHash);
} catch (Throwable e) {
proxyConnection.kickClient("§cFailed to authenticate with Mojang servers! Please try again in a couple of seconds.");
}
} else {
proxyConnection.kickClient("§cThis server is in online mode and requires a valid authentication mode.");
} catch (CloseAndReturn e) {
throw e;
} catch (Throwable e) {
Logger.LOGGER.error("Failed to join online mode server", e);
proxyConnection.kickClient("§cFailed to join online mode server. See console for more information.");
}
}