Add debug info to fail load, use same defineMethod instance

This commit is contained in:
libraryaddict 2020-02-25 22:42:23 +13:00
parent 061161dad7
commit b8de529b25
No known key found for this signature in database
GPG Key ID: 052E4FBCD257AEA4
3 changed files with 39 additions and 22 deletions

View File

@ -1,6 +1,7 @@
package me.libraryaddict.disguise.utilities.reflection.asm;
import com.google.gson.Gson;
import lombok.Getter;
import org.objectweb.asm.*;
import java.io.IOException;
@ -14,9 +15,16 @@ import java.util.Map;
* Created by libraryaddict on 17/02/2020.
*/
public class Asm13 implements IAsm {
@Getter
private Method defineMethod;
public Asm13() throws NoSuchMethodException {
defineMethod = getDefineClassMethod();
}
public Class<?> createClassWithoutMethods(String className,
ArrayList<Map.Entry<String, String>> illegalMethods) throws IOException, InvocationTargetException,
IllegalAccessException, NoSuchMethodException, NoSuchFieldException {
IllegalAccessException, NoSuchFieldException {
ClassReader cr = new ClassReader(
getClass().getClassLoader().getResourceAsStream(className.replace(".", "/") + ".class"));
ClassWriter writer = new ClassWriter(cr, 0);
@ -42,14 +50,13 @@ public class Asm13 implements IAsm {
Field field = loader.getClass().getDeclaredField("classes");
field.setAccessible(true);
Map<String, Class<?>> map = (Map<String, Class<?>>) field.get(loader);
Class newClass = (Class<?>) getDefineClassMethod()
.invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length);
Class newClass = (Class<?>) defineMethod.invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length);
map.put(className, newClass);
return newClass;
}
private static Method getDefineClassMethod() throws NoSuchMethodException {
private Method getDefineClassMethod() throws NoSuchMethodException {
Method defineClass = ClassLoader.class
.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
defineClass.setAccessible(true);

View File

@ -1,5 +1,6 @@
package me.libraryaddict.disguise.utilities.reflection.asm;
import lombok.Getter;
import org.bukkit.craftbukkit.libs.org.objectweb.asm.*;
import java.io.IOException;
@ -13,9 +14,16 @@ import java.util.Map;
* Created by libraryaddict on 17/02/2020.
*/
public class Asm14 implements IAsm {
@Getter
private Method defineMethod;
public Asm14() throws NoSuchMethodException {
defineMethod = getDefineClassMethod();
}
public Class<?> createClassWithoutMethods(String className,
ArrayList<Map.Entry<String, String>> illegalMethods) throws IOException, InvocationTargetException,
IllegalAccessException, NoSuchMethodException, NoSuchFieldException {
IllegalAccessException, NoSuchFieldException {
ClassReader cr = new ClassReader(
getClass().getClassLoader().getResourceAsStream(className.replace(".", "/") + ".class"));
ClassWriter writer = new ClassWriter(cr, 0);
@ -41,16 +49,13 @@ public class Asm14 implements IAsm {
Field field = loader.getClass().getDeclaredField("classes");
field.setAccessible(true);
Map<String, Class<?>> map = (Map<String, Class<?>>) field.get(loader);
Class newClass =
(Class<?>) getDefineClassMethod()
.invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length);
Class newClass = (Class<?>) defineMethod.invoke(getClass().getClassLoader(), className, bytes, 0, bytes.length);
map.put(className, newClass);
return newClass;
}
private static Method getDefineClassMethod() throws NoSuchMethodException {
private Method getDefineClassMethod() throws NoSuchMethodException {
Method defineClass = ClassLoader.class
.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
defineClass.setAccessible(true);

View File

@ -1,5 +1,6 @@
package me.libraryaddict.disguise.utilities.reflection.asm;
import com.google.gson.Gson;
import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
@ -78,19 +79,21 @@ public class WatcherSanitizer {
e.printStackTrace();
}
IAsm asm;
if (NmsVersion.v1_14.isSupported()) {
asm = new Asm14();
} else {
if (!NmsVersion.v1_13.isSupported()) {
new AsmDownloader();
}
asm = new Asm13();
}
ArrayList<String> mapped = new ArrayList<>();
try (InputStream stream = LibsDisguises.getInstance().getResource("ANTI_PIRACY_ENCRYPTION")) {
IAsm asm;
if (NmsVersion.v1_14.isSupported()) {
asm = new Asm14();
} else {
if (!NmsVersion.v1_13.isSupported()) {
new AsmDownloader();
}
asm = new Asm13();
}
List<String> lines = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines()
.collect(Collectors.toList());
@ -115,10 +118,12 @@ public class WatcherSanitizer {
for (Map.Entry<String, ArrayList<Map.Entry<String, String>>> entry : toRemove.entrySet()) {
Class result = asm.createClassWithoutMethods(entry.getKey(), entry.getValue());
mapped.add(entry.getKey());
}
}
catch (IOException | NoClassDefFoundError | IllegalAccessException | InvocationTargetException | NoSuchMethodException | NoSuchFieldException e) {
catch (IOException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | NoSuchFieldException | LinkageError e) {
e.printStackTrace();
LibsDisguises.getInstance().getLogger().severe("Registered: " + new Gson().toJson(mapped));
}
}
}