Skip to content

Commit

Permalink
Better config reading
Browse files Browse the repository at this point in the history
  • Loading branch information
Bumer-32 committed Jan 7, 2025
1 parent 693fbef commit cbb67fd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/ua/pp/lumivoid/iwtcms/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ object Constants {
const val MOD_ID = "iwtcms"
val LOGGER = LoggerFactory.getLogger(MOD_ID)
val EMBEDDED_SERVER_LOGGER = LoggerFactory.getLogger("iwtcms embedded server")
val CONFIG_FOLDER = "${System.getProperty("user.dir")}/$MOD_ID" // FabricLoader.getInstance().configDir.toString() // Temporary, idk why but FabricLoader works wrong at jar https://github.com/Bumer-32/I-Want-To-Control-My-Server/issues/7
val CONFIG_FILE = CONFIG_FOLDER + "/iwtcms.conf"
val SSL_SERTIFICATE_FILE = CONFIG_FOLDER + "/keystore.jks"
val CONFIG_FOLDER = "${System.getProperty("user.dir")}/$MOD_ID" // FabricLoader.getInstance().configDir.toString() // Temporary, idk why but FabricLoader works wrongly at jar https://github.com/Bumer-32/I-Want-To-Control-My-Server/issues/7
val CONFIG_FILE = "$CONFIG_FOLDER/iwtcms.conf"
val SSL_CERTIFICATE_FILE = "$CONFIG_FOLDER/keystore.jks"
const val SPARK_FABRIC_ID = "spark"
val MOD_VERSION = FabricLoader.getInstance().getModContainer(MOD_ID).get().metadata.version.toString()
val DEV_WEB_FOLDER = "$CONFIG_FOLDER/dev-web"
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/ua/pp/lumivoid/iwtcms/PREIWTCMS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ua.pp.lumivoid.iwtcms.ktor.KtorServer
import ua.pp.lumivoid.iwtcms.util.Config
import ua.pp.lumivoid.iwtcms.util.CustomLogger
import java.io.File
import kotlin.system.exitProcess

object PREIWTCMS: PreLaunchEntrypoint {
private val logger = LoggerFactory.getLogger("iwtcms pre launch")
Expand Down Expand Up @@ -64,7 +65,7 @@ object PREIWTCMS: PreLaunchEntrypoint {
logger.error("| |")
logger.error("+-----------------------------------------------------------------------------------------+")

System.exit(1)
exitProcess(1)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/ua/pp/lumivoid/iwtcms/ktor/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ua.pp.lumivoid.iwtcms.util.Config
import ua.pp.lumivoid.iwtcms.util.MinecraftServerHandler
import java.io.File
import java.security.KeyStore
import kotlin.system.exitProcess

private val logger = Constants.EMBEDDED_SERVER_LOGGER
private val modConfig = Config.readConfig()
Expand All @@ -24,7 +25,7 @@ fun ApplicationEngine.Configuration.envConfig() {
logger.info("Configuring environment")

if (Config.readConfig().useSSL) {
val keyStoreFile = File(Constants.SSL_SERTIFICATE_FILE)
val keyStoreFile = File(Constants.SSL_CERTIFICATE_FILE)
val keyStore: KeyStore

if (Config.readConfig().customSertificate) {
Expand Down Expand Up @@ -63,7 +64,7 @@ fun ApplicationEngine.Configuration.envConfig() {



System.exit(1)
exitProcess(1)

return
}
Expand Down
46 changes: 18 additions & 28 deletions src/main/kotlin/ua/pp/lumivoid/iwtcms/util/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.apache.commons.codec.digest.DigestUtils
import ua.pp.lumivoid.iwtcms.Constants
import ua.pp.lumivoid.iwtcms.ktor.api.User
import java.io.File
import kotlin.system.exitProcess

object Config {
private val logger = Constants.LOGGER
Expand Down Expand Up @@ -44,37 +45,24 @@ object Config {

config.getList("auth.users").forEach { configUser ->
val user = (configUser as ConfigObject).toConfig()
val newUsername = user.getString("name")
val newPassword = if (user.hasPath("password") && user.getString("password").isNotEmpty()) {

val username = user.getString("name")
val password = if (user.hasPath("password") && user.getString("password").isNotEmpty()) {
user.getString("password")
} else {
null
}
val newPermits: MutableMap<String, Boolean> = mutableMapOf()


val permits = user.getConfig("permits")

if (permits.hasPath("read real time logs")) newPermits.put(
"read real time logs",
permits.getBoolean("read real time logs")
)
if (permits.hasPath("read logs history")) newPermits.put(
"read logs history",
permits.getBoolean("read logs history")
)
if (permits.hasPath("execute commands")) newPermits.put(
"execute commands",
permits.getBoolean("execute commands")
)
if (permits.hasPath("access to server stats")) newPermits.put(
"access to server stats",
permits.getBoolean("access to server stats")
)

val newId: String = DigestUtils.sha256Hex((newUsername + newPassword.toString()))

val newUser = User(newId, newUsername, newPassword, newPermits)


val permits: MutableMap<String, Boolean> = user.getConfig("permits").entrySet().associate {
it.key.replace("\"", "") to it.value.unwrapped() as Boolean
} as MutableMap<String, Boolean>

println(permits)

val id: String = DigestUtils.sha256Hex((username + password.toString()))

val newUser = User(id, username, password, permits)
users.add(newUser)
}

Expand Down Expand Up @@ -135,12 +123,14 @@ object Config {
logger.error("| |")
logger.error("+-----------------------------------------------------------------------------------------+")

System.exit(1) // STOP
exitProcess(1) // STOP

return createConfigData(ConfigFactory.parseFile(File(defaultConfig.file))) // it never will be launched
}
}

data class TESDFSDFS(val s: String, val i: Boolean)

@Serializable
data class ConfigData(
val ip: String,
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/iwtcms.conf
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ stuff {

web {
# enable IWTCMS control panel: enables cutie web interface with different features
enable IWTCMS control panel = false
enable IWTCMS control panel = true
}

auth {
# useAuthentication: enabling password for clients, client can't receive/send messages from/to server before login. By default it's disabled(false) BUT highly recommended to enable(true) it password.
use Authentication = false // boolean
# useAuthentication: enabling password for clients, client can't receive/send messages from/to server before login. By default it's disabled(false) BUT highly recommended to enable(true) it.
use Authentication = true // boolean

# needs ONLY IF useAuthentication enabled
# here you can configure users
Expand Down

0 comments on commit cbb67fd

Please sign in to comment.