framecodec: input.clear() when inactive, fix duckdns.org addresses

This commit is contained in:
creeper123123321 2021-03-30 20:01:40 -03:00
parent 1c0f4f3b16
commit 577560dffa
2 changed files with 47 additions and 41 deletions

View File

@ -25,6 +25,7 @@ import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
import javax.naming.NameNotFoundException
import javax.naming.ServiceUnavailableException
import javax.naming.directory.InitialDirContext
val badLength = DecoderException("Invalid length!")
@ -32,44 +33,20 @@ val mcLogger = LoggerFactory.getLogger("VIAaaS MC")
val webLogger = LoggerFactory.getLogger("VIAaaS Web")
val viaaasLogger = LoggerFactory.getLogger("VIAaaS")
// https://github.com/VelocityPowered/Velocity/blob/6467335f74a7d1617512a55cc9acef5e109b51ac/api/src/main/java/com/velocitypowered/api/util/UuidUtils.java
fun parseUndashedId(string: String): UUID {
Preconditions.checkArgument(string.length == 32, "Length is incorrect")
return UUID(
string.substring(0, 16).toULong(16).toLong(),
string.substring(16).toULong(16).toLong()
)
}
// https://github.com/VelocityPowered/Velocity/blob/0dd6fe1ef2783fe1f9322af06c6fd218aa67cdb1/proxy/src/main/java/com/velocitypowered/proxy/util/EncryptionUtils.java
fun generateServerHash(serverId: String, sharedSecret: ByteArray?, key: PublicKey): String {
val digest = MessageDigest.getInstance("SHA-1")
digest.update(serverId.toByteArray(Charsets.ISO_8859_1))
digest.update(sharedSecret)
digest.update(key.encoded)
return twosComplementHexdigest(digest.digest())
}
fun twosComplementHexdigest(digest: ByteArray): String {
return BigInteger(digest).toString(16)
}
fun Channel.setAutoRead(b: Boolean) {
this.config().isAutoRead = b
if (b) this.read()
}
val secureRandom = if (VIAaaSConfig.useStrongRandom) SecureRandom.getInstanceStrong() else SecureRandom()
fun resolveSrv(address: String, port: Int): Pair<String, Int> {
if (port == 25565) {
try {
// https://github.com/GeyserMC/Geyser/blob/99e72f35b308542cf0dbfb5b58816503c3d6a129/connector/src/main/java/org/geysermc/connector/GeyserConnector.java
val attr = InitialDirContext()
.getAttributes("dns:///_minecraft._tcp.$address", arrayOf("SRV"))["SRV"]
.getAttributes("dns:///_minecraft._tcp.$address", arrayOf("SRV"))["SRV"]
if (attr != null && attr.size() > 0) {
val record = (attr.get(0) as String).split(" ")
return record[3] to record[2].toInt()
}
} catch (ignored: NameNotFoundException) {
} catch (ignored: ServiceUnavailableException) { // DuckDNS workaround
}
}
return address to port
@ -94,6 +71,32 @@ fun mcCfb8(key: ByteArray, mode: Int): Cipher {
}
}
// https://github.com/VelocityPowered/Velocity/blob/6467335f74a7d1617512a55cc9acef5e109b51ac/api/src/main/java/com/velocitypowered/api/util/UuidUtils.java
fun parseUndashedId(string: String): UUID {
Preconditions.checkArgument(string.length == 32, "Length is incorrect")
return UUID(
string.substring(0, 16).toULong(16).toLong(),
string.substring(16).toULong(16).toLong()
)
}
// https://github.com/VelocityPowered/Velocity/blob/0dd6fe1ef2783fe1f9322af06c6fd218aa67cdb1/proxy/src/main/java/com/velocitypowered/proxy/util/EncryptionUtils.java
fun generateServerHash(serverId: String, sharedSecret: ByteArray?, key: PublicKey): String {
val digest = MessageDigest.getInstance("SHA-1")
digest.update(serverId.toByteArray(Charsets.ISO_8859_1))
digest.update(sharedSecret)
digest.update(key.encoded)
return twosComplementHexdigest(digest.digest())
}
fun twosComplementHexdigest(digest: ByteArray): String {
return BigInteger(digest).toString(16)
}
// https://github.com/VelocityPowered/Velocity/blob/e3f17eeb245b8d570f16c1f2aff5e7eafb698d5e/api/src/main/java/com/velocitypowered/api/util/UuidUtils.java
fun generateOfflinePlayerUuid(username: String) = UUID.nameUUIDFromBytes(
"OfflinePlayer:$username".toByteArray(Charsets.UTF_8))
fun checkLocalAddress(inetAddress: InetAddress): Boolean {
return VIAaaSConfig.blockLocalAddress && (inetAddress.isAnyLocalAddress
|| inetAddress.isLinkLocalAddress
@ -104,10 +107,10 @@ fun checkLocalAddress(inetAddress: InetAddress): Boolean {
|| inetAddress.isMCOrgLocal
|| inetAddress.isMCSiteLocal
|| NetworkInterface.networkInterfaces().flatMap { it.inetAddresses() }
.anyMatch {
// This public address acts like a localhost, let's block it
it == inetAddress
})
.anyMatch {
// This public address acts like a localhost, let's block it
it == inetAddress
})
}
fun matchesAddress(addr: InetSocketAddress, list: List<String>): Boolean {
@ -132,9 +135,10 @@ private fun matchAddress(addr: String, list: List<String>): Boolean {
}
}
// https://github.com/VelocityPowered/Velocity/blob/e3f17eeb245b8d570f16c1f2aff5e7eafb698d5e/api/src/main/java/com/velocitypowered/api/util/UuidUtils.java
fun generateOfflinePlayerUuid(username: String) =
UUID.nameUUIDFromBytes("OfflinePlayer:$username".toByteArray(Charsets.UTF_8))
fun Channel.setAutoRead(b: Boolean) {
this.config().isAutoRead = b
if (b) this.read()
}
fun send(ch: Channel, obj: Any, flush: Boolean = false) {
if (flush) {
@ -148,20 +152,17 @@ fun writeFlushClose(ch: Channel, obj: Any) {
ch.writeAndFlush(obj).addListener(ChannelFutureListener.CLOSE)
}
val secureRandom = if (VIAaaSConfig.useStrongRandom) SecureRandom.getInstanceStrong() else SecureRandom()
fun readableToByteArray(byteBuf: ByteBuf) = ByteArray(byteBuf.readableBytes()).also { byteBuf.readBytes(it) }
suspend fun hasJoined(username: String, hash: String): JsonObject {
return httpClient.get(
"https://sessionserver.mojang.com/session/minecraft/hasJoined?username=" +
UrlEscapers.urlFormParameterEscaper().escape(username) +
"&serverId=$hash"
"https://sessionserver.mojang.com/session/minecraft/hasJoined?username=" +
UrlEscapers.urlFormParameterEscaper().escape(username) +
"&serverId=$hash"
) ?: throw IllegalArgumentException("Couldn't authenticate with session servers")
}
fun generate128Bits() = ByteArray(16).also { secureRandom.nextBytes(it) }
fun generateServerId() = ByteArray(13).let {
secureRandom.nextBytes(it)
Base64.getEncoder().withoutPadding().encodeToString(it)

View File

@ -5,10 +5,15 @@ import io.netty.buffer.ByteBuf
import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.ByteToMessageCodec
import us.myles.ViaVersion.api.type.Type
import us.myles.ViaVersion.exception.CancelDecoderException
class FrameCodec : ByteToMessageCodec<ByteBuf>() {
override fun decode(ctx: ChannelHandlerContext, input: ByteBuf, out: MutableList<Any>) {
if (!ctx.channel().isActive) return
if (!ctx.channel().isActive) {
input.clear()
// Netty throws an exception when there's no output
throw CancelDecoderException.CACHED
}
// Ignore, should prevent DoS https://github.com/SpigotMC/BungeeCord/pull/2908
val index = input.readerIndex()