finish change to IntendedState

This commit is contained in:
creeper123123321 2024-05-29 11:44:00 -03:00
parent 72d6e4a188
commit dec53223e4
9 changed files with 30 additions and 43 deletions

View File

@ -2,7 +2,6 @@ package com.viaversion.aas.codec.packet.handshake;
import com.viaversion.aas.codec.packet.Packet; import com.viaversion.aas.codec.packet.Packet;
import com.viaversion.aas.util.IntendedState; import com.viaversion.aas.util.IntendedState;
import com.viaversion.viaversion.api.protocol.packet.State;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Type;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
@ -12,7 +11,6 @@ public class Handshake implements Packet {
private int protocolId; private int protocolId;
private String address; private String address;
private int port; private int port;
private State nextState;
private IntendedState intendedState; private IntendedState intendedState;
@Override @Override
@ -20,11 +18,7 @@ public class Handshake implements Packet {
protocolId = Type.VAR_INT.readPrimitive(byteBuf); protocolId = Type.VAR_INT.readPrimitive(byteBuf);
address = Type.STRING.read(byteBuf); address = Type.STRING.read(byteBuf);
port = byteBuf.readUnsignedShort(); port = byteBuf.readUnsignedShort();
if (protocolVersion.newerThanOrEqualTo(ProtocolVersion.v1_20_5)) { intendedState = IntendedState.values()[Type.VAR_INT.readPrimitive(byteBuf)];
intendedState = IntendedState.values()[Type.VAR_INT.readPrimitive(byteBuf) - 1];
} else {
nextState = State.values()[Type.VAR_INT.readPrimitive(byteBuf)];
}
} }
@Override @Override
@ -32,11 +26,7 @@ public class Handshake implements Packet {
Type.VAR_INT.writePrimitive(byteBuf, protocolId); Type.VAR_INT.writePrimitive(byteBuf, protocolId);
Type.STRING.write(byteBuf, address); Type.STRING.write(byteBuf, address);
byteBuf.writeShort(port); byteBuf.writeShort(port);
if (protocolVersion.newerThanOrEqualTo(ProtocolVersion.v1_20_5)) { Type.VAR_INT.writePrimitive(byteBuf, intendedState.ordinal());
Type.VAR_INT.writePrimitive(byteBuf, intendedState.ordinal() + 1);
} else {
byteBuf.writeByte(nextState.ordinal()); // var int is too small, fits in a byte
}
} }
public int getProtocolId() { public int getProtocolId() {
@ -63,14 +53,6 @@ public class Handshake implements Packet {
this.port = port; this.port = port;
} }
public State getNextState() {
return nextState;
}
public void setNextState(State nextState) {
this.nextState = nextState;
}
public IntendedState getIntendedState() { public IntendedState getIntendedState() {
return intendedState; return intendedState;
} }

View File

@ -3,7 +3,7 @@ package com.viaversion.aas.util;
import com.viaversion.viaversion.api.protocol.packet.State; import com.viaversion.viaversion.api.protocol.packet.State;
public enum IntendedState { public enum IntendedState {
INVALID(null),
STATUS(State.STATUS), STATUS(State.STATUS),
LOGIN(State.LOGIN), LOGIN(State.LOGIN),
TRANSFER(State.LOGIN); TRANSFER(State.LOGIN);

View File

@ -7,11 +7,10 @@ import com.google.common.primitives.Ints
import com.google.gson.JsonObject import com.google.gson.JsonObject
import com.viaversion.aas.config.VIAaaSConfig import com.viaversion.aas.config.VIAaaSConfig
import com.viaversion.aas.util.StacklessException import com.viaversion.aas.util.StacklessException
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion
import com.viaversion.viaversion.api.type.Type import com.viaversion.viaversion.api.type.Type
import io.ktor.client.call.*
import io.ktor.client.call.body import io.ktor.client.call.body
import io.ktor.client.request.* import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.server.netty.* import io.ktor.server.netty.*
import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBuf
import io.netty.channel.Channel import io.netty.channel.Channel
@ -191,11 +190,13 @@ fun readRemainingBytes(byteBuf: ByteBuf) = Type.REMAINING_BYTES.read(byteBuf)!!
fun ByteBuf.readByteArray(length: Int) = ByteArray(length).also { readBytes(it) } fun ByteBuf.readByteArray(length: Int) = ByteArray(length).also { readBytes(it) }
suspend fun hasJoined(username: String, hash: String): JsonObject { suspend fun hasJoined(username: String, hash: String): JsonObject {
return try { try {
AspirinServer.httpClient.get( val req = AspirinServer.httpClient.get(
"https://sessionserver.mojang.com/session/minecraft/hasJoined?username=" + "https://sessionserver.mojang.com/session/minecraft/hasJoined?username=" +
UrlEscapers.urlFormParameterEscaper().escape(username) + "&serverId=$hash" UrlEscapers.urlFormParameterEscaper().escape(username) + "&serverId=$hash"
).body() )
if (!req.status.isSuccess() || req.status == HttpStatusCode.NoContent) throw StacklessException("http code ${req.status}")
return req.body()
} catch (e: Exception) { } catch (e: Exception) {
throw StacklessException("Couldn't authenticate with session servers", e) throw StacklessException("Couldn't authenticate with session servers", e)
} }

View File

@ -151,7 +151,8 @@ object PacketRegistry {
ProtocolVersion.v1_20_5.singleton to ServerboundPackets1_20_5.CONFIGURATION_ACKNOWLEDGED.id ProtocolVersion.v1_20_5.singleton to ServerboundPackets1_20_5.CONFIGURATION_ACKNOWLEDGED.id
) )
) )
// todo update this to latest version // todo update chat to latest version
// todo handle transfer packets
register( register(
State.PLAY, Direction.SERVERBOUND, ::ServerboundChatCommand, State.PLAY, Direction.SERVERBOUND, ::ServerboundChatCommand,
mapOf( mapOf(

View File

@ -13,7 +13,7 @@ import com.viaversion.aas.handler.ConnectionData
import com.viaversion.aas.handler.MinecraftHandler import com.viaversion.aas.handler.MinecraftHandler
import com.viaversion.aas.handler.addProxyHandler import com.viaversion.aas.handler.addProxyHandler
import com.viaversion.aas.send import com.viaversion.aas.send
import com.viaversion.viaversion.api.protocol.packet.State import com.viaversion.aas.util.IntendedState
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion import com.viaversion.viaversion.api.protocol.version.ProtocolVersion
import io.ktor.server.netty.* import io.ktor.server.netty.*
import io.netty.bootstrap.Bootstrap import io.netty.bootstrap.Bootstrap
@ -66,7 +66,7 @@ object ProtocolDetector {
handshake.address = address.hostString handshake.address = address.hostString
handshake.port = address.port handshake.port = address.port
handshake.protocolId = -1 handshake.protocolId = -1
handshake.nextState = State.STATUS handshake.intendedState = IntendedState.STATUS
send(ch.channel(), handshake) send(ch.channel(), handshake)
send(ch.channel(), StatusRequest(), flush = true) send(ch.channel(), StatusRequest(), flush = true)
} }

View File

@ -8,8 +8,8 @@ import com.viaversion.aas.handler.BackEndInit
import com.viaversion.aas.handler.MinecraftHandler import com.viaversion.aas.handler.MinecraftHandler
import com.viaversion.aas.handler.autoprotocol.ProtocolDetector import com.viaversion.aas.handler.autoprotocol.ProtocolDetector
import com.viaversion.aas.handler.forward import com.viaversion.aas.handler.forward
import com.viaversion.aas.util.IntendedState
import com.viaversion.aas.util.StacklessException import com.viaversion.aas.util.StacklessException
import com.viaversion.viaversion.api.protocol.packet.State
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion import com.viaversion.viaversion.api.protocol.version.ProtocolVersion
import com.viaversion.viaversion.api.type.Type import com.viaversion.viaversion.api.type.Type
import io.ktor.server.netty.* import io.ktor.server.netty.*
@ -30,7 +30,7 @@ import kotlin.math.ceil
private suspend fun createBackChannel( private suspend fun createBackChannel(
handler: MinecraftHandler, handler: MinecraftHandler,
socketAddr: InetSocketAddress, socketAddr: InetSocketAddress,
state: State, state: IntendedState,
extraData: String?, extraData: String?,
proxyUri: URI?, proxyUri: URI?,
proxyAddress: InetSocketAddress? proxyAddress: InetSocketAddress?
@ -53,7 +53,7 @@ private suspend fun createBackChannel(
.channel() .channel()
(channel.pipeline()["proxy"] as? ProxyHandler)?.connectFuture()?.suspendAwait() (channel.pipeline()["proxy"] as? ProxyHandler)?.connectFuture()?.suspendAwait()
if (state == State.LOGIN) { if (state == IntendedState.LOGIN) {
mcLogger.info("+ L {} -> {}", handler.endRemoteAddress, socketAddr) mcLogger.info("+ L {} -> {}", handler.endRemoteAddress, socketAddr)
} else { } else {
mcLogger.debug("+ {} {} -> {}", state.name[0], handler.endRemoteAddress, socketAddr) mcLogger.debug("+ {} {} -> {}", state.name[0], handler.endRemoteAddress, socketAddr)
@ -61,7 +61,7 @@ private suspend fun createBackChannel(
handler.data.backChannel = channel as SocketChannel handler.data.backChannel = channel as SocketChannel
val packet = Handshake() val packet = Handshake()
packet.nextState = state packet.intendedState = state
packet.protocolId = handler.data.frontVer!!.version packet.protocolId = handler.data.frontVer!!.version
packet.address = socketAddr.hostString + if (extraData != null) 0.toChar() + extraData else "" packet.address = socketAddr.hostString + if (extraData != null) 0.toChar() + extraData else ""
packet.port = socketAddr.port packet.port = socketAddr.port
@ -95,7 +95,7 @@ private suspend fun autoDetectVersion(handler: MinecraftHandler, socketAddr: Ine
private suspend fun tryBackAddresses( private suspend fun tryBackAddresses(
handler: MinecraftHandler, handler: MinecraftHandler,
addresses: Iterable<InetSocketAddress>, addresses: Iterable<InetSocketAddress>,
state: State, state: IntendedState,
extraData: String? extraData: String?
) { ) {
var latestException: Exception? = null var latestException: Exception? = null
@ -145,7 +145,7 @@ private suspend fun resolveBackendAddresses(hostAndPort: HostAndPort): List<Inet
suspend fun connectBack( suspend fun connectBack(
handler: MinecraftHandler, handler: MinecraftHandler,
address: HostAndPort, address: HostAndPort,
state: State, state: IntendedState,
extraData: String? = null extraData: String? = null
) { ) {
val addresses = resolveBackendAddresses(address) val addresses = resolveBackendAddresses(address)

View File

@ -11,6 +11,7 @@ import com.viaversion.aas.config.VIAaaSConfig
import com.viaversion.aas.handler.MinecraftHandler import com.viaversion.aas.handler.MinecraftHandler
import com.viaversion.aas.mcLogger import com.viaversion.aas.mcLogger
import com.viaversion.aas.util.AddressParser import com.viaversion.aas.util.AddressParser
import com.viaversion.aas.util.IntendedState
import com.viaversion.aas.util.StacklessException import com.viaversion.aas.util.StacklessException
import com.viaversion.viaversion.api.protocol.packet.State import com.viaversion.viaversion.api.protocol.packet.State
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion import com.viaversion.viaversion.api.protocol.version.ProtocolVersion
@ -36,12 +37,12 @@ class HandshakeState : ConnectionState {
override val state: State override val state: State
get() = State.HANDSHAKE get() = State.HANDSHAKE
private fun checkRateLimit(handler: MinecraftHandler, state: State) { private fun checkRateLimit(handler: MinecraftHandler, state: IntendedState) {
val socketAddress = (handler.endRemoteAddress as InetSocketAddress).address val socketAddress = (handler.endRemoteAddress as InetSocketAddress).address
val limit = RateLimit.rateLimitByIp[socketAddress] val limit = RateLimit.rateLimitByIp[socketAddress]
if (!limit.handshakeLimiter.tryAcquire() if (!limit.handshakeLimiter.tryAcquire()
|| (state == State.LOGIN && !limit.loginLimiter.tryAcquire()) || (state == IntendedState.LOGIN && !limit.loginLimiter.tryAcquire())
) { ) {
throw StacklessException("Rate-limited") throw StacklessException("Rate-limited")
} }
@ -50,9 +51,9 @@ class HandshakeState : ConnectionState {
private fun handleNextState(handler: MinecraftHandler, packet: Handshake) { private fun handleNextState(handler: MinecraftHandler, packet: Handshake) {
handler.data.frontVer = ProtocolVersion.getProtocol(packet.protocolId) handler.data.frontVer = ProtocolVersion.getProtocol(packet.protocolId)
when (packet.nextState.ordinal) { when (packet.intendedState) {
1 -> handler.data.state = StatusState() IntendedState.STATUS -> handler.data.state = StatusState()
2 -> handler.data.state = LoginState() IntendedState.LOGIN -> handler.data.state = LoginState()
else -> throw StacklessException("Invalid next state") else -> throw StacklessException("Invalid next state")
} }
} }
@ -117,7 +118,7 @@ class HandshakeState : ConnectionState {
if (packet !is Handshake) throw StacklessException("Invalid packet!") if (packet !is Handshake) throw StacklessException("Invalid packet!")
handleNextState(handler, packet) handleNextState(handler, packet)
checkRateLimit(handler, packet.nextState) checkRateLimit(handler, packet.intendedState)
handleVirtualHost(handler, packet) handleVirtualHost(handler, packet)
handler.data.state.start(handler) handler.data.state.start(handler)

View File

@ -10,6 +10,7 @@ import com.viaversion.aas.config.VIAaaSConfig
import com.viaversion.aas.handler.MinecraftHandler import com.viaversion.aas.handler.MinecraftHandler
import com.viaversion.aas.handler.forward import com.viaversion.aas.handler.forward
import com.viaversion.aas.handler.setCompression import com.viaversion.aas.handler.setCompression
import com.viaversion.aas.util.IntendedState
import com.viaversion.aas.util.StacklessException import com.viaversion.aas.util.StacklessException
import com.viaversion.viaversion.api.protocol.packet.State import com.viaversion.viaversion.api.protocol.packet.State
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion import com.viaversion.viaversion.api.protocol.version.ProtocolVersion
@ -291,7 +292,7 @@ class LoginState : ConnectionState {
connectBack( connectBack(
handler, handler,
HostAndPort.fromParts(backAddress!!.host, backAddress!!.port), HostAndPort.fromParts(backAddress!!.host, backAddress!!.port),
State.LOGIN, IntendedState.LOGIN,
extraData extraData
) )
loginStart.username = backName!! loginStart.username = backName!!

View File

@ -10,6 +10,7 @@ import com.viaversion.aas.codec.packet.status.StatusResponse
import com.viaversion.aas.config.VIAaaSConfig import com.viaversion.aas.config.VIAaaSConfig
import com.viaversion.aas.handler.MinecraftHandler import com.viaversion.aas.handler.MinecraftHandler
import com.viaversion.aas.handler.forward import com.viaversion.aas.handler.forward
import com.viaversion.aas.util.IntendedState
import com.viaversion.aas.util.StacklessException import com.viaversion.aas.util.StacklessException
import com.viaversion.viaversion.api.protocol.packet.State import com.viaversion.viaversion.api.protocol.packet.State
import io.netty.channel.ChannelHandlerContext import io.netty.channel.ChannelHandlerContext
@ -76,7 +77,7 @@ class StatusState : ConnectionState {
handler.coroutineScope.launch(Dispatchers.IO) { handler.coroutineScope.launch(Dispatchers.IO) {
try { try {
if (address != null) { if (address != null) {
connectBack(handler, address!!, state) connectBack(handler, address!!, IntendedState.STATUS)
} else { } else {
handler.disconnect("VIAaaS") handler.disconnect("VIAaaS")
} }