Skip to content

Commit

Permalink
feat: Implement basic player verification
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Jun 15, 2023
1 parent b09ed54 commit 5b90be0
Show file tree
Hide file tree
Showing 19 changed files with 518 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,10 @@ import com.google.inject.Stage
import com.google.inject.matcher.Matchers
import com.google.inject.util.Modules
import com.xpdustry.foundation.common.misc.ExitStatus
import com.xpdustry.foundation.common.misc.logger
import org.slf4j.Logger
import com.xpdustry.foundation.common.misc.LoggerDelegate
import kotlin.reflect.KClass

open class SimpleFoundationApplication(
common: Module,
implementation: Module,
private val logger: Logger = logger(SimpleFoundationApplication::class),
) : FoundationApplication {
open class SimpleFoundationApplication(common: Module, implementation: Module) : FoundationApplication {

private val listeners = arrayListOf<FoundationListener>()
private val injector: Injector = Guice.createInjector(
Expand Down Expand Up @@ -81,4 +76,8 @@ open class SimpleFoundationApplication(
}
}
}

companion object {
private val logger by LoggerDelegate()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ import com.google.inject.Provider
import com.sksamuel.hoplite.ConfigLoaderBuilder
import com.sksamuel.hoplite.addPathSource
import com.xpdustry.foundation.common.annotation.FoundationDir
import com.xpdustry.foundation.common.misc.logger
import com.xpdustry.foundation.common.misc.LoggerDelegate
import jakarta.inject.Inject
import java.nio.file.Path
import kotlin.io.path.absolutePathString
import kotlin.io.path.notExists

private val logger = logger(FoundationConfigProvider::class)

class FoundationConfigProvider @Inject constructor(@FoundationDir directory: Path) : Provider<FoundationConfig> {

private val file = directory.resolve("config.yaml")
Expand All @@ -53,4 +51,8 @@ class FoundationConfigProvider @Inject constructor(@FoundationDir directory: Pat
}
return loader.loadConfigOrThrow()
}

companion object {
private val logger by LoggerDelegate()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,19 @@ import java.time.Instant

data class Punishment(
override val id: ObjectId,
var targets: List<InetAddress> = emptyList(),
var kind: Kind = Kind.KICK,
var targetIp: InetAddress,
var targetUuid: MindustryUUID,
var reason: String = "Unknown",
var duration: Duration = Duration.ZERO,
var duration: Duration? = Duration.ofDays(1L),
var pardonned: Boolean = false,
) : Entity<ObjectId> {

val expired: Boolean
get() = pardonned || timestamp.plus(duration).isBefore(Instant.now())

val active: Boolean
get() = !expired

val timestamp: Instant
get() = id.date.toInstant()

val expiration: Instant
get() = timestamp.plus(duration)
val expired: Boolean
get() = duration != null && (pardonned || timestamp.plus(duration).isBefore(Instant.now()))

val remaining: Duration
get() =
if (expiration.isBefore(Instant.now())) {
Duration.ZERO
} else Duration.between(Instant.now(), expiration)

enum class Kind {
MUTE,
KICK,
BAN,
}
get() = if (duration == null) Duration.ZERO else duration!!.minus(Duration.between(Instant.now(), timestamp))
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ import reactor.core.publisher.Flux
import java.net.InetAddress

interface PunishmentManager : EntityManager<ObjectId, Punishment> {
fun findAllByTarget(target: InetAddress): Flux<Punishment>
fun findAllByTargetIp(target: InetAddress): Flux<Punishment>
fun findAllByTargetUuid(target: MindustryUUID): Flux<Punishment>
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.mongodb.connection.SslSettings
import com.mongodb.reactivestreams.client.MongoClient
import com.mongodb.reactivestreams.client.MongoClients
import com.xpdustry.foundation.common.application.FoundationListener
import com.xpdustry.foundation.common.application.FoundationMetadata
import com.xpdustry.foundation.common.config.FoundationConfig
import com.xpdustry.foundation.common.database.Database
import com.xpdustry.foundation.common.database.model.Punishment
Expand All @@ -43,7 +44,7 @@ import org.bson.codecs.pojo.Conventions
import org.bson.codecs.pojo.PojoCodecProvider
import java.time.Duration

class MongoDatabase @Inject constructor(private val config: FoundationConfig) : Database, FoundationListener {
class MongoDatabase @Inject constructor(private val config: FoundationConfig, private val metadata: FoundationMetadata) : Database, FoundationListener {

private lateinit var client: MongoClient
override lateinit var users: UserManager
Expand All @@ -52,7 +53,7 @@ class MongoDatabase @Inject constructor(private val config: FoundationConfig) :
override fun onFoundationInit() {
client = MongoClients.create(
MongoClientSettings.builder()
.applicationName("Foundation")
.applicationName("foundation-${metadata.name}")
.applyToClusterSettings {
it.hosts(listOf(ServerAddress(config.mongo.host, config.mongo.port)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.xpdustry.foundation.common.database.mongo

import com.mongodb.client.model.Filters
import com.mongodb.reactivestreams.client.MongoCollection
import com.xpdustry.foundation.common.database.model.MindustryUUID
import com.xpdustry.foundation.common.database.model.Punishment
import com.xpdustry.foundation.common.database.model.PunishmentManager
import com.xpdustry.foundation.common.misc.toValueFlux
Expand All @@ -30,6 +31,9 @@ import java.net.InetAddress
class MongoPunishmentManager @Inject constructor(collection: MongoCollection<Punishment>) :
MongoEntityManager<Punishment, ObjectId>(collection), PunishmentManager {

override fun findAllByTarget(target: InetAddress): Flux<Punishment> =
collection.find(Filters.`in`("targets", target.hostAddress)).toValueFlux()
override fun findAllByTargetIp(target: InetAddress): Flux<Punishment> =
collection.find(Filters.eq("target_ip", target.hostAddress)).toValueFlux()

override fun findAllByTargetUuid(target: MindustryUUID): Flux<Punishment> =
collection.find(Filters.eq("target_uuid", target)).toValueFlux()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Foundation, the software collection powering the Xpdustry network.
* Copyright (C) 2023 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.foundation.common.misc

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
import kotlin.reflect.full.companionObject

class LoggerDelegate<in R : Any> : ReadOnlyProperty<R, Logger> {
override fun getValue(thisRef: R, property: KProperty<*>): Logger =
LoggerFactory.getLogger(getClassForLogging(thisRef.javaClass))
}

private fun <T : Any> getClassForLogging(javaClass: Class<T>): Class<*> {
return javaClass.enclosingClass?.takeIf { it.kotlin.companionObject?.java == javaClass } ?: javaClass
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
*/
package com.xpdustry.foundation.common.misc

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import com.google.common.net.InetAddresses
import java.awt.Color
import java.net.InetAddress
import java.util.Locale
import kotlin.reflect.KClass

// TODO: Cleanup this file ?

Expand All @@ -35,8 +34,5 @@ fun String.capitalize(locale: Locale = Locale.ROOT, all: Boolean = false): Strin
fun Color.toHexString(): String =
if (this.alpha == 255) String.format("#%02x%02x%02x", red, green, blue) else String.format("#%02x%02x%02x%02x", alpha, red, green, blue)

fun logger(name: String): Logger =
LoggerFactory.getLogger(name)

fun logger(klass: KClass<*>): Logger =
LoggerFactory.getLogger(klass.java)
fun String.toInetAddress(): InetAddress =
InetAddresses.forString(this)
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class SimpleDiscovery @Inject constructor(
}
heartbeatTask = sendDiscovery(DiscoveryMessage.Type.DISCOVER)
.onErrorComplete()
// TODO: Make delay configurable
.delaySubscription(Duration.ofSeconds(5L))
.doFinally { if (it == SignalType.ON_COMPLETE) heartbeat() }
.subscribe()
Expand Down
1 change: 1 addition & 0 deletions foundation-mindustry-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {
exclude("org.jetbrains.kotlin", "kotlin-reflect")
exclude("org.slf4j")
}
implementation(libs.jsoup)
mindustryDependencies()
compileOnly(libs.distributor.api)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import com.xpdustry.foundation.common.version.FoundationVersion
import com.xpdustry.foundation.mindustry.core.annotation.ClientSide
import com.xpdustry.foundation.mindustry.core.annotation.ServerSide
import com.xpdustry.foundation.mindustry.core.command.FoundationPluginCommandManager
import com.xpdustry.foundation.mindustry.core.security.verif.SimpleVerificationService
import com.xpdustry.foundation.mindustry.core.security.verif.VerificationService
import java.nio.file.Path

class FoundationMindustryModule(private val plugin: FoundationPlugin) : KotlinAbstractModule() {
Expand All @@ -51,8 +53,11 @@ class FoundationMindustryModule(private val plugin: FoundationPlugin) : KotlinAb

@Provides @Singleton
fun provideMetadata(config: FoundationConfig): FoundationMetadata = FoundationMetadata(
config.mindustry.serverName.replace(" ", "-"),
config.mindustry.serverName.lowercase().replace(" ", "-"),
FoundationPlatform.MINDUSTRY,
FoundationVersion.parse(plugin.descriptor.version),
)

@Provides @Singleton
fun provideVerificationService(): VerificationService = SimpleVerificationService()
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import com.xpdustry.foundation.common.application.SimpleFoundationApplication
import com.xpdustry.foundation.common.misc.ExitStatus
import com.xpdustry.foundation.mindustry.core.command.FoundationPluginCommandManager
import com.xpdustry.foundation.mindustry.core.listener.ConventionListener
import com.xpdustry.foundation.mindustry.core.security.verif.DdosVerification
import com.xpdustry.foundation.mindustry.core.security.verif.PunishmentVerification
import fr.xpdustry.distributor.api.DistributorProvider
import fr.xpdustry.distributor.api.plugin.AbstractMindustryPlugin
import fr.xpdustry.distributor.api.plugin.MindustryPlugin
Expand Down Expand Up @@ -64,6 +66,8 @@ class FoundationPlugin : AbstractMindustryPlugin() {
)

application.register(ConventionListener::class)
application.register(PunishmentVerification::class)
application.register(DdosVerification::class)

application.init()
}
Expand All @@ -77,8 +81,7 @@ private class PluginFoundationApplication(
common: Module,
implementation: Module,
private val plugin: MindustryPlugin,
) : SimpleFoundationApplication(common, implementation, plugin.logger) {

) : SimpleFoundationApplication(common, implementation) {
override fun exit(status: ExitStatus) {
super.exit(status)
when (status) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Foundation, the software collection powering the Xpdustry network.
* Copyright (C) 2023 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.foundation.mindustry.core.misc

import arc.Core
import reactor.core.scheduler.Scheduler
import reactor.core.scheduler.Schedulers

object MindustryScheduler : Scheduler by Schedulers.fromExecutor(Core.app::post)
Loading

0 comments on commit 5b90be0

Please sign in to comment.