From f97405604deef438f759a05ad18037a66e15eff5 Mon Sep 17 00:00:00 2001 From: RaphiMC <50594595+RaphiMC@users.noreply.github.com> Date: Thu, 2 Nov 2023 19:30:46 +0100 Subject: [PATCH] Added event to skip Mojang auth --- .../plugins/events/PreMojangAuthEvent.java | 35 +++++++++++++++++++ .../packethandler/LoginPacketHandler.java | 28 ++++++++------- 2 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 src/main/java/net/raphimc/viaproxy/plugins/events/PreMojangAuthEvent.java diff --git a/src/main/java/net/raphimc/viaproxy/plugins/events/PreMojangAuthEvent.java b/src/main/java/net/raphimc/viaproxy/plugins/events/PreMojangAuthEvent.java new file mode 100644 index 00000000..e8fd357d --- /dev/null +++ b/src/main/java/net/raphimc/viaproxy/plugins/events/PreMojangAuthEvent.java @@ -0,0 +1,35 @@ +/* + * 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.plugins.events; + +import net.raphimc.viaproxy.plugins.events.types.EventCancellable; +import net.raphimc.viaproxy.proxy.session.ProxyConnection; + +public class PreMojangAuthEvent extends EventCancellable { + + private final ProxyConnection proxyConnection; + + public PreMojangAuthEvent(final ProxyConnection proxyConnection) { + this.proxyConnection = proxyConnection; + } + + public ProxyConnection getProxyConnection() { + return this.proxyConnection; + } + +} diff --git a/src/main/java/net/raphimc/viaproxy/proxy/packethandler/LoginPacketHandler.java b/src/main/java/net/raphimc/viaproxy/proxy/packethandler/LoginPacketHandler.java index 91e14e52..a561d635 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/packethandler/LoginPacketHandler.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/packethandler/LoginPacketHandler.java @@ -32,6 +32,7 @@ import net.raphimc.viaproxy.cli.options.Options; import net.raphimc.viaproxy.plugins.PluginManager; import net.raphimc.viaproxy.plugins.events.ClientLoggedInEvent; +import net.raphimc.viaproxy.plugins.events.PreMojangAuthEvent; import net.raphimc.viaproxy.proxy.LoginState; import net.raphimc.viaproxy.proxy.external_interface.AuthLibServices; import net.raphimc.viaproxy.proxy.external_interface.ExternalInterface; @@ -121,20 +122,21 @@ public boolean handleC2P(IPacket packet, List listeners) final SecretKey secretKey = CryptUtil.decryptSecretKey(KEY_PAIR.getPrivate(), loginKeyPacket.encryptedSecretKey); this.proxyConnection.getC2P().attr(MCPipeline.ENCRYPTION_ATTRIBUTE_KEY).set(new AESEncryption(secretKey)); - final String userName = this.proxyConnection.getGameProfile().getName(); - - try { - final String serverHash = new BigInteger(CryptUtil.computeServerIdHash("", KEY_PAIR.getPublic(), secretKey)).toString(16); - final GameProfile mojangProfile = AuthLibServices.SESSION_SERVICE.hasJoinedServer(this.proxyConnection.getGameProfile(), serverHash, null); - if (mojangProfile == null) { - Logger.u_err("auth", this.proxyConnection.getC2P().remoteAddress(), this.proxyConnection.getGameProfile(), "Invalid session"); - this.proxyConnection.kickClient("§cInvalid session! Please restart minecraft (and the launcher) and try again."); - } else { - this.proxyConnection.setGameProfile(mojangProfile); + if (!PluginManager.EVENT_MANAGER.call(new PreMojangAuthEvent(this.proxyConnection)).isCancelled()) { + final String userName = this.proxyConnection.getGameProfile().getName(); + try { + final String serverHash = new BigInteger(CryptUtil.computeServerIdHash("", KEY_PAIR.getPublic(), secretKey)).toString(16); + final GameProfile mojangProfile = AuthLibServices.SESSION_SERVICE.hasJoinedServer(this.proxyConnection.getGameProfile(), serverHash, null); + if (mojangProfile == null) { + Logger.u_err("auth", this.proxyConnection.getC2P().remoteAddress(), this.proxyConnection.getGameProfile(), "Invalid session"); + this.proxyConnection.kickClient("§cInvalid session! Please restart minecraft (and the launcher) and try again."); + } else { + this.proxyConnection.setGameProfile(mojangProfile); + } + Logger.u_info("auth", this.proxyConnection.getC2P().remoteAddress(), this.proxyConnection.getGameProfile(), "Authenticated as " + this.proxyConnection.getGameProfile().getId().toString()); + } catch (Throwable e) { + throw new RuntimeException("Failed to make session request for user '" + userName + "'!", e); } - Logger.u_info("auth", this.proxyConnection.getC2P().remoteAddress(), this.proxyConnection.getGameProfile(), "Authenticated as " + this.proxyConnection.getGameProfile().getId().toString()); - } catch (Throwable e) { - throw new RuntimeException("Failed to make session request for user '" + userName + "'!", e); } PluginManager.EVENT_MANAGER.call(new ClientLoggedInEvent(proxyConnection));