From 27ba9eb9595d281202b93944c49aa1303f9bf694 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 9 Jul 2022 19:55:35 +0100 Subject: [PATCH] Restore full Java 8 support --- .../bungee/SpoofedLoginResult.java | 26 +++++++-- ...ava => SpoofedLoginResultJdkInternal.java} | 6 +- .../bungee/SpoofedLoginResultReflection.java | 58 +++++++++++++++++++ 3 files changed, 81 insertions(+), 9 deletions(-) rename bungeeguard-bungee/src/main/java/me/lucko/bungeeguard/bungee/{SpoofedLoginResultJava8.java => SpoofedLoginResultJdkInternal.java} (88%) create mode 100644 bungeeguard-bungee/src/main/java/me/lucko/bungeeguard/bungee/SpoofedLoginResultReflection.java diff --git a/bungeeguard-bungee/src/main/java/me/lucko/bungeeguard/bungee/SpoofedLoginResult.java b/bungeeguard-bungee/src/main/java/me/lucko/bungeeguard/bungee/SpoofedLoginResult.java index 8017fb8..115e781 100644 --- a/bungeeguard-bungee/src/main/java/me/lucko/bungeeguard/bungee/SpoofedLoginResult.java +++ b/bungeeguard-bungee/src/main/java/me/lucko/bungeeguard/bungee/SpoofedLoginResult.java @@ -48,12 +48,17 @@ abstract class SpoofedLoginResult extends LoginResult { static { Class implClass; - try { - // try to use stackwalker if running Java 9 or newer - Class.forName("java.lang.StackWalker"); - implClass = Class.forName("me.lucko.bungeeguard.bungee.SpoofedLoginResultJava9").asSubclass(SpoofedLoginResult.class); - } catch (ClassNotFoundException e) { - implClass = SpoofedLoginResultJava8.class; + if (classExists("java.lang.StackWalker")) { + try { + implClass = Class.forName("me.lucko.bungeeguard.bungee.SpoofedLoginResultJava9") + .asSubclass(SpoofedLoginResult.class); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } else if (classExists("jdk.internal.reflect.Reflection")) { + implClass = SpoofedLoginResultJdkInternal.class; + } else { + implClass = SpoofedLoginResultReflection.class; } try { @@ -89,6 +94,15 @@ static void inject(InitialHandler handler, String token) { } } + private static boolean classExists(String className) { + try { + Class.forName(className); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } + private final Property bungeeGuardToken; private final Property[] bungeeGuardTokenArray; private final boolean offline; diff --git a/bungeeguard-bungee/src/main/java/me/lucko/bungeeguard/bungee/SpoofedLoginResultJava8.java b/bungeeguard-bungee/src/main/java/me/lucko/bungeeguard/bungee/SpoofedLoginResultJdkInternal.java similarity index 88% rename from bungeeguard-bungee/src/main/java/me/lucko/bungeeguard/bungee/SpoofedLoginResultJava8.java rename to bungeeguard-bungee/src/main/java/me/lucko/bungeeguard/bungee/SpoofedLoginResultJdkInternal.java index eef31db..2c79a99 100644 --- a/bungeeguard-bungee/src/main/java/me/lucko/bungeeguard/bungee/SpoofedLoginResultJava8.java +++ b/bungeeguard-bungee/src/main/java/me/lucko/bungeeguard/bungee/SpoofedLoginResultJdkInternal.java @@ -30,15 +30,15 @@ import jdk.internal.reflect.Reflection; import net.md_5.bungee.protocol.Property; -public class SpoofedLoginResultJava8 extends SpoofedLoginResult { +public class SpoofedLoginResultJdkInternal extends SpoofedLoginResult { // online mode constructor - public SpoofedLoginResultJava8(LoginResult oldProfile, String bungeeGuardToken) { + public SpoofedLoginResultJdkInternal(LoginResult oldProfile, String bungeeGuardToken) { super(oldProfile, bungeeGuardToken); } // offline mode constructor - public SpoofedLoginResultJava8(String bungeeGuardToken) { + public SpoofedLoginResultJdkInternal(String bungeeGuardToken) { super(bungeeGuardToken); } diff --git a/bungeeguard-bungee/src/main/java/me/lucko/bungeeguard/bungee/SpoofedLoginResultReflection.java b/bungeeguard-bungee/src/main/java/me/lucko/bungeeguard/bungee/SpoofedLoginResultReflection.java new file mode 100644 index 0000000..c8b2646 --- /dev/null +++ b/bungeeguard-bungee/src/main/java/me/lucko/bungeeguard/bungee/SpoofedLoginResultReflection.java @@ -0,0 +1,58 @@ +/* + * This file is part of BungeeGuard, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.bungeeguard.bungee; + +import net.md_5.bungee.connection.LoginResult; +import net.md_5.bungee.protocol.Property; + +public class SpoofedLoginResultReflection extends SpoofedLoginResult { + + // online mode constructor + public SpoofedLoginResultReflection(LoginResult oldProfile, String bungeeGuardToken) { + super(oldProfile, bungeeGuardToken); + } + + // offline mode constructor + public SpoofedLoginResultReflection(String bungeeGuardToken) { + super(bungeeGuardToken); + } + + @Override + public Property[] getProperties() { + StackTraceElement[] trace = new Exception().getStackTrace(); + + Class caller = null; + if (trace.length >= 2) { + try { + caller = Class.forName(trace[1].getClassName()); + } catch (ClassNotFoundException e) { + // ignore + } + } + + return getSpoofedProperties(caller); + } +}