diff --git a/velocity/src/main/kotlin/de/astride/maintenance/Maintenance.kt b/velocity/src/main/kotlin/de/astride/maintenance/Maintenance.kt deleted file mode 100644 index d0a4345..0000000 --- a/velocity/src/main/kotlin/de/astride/maintenance/Maintenance.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * © Copyright - Lars Artmann aka. LartyHD 2019. - */ - -package de.astride.maintenance - -import com.velocitypowered.api.event.Subscribe -import com.velocitypowered.api.event.proxy.ProxyInitializeEvent -import com.velocitypowered.api.plugin.Plugin -import com.velocitypowered.api.proxy.ProxyServer -import org.slf4j.Logger - -/** - * @author Lars Artmann | LartyHD - * Created by Lars Artmann | LartyHD on 13.02.2019 09:52. - * Current Version: 1.0 (13.02.2019 - 13.02.2019) - */ -@Plugin( - id = "maintenance", - name = "Maintenance", - version = "@version@", - url = "Astride.de", - authors = ["Lars Artmann | LartyHD"] -) -class Maintenance private constructor( - private val proxyServer: ProxyServer, - private val logger: Logger -) { - - @Suppress("UNUSED_PARAMETER") - @Subscribe - fun on(event: ProxyInitializeEvent) { - logger.info("Maintenance loaded :D") - } - -} \ No newline at end of file diff --git a/velocity/src/main/kotlin/de/astride/maintenance/MaintenancePlugin.kt b/velocity/src/main/kotlin/de/astride/maintenance/MaintenancePlugin.kt new file mode 100644 index 0000000..a2dbba3 --- /dev/null +++ b/velocity/src/main/kotlin/de/astride/maintenance/MaintenancePlugin.kt @@ -0,0 +1,52 @@ +/* + * © Copyright - Lars Artmann aka. LartyHD 2019. + */ + +package de.astride.maintenance + +import com.google.inject.Inject +import com.velocitypowered.api.event.EventManager +import com.velocitypowered.api.event.Subscribe +import com.velocitypowered.api.event.proxy.ProxyInitializeEvent +import com.velocitypowered.api.event.proxy.ProxyReloadEvent +import com.velocitypowered.api.plugin.Plugin +import com.velocitypowered.api.plugin.annotation.DataDirectory +import org.slf4j.Logger +import java.nio.file.Path + +/** + * @author Lars Artmann | LartyHD + * Created by Lars Artmann | LartyHD on 13.02.2019 09:52. + * Current Version: 1.0 (13.02.2019 - 02.03.2019) + */ +@Suppress("UNUSED_PARAMETER") +@Plugin( + id = "registry", + name = "Registry", + version = "@version@", + url = "Astride.de", + authors = ["Lars Artmann | LartyHD"] +) +class MaintenancePlugin @Inject constructor( + private val eventManager: EventManager, + @DataDirectory val path: Path, + private val logger: Logger +) { + + private lateinit var registry: Registry + + @Subscribe + fun on(event: ProxyInitializeEvent) { + registry = Registry(this, eventManager, path.toFile(), logger) + registry.register() + logger.info("Maintenance loaded") + //proxyServer.allServers.first().serverInfo.address.address.isReachable(10) + } + + @Subscribe + fun on(event: ProxyReloadEvent) { + registry.register() + logger.info("Maintenance reloaded") + } + +} \ No newline at end of file diff --git a/velocity/src/main/kotlin/de/astride/maintenance/Registry.kt b/velocity/src/main/kotlin/de/astride/maintenance/Registry.kt new file mode 100644 index 0000000..80272c9 --- /dev/null +++ b/velocity/src/main/kotlin/de/astride/maintenance/Registry.kt @@ -0,0 +1,67 @@ +/* + * © Copyright - Lars Artmann aka. LartyHD 2019. + */ + +package de.astride.maintenance + +import com.velocitypowered.api.event.EventManager +import de.astride.maintenance.config.ConfigProvider +import de.astride.maintenance.listener.Listener +import de.astride.maintenance.webinterface.WebInjector +import org.slf4j.Logger +import java.io.File + +/** + * @author Lars Artmann | LartyHD + * Created by Lars Artmann | LartyHD on 02.03.2019 09:02. + * Current Version: 1.0 (02.03.2019 - 02.03.2019) + */ +@Suppress("UNUSED_PARAMETER") +class Registry( + private val plugin: Any, + private val eventManager: EventManager, + private val file: File, + private val logger: Logger +) { + + companion object { + var isActive: Boolean = false + lateinit var provider: ConfigProvider + } + + private lateinit var listener: Listener + private lateinit var webInjector: WebInjector + + fun register(boolean: Boolean = true) { + registerConfigProvider(boolean) + if (provider.webInterface.isActive && boolean) registerWebInjector() + updateListener() + } + + private fun registerConfigProvider(boolean: Boolean = true) { + provider = ConfigProvider(file) + if (boolean) isActive = provider.config.isActive + } + + private fun updateListener() { + if (::listener.isInitialized) eventManager.unregisterListener(plugin, listener) + if (isActive) registerListener() + } + + private fun registerListener() { + listener = Listener(provider) + eventManager.register(plugin, listener) + } + + private fun registerWebInjector() { + logger.info("register WebInjector") + if (::webInjector.isInitialized) return//webInjector.stop() + webInjector = WebInjector(this) + webInjector.start() + logger.info(webInjector.server.toString()) + logger.info(webInjector.server.address.toString()) + logger.info("registered WebInjector") + } + + +} \ No newline at end of file diff --git a/velocity/src/main/kotlin/de/astride/maintenance/config/ConfigProvider.kt b/velocity/src/main/kotlin/de/astride/maintenance/config/ConfigProvider.kt new file mode 100644 index 0000000..6ede647 --- /dev/null +++ b/velocity/src/main/kotlin/de/astride/maintenance/config/ConfigProvider.kt @@ -0,0 +1,63 @@ +package de.astride.maintenance.config + +import net.darkdevelopers.darkbedrock.darkness.general.configs.ConfigData +import net.darkdevelopers.darkbedrock.darkness.general.configs.gson.GsonService +import net.kyori.text.Component +import net.kyori.text.serializer.ComponentSerializers +import java.io.File + +/** + * @author Lars Artmann | LartyHD + * Created by Lars Artmann | LartyHD on 15.02.2019 00:10. + * Current Version: 1.0 (15.02.2019 - 15.02.2019) + */ +class ConfigProvider(var directory: File) { + + /* SubClass */ + val config by lazy { Config() } + val motd by lazy { Motd() } + val webInterface by lazy { WebInterface() } + + inner class Config internal constructor() { + + /* Main */ + private val configData by lazy { ConfigData(directory, "config.json") } + private val config by lazy { GsonService.loadAsJsonObject(configData) } + /* Values */ + val isActive by lazy { config["IsActive"]?.asBoolean ?: false } + val motdFileName by lazy { config["MotdFileName"]?.asString ?: "motd.json" } + val webInterfaceFileName by lazy { config["WebInterfaceFileName"]?.asString ?: "web-interface.json" } + val byPassPermission by lazy { config["ByPassPermission"]?.asString ?: "maintenance.bypass" } + val kickMessage: Component by lazy { + ComponentSerializers.LEGACY.deserialize(config["KickMessage"]?.asString ?: "§cCosmicSky.net | Registry") + } + + } + + inner class Motd internal constructor() { + + /* Main */ + private val configData by lazy { ConfigData(directory, this@ConfigProvider.config.motdFileName) } + private val config by lazy { GsonService.loadAsJsonObject(configData) } + /* Values */ + val versionName: String by lazy { config["VerisonName"]?.asString ?: "CosmicProxy" } + val description: Component by lazy { + ComponentSerializers.LEGACY.deserialize( + config["Description"]?.asString?.replace("\\n", "\n") ?: "§cCosmicSky.net | Registry" + ) + } + + } + + inner class WebInterface internal constructor() { + + /* Main */ + private val configData by lazy { ConfigData(directory, this@ConfigProvider.config.webInterfaceFileName) } + private val config by lazy { GsonService.loadAsJsonObject(configData) } + /* Values */ + val isActive by lazy { config["IsActive"]?.asBoolean ?: false } + val port by lazy { config["Port"]?.asInt ?: 4543 } + + } + +} \ No newline at end of file diff --git a/velocity/src/main/kotlin/de/astride/maintenance/listener/Listener.kt b/velocity/src/main/kotlin/de/astride/maintenance/listener/Listener.kt new file mode 100644 index 0000000..20978bb --- /dev/null +++ b/velocity/src/main/kotlin/de/astride/maintenance/listener/Listener.kt @@ -0,0 +1,35 @@ +package de.astride.maintenance.listener + +import com.velocitypowered.api.event.ResultedEvent +import com.velocitypowered.api.event.Subscribe +import com.velocitypowered.api.event.connection.LoginEvent +import com.velocitypowered.api.event.proxy.ProxyPingEvent +import com.velocitypowered.api.proxy.server.ServerPing +import de.astride.maintenance.config.ConfigProvider + +/** + * @author Lars Artmann | LartyHD + * Created by Lars Artmann | LartyHD on 14.02.2019 23:31. + * Current Version: 1.0 (14.02.2019 - 15.02.2019) + */ +class Listener(private val provider: ConfigProvider) { + + @Subscribe + fun on(event: ProxyPingEvent) { + val motd = provider.motd + val version = ServerPing.Version(event.ping.version.protocol, motd.versionName) + + event.ping = event.ping.asBuilder() + .version(version) + .description(motd.description) + .nullPlayers() + .build() + } + + @Subscribe + fun on(event: LoginEvent) { + if (event.player.hasPermission(provider.config.byPassPermission)) return + event.result = ResultedEvent.ComponentResult.denied(provider.config.kickMessage) + } + +} \ No newline at end of file diff --git a/velocity/src/main/kotlin/de/astride/maintenance/webinterface/WebInjector.kt b/velocity/src/main/kotlin/de/astride/maintenance/webinterface/WebInjector.kt new file mode 100644 index 0000000..dd75df4 --- /dev/null +++ b/velocity/src/main/kotlin/de/astride/maintenance/webinterface/WebInjector.kt @@ -0,0 +1,165 @@ +/* + * © Copyright - Lars Artmann aka. LartyHD 2019. + */ + +package de.astride.maintenance.webinterface + +import com.sun.net.httpserver.HttpExchange +import com.sun.net.httpserver.HttpServer +import de.astride.maintenance.Registry +import java.net.InetSocketAddress + + +/** + * @author Lars Artmann | LartyHD + * Created by Lars Artmann | LartyHD on 02.03.2019 07:39. + * Current Version: 1.0 (02.03.2019 - 02.03.2019) + */ +class WebInjector(private val registry: Registry) { + + val server = HttpServer.create(InetSocketAddress(Registry.provider.webInterface.port), 0) + + fun start() { + + server.createContext("/status") { exchange -> + val input = Registry.isActive.toString() + exchange.sendResponseHeaders(200, input.length.toLong()) + exchange.send(input.toByteArray()) + } + + server.createContext("/") { exchange -> + try { +// val resourceAsStream = ClassLoader::class.java.getResourceAsStream("acp.html") + val input = """ + + + + + + + + + + + + + + Maintenance | ACP + + + + + + +
+ +
+ + + + + + + + + + +""".toByteArray() + + + exchange.sendResponseHeaders(200, input.size.toLong()) + exchange.send(input) + } catch (ex: Throwable) { + ex.printStackTrace() + } + } + server.createContext("/toggle") { exchange -> + Registry.isActive = !Registry.isActive + registry.register(false) + + val input = Registry.isActive.toString() + exchange.sendResponseHeaders(200, input.length.toLong()) + exchange.send(input.toByteArray()) + } + server.executor = null // creates a default executor + server.start() + + } + + private fun HttpExchange.send(bytes: ByteArray) = responseBody.run { + write(bytes) + close() + } + + fun stop() = server.stop(10) + +} + diff --git a/velocity/src/main/resources/acp.html b/velocity/src/main/resources/acp.html new file mode 100644 index 0000000..6744114 --- /dev/null +++ b/velocity/src/main/resources/acp.html @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + Maintenance | ACP + + + + + + +
+ +
+ + + + + + + + + \ No newline at end of file diff --git a/velocity/src/main/resources/velocity-plugin.json b/velocity/src/main/resources/velocity-plugin.json new file mode 100644 index 0000000..fd8cce0 --- /dev/null +++ b/velocity/src/main/resources/velocity-plugin.json @@ -0,0 +1,13 @@ +{ + "id": "maintenance", + "name": "Maintenance", + "version": "1.0-SNAPSHOT", + "description": "Maintenance", + "url": "Astride.de", + "authors": [ + "Lars Artmann | LartyHD" + ], + "dependencies": [ + ], + "main": "de.astride.maintenance.MaintenancePlugin" +} \ No newline at end of file diff --git a/velocity/src/test/resources/config.json b/velocity/src/test/resources/config.json new file mode 100644 index 0000000..6b5b282 --- /dev/null +++ b/velocity/src/test/resources/config.json @@ -0,0 +1,6 @@ +{ + "IsActive": true, + "MotdFileName": "motd.json", + "ByPassPermission": "maintenance.bypass", + "KickMessage": "§aWir sind aktiv am verbessern des Servers,\n§adein §fCosmicSky.net §aTeam\n\n§aMehr Infos auf §fdiscord.comsicsky.net" +} \ No newline at end of file diff --git a/velocity/src/test/resources/motd.json b/velocity/src/test/resources/motd.json new file mode 100644 index 0000000..9d9a4ad --- /dev/null +++ b/velocity/src/test/resources/motd.json @@ -0,0 +1,4 @@ +{ + "VerisonName": "CosmicProxy", + "Description": "§aWir sind aktiv am verbessern des Servers,\n§adein §fCosmicSky.net §aTeam" +} \ No newline at end of file diff --git a/velocity/src/test/resources/web-interface.json b/velocity/src/test/resources/web-interface.json new file mode 100644 index 0000000..589df46 --- /dev/null +++ b/velocity/src/test/resources/web-interface.json @@ -0,0 +1,4 @@ +{ + "IsActive": true, + "Port": 4543 +} \ No newline at end of file