From c2c8d0c318b161a38efee8ec124f6babcdff4a15 Mon Sep 17 00:00:00 2001 From: overdyn Date: Sun, 1 Nov 2026 00:56:59 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9F=D0=BE=D0=B4=D0=BA=D0=BB=D1=8E=D1=87?= =?UTF-8?q?=D0=B8=D1=82=D1=8C=20pnLibrary=200.0.1-dev.6:=20bootstrap,=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D0=B8=D1=82=D0=B8=D0=BA=D0=B0,=20=D0=B4?= =?UTF-8?q?=D0=B8=D0=B0=D0=B3=D0=BD=D0=BE=D1=81=D1=82=D0=B8=D0=BA=D0=B0,?= =?UTF-8?q?=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?,=20bStats?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .pnlibrary/releases.json | 5 + .../pnauth/bungee/PnAuthBungeePlugin.kt | 33 ++++++ bungeecord/src/main/resources/bungee.yml | 2 + .../pnauth/paper/PnAuthPaperPlugin.kt | 40 +++++++ .../paper/PnLibraryLifecycleListener.kt | 16 +++ paper/src/main/resources/plugin.yml | 23 +++- plugin/build.gradle | 12 ++ .../pnauth/integration/PnLibraryRuntime.kt | 111 ++++++++++++++++++ .../pnauth/velocity/PnAuthVelocityPlugin.kt | 32 ++++- 9 files changed, 272 insertions(+), 2 deletions(-) create mode 100644 .pnlibrary/releases.json create mode 100644 paper/src/main/kotlin/ru/privatenull/pnauth/paper/PnLibraryLifecycleListener.kt create mode 100644 plugin/src/main/kotlin/ru/privatenull/pnauth/integration/PnLibraryRuntime.kt diff --git a/.pnlibrary/releases.json b/.pnlibrary/releases.json new file mode 100644 index 0000000..40ab2b7 --- /dev/null +++ b/.pnlibrary/releases.json @@ -0,0 +1,5 @@ +{ + "schema": 1, + "product": "pnauth", + "releases": [] +} diff --git a/bungeecord/src/main/kotlin/ru/privatenull/pnauth/bungee/PnAuthBungeePlugin.kt b/bungeecord/src/main/kotlin/ru/privatenull/pnauth/bungee/PnAuthBungeePlugin.kt index 5fdc6e1..f03a3b0 100644 --- a/bungeecord/src/main/kotlin/ru/privatenull/pnauth/bungee/PnAuthBungeePlugin.kt +++ b/bungeecord/src/main/kotlin/ru/privatenull/pnauth/bungee/PnAuthBungeePlugin.kt @@ -3,22 +3,55 @@ package ru.privatenull.pnauth.bungee import net.md_5.bungee.api.plugin.Plugin import ru.privatenull.pnauth.api.AuthApi import ru.privatenull.pnauth.bungee.proxy.BungeeProxyAdapter +import org.pnfolder.pnlibrary.bungee.bootstrap.PnLibraryBootstrap +import ru.privatenull.pnauth.integration.PnLibraryRuntime import ru.privatenull.pnauth.kernel.ExtensionKernel import ru.privatenull.pnauth.platform.Platform +import java.util.logging.Level class PnAuthBungeePlugin : Plugin() { private var runtime: PnAuthBungeeRuntime? = null + private var pnLibraryReady = false + private var pnLibrary: PnLibraryRuntime? = null override fun onLoad() { + // A missing pnLibrary is downloaded for the next start; pnAuth itself keeps working without it. + pnLibraryReady = try { + PnLibraryBootstrap.ensureInstalled(this, PnLibraryRuntime.MINIMUM_VERSION) + } catch (failure: Throwable) { + logger.log(Level.WARNING, "Не удалось подготовить pnLibrary; pnAuth работает без обновлений и диагностики.", failure) + false + } val created = PnAuthBungeeRuntime.builder().plugin(this).build() runtime = if (created.onLoad()) created else null } override fun onEnable() { runtime?.onEnable() + if (runtime != null) connectPnLibrary() + } + + private fun connectPnLibrary() { + if (!pnLibraryReady) return + try { + val connected = PnLibraryRuntime.connect(this) + pnLibrary = connected + connected.register(dataFolder.toPath(), "BungeeCord") { mapOf("version" to description.version) } + connected.enabled("BungeeCord") + } catch (failure: Throwable) { + logger.log(Level.WARNING, "pnLibrary не подключена к pnAuth; плагин работает без неё.", failure) + closePnLibrary() + } + } + + private fun closePnLibrary() { + val closing = pnLibrary ?: return + pnLibrary = null + runCatching { closing.close() } } override fun onDisable() { + closePnLibrary() runtime?.close() } diff --git a/bungeecord/src/main/resources/bungee.yml b/bungeecord/src/main/resources/bungee.yml index 4b3722c..3e97893 100644 --- a/bungeecord/src/main/resources/bungee.yml +++ b/bungeecord/src/main/resources/bungee.yml @@ -5,3 +5,5 @@ author: privatenull description: Extensible authentication and player API for BungeeCord, Velocity, Paper, and Folia. depends: - packetevents +softDepends: + - pnLibrary diff --git a/paper/src/main/kotlin/ru/privatenull/pnauth/paper/PnAuthPaperPlugin.kt b/paper/src/main/kotlin/ru/privatenull/pnauth/paper/PnAuthPaperPlugin.kt index d23849a..8931a60 100644 --- a/paper/src/main/kotlin/ru/privatenull/pnauth/paper/PnAuthPaperPlugin.kt +++ b/paper/src/main/kotlin/ru/privatenull/pnauth/paper/PnAuthPaperPlugin.kt @@ -5,14 +5,26 @@ import org.bukkit.plugin.java.JavaPlugin import ru.privatenull.pnauth.api.AuthApi import ru.privatenull.pnauth.kernel.ExtensionKernel import ru.privatenull.pnauth.platform.Platform +import org.pnfolder.pnlibrary.bukkit.bootstrap.PnLibraryBootstrap import ru.privatenull.pnauth.dependency.PacketEventsBootstrap +import ru.privatenull.pnauth.integration.PnLibraryRuntime +import java.util.logging.Level /** Paper/Folia bootstrap. All reusable behavior remains in the shared module. */ class PnAuthPaperPlugin : JavaPlugin() { private var runtime: PnAuthPaperRuntime? = null private var dependencyReady = false + private var pnLibraryReady = false + private var pnLibrary: PnLibraryRuntime? = null override fun onLoad() { + // pnAuth keeps protecting the server even when pnLibrary cannot be prepared; only the shared services go missing. + pnLibraryReady = try { + PnLibraryBootstrap.ensureInstalled(this, PnLibraryRuntime.MINIMUM_VERSION) + } catch (failure: Throwable) { + logger.log(Level.WARNING, "Не удалось подготовить pnLibrary; pnAuth работает без обновлений и диагностики.", failure) + false + } val result = PacketEventsBootstrap.ensure( PacketEventsBootstrap.Platform.PAPER, dataFolder.toPath(), @@ -36,9 +48,37 @@ class PnAuthPaperPlugin : JavaPlugin() { val created = PnAuthPaperRuntime.builder().plugin(this).build() runtime = created created.onEnable() + connectPnLibrary() + } + + private fun connectPnLibrary() { + if (!pnLibraryReady) return + try { + val connected = PnLibraryRuntime.connect(this) + pnLibrary = connected + connected.register(dataFolder.toPath(), "Paper") { + mapOf("version" to description.version, "folia" to isFolia()) + } + connected.enabled("Paper") + server.pluginManager.registerEvents(PnLibraryLifecycleListener(this), this) + } catch (failure: Throwable) { + logger.log(Level.WARNING, "pnLibrary не подключена к pnAuth; плагин работает без неё.", failure) + closePnLibrary() + } } + internal fun closePnLibrary() { + val closing = pnLibrary ?: return + pnLibrary = null + runCatching { closing.close() } + } + + private fun isFolia(): Boolean = runCatching { + Class.forName("io.papermc.paper.threadedregions.RegionizedServer") + }.isSuccess + override fun onDisable() { + closePnLibrary() runtime?.close() } diff --git a/paper/src/main/kotlin/ru/privatenull/pnauth/paper/PnLibraryLifecycleListener.kt b/paper/src/main/kotlin/ru/privatenull/pnauth/paper/PnLibraryLifecycleListener.kt new file mode 100644 index 0000000..06f60f4 --- /dev/null +++ b/paper/src/main/kotlin/ru/privatenull/pnauth/paper/PnLibraryLifecycleListener.kt @@ -0,0 +1,16 @@ +package ru.privatenull.pnauth.paper + +import org.bukkit.event.EventHandler +import org.bukkit.event.EventPriority +import org.bukkit.event.Listener +import org.bukkit.event.server.PluginDisableEvent + +/** Releases pnAuth's pnLibrary registration before the shared runtime shuts down or is unloaded by hand. */ +class PnLibraryLifecycleListener(private val plugin: PnAuthPaperPlugin) : Listener { + + @EventHandler(priority = EventPriority.MONITOR) + fun onPluginDisable(event: PluginDisableEvent) { + if (!event.plugin.name.equals("pnLibrary", ignoreCase = true)) return + plugin.closePnLibrary() + } +} diff --git a/paper/src/main/resources/plugin.yml b/paper/src/main/resources/plugin.yml index 1dbd28d..b9f60b4 100644 --- a/paper/src/main/resources/plugin.yml +++ b/paper/src/main/resources/plugin.yml @@ -5,7 +5,7 @@ api-version: '1.20' folia-supported: true author: privatenull description: Extensible authentication and player API for Paper and Folia. -softdepend: [packetevents] +softdepend: [packetevents, pnLibrary] commands: _pnauthui: description: Internal pnAuth UI action dispatcher. @@ -27,3 +27,24 @@ permissions: pnauth.admin.reload: description: Allows reloading the pnAuth configuration. default: op + pnauth.admin.commands.unregister: + description: Allows /auth unregister. + default: op + pnauth.admin.commands.changepassword: + description: Allows /auth changepassword. + default: op + pnauth.admin.commands.forcelogin: + description: Allows /auth forcelogin. + default: op + pnauth.admin.commands.forceregister: + description: Allows /auth forceregister. + default: op + pnauth.admin.commands.forcepremium: + description: Allows /auth forcepremium. + default: op + pnauth.admin.commands.broadcast: + description: Allows /auth broadcast. + default: op + pnauth.admin.commands.migrate: + description: Allows /auth migrate. + default: op diff --git a/plugin/build.gradle b/plugin/build.gradle index 993bbab..721ab5c 100644 --- a/plugin/build.gradle +++ b/plugin/build.gradle @@ -4,6 +4,7 @@ plugins { } def pluginVersion = version.toString() +def pnLibraryVersion = '0.0.1-dev.6' def generatedSources = layout.buildDirectory.dir('generated/sources/pnauth') configurations.runtimeClasspath { // PacketEvents is a required external plugin and owns Adventure on BungeeCord. @@ -74,6 +75,12 @@ dependencies { compileOnly 'com.github.retrooper:packetevents-bungeecord:2.13.1-SNAPSHOT' compileOnly 'com.github.retrooper:packetevents-spigot:2.13.0' kapt 'com.velocitypowered:velocity-api:3.3.0-SNAPSHOT' + // The shared runtime plugin is installed by the pnLibrary bootstrap that is shaded into the jar; + // only its public API is needed to compile. + implementation "io.github.pnfolder:pnlibrary-bootstrap-bukkit:${pnLibraryVersion}" + implementation "io.github.pnfolder:pnlibrary-bootstrap-bungee:${pnLibraryVersion}" + implementation "io.github.pnfolder:pnlibrary-bootstrap-velocity:${pnLibraryVersion}" + compileOnly "io.github.pnfolder:pnlibrary-api:${pnLibraryVersion}" implementation project(':shared') implementation 'net.kyori:adventure-text-minimessage:4.26.1' implementation 'net.kyori:adventure-text-serializer-gson:4.26.1' @@ -109,6 +116,11 @@ tasks.named('shadowJar') { relocate 'org.yaml.snakeyaml', 'ru.privatenull.pnauth.libs.snakeyaml' relocate 'net.elytrium.serializer', 'ru.privatenull.pnauth.libs.serializer' relocate 'com.fasterxml.jackson', 'ru.privatenull.pnauth.libs.jackson' + relocate 'org.pnfolder.pnlibrary.bootstrap', 'ru.privatenull.pnauth.libs.pnbootstrap' + relocate 'org.pnfolder.pnlibrary.bukkit.bootstrap', 'ru.privatenull.pnauth.libs.pnbootstrap.bukkit' + relocate 'org.pnfolder.pnlibrary.bungee.bootstrap', 'ru.privatenull.pnauth.libs.pnbootstrap.bungee' + relocate 'org.pnfolder.pnlibrary.velocity.bootstrap', 'ru.privatenull.pnauth.libs.pnbootstrap.velocity' + relocate 'org.pnfolder.pnlibrary.console', 'ru.privatenull.pnauth.libs.pnbootstrap.console' manifest { attributes( 'Implementation-Title': 'pnAuth', diff --git a/plugin/src/main/kotlin/ru/privatenull/pnauth/integration/PnLibraryRuntime.kt b/plugin/src/main/kotlin/ru/privatenull/pnauth/integration/PnLibraryRuntime.kt new file mode 100644 index 0000000..506a5f0 --- /dev/null +++ b/plugin/src/main/kotlin/ru/privatenull/pnauth/integration/PnLibraryRuntime.kt @@ -0,0 +1,111 @@ +package ru.privatenull.pnauth.integration + +import org.pnfolder.pnlibrary.api.diagnostics.DiagnosticConfiguration +import org.pnfolder.pnlibrary.api.diagnostics.DiagnosticContainer +import org.pnfolder.pnlibrary.api.plugin.ModuleContext +import org.pnfolder.pnlibrary.api.plugin.PluginRegistration +import org.pnfolder.pnlibrary.api.remote.PolicyErrorAction +import org.pnfolder.pnlibrary.api.remote.RemotePolicySettings +import org.pnfolder.pnlibrary.api.runtime.PnLibrary +import org.pnfolder.pnlibrary.api.runtime.PnLibraryIdentity +import org.pnfolder.pnlibrary.api.runtime.PnLibraryProvider +import org.pnfolder.pnlibrary.api.updates.UpdateChannel +import java.nio.file.Path +import java.util.function.Supplier + +/** + * pnLibrary integration shared by BungeeCord, Velocity and Paper. Loaded only after the pnLibrary bootstrap + * confirmed that a suitable runtime is installed; any failure here must never stop pnAuth itself. + */ +class PnLibraryRuntime private constructor( + private val owner: Any, + private val runtime: PnLibrary +) : AutoCloseable { + private var registration: PluginRegistration? = null + private var module: ModuleContext? = null + + /** Registers pnAuth with the policy, diagnostics and update services. */ + fun register(dataFolder: Path, platform: String, snapshot: Supplier>) { + check(registration == null) { "pnAuth уже зарегистрирован в pnLibrary" } + val created = runtime.plugins.register(owner) + registration = created + try { + module = created.registerModule(MODULE_ID) { builder -> + builder + // A policy that cannot be checked (offline, JRE without a compiler) never stops pnAuth; + // only an explicit denial from the policy does. + .remotePolicy( + RemotePolicySettings.builder() + .source(POLICY_SOURCE) + .onError(PolicyErrorAction.KEEP_RUNNING) + .support(PnLibraryIdentity.SUPPORT_URL) + .build() + ) + .metrics { metrics -> metrics.bStats(BSTATS_PROJECT_ID) } + .diagnostics( + dataFolder, + DiagnosticContainer.builder(MODULE_ID) + .value("platform", platform) + .snapshot(snapshot) + .configuration( + DiagnosticConfiguration.builder("config.yml") + .secretKeyRegex(SECRET_KEYS) + .build() + ) + .build() + ) + .updates("pnFolder", "pnAuth") { updater -> + updater + .channel(UpdateChannel.STABLE) + .automaticDownload(false) + .artifact(ARTIFACT_PATTERN, 17) + } + } + } catch (failure: RuntimeException) { + closeRegistration() + throw failure + } + } + + /** Prints the "enabled" card in the console. */ + fun enabled(platform: String) { + module?.lifecycle?.enabled() + ?.ok("Платформа", platform) + ?.show() + } + + override fun close() { + val closing = module + module = null + if (closing != null && !closing.isClosed) { + runCatching { closing.lifecycle.disabled().show() } + } + closeRegistration() + } + + private fun closeRegistration() { + val closing = registration + registration = null + if (closing != null && !closing.isClosed) closing.close() + } + + companion object { + const val MINIMUM_VERSION = "0.0.1-dev.6" + private const val MODULE_ID = "pnauth" + private const val BSTATS_PROJECT_ID = 35046 + private const val POLICY_SOURCE = + "https://raw.githubusercontent.com/pnFolder/pnlibrary-policy/main/.pnlibrary/policies/PnAuthPolicy.java" + private const val ARTIFACT_PATTERN = "(?i)^pnAuth-[0-9].*[.]jar$" + private const val SECRET_KEYS = + "(?i).*(password|passwd|secret|token|key|salt|uri|url|user(name)?|webhook|api)$" + + @JvmStatic + fun connect(owner: Any): PnLibraryRuntime { + val runtime = PnLibraryProvider.get() + check(runtime.isAtLeastVersion(MINIMUM_VERSION)) { + "pnAuth требует pnLibrary $MINIMUM_VERSION или новее, установлена ${runtime.version}" + } + return PnLibraryRuntime(owner, runtime) + } + } +} diff --git a/velocity/src/main/kotlin/ru/privatenull/pnauth/velocity/PnAuthVelocityPlugin.kt b/velocity/src/main/kotlin/ru/privatenull/pnauth/velocity/PnAuthVelocityPlugin.kt index 04a9322..d02ec79 100644 --- a/velocity/src/main/kotlin/ru/privatenull/pnauth/velocity/PnAuthVelocityPlugin.kt +++ b/velocity/src/main/kotlin/ru/privatenull/pnauth/velocity/PnAuthVelocityPlugin.kt @@ -9,7 +9,9 @@ import com.velocitypowered.api.plugin.Dependency import com.velocitypowered.api.plugin.Plugin import com.velocitypowered.api.plugin.annotation.DataDirectory import com.velocitypowered.api.proxy.ProxyServer +import org.pnfolder.pnlibrary.velocity.bootstrap.PnLibraryBootstrap import org.slf4j.Logger +import ru.privatenull.pnauth.integration.PnLibraryRuntime import ru.privatenull.pnauth.api.AuthApi import ru.privatenull.pnauth.kernel.ExtensionKernel import ru.privatenull.pnauth.platform.Platform @@ -21,7 +23,7 @@ import java.nio.file.Path name = "pnAuth", version = PnAuthBuild.VERSION, authors = ["privatenull"], - dependencies = [Dependency(id = "packetevents", optional = true)] + dependencies = [Dependency(id = "packetevents", optional = true), Dependency(id = "pnlibrary", optional = true)] ) class PnAuthVelocityPlugin @Inject constructor( private val proxy: ProxyServer, @@ -29,9 +31,17 @@ class PnAuthVelocityPlugin @Inject constructor( @DataDirectory private val dataDirectory: Path ) { private var runtime: PnAuthVelocityRuntime? = null + private var pnLibrary: PnLibraryRuntime? = null @Subscribe fun onProxyInitialization(event: ProxyInitializeEvent) { + // A missing pnLibrary is downloaded for the next start; pnAuth itself keeps working without it. + val pnLibraryReady = try { + PnLibraryBootstrap.ensureInstalled(proxy, this, PnLibraryRuntime.MINIMUM_VERSION) + } catch (failure: Throwable) { + logger.warn("Не удалось подготовить pnLibrary; pnAuth работает без обновлений и диагностики.", failure) + false + } val created = PnAuthVelocityRuntime.builder() .owner(this) .proxy(proxy) @@ -40,6 +50,25 @@ class PnAuthVelocityPlugin @Inject constructor( .build() runtime = created created.onProxyInitialization() + if (pnLibraryReady) connectPnLibrary() + } + + private fun connectPnLibrary() { + try { + val connected = PnLibraryRuntime.connect(this) + pnLibrary = connected + connected.register(dataDirectory, "Velocity") { mapOf("version" to PnAuthBuild.VERSION) } + connected.enabled("Velocity") + } catch (failure: Throwable) { + logger.warn("pnLibrary не подключена к pnAuth; плагин работает без неё.", failure) + closePnLibrary() + } + } + + private fun closePnLibrary() { + val closing = pnLibrary ?: return + pnLibrary = null + runCatching { closing.close() } } @Subscribe @@ -49,6 +78,7 @@ class PnAuthVelocityPlugin @Inject constructor( @Subscribe fun onProxyShutdown(event: ProxyShutdownEvent) { + closePnLibrary() runtime?.close() } From 93e19ce6d669f8d1ac563cb6ec6803d61dca7c5f Mon Sep 17 00:00:00 2001 From: overdyn Date: Sun, 1 Nov 2026 00:58:33 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9C=D0=B5=D1=82=D1=80=D0=B8=D0=BA=D0=B8:?= =?UTF-8?q?=20=D0=BE=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20bSt?= =?UTF-8?q?ats-=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=BA=D0=B0=D0=B6=D0=B4=D1=83=D1=8E=20=D0=BF=D0=BB=D0=B0=D1=82?= =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/privatenull/pnauth/integration/PnLibraryRuntime.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugin/src/main/kotlin/ru/privatenull/pnauth/integration/PnLibraryRuntime.kt b/plugin/src/main/kotlin/ru/privatenull/pnauth/integration/PnLibraryRuntime.kt index 506a5f0..9898fca 100644 --- a/plugin/src/main/kotlin/ru/privatenull/pnauth/integration/PnLibraryRuntime.kt +++ b/plugin/src/main/kotlin/ru/privatenull/pnauth/integration/PnLibraryRuntime.kt @@ -31,6 +31,8 @@ class PnLibraryRuntime private constructor( registration = created try { module = created.registerModule(MODULE_ID) { builder -> + // bStats держит отдельный проект на каждую платформу; без ID платформы метрики не включаются. + BSTATS_PROJECT_IDS[platform]?.let { id -> builder.metrics { metrics -> metrics.bStats(id) } } builder // A policy that cannot be checked (offline, JRE without a compiler) never stops pnAuth; // only an explicit denial from the policy does. @@ -41,7 +43,6 @@ class PnLibraryRuntime private constructor( .support(PnLibraryIdentity.SUPPORT_URL) .build() ) - .metrics { metrics -> metrics.bStats(BSTATS_PROJECT_ID) } .diagnostics( dataFolder, DiagnosticContainer.builder(MODULE_ID) @@ -92,7 +93,8 @@ class PnLibraryRuntime private constructor( companion object { const val MINIMUM_VERSION = "0.0.1-dev.6" private const val MODULE_ID = "pnauth" - private const val BSTATS_PROJECT_ID = 35046 + // Ключ - название платформы, которое передаёт плагин. Для BungeeCord и Velocity нужны свои проекты в bStats. + private val BSTATS_PROJECT_IDS: Map = mapOf("Paper" to 35046) private const val POLICY_SOURCE = "https://raw.githubusercontent.com/pnFolder/pnlibrary-policy/main/.pnlibrary/policies/PnAuthPolicy.java" private const val ARTIFACT_PATTERN = "(?i)^pnAuth-[0-9].*[.]jar$"