code cleanup, try to fix #105

This commit is contained in:
creeper123123321 2021-03-25 21:51:44 -03:00
parent 14e5ae62bc
commit 6a1b9760ee
11 changed files with 38 additions and 39 deletions

View File

@ -11,6 +11,7 @@ import io.netty.channel.Channel
import io.netty.channel.ChannelFutureListener
import io.netty.handler.codec.DecoderException
import org.slf4j.LoggerFactory
import us.myles.ViaVersion.api.protocol.ProtocolVersion
import java.math.BigInteger
import java.net.InetAddress
import java.net.InetSocketAddress
@ -166,3 +167,5 @@ fun generateServerId() = ByteArray(13).let {
Base64.getEncoder().withoutPadding().encodeToString(it)
// https://developer.mozilla.org/en-US/docs/Glossary/Base64 133% of original
}
fun Int.parseProtocol() = ProtocolVersion.getProtocol(this)

View File

@ -1,10 +1,10 @@
package com.github.creeper123123321.viaaas.command.sub
import com.github.creeper123123321.viaaas.handler.MinecraftHandler
import com.github.creeper123123321.viaaas.parseProtocol
import us.myles.ViaVersion.api.Via
import us.myles.ViaVersion.api.command.ViaCommandSender
import us.myles.ViaVersion.api.command.ViaSubCommand
import us.myles.ViaVersion.api.protocol.ProtocolVersion
object ConnectionsSubCommand : ViaSubCommand() {
override fun name(): String = "connections"
@ -12,16 +12,12 @@ object ConnectionsSubCommand : ViaSubCommand() {
override fun execute(p0: ViaCommandSender, p1: Array<out String>): Boolean {
p0.sendMessage("List of player connections: ")
Via.getPlatform().connectionManager.connections.forEach {
val backAddr = it.channel?.remoteAddress()
val pVer = it.protocolInfo?.protocolVersion?.let {
ProtocolVersion.getProtocol(it)
}
val handler = it.channel?.pipeline()?.get(MinecraftHandler::class.java)
val backAddr = handler?.endRemoteAddress
val pVer = it.protocolInfo?.protocolVersion?.parseProtocol()
val backName = it.protocolInfo?.username
val backVer = it.protocolInfo?.serverProtocolVersion?.let {
ProtocolVersion.getProtocol(it)
}
val pAddr =
it.channel?.pipeline()?.get(MinecraftHandler::class.java)?.other?.remoteAddress()
val backVer = it.protocolInfo?.serverProtocolVersion?.parseProtocol()
val pAddr = handler?.data?.frontHandler?.endRemoteAddress
p0.sendMessage("$pAddr $pVer -> $backVer ($backName) $backAddr")
}
return true

View File

@ -1,11 +1,12 @@
package com.github.creeper123123321.viaaas.handler
import com.github.creeper123123321.viaaas.packet.Packet
import com.github.creeper123123321.viaaas.mcLogger
import com.github.creeper123123321.viaaas.packet.Packet
import com.github.creeper123123321.viaaas.setAutoRead
import io.netty.channel.Channel
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.SimpleChannelInboundHandler
import io.netty.handler.proxy.Socks5ProxyHandler
import us.myles.ViaVersion.exception.CancelCodecException
import java.net.SocketAddress
@ -13,7 +14,7 @@ class MinecraftHandler(
val data: ConnectionData,
val frontEnd: Boolean
) : SimpleChannelInboundHandler<Packet>() {
lateinit var remoteAddress: SocketAddress
lateinit var endRemoteAddress: SocketAddress
val other: Channel? get() = if (frontEnd) data.backChannel else data.frontChannel
var msgDisconnected = false
@ -24,7 +25,8 @@ class MinecraftHandler(
}
override fun channelActive(ctx: ChannelHandlerContext) {
remoteAddress = ctx.channel().remoteAddress()
endRemoteAddress = ctx.channel().pipeline().get(Socks5ProxyHandler::class.java)?.destinationAddress()
?: ctx.channel().remoteAddress()
}
override fun channelInactive(ctx: ChannelHandlerContext) {

View File

@ -5,6 +5,7 @@ import com.github.creeper123123321.viaaas.handler.state.MinecraftConnectionState
import com.github.creeper123123321.viaaas.mcLogger
import com.github.creeper123123321.viaaas.packet.Packet
import com.github.creeper123123321.viaaas.packet.status.StatusResponse
import com.github.creeper123123321.viaaas.parseProtocol
import com.google.gson.JsonParser
import io.netty.channel.ChannelHandlerContext
import us.myles.ViaVersion.api.protocol.ProtocolVersion
@ -19,11 +20,10 @@ class ProtocolDetectionState(val future: CompletableFuture<ProtocolVersion>) : M
override fun handlePacket(handler: MinecraftHandler, ctx: ChannelHandlerContext, packet: Packet) {
handler.data.frontChannel.close()
if (packet !is StatusResponse) throw IllegalArgumentException()
val ver = ProtocolVersion.getProtocol(
JsonParser.parseString(packet.json).asJsonObject.getAsJsonObject("version").get("protocol").asInt
)
val ver = JsonParser.parseString(packet.json).asJsonObject
.getAsJsonObject("version").get("protocol").asInt.parseProtocol()
future.complete(ver)
mcLogger.info("A.D.: ${handler.remoteAddress} $ver")
mcLogger.info("A.D.: ${handler.endRemoteAddress} $ver")
}
override fun disconnect(handler: MinecraftHandler, msg: String) {

View File

@ -1,6 +1,7 @@
package com.github.creeper123123321.viaaas.handler.autoprotocol
import com.github.creeper123123321.viaaas.handler.ConnectionData
import com.github.creeper123123321.viaaas.handler.MinecraftHandler
import com.github.creeper123123321.viaaas.mcLogger
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelPromise
@ -19,16 +20,16 @@ class ProtocolDetectorHandler(val connectionData: ConnectionData) : ChannelDuple
@Throws(Exception::class)
override fun channelActive(ctx: ChannelHandlerContext) {
super.channelActive(ctx)
if (ctx.channel().remoteAddress() is InetSocketAddress) {
val handler = ctx.channel().pipeline().get(MinecraftHandler::class.java)
val address = handler.endRemoteAddress
if (address is InetSocketAddress) {
val timeoutRun = ctx.executor().schedule({
mcLogger.warn(
"Timeout protocol A.D. " + ctx.channel().remoteAddress()
)
mcLogger.warn("Timeout protocol A.D. $address")
hold = false
drainQueue(ctx)
ctx.pipeline().remove(this)
}, 10, TimeUnit.SECONDS)
ProtocolDetector.detectVersion(ctx.channel().remoteAddress() as InetSocketAddress)
ProtocolDetector.detectVersion(address)
.whenComplete { protocol, _ ->
if (protocol != null && protocol.version != -1) {
connectionData.viaBackServerVer = protocol.version

View File

@ -37,7 +37,7 @@ class HandshakeState : MinecraftConnectionState {
else -> throw IllegalStateException("Invalid next state")
}
if (!RateLimit.rateLimitByIp.get((handler.remoteAddress as InetSocketAddress).address).tryAcquire()) {
if (!RateLimit.rateLimitByIp.get((handler.endRemoteAddress as InetSocketAddress).address).tryAcquire()) {
throw IllegalStateException("Rate-limited")
}
val virtualPort = packet.port
@ -66,7 +66,7 @@ class HandshakeState : MinecraftConnectionState {
it.backAddress = packet.address to packet.port
}
val playerAddr = handler.data.frontHandler.remoteAddress
val playerAddr = handler.data.frontHandler.endRemoteAddress
mcLogger.info(
"HS: $playerAddr ${handler.data.state.state.toString().substring(0, 1)} " +
"$virtualHostNoExtra $virtualPort v${handler.data.frontVer}"

View File

@ -107,8 +107,8 @@ class LoginState : MinecraftConnectionState {
parseUndashedId(playerId),
backName!!,
backHash,
frontHandler.remoteAddress,
handler.data.backHandler!!.remoteAddress
frontHandler.endRemoteAddress,
handler.data.backHandler!!.endRemoteAddress
).whenCompleteAsync({ _, throwable ->
if (throwable != null) {
frontHandler.data.backHandler!!.disconnect("Online mode error: $throwable")
@ -178,7 +178,7 @@ class LoginState : MinecraftConnectionState {
if (e != null) {
disconnect(handler, "Profile error: $e")
} else {
mcLogger.info("Login: ${handler.remoteAddress} $frontName $id")
mcLogger.info("Login: ${handler.endRemoteAddress} $frontName $id")
if (frontOnline != null) {
connect()
}

View File

@ -16,11 +16,11 @@ interface MinecraftConnectionState {
fun disconnect(handler: MinecraftHandler, msg: String) {
if (!handler.msgDisconnected) {
handler.msgDisconnected = true
mcLogger.info("DC ${handler.remoteAddress}: $msg")
mcLogger.info("DC ${handler.endRemoteAddress}: $msg")
}
}
fun onInactivated(handler: MinecraftHandler) {
mcLogger.info("- ${handler.remoteAddress}")
mcLogger.info("- ${handler.endRemoteAddress}")
}
}

View File

@ -8,13 +8,13 @@ import com.github.creeper123123321.viaaas.packet.Packet
import com.github.creeper123123321.viaaas.packet.UnknownPacket
import com.github.creeper123123321.viaaas.packet.play.Kick
import com.github.creeper123123321.viaaas.packet.play.PluginMessage
import com.github.creeper123123321.viaaas.parseProtocol
import com.github.creeper123123321.viaaas.readableToByteArray
import com.github.creeper123123321.viaaas.writeFlushClose
import com.google.gson.JsonPrimitive
import io.netty.buffer.ByteBufAllocator
import io.netty.buffer.Unpooled
import io.netty.channel.ChannelHandlerContext
import us.myles.ViaVersion.api.protocol.ProtocolVersion
import us.myles.ViaVersion.api.type.Type
import us.myles.ViaVersion.packets.State
@ -38,11 +38,8 @@ object PlayState : MinecraftConnectionState {
String(pluginMessage.data, Charsets.UTF_8)
} else {
Type.STRING.read(Unpooled.wrappedBuffer(pluginMessage.data))
} + " (VIAaaS C: ${ProtocolVersion.getProtocol(handler.data.frontVer!!)} S: ${
ProtocolVersion.getProtocol(
handler.data.viaBackServerVer!!
)
})"
} + " (VIAaaS C: ${handler.data.frontVer!!.parseProtocol()} S: ${
handler.data.viaBackServerVer!!.parseProtocol()})"
if (is1_7(handler)) {
pluginMessage.data = brand.toByteArray(Charsets.UTF_8)

View File

@ -6,13 +6,13 @@ import com.github.creeper123123321.viaaas.handler.forward
import com.github.creeper123123321.viaaas.packet.Packet
import com.github.creeper123123321.viaaas.packet.UnknownPacket
import com.github.creeper123123321.viaaas.packet.status.StatusResponse
import com.github.creeper123123321.viaaas.parseProtocol
import com.github.creeper123123321.viaaas.writeFlushClose
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import io.netty.channel.ChannelHandlerContext
import us.myles.ViaVersion.api.protocol.ProtocolVersion
import us.myles.ViaVersion.packets.State
import java.util.*
@ -37,8 +37,8 @@ object StatusState : MinecraftConnectionState {
it.addProperty("id", UUID.nameUUIDFromBytes("VIAaaS".toByteArray(Charsets.UTF_8)).toString())
it.addProperty(
"name",
"§9VIAaaS§r (C: §7${ProtocolVersion.getProtocol(handler.data.frontVer!!)}§r S: §7${
ProtocolVersion.getProtocol(handler.data.viaBackServerVer!!)
"§9VIAaaS§r (C: §7${handler.data.frontVer!!.parseProtocol()}§r S: §7${
handler.data.viaBackServerVer!!.parseProtocol()
}§r)"
)
})

View File

@ -30,7 +30,7 @@ private fun createBackChannel(handler: MinecraftHandler, socketAddr: InetSocketA
.connect(socketAddr)
.addListener(ChannelFutureListener {
if (it.isSuccess) {
mcLogger.info("+ ${handler.remoteAddress} -> $socketAddr")
mcLogger.info("+ ${handler.endRemoteAddress} -> $socketAddr")
handler.data.backChannel = it.channel() as SocketChannel
val packet = Handshake()