Changed configuration format to HOCON.

This commit is contained in:
Daan Vanden Bosch 2021-08-04 15:27:47 +02:00
parent f43e154bc7
commit bc3c3c19b2
3 changed files with 79 additions and 64 deletions

View File

@ -2,8 +2,10 @@
## Configuration ## Configuration
Put a config.json file in the directory where psoserv will run or pass Put a psoserv.conf file in the directory where psoserv will run or pass
the `--config=/path/to/config.json` parameter to specify a configuration file. the `--config=/path/to/file.conf` parameter to specify a configuration file.
The [HOCON](https://github.com/lightbend/config#using-hocon-the-json-superset) format is used to
describe configurations.
## Proxy ## Proxy
@ -11,55 +13,60 @@ Phantasmal PSO server can proxy any other PSO server. Below is a sample configur
locally running Tethealla server using the standard Tethealla client. Be sure to modify locally running Tethealla server using the standard Tethealla client. Be sure to modify
tethealla.ini and set server port to 22000. tethealla.ini and set server port to 22000.
```json ```hocon
proxy: {
# Default local address used by all proxies, can be overwritten per proxy.
bindAddress: localhost
# Default address of the remote server used by all proxies, can be overwritten per proxy.
remoteAddress: localhost
# One server configuration per address/port pair that needs to be proxied.
servers: [
{ {
"proxy": { # Name used for e.g. the logs.
"bindAddress": "localhost", name: patch_proxy
"remoteAddress": "localhost", # PC or BB, determines the message format encryption cipher used.
"servers": [ version: PC
# Local port the proxy will listen on.
bindPort: 11000
# Remote port the proxy will connect to.
remotePort: 21000
}
{ {
"name": "patch_proxy", name: patch_data_proxy
"version": "PC", version: PC
"bindPort": 11000, bindPort: 11001
"remotePort": 21000 remotePort: 21001
}, }
{ {
"name": "patch_data_proxy", name: login_proxy
"version": "PC", version: BB
"bindPort": 11001, bindPort: 12000
"remotePort": 21001 remotePort: 22000
}, }
{ {
"name": "login_proxy", name: character_proxy
"version": "BB", version: BB
"bindPort": 12000, bindPort: 12001
"remotePort": 22000 remotePort: 22001
}, }
{ {
"name": "character_proxy", name: ship_proxy
"version": "BB", version: BB
"bindPort": 12001, bindPort: 13000
"remotePort": 22001 remotePort: 5278
}, }
{ {
"name": "ship_proxy", name: block_1_proxy
"version": "BB", version: BB
"bindPort": 13000, bindPort: 13001
"remotePort": 5278 remotePort: 5279
}, }
{ {
"name": "block_1_proxy", name: block_2_proxy
"version": "BB", version: BB
"bindPort": 13001, bindPort: 13002
"remotePort": 5279 remotePort: 5280
},
{
"name": "block_2_proxy",
"version": "BB",
"bindPort": 13002,
"remotePort": 5280
} }
] ]
} }
}
``` ```

View File

@ -13,5 +13,5 @@ val serializationVersion: String by project.extra
dependencies { dependencies {
implementation(project(":core")) implementation(project(":core"))
implementation(project(":psolib")) implementation(project(":psolib"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$serializationVersion") implementation("org.jetbrains.kotlinx:kotlinx-serialization-hocon:$serializationVersion")
} }

View File

@ -1,7 +1,9 @@
package world.phantasmal.psoserv package world.phantasmal.psoserv
import kotlinx.serialization.decodeFromString import com.typesafe.config.ConfigFactory
import kotlinx.serialization.json.Json import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.hocon.Hocon
import kotlinx.serialization.hocon.decodeFromConfig
import mu.KotlinLogging import mu.KotlinLogging
import world.phantasmal.psoserv.encryption.BbCipher import world.phantasmal.psoserv.encryption.BbCipher
import world.phantasmal.psoserv.encryption.Cipher import world.phantasmal.psoserv.encryption.Cipher
@ -47,22 +49,25 @@ fun main(args: Array<String>) {
// Try default config file location if no file specified with --config argument. // Try default config file location if no file specified with --config argument.
if (configFile == null) { if (configFile == null) {
configFile = File("config.json").takeIf { it.isFile } configFile = File("psoserv.conf").takeIf { it.isFile }
} }
val config: Config
// Parse the config file if we found one, otherwise use default config. // Parse the config file if we found one, otherwise use default config.
if (configFile != null) { val config: Config = if (configFile != null) {
LOGGER.info { "Using configuration file $configFile." } LOGGER.info { "Using configuration file $configFile." }
val json = Json { if (!configFile.exists()) {
ignoreUnknownKeys = true error(""""$configFile" does not exist.""")
} else if (!configFile.isFile) {
error(""""$configFile" is not a file.""")
} else if (!configFile.canRead()) {
error("""Don't have permission to read "$configFile".""")
} }
config = json.decodeFromString(configFile.readText()) @OptIn(ExperimentalSerializationApi::class)
Hocon.decodeFromConfig(ConfigFactory.parseFile(configFile))
} else { } else {
config = DEFAULT_CONFIG DEFAULT_CONFIG
} }
// Initialize and start the server. // Initialize and start the server.
@ -70,7 +75,9 @@ fun main(args: Array<String>) {
LOGGER.info { "Starting up." } LOGGER.info { "Starting up." }
server.start() if (!server.start()) {
LOGGER.info { "Nothing to do, stopping." }
}
} catch (e: Throwable) { } catch (e: Throwable) {
LOGGER.error(e) { "Failed to start up." } LOGGER.error(e) { "Failed to start up." }
} }
@ -80,9 +87,10 @@ private class PhantasmalServer(
private val servers: List<Server>, private val servers: List<Server>,
private val proxyServers: List<ProxyServer>, private val proxyServers: List<ProxyServer>,
) { ) {
fun start() { fun start(): Boolean {
servers.forEach(Server::start) servers.forEach(Server::start)
proxyServers.forEach(ProxyServer::start) proxyServers.forEach(ProxyServer::start)
return servers.isNotEmpty() || proxyServers.isNotEmpty()
} }
fun stop() { fun stop() {