merged port and bind-address config, some modifications

This commit is contained in:
creeper123123321 2022-06-05 09:50:47 -03:00
parent 39f12e1039
commit 7abbb57b76
4 changed files with 103 additions and 67 deletions

View File

@ -30,7 +30,6 @@ import io.netty.resolver.dns.DnsNameResolverBuilder
import io.netty.util.concurrent.Future
import java.io.File
import java.lang.management.ManagementFactory
import java.net.InetAddress
import java.security.KeyPair
import java.security.KeyPairGenerator
import java.util.concurrent.CompletableFuture
@ -122,15 +121,15 @@ object AspirinServer {
.childOption(ChannelOption.IP_TOS, 0x18)
.childOption(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.TCP_FASTOPEN, 32)
VIAaaSConfig.port.forEach {
chFutures.add(serverBootstrap.bind(InetAddress.getByName(VIAaaSConfig.bindAddress), it))
VIAaaSConfig.bindAddresses.forEach {
chFutures.add(serverBootstrap.bind(it.host, it.port))
}
ktorServer = embeddedServer(Netty, commandLineEnvironment(args)) {}.start(false)
viaaasLogger.info("Using compression: ${Natives.compress.loadedVariant}, crypto: ${Natives.cipher.loadedVariant}")
chFutures.forEach {
viaaasLogger.info("Binded minecraft into into " + it.sync().channel().localAddress())
viaaasLogger.info("Binded minecraft into " + it.sync().channel().localAddress())
}
viaaasLogger.info(
"Application started in " + ManagementFactory.getRuntimeMXBean().uptime

View File

@ -1,5 +1,6 @@
package com.viaversion.aas.config
import com.google.common.net.HostAndPort
import com.viaversion.aas.secureRandom
import com.viaversion.aas.util.AddressParser
import com.viaversion.viaversion.util.Config
@ -13,13 +14,71 @@ import java.net.URL
import java.util.*
object VIAaaSConfig : Config(File("config/viaaas.yml")) {
var defaultParameters: Map<Int, AddressParser> = emptyMap()
var bindAddresses = emptyList<HostAndPort>()
var hostName: List<String> = emptyList()
var blockLocalAddress = true
var requireHostName: Boolean = true
var defaultBackendPort: Int? = null
var blockedBackAddresses: List<String> = emptyList()
var allowedBackAddresses: List<String> = emptyList()
var forceOnlineMode: Boolean = false
var showVersionPing: Boolean = true
var showBrandInfo: Boolean = true
var rateLimitWs: Double = 1.0
var rateLimitConnectionMc: Double = 10.0
var listeningWsLimit: Int = 16
var jwtSecret: String = ""
var rateLimitLoginMc: Double = 0.2
var faviconUrl: String? = null
var maxPlayers: Int? = null
var backendProxy: URI? = null
var protocolDetectorCache: Int = 30
var compressionLevel: Int = 6
init {
reloadConfig()
}
override fun reloadConfig() {
super.reloadConfig()
reloadFields()
}
private fun reloadFields() {
reloadIcon()
defaultParameters = this.get("default-parameters", Map::class.java, emptyMap<Int, String>())!!.map {
(it.key as Number).toInt() to AddressParser().parse(it.value.toString())
}.toMap()
bindAddresses = this.getStringList("bind-addresses").map { HostAndPort.fromString(it).withDefaultPort(25565) }
hostName = this.get("host-name", List::class.java, emptyList<String>())!!.map { it.toString() }
blockLocalAddress = this.getBoolean("block-local-address", true)
requireHostName = this.getBoolean("require-host-name", true)
defaultBackendPort = this.getInt("default-backend-port", 25565).let { if (it == -1) null else it }
blockedBackAddresses = this.get(
"blocked-back-addresses",
List::class.java,
emptyList<String>()
)!!.map { it.toString() }
allowedBackAddresses = this.get(
"allowed-back-addresses",
List::class.java,
emptyList<String>()
)!!.map { it.toString() }
forceOnlineMode = this.getBoolean("force-online-mode", false)
showVersionPing = this.getBoolean("show-version-ping", true)
showBrandInfo = this.getBoolean("show-brand-info", true)
rateLimitWs = this.getDouble("rate-limit-ws", 1.0)
rateLimitConnectionMc = this.getDouble("rate-limit-connection-mc", 10.0)
listeningWsLimit = this.getInt("listening-ws-limit", 16)
jwtSecret = this.getString("jwt-secret", null).let {
if (it.isNullOrBlank()) throw IllegalStateException("invalid jwt-secret") else it
}
rateLimitLoginMc = this.getDouble("rate-limit-login-mc", 0.2)
maxPlayers = this.getInt("max-players", 20).let { if (it == -1) null else it }
backendProxy = this.getString("backend-proxy", "").let { if (it.isNullOrEmpty()) null else URI.create(it) }
protocolDetectorCache = this.getInt("protocol-detector-cache", 30)
compressionLevel = this.getInt("compression-level", 6)
}
fun reloadIcon() {
@ -46,13 +105,19 @@ object VIAaaSConfig : Config(File("config/viaaas.yml")) {
override fun getUnsupportedOptions() = emptyList<String>()
override fun getDefaultConfigURL() = VIAaaSConfig::class.java.classLoader.getResource("viaaas.yml")!!
override fun handleConfig(map: MutableMap<String, Any>) {
// Migration from older config versions
fixConfig(map)
upgradeConfig(map)
}
private fun fixConfig(map: MutableMap<String, Any>) {
if (map["jwt-secret"]?.toString().isNullOrBlank()) {
map["jwt-secret"] = Base64.getEncoder()
.encodeToString(ByteArray(64)
.also { secureRandom.nextBytes(it) })
}
}
private fun upgradeConfig(map: MutableMap<String, Any>) {
if (map["host-name"] is String) {
map["host-name"] = map["host-name"].toString().split(',').map { it.trim() }
}
@ -60,52 +125,13 @@ object VIAaaSConfig : Config(File("config/viaaas.yml")) {
val oldSocks = map.remove("backend-socks5-proxy-address")
val oldSocksPort = map.remove("backend-socks5-proxy-port")
if (oldSocks is String && oldSocks.isNotBlank()) {
map["backend-proxy"] = "socks5://$oldSocks:$oldSocksPort"
map["backend-proxy"] = "socks5://${HostAndPort.fromParts(oldSocks, oldSocksPort.toString().toInt())}"
}
val oldBind = map.remove("bind-address")?.toString()
val oldPort = map.remove("port")?.toString()
if (!oldBind.isNullOrEmpty() && !oldPort.isNullOrEmpty()) {
map["bind-addresses"] = listOf(HostAndPort.fromParts(oldBind, oldPort.toInt()).toString())
}
}
val port: List<Int> get() = Objects.toString(this.get("port", Any::class.java, "25565"))!!.split(',').map { it.trim().toInt() }.distinct()
val defaultAddressParsers: Map<Int, AddressParser> get() = this.getStringList("default-address-parsers")!!.map {
val split = it.split(':', limit = 2)
if (split.size != 2) {
throw IllegalArgumentException("Invalid default address parser: $it")
}
split[0].trim().toInt() to AddressParser().parse(split[1].trim())
}.toMap()
val bindAddress: String get() = this.getString("bind-address", "localhost")!!
val hostName: List<String>
get() = this.get("host-name", List::class.java, listOf("viaaas.localhost"))!!.map { it.toString() }
val blockLocalAddress: Boolean get() = this.getBoolean("block-local-address", true)
val requireHostName: Boolean get() = this.getBoolean("require-host-name", true)
val defaultBackendPort: Int? get() = this.getInt("default-backend-port", 25565).let { if (it == -1) null else it }
val blockedBackAddresses: List<String>
get() = this.get(
"blocked-back-addresses",
List::class.java,
emptyList<String>()
)!!.map { it.toString() }
val allowedBackAddresses: List<String>
get() = this.get(
"allowed-back-addresses",
List::class.java,
emptyList<String>()
)!!.map { it.toString() }
val forceOnlineMode: Boolean get() = this.getBoolean("force-online-mode", false)
val showVersionPing: Boolean get() = this.getBoolean("show-version-ping", true)
val showBrandInfo: Boolean get() = this.getBoolean("show-brand-info", true)
val rateLimitWs: Double get() = this.getDouble("rate-limit-ws", 1.0)
val rateLimitConnectionMc: Double get() = this.getDouble("rate-limit-connection-mc", 10.0)
val listeningWsLimit: Int get() = this.getInt("listening-ws-limit", 16)
val jwtSecret: String
get() = this.getString("jwt-secret", null).let {
if (it.isNullOrBlank()) throw IllegalStateException("invalid jwt-secret") else it
}
val rateLimitLoginMc: Double get() = this.getDouble("rate-limit-login-mc", 0.2)
var faviconUrl: String? = null
val maxPlayers: Int? get() = this.getInt("max-players", 20).let { if (it == -1) null else it }
val backendProxy: URI?
get() = this.getString("backend-proxy", "").let { if (it.isNullOrEmpty()) null else URI.create(it) }
val protocolDetectorCache: Int
get() = this.getInt("protocol-detector-cache", 30)
val compressionLevel: Int get() = this.getInt("compression-level", 6)
}

View File

@ -61,11 +61,15 @@ class HandshakeState : ConnectionState {
var parsed = AddressParser().parse(virtualHostNoExtra, VIAaaSConfig.hostName)
val hadHostname = parsed.viaSuffix != null
var useDefault = false;
var usedDefault = false
if (!hadHostname && VIAaaSConfig.defaultAddressParsers.containsKey(virtualPort)) {
parsed = VIAaaSConfig.defaultAddressParsers[virtualPort]
useDefault = true
if (!hadHostname) {
var defaultBack = VIAaaSConfig.defaultParameters[virtualPort]
if (defaultBack == null) defaultBack = VIAaaSConfig.defaultParameters[-1]
if (defaultBack != null) {
parsed = defaultBack
usedDefault = true
}
}
val backProto = parsed.protocol ?: -2
@ -99,7 +103,7 @@ class HandshakeState : ConnectionState {
"$virtualHostNoExtra $virtualPort v${handler.data.frontVer}"
)
if (!useDefault && !hadHostname && VIAaaSConfig.requireHostName && !addressFromWeb
if (!usedDefault && !hadHostname && VIAaaSConfig.requireHostName && !addressFromWeb
) {
throw StacklessException("Missing parts in hostname")
}

View File

@ -6,26 +6,17 @@
######
# Minecraft networking
######
# Port used for binding
port: 25565
# Address to bind
bind-address: 0.0.0.0
# Socket address to bind, default port is 25565
bind-addresses: ["0.0.0.0"]
# Proxy used to connect to backend servers
# Example: socks5://localhost:9050, socks4://localhost:9050, http://foo:bar@localhost:9080
backend-proxy: ''
# Migrated to backend-proxy
backend-socks5-proxy-address: ''
backend-socks5-proxy-port: 9050
# Zlib Compression level
compression-level: 6
#
######
# VIAaaS virtual hosts options
######
# A list of default address parsers if the address host-name is missing
#default-address-parsers
# - "25565:test.geysermc.org"
default-address-parsers: []
# Requires virtual host to contain the value from "host-name" as a suffix.
# A false value will allow virtual hosts with no suffix, connecting to the hostname sent by client.
# A false value could be used for transparent proxying or for MiTM.
@ -38,6 +29,13 @@ host-name:
# Default port to be used when connecting to the backend server.
# Use -1 to reuse the port sent by client, useful for transparent proxying.
default-backend-port: 25565
# A list of backend address if the address host-name is missing
# It will be chosen via virtual port
# May be useful for BungeeCord
# default-backends:
# -1: "anyport.example.com._v1_12._of"
# 25565: "test.geysermc.org._v1_8"
default-parameters: {}
#
######
# Address filtering
@ -88,3 +86,12 @@ protocol-detector-cache: 30
#####
# Secret key used to generate Minecraft tokens for listening logins
jwt-secret: ''
#####
# Replaced to new options
#####
backend-socks5-proxy-address: ''
backend-socks5-proxy-port: 9050
backend-address: ''
bind-address: ''
port: ''