mirror of
https://github.com/ViaVersion/VIAaaS.git
synced 2025-02-03 23:42:06 +01:00
code cleanup, try to fix #105
This commit is contained in:
parent
14e5ae62bc
commit
6a1b9760ee
@ -11,6 +11,7 @@ import io.netty.channel.Channel
|
|||||||
import io.netty.channel.ChannelFutureListener
|
import io.netty.channel.ChannelFutureListener
|
||||||
import io.netty.handler.codec.DecoderException
|
import io.netty.handler.codec.DecoderException
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
import us.myles.ViaVersion.api.protocol.ProtocolVersion
|
||||||
import java.math.BigInteger
|
import java.math.BigInteger
|
||||||
import java.net.InetAddress
|
import java.net.InetAddress
|
||||||
import java.net.InetSocketAddress
|
import java.net.InetSocketAddress
|
||||||
@ -166,3 +167,5 @@ fun generateServerId() = ByteArray(13).let {
|
|||||||
Base64.getEncoder().withoutPadding().encodeToString(it)
|
Base64.getEncoder().withoutPadding().encodeToString(it)
|
||||||
// https://developer.mozilla.org/en-US/docs/Glossary/Base64 133% of original
|
// https://developer.mozilla.org/en-US/docs/Glossary/Base64 133% of original
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun Int.parseProtocol() = ProtocolVersion.getProtocol(this)
|
@ -1,10 +1,10 @@
|
|||||||
package com.github.creeper123123321.viaaas.command.sub
|
package com.github.creeper123123321.viaaas.command.sub
|
||||||
|
|
||||||
import com.github.creeper123123321.viaaas.handler.MinecraftHandler
|
import com.github.creeper123123321.viaaas.handler.MinecraftHandler
|
||||||
|
import com.github.creeper123123321.viaaas.parseProtocol
|
||||||
import us.myles.ViaVersion.api.Via
|
import us.myles.ViaVersion.api.Via
|
||||||
import us.myles.ViaVersion.api.command.ViaCommandSender
|
import us.myles.ViaVersion.api.command.ViaCommandSender
|
||||||
import us.myles.ViaVersion.api.command.ViaSubCommand
|
import us.myles.ViaVersion.api.command.ViaSubCommand
|
||||||
import us.myles.ViaVersion.api.protocol.ProtocolVersion
|
|
||||||
|
|
||||||
object ConnectionsSubCommand : ViaSubCommand() {
|
object ConnectionsSubCommand : ViaSubCommand() {
|
||||||
override fun name(): String = "connections"
|
override fun name(): String = "connections"
|
||||||
@ -12,16 +12,12 @@ object ConnectionsSubCommand : ViaSubCommand() {
|
|||||||
override fun execute(p0: ViaCommandSender, p1: Array<out String>): Boolean {
|
override fun execute(p0: ViaCommandSender, p1: Array<out String>): Boolean {
|
||||||
p0.sendMessage("List of player connections: ")
|
p0.sendMessage("List of player connections: ")
|
||||||
Via.getPlatform().connectionManager.connections.forEach {
|
Via.getPlatform().connectionManager.connections.forEach {
|
||||||
val backAddr = it.channel?.remoteAddress()
|
val handler = it.channel?.pipeline()?.get(MinecraftHandler::class.java)
|
||||||
val pVer = it.protocolInfo?.protocolVersion?.let {
|
val backAddr = handler?.endRemoteAddress
|
||||||
ProtocolVersion.getProtocol(it)
|
val pVer = it.protocolInfo?.protocolVersion?.parseProtocol()
|
||||||
}
|
|
||||||
val backName = it.protocolInfo?.username
|
val backName = it.protocolInfo?.username
|
||||||
val backVer = it.protocolInfo?.serverProtocolVersion?.let {
|
val backVer = it.protocolInfo?.serverProtocolVersion?.parseProtocol()
|
||||||
ProtocolVersion.getProtocol(it)
|
val pAddr = handler?.data?.frontHandler?.endRemoteAddress
|
||||||
}
|
|
||||||
val pAddr =
|
|
||||||
it.channel?.pipeline()?.get(MinecraftHandler::class.java)?.other?.remoteAddress()
|
|
||||||
p0.sendMessage("$pAddr $pVer -> $backVer ($backName) $backAddr")
|
p0.sendMessage("$pAddr $pVer -> $backVer ($backName) $backAddr")
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
package com.github.creeper123123321.viaaas.handler
|
package com.github.creeper123123321.viaaas.handler
|
||||||
|
|
||||||
import com.github.creeper123123321.viaaas.packet.Packet
|
|
||||||
import com.github.creeper123123321.viaaas.mcLogger
|
import com.github.creeper123123321.viaaas.mcLogger
|
||||||
|
import com.github.creeper123123321.viaaas.packet.Packet
|
||||||
import com.github.creeper123123321.viaaas.setAutoRead
|
import com.github.creeper123123321.viaaas.setAutoRead
|
||||||
import io.netty.channel.Channel
|
import io.netty.channel.Channel
|
||||||
import io.netty.channel.ChannelHandlerContext
|
import io.netty.channel.ChannelHandlerContext
|
||||||
import io.netty.channel.SimpleChannelInboundHandler
|
import io.netty.channel.SimpleChannelInboundHandler
|
||||||
|
import io.netty.handler.proxy.Socks5ProxyHandler
|
||||||
import us.myles.ViaVersion.exception.CancelCodecException
|
import us.myles.ViaVersion.exception.CancelCodecException
|
||||||
import java.net.SocketAddress
|
import java.net.SocketAddress
|
||||||
|
|
||||||
@ -13,7 +14,7 @@ class MinecraftHandler(
|
|||||||
val data: ConnectionData,
|
val data: ConnectionData,
|
||||||
val frontEnd: Boolean
|
val frontEnd: Boolean
|
||||||
) : SimpleChannelInboundHandler<Packet>() {
|
) : SimpleChannelInboundHandler<Packet>() {
|
||||||
lateinit var remoteAddress: SocketAddress
|
lateinit var endRemoteAddress: SocketAddress
|
||||||
val other: Channel? get() = if (frontEnd) data.backChannel else data.frontChannel
|
val other: Channel? get() = if (frontEnd) data.backChannel else data.frontChannel
|
||||||
var msgDisconnected = false
|
var msgDisconnected = false
|
||||||
|
|
||||||
@ -24,7 +25,8 @@ class MinecraftHandler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun channelActive(ctx: ChannelHandlerContext) {
|
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) {
|
override fun channelInactive(ctx: ChannelHandlerContext) {
|
||||||
|
@ -5,6 +5,7 @@ import com.github.creeper123123321.viaaas.handler.state.MinecraftConnectionState
|
|||||||
import com.github.creeper123123321.viaaas.mcLogger
|
import com.github.creeper123123321.viaaas.mcLogger
|
||||||
import com.github.creeper123123321.viaaas.packet.Packet
|
import com.github.creeper123123321.viaaas.packet.Packet
|
||||||
import com.github.creeper123123321.viaaas.packet.status.StatusResponse
|
import com.github.creeper123123321.viaaas.packet.status.StatusResponse
|
||||||
|
import com.github.creeper123123321.viaaas.parseProtocol
|
||||||
import com.google.gson.JsonParser
|
import com.google.gson.JsonParser
|
||||||
import io.netty.channel.ChannelHandlerContext
|
import io.netty.channel.ChannelHandlerContext
|
||||||
import us.myles.ViaVersion.api.protocol.ProtocolVersion
|
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) {
|
override fun handlePacket(handler: MinecraftHandler, ctx: ChannelHandlerContext, packet: Packet) {
|
||||||
handler.data.frontChannel.close()
|
handler.data.frontChannel.close()
|
||||||
if (packet !is StatusResponse) throw IllegalArgumentException()
|
if (packet !is StatusResponse) throw IllegalArgumentException()
|
||||||
val ver = ProtocolVersion.getProtocol(
|
val ver = JsonParser.parseString(packet.json).asJsonObject
|
||||||
JsonParser.parseString(packet.json).asJsonObject.getAsJsonObject("version").get("protocol").asInt
|
.getAsJsonObject("version").get("protocol").asInt.parseProtocol()
|
||||||
)
|
|
||||||
future.complete(ver)
|
future.complete(ver)
|
||||||
mcLogger.info("A.D.: ${handler.remoteAddress} $ver")
|
mcLogger.info("A.D.: ${handler.endRemoteAddress} $ver")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun disconnect(handler: MinecraftHandler, msg: String) {
|
override fun disconnect(handler: MinecraftHandler, msg: String) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.github.creeper123123321.viaaas.handler.autoprotocol
|
package com.github.creeper123123321.viaaas.handler.autoprotocol
|
||||||
|
|
||||||
import com.github.creeper123123321.viaaas.handler.ConnectionData
|
import com.github.creeper123123321.viaaas.handler.ConnectionData
|
||||||
|
import com.github.creeper123123321.viaaas.handler.MinecraftHandler
|
||||||
import com.github.creeper123123321.viaaas.mcLogger
|
import com.github.creeper123123321.viaaas.mcLogger
|
||||||
import io.netty.channel.ChannelHandlerContext
|
import io.netty.channel.ChannelHandlerContext
|
||||||
import io.netty.channel.ChannelPromise
|
import io.netty.channel.ChannelPromise
|
||||||
@ -19,16 +20,16 @@ class ProtocolDetectorHandler(val connectionData: ConnectionData) : ChannelDuple
|
|||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
override fun channelActive(ctx: ChannelHandlerContext) {
|
override fun channelActive(ctx: ChannelHandlerContext) {
|
||||||
super.channelActive(ctx)
|
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({
|
val timeoutRun = ctx.executor().schedule({
|
||||||
mcLogger.warn(
|
mcLogger.warn("Timeout protocol A.D. $address")
|
||||||
"Timeout protocol A.D. " + ctx.channel().remoteAddress()
|
|
||||||
)
|
|
||||||
hold = false
|
hold = false
|
||||||
drainQueue(ctx)
|
drainQueue(ctx)
|
||||||
ctx.pipeline().remove(this)
|
ctx.pipeline().remove(this)
|
||||||
}, 10, TimeUnit.SECONDS)
|
}, 10, TimeUnit.SECONDS)
|
||||||
ProtocolDetector.detectVersion(ctx.channel().remoteAddress() as InetSocketAddress)
|
ProtocolDetector.detectVersion(address)
|
||||||
.whenComplete { protocol, _ ->
|
.whenComplete { protocol, _ ->
|
||||||
if (protocol != null && protocol.version != -1) {
|
if (protocol != null && protocol.version != -1) {
|
||||||
connectionData.viaBackServerVer = protocol.version
|
connectionData.viaBackServerVer = protocol.version
|
||||||
|
@ -37,7 +37,7 @@ class HandshakeState : MinecraftConnectionState {
|
|||||||
else -> throw IllegalStateException("Invalid next state")
|
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")
|
throw IllegalStateException("Rate-limited")
|
||||||
}
|
}
|
||||||
val virtualPort = packet.port
|
val virtualPort = packet.port
|
||||||
@ -66,7 +66,7 @@ class HandshakeState : MinecraftConnectionState {
|
|||||||
it.backAddress = packet.address to packet.port
|
it.backAddress = packet.address to packet.port
|
||||||
}
|
}
|
||||||
|
|
||||||
val playerAddr = handler.data.frontHandler.remoteAddress
|
val playerAddr = handler.data.frontHandler.endRemoteAddress
|
||||||
mcLogger.info(
|
mcLogger.info(
|
||||||
"HS: $playerAddr ${handler.data.state.state.toString().substring(0, 1)} " +
|
"HS: $playerAddr ${handler.data.state.state.toString().substring(0, 1)} " +
|
||||||
"$virtualHostNoExtra $virtualPort v${handler.data.frontVer}"
|
"$virtualHostNoExtra $virtualPort v${handler.data.frontVer}"
|
||||||
|
@ -107,8 +107,8 @@ class LoginState : MinecraftConnectionState {
|
|||||||
parseUndashedId(playerId),
|
parseUndashedId(playerId),
|
||||||
backName!!,
|
backName!!,
|
||||||
backHash,
|
backHash,
|
||||||
frontHandler.remoteAddress,
|
frontHandler.endRemoteAddress,
|
||||||
handler.data.backHandler!!.remoteAddress
|
handler.data.backHandler!!.endRemoteAddress
|
||||||
).whenCompleteAsync({ _, throwable ->
|
).whenCompleteAsync({ _, throwable ->
|
||||||
if (throwable != null) {
|
if (throwable != null) {
|
||||||
frontHandler.data.backHandler!!.disconnect("Online mode error: $throwable")
|
frontHandler.data.backHandler!!.disconnect("Online mode error: $throwable")
|
||||||
@ -178,7 +178,7 @@ class LoginState : MinecraftConnectionState {
|
|||||||
if (e != null) {
|
if (e != null) {
|
||||||
disconnect(handler, "Profile error: $e")
|
disconnect(handler, "Profile error: $e")
|
||||||
} else {
|
} else {
|
||||||
mcLogger.info("Login: ${handler.remoteAddress} $frontName $id")
|
mcLogger.info("Login: ${handler.endRemoteAddress} $frontName $id")
|
||||||
if (frontOnline != null) {
|
if (frontOnline != null) {
|
||||||
connect()
|
connect()
|
||||||
}
|
}
|
||||||
|
@ -16,11 +16,11 @@ interface MinecraftConnectionState {
|
|||||||
fun disconnect(handler: MinecraftHandler, msg: String) {
|
fun disconnect(handler: MinecraftHandler, msg: String) {
|
||||||
if (!handler.msgDisconnected) {
|
if (!handler.msgDisconnected) {
|
||||||
handler.msgDisconnected = true
|
handler.msgDisconnected = true
|
||||||
mcLogger.info("DC ${handler.remoteAddress}: $msg")
|
mcLogger.info("DC ${handler.endRemoteAddress}: $msg")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onInactivated(handler: MinecraftHandler) {
|
fun onInactivated(handler: MinecraftHandler) {
|
||||||
mcLogger.info("- ${handler.remoteAddress}")
|
mcLogger.info("- ${handler.endRemoteAddress}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,13 +8,13 @@ import com.github.creeper123123321.viaaas.packet.Packet
|
|||||||
import com.github.creeper123123321.viaaas.packet.UnknownPacket
|
import com.github.creeper123123321.viaaas.packet.UnknownPacket
|
||||||
import com.github.creeper123123321.viaaas.packet.play.Kick
|
import com.github.creeper123123321.viaaas.packet.play.Kick
|
||||||
import com.github.creeper123123321.viaaas.packet.play.PluginMessage
|
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.readableToByteArray
|
||||||
import com.github.creeper123123321.viaaas.writeFlushClose
|
import com.github.creeper123123321.viaaas.writeFlushClose
|
||||||
import com.google.gson.JsonPrimitive
|
import com.google.gson.JsonPrimitive
|
||||||
import io.netty.buffer.ByteBufAllocator
|
import io.netty.buffer.ByteBufAllocator
|
||||||
import io.netty.buffer.Unpooled
|
import io.netty.buffer.Unpooled
|
||||||
import io.netty.channel.ChannelHandlerContext
|
import io.netty.channel.ChannelHandlerContext
|
||||||
import us.myles.ViaVersion.api.protocol.ProtocolVersion
|
|
||||||
import us.myles.ViaVersion.api.type.Type
|
import us.myles.ViaVersion.api.type.Type
|
||||||
import us.myles.ViaVersion.packets.State
|
import us.myles.ViaVersion.packets.State
|
||||||
|
|
||||||
@ -38,11 +38,8 @@ object PlayState : MinecraftConnectionState {
|
|||||||
String(pluginMessage.data, Charsets.UTF_8)
|
String(pluginMessage.data, Charsets.UTF_8)
|
||||||
} else {
|
} else {
|
||||||
Type.STRING.read(Unpooled.wrappedBuffer(pluginMessage.data))
|
Type.STRING.read(Unpooled.wrappedBuffer(pluginMessage.data))
|
||||||
} + " (VIAaaS C: ${ProtocolVersion.getProtocol(handler.data.frontVer!!)} S: ${
|
} + " (VIAaaS C: ${handler.data.frontVer!!.parseProtocol()} S: ${
|
||||||
ProtocolVersion.getProtocol(
|
handler.data.viaBackServerVer!!.parseProtocol()})"
|
||||||
handler.data.viaBackServerVer!!
|
|
||||||
)
|
|
||||||
})"
|
|
||||||
|
|
||||||
if (is1_7(handler)) {
|
if (is1_7(handler)) {
|
||||||
pluginMessage.data = brand.toByteArray(Charsets.UTF_8)
|
pluginMessage.data = brand.toByteArray(Charsets.UTF_8)
|
||||||
|
@ -6,13 +6,13 @@ import com.github.creeper123123321.viaaas.handler.forward
|
|||||||
import com.github.creeper123123321.viaaas.packet.Packet
|
import com.github.creeper123123321.viaaas.packet.Packet
|
||||||
import com.github.creeper123123321.viaaas.packet.UnknownPacket
|
import com.github.creeper123123321.viaaas.packet.UnknownPacket
|
||||||
import com.github.creeper123123321.viaaas.packet.status.StatusResponse
|
import com.github.creeper123123321.viaaas.packet.status.StatusResponse
|
||||||
|
import com.github.creeper123123321.viaaas.parseProtocol
|
||||||
import com.github.creeper123123321.viaaas.writeFlushClose
|
import com.github.creeper123123321.viaaas.writeFlushClose
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.google.gson.JsonArray
|
import com.google.gson.JsonArray
|
||||||
import com.google.gson.JsonObject
|
import com.google.gson.JsonObject
|
||||||
import com.google.gson.JsonParser
|
import com.google.gson.JsonParser
|
||||||
import io.netty.channel.ChannelHandlerContext
|
import io.netty.channel.ChannelHandlerContext
|
||||||
import us.myles.ViaVersion.api.protocol.ProtocolVersion
|
|
||||||
import us.myles.ViaVersion.packets.State
|
import us.myles.ViaVersion.packets.State
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
@ -37,8 +37,8 @@ object StatusState : MinecraftConnectionState {
|
|||||||
it.addProperty("id", UUID.nameUUIDFromBytes("VIAaaS".toByteArray(Charsets.UTF_8)).toString())
|
it.addProperty("id", UUID.nameUUIDFromBytes("VIAaaS".toByteArray(Charsets.UTF_8)).toString())
|
||||||
it.addProperty(
|
it.addProperty(
|
||||||
"name",
|
"name",
|
||||||
"§9VIAaaS§r (C: §7${ProtocolVersion.getProtocol(handler.data.frontVer!!)}§r S: §7${
|
"§9VIAaaS§r (C: §7${handler.data.frontVer!!.parseProtocol()}§r S: §7${
|
||||||
ProtocolVersion.getProtocol(handler.data.viaBackServerVer!!)
|
handler.data.viaBackServerVer!!.parseProtocol()
|
||||||
}§r)"
|
}§r)"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
@ -30,7 +30,7 @@ private fun createBackChannel(handler: MinecraftHandler, socketAddr: InetSocketA
|
|||||||
.connect(socketAddr)
|
.connect(socketAddr)
|
||||||
.addListener(ChannelFutureListener {
|
.addListener(ChannelFutureListener {
|
||||||
if (it.isSuccess) {
|
if (it.isSuccess) {
|
||||||
mcLogger.info("+ ${handler.remoteAddress} -> $socketAddr")
|
mcLogger.info("+ ${handler.endRemoteAddress} -> $socketAddr")
|
||||||
handler.data.backChannel = it.channel() as SocketChannel
|
handler.data.backChannel = it.channel() as SocketChannel
|
||||||
|
|
||||||
val packet = Handshake()
|
val packet = Handshake()
|
||||||
|
Loading…
Reference in New Issue
Block a user