From 8a0ddf55fe5a7c74dc482e836d45da96ebe32597 Mon Sep 17 00:00:00 2001 From: RaphiMC <50594595+RaphiMC@users.noreply.github.com> Date: Wed, 25 Sep 2024 22:27:47 +0200 Subject: [PATCH] Updated betacraft auth --- build.gradle | 2 +- .../ViaProxyClassicMPPassProvider.java | 40 +++++++++---------- .../packethandler/LoginPacketHandler.java | 6 +-- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/build.gradle b/build.gradle index f31f040..e3214ea 100644 --- a/build.gradle +++ b/build.gradle @@ -98,7 +98,7 @@ dependencies { include("net.raphimc.netminecraft:all:3.0.0-SNAPSHOT") { exclude group: "com.google.code.gson", module: "gson" } - include("net.raphimc:MinecraftAuth:4.1.1-SNAPSHOT") { + include("net.raphimc:MinecraftAuth:4.1.1") { exclude group: "com.google.code.gson", module: "gson" exclude group: "org.slf4j", module: "slf4j-api" } diff --git a/src/main/java/net/raphimc/viaproxy/protocoltranslator/providers/ViaProxyClassicMPPassProvider.java b/src/main/java/net/raphimc/viaproxy/protocoltranslator/providers/ViaProxyClassicMPPassProvider.java index 823fa86..fe6b916 100644 --- a/src/main/java/net/raphimc/viaproxy/protocoltranslator/providers/ViaProxyClassicMPPassProvider.java +++ b/src/main/java/net/raphimc/viaproxy/protocoltranslator/providers/ViaProxyClassicMPPassProvider.java @@ -18,18 +18,18 @@ package net.raphimc.viaproxy.protocoltranslator.providers; import com.google.common.hash.Hashing; -import com.google.common.io.Resources; import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.connection.UserConnection; +import net.lenni0451.commons.httpclient.HttpClient; +import net.lenni0451.commons.httpclient.handler.ThrowingResponseHandler; +import net.lenni0451.commons.httpclient.requests.impl.GetRequest; import net.raphimc.vialegacy.protocol.classic.c0_28_30toa1_0_15.provider.ClassicMPPassProvider; import net.raphimc.vialegacy.protocol.release.r1_2_4_5tor1_3_1_2.provider.OldAuthProvider; -import net.raphimc.vialegacy.protocol.release.r1_6_4tor1_7_2_5.storage.HandshakeStorage; import net.raphimc.viaproxy.ViaProxy; import net.raphimc.viaproxy.proxy.session.ProxyConnection; -import java.net.InetAddress; -import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.HexFormat; import java.util.logging.Level; public class ViaProxyClassicMPPassProvider extends ClassicMPPassProvider { @@ -37,27 +37,25 @@ public class ViaProxyClassicMPPassProvider extends ClassicMPPassProvider { @Override public String getMpPass(UserConnection user) { final String mppass = ProxyConnection.fromUserConnection(user).getUserOptions().classicMpPass(); - if (mppass != null && !mppass.isEmpty() && !mppass.equals("0")) { + if (mppass != null && !mppass.isBlank()) { return mppass; } else if (ViaProxy.getConfig().useBetacraftAuth()) { - final HandshakeStorage handshakeStorage = user.get(HandshakeStorage.class); - return getBetacraftMpPass(user, user.getProtocolInfo().getUsername(), handshakeStorage.getHostname(), handshakeStorage.getPort()); - } else { - return super.getMpPass(user); + try { + final HttpClient httpClient = new HttpClient(); + String externalIp; + try { + externalIp = httpClient.execute(new GetRequest("https://checkip.amazonaws.com"), new ThrowingResponseHandler()).getContentAsString(); + } catch (Throwable e) { + throw new RuntimeException("Failed to get external IP address!", e); + } + final byte[] hash = Hashing.sha1().hashString(externalIp.strip(), StandardCharsets.UTF_8).asBytes(); + Via.getManager().getProviders().get(OldAuthProvider.class).sendAuthRequest(user, HexFormat.of().formatHex(hash)); + } catch (Throwable e) { + Via.getPlatform().getLogger().log(Level.WARNING, "An unknown error occurred while authenticating with BetaCraft", e); + } } - } - private static String getBetacraftMpPass(final UserConnection user, final String username, final String serverIp, final int port) { - try { - final String server = InetAddress.getByName(serverIp).getHostAddress() + ":" + port; - Via.getManager().getProviders().get(OldAuthProvider.class).sendAuthRequest(user, Hashing.sha1().hashBytes(server.getBytes()).toString()); - final String mppass = Resources.toString(new URL("http://api.betacraft.uk/getmppass.jsp?user=" + username + "&server=" + server), StandardCharsets.UTF_8); - if (mppass.contains("FAILED") || mppass.contains("SERVER NOT FOUND")) return "0"; - return mppass; - } catch (Throwable e) { - Via.getPlatform().getLogger().log(Level.WARNING, "An unknown error occurred while authenticating with BetaCraft", e); - } - return "0"; + return super.getMpPass(user); } } 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 88ba13a..5fa03e1 100644 --- a/src/main/java/net/raphimc/viaproxy/proxy/packethandler/LoginPacketHandler.java +++ b/src/main/java/net/raphimc/viaproxy/proxy/packethandler/LoginPacketHandler.java @@ -73,9 +73,9 @@ public class LoginPacketHandler extends PacketHandler { if (this.loginState != LoginState.FIRST_PACKET) throw CloseAndReturn.INSTANCE; this.loginState = LoginState.SENT_HELLO; - if (loginHelloPacket.expiresAt != null && loginHelloPacket.expiresAt.isBefore(Instant.now())) { - throw new IllegalStateException("Expired public key"); - } + if (loginHelloPacket.expiresAt != null && loginHelloPacket.expiresAt.isBefore(Instant.now())) { + throw new IllegalStateException("Expired public key"); + } proxyConnection.setLoginHelloPacket(loginHelloPacket); if (loginHelloPacket.uuid != null) {