|
| 1 | +package net.lenni0451.authhook; |
| 2 | + |
| 3 | +import org.objectweb.asm.ClassReader; |
| 4 | +import org.objectweb.asm.ClassWriter; |
| 5 | +import org.objectweb.asm.tree.AbstractInsnNode; |
| 6 | +import org.objectweb.asm.tree.ClassNode; |
| 7 | +import org.objectweb.asm.tree.LdcInsnNode; |
| 8 | +import org.objectweb.asm.tree.MethodNode; |
| 9 | + |
| 10 | +import java.lang.instrument.ClassFileTransformer; |
| 11 | +import java.security.ProtectionDomain; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +public class URLRedirector implements ClassFileTransformer { |
| 15 | + |
| 16 | + private static final String URL = "https://sessionserver.mojang.com"; |
| 17 | + |
| 18 | + private final String targetAddress; |
| 19 | + private final String secretKey; |
| 20 | + |
| 21 | + public URLRedirector(final Map<String, String> config) { |
| 22 | + this.targetAddress = this.formatURL(config.get(Config.TARGET_ADDRESS)); |
| 23 | + this.secretKey = config.get(Config.SECRET_KEY); |
| 24 | + } |
| 25 | + |
| 26 | + private String formatURL(String url) { |
| 27 | + if (!url.startsWith("http://") && !url.startsWith("https://")) { |
| 28 | + throw new IllegalArgumentException("Invalid URL (missing protocol): " + url); |
| 29 | + } |
| 30 | + while (url.endsWith("/")) url = url.substring(0, url.length() - 1); |
| 31 | + return url; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { |
| 36 | + try { |
| 37 | + ClassNode node = this.read(classfileBuffer); |
| 38 | + boolean modified = false; |
| 39 | + for (MethodNode method : node.methods) { |
| 40 | + for (AbstractInsnNode insn : method.instructions) { |
| 41 | + if (insn instanceof LdcInsnNode ldc && ldc.cst instanceof String str) { |
| 42 | + if (str.startsWith(URL)) { |
| 43 | + str = str.substring(URL.length()); |
| 44 | + str = this.targetAddress + "/" + this.secretKey + str; |
| 45 | + ldc.cst = str; |
| 46 | + |
| 47 | + modified = true; |
| 48 | + System.out.println("Redirected Auth URL in class '" + node.name + "' method '" + method.name + "'"); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + return modified ? this.write(node) : null; |
| 54 | + } catch (Throwable ignored) { |
| 55 | + } |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + private ClassNode read(final byte[] bytes) { |
| 60 | + ClassNode node = new ClassNode(); |
| 61 | + ClassReader reader = new ClassReader(bytes); |
| 62 | + reader.accept(node, ClassReader.EXPAND_FRAMES); |
| 63 | + return node; |
| 64 | + } |
| 65 | + |
| 66 | + private byte[] write(final ClassNode node) { |
| 67 | + ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); |
| 68 | + node.accept(writer); |
| 69 | + return writer.toByteArray(); |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments