/* * This file is a part of the SteamWar software. * * Copyright (C) 2026 SteamWar.de-Serverteam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package de.steamwar; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.instrument.Instrumentation; import java.lang.reflect.Field; import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Path; import java.util.*; import java.util.logging.Logger; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; /** * Scans all plugin ClassLoaders for "plugin.accesswidener" resources * and returns the parsed entries. */ public final class AccessWidenerScanner { private static final Logger LOG = Logger.getLogger("AccessWidenerScanner"); private AccessWidenerScanner() { } /** * Scans every ClassLoader visible from the already-loaded classes for */ public static List scanAll(Instrumentation inst) { List allEntries = new ArrayList<>(); Set seen = Collections.newSetFromMap(new IdentityHashMap<>()); for (Class clazz : inst.getAllLoadedClasses()) { ClassLoader cl = clazz.getClassLoader(); if (cl == null) continue; // bootstrap — skip if (!seen.add(cl)) continue; // already processed if (!isPluginClassLoader(cl)) continue; allEntries.addAll(scanClassLoader(cl)); } return allEntries; } /** * Uses getResources() so it finds ALL matching files on the loader's classpath. */ public static List scanClassLoader(ClassLoader cl) { List entries = new ArrayList<>(); try { for (URL url : findAccessWideners(cl)) { try (InputStream in = url.openStream()) { List parsed = AccessWidenerParser.parse(in); LOG.info("[AccessWidener] Loaded " + parsed.size() + " entries from " + url); entries.addAll(parsed); } catch (IOException e) { LOG.warning("[AccessWidener] Failed to read " + url + ": " + e.getMessage()); } } } catch (IOException e) { LOG.warning("[AccessWidener] Failed to scan " + cl + ": " + e.getMessage()); } return entries; } public static List findAccessWideners(ClassLoader cl) throws IOException { List results = new ArrayList<>(); // Standard path — works on most Paper versions if (cl instanceof URLClassLoader urlCl) { return scanUrls(urlCl.getURLs()); } // Newer Paper — PluginClassLoader stores the jar as a field try { // Try common field names used across Paper versions for (String fieldName : List.of("file", "jarFile", "pluginJar", "source")) { try { Field f = cl.getClass().getDeclaredField(fieldName); f.setAccessible(true); Object value = f.get(cl); if (value instanceof File file) { return scanUrls(new URL[]{ file.toURI().toURL() }); } if (value instanceof Path path) { return scanUrls(new URL[]{ path.toUri().toURL() }); } } catch (NoSuchFieldException ignored) {} } } catch (Exception e) { LOG.warning("[AccessWidener] Could not extract JAR path from " + cl + ": " + e.getMessage()); } return results; } private static List scanUrls(URL[] urls) throws IOException { List results = new ArrayList<>(); for (URL url : urls) { if (!url.getPath().endsWith(".jar")) continue; try (ZipInputStream zip = new ZipInputStream(url.openStream())) { ZipEntry entry; while ((entry = zip.getNextEntry()) != null) { if (entry.getName().endsWith(".accesswidener")) { results.add(new URL("jar:" + url + "!/" + entry.getName())); } } } } return results; } /** * Returns true if the classloader looks like a Paper plugin classloader. * Checks by name so we don't need a direct dependency on Paper internals. */ public static boolean isPluginClassLoader(ClassLoader cl) { String name = cl.getClass().getName(); return name.contains("PluginClassLoader") // legacy Paper / Spigot || name.contains("PaperPluginLoader") // Paper 1.20.5+ || name.contains("PaperSimplePluginManager"); // just in case } }