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

View File

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

View File

@ -7,11 +7,10 @@ import com.google.common.primitives.Ints
import com.google.gson.JsonObject
import com.viaversion.aas.config.VIAaaSConfig
import com.viaversion.aas.util.StacklessException
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion
import com.viaversion.viaversion.api.type.Type
import io.ktor.client.call.*
import io.ktor.client.call.body
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.server.netty.*
import io.netty.buffer.ByteBuf
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) }
suspend fun hasJoined(username: String, hash: String): JsonObject {
return try {
AspirinServer.httpClient.get(
try {
val req = AspirinServer.httpClient.get(
"https://sessionserver.mojang.com/session/minecraft/hasJoined?username=" +
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) {
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
)
)
// todo update this to latest version
// todo update chat to latest version
// todo handle transfer packets
register(
State.PLAY, Direction.SERVERBOUND, ::ServerboundChatCommand,
mapOf(

View File

@ -13,7 +13,7 @@ import com.viaversion.aas.handler.ConnectionData
import com.viaversion.aas.handler.MinecraftHandler
import com.viaversion.aas.handler.addProxyHandler
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 io.ktor.server.netty.*
import io.netty.bootstrap.Bootstrap
@ -66,7 +66,7 @@ object ProtocolDetector {
handshake.address = address.hostString
handshake.port = address.port
handshake.protocolId = -1
handshake.nextState = State.STATUS
handshake.intendedState = IntendedState.STATUS
send(ch.channel(), handshake)
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.autoprotocol.ProtocolDetector
import com.viaversion.aas.handler.forward
import com.viaversion.aas.util.IntendedState
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.type.Type
import io.ktor.server.netty.*
@ -30,7 +30,7 @@ import kotlin.math.ceil
private suspend fun createBackChannel(
handler: MinecraftHandler,
socketAddr: InetSocketAddress,
state: State,
state: IntendedState,
extraData: String?,
proxyUri: URI?,
proxyAddress: InetSocketAddress?
@ -53,7 +53,7 @@ private suspend fun createBackChannel(
.channel()
(channel.pipeline()["proxy"] as? ProxyHandler)?.connectFuture()?.suspendAwait()
if (state == State.LOGIN) {
if (state == IntendedState.LOGIN) {
mcLogger.info("+ L {} -> {}", handler.endRemoteAddress, socketAddr)
} else {
mcLogger.debug("+ {} {} -> {}", state.name[0], handler.endRemoteAddress, socketAddr)
@ -61,7 +61,7 @@ private suspend fun createBackChannel(
handler.data.backChannel = channel as SocketChannel
val packet = Handshake()
packet.nextState = state
packet.intendedState = state
packet.protocolId = handler.data.frontVer!!.version
packet.address = socketAddr.hostString + if (extraData != null) 0.toChar() + extraData else ""
packet.port = socketAddr.port
@ -95,7 +95,7 @@ private suspend fun autoDetectVersion(handler: MinecraftHandler, socketAddr: Ine
private suspend fun tryBackAddresses(
handler: MinecraftHandler,
addresses: Iterable<InetSocketAddress>,
state: State,
state: IntendedState,
extraData: String?
) {
var latestException: Exception? = null
@ -145,7 +145,7 @@ private suspend fun resolveBackendAddresses(hostAndPort: HostAndPort): List<Inet
suspend fun connectBack(
handler: MinecraftHandler,
address: HostAndPort,
state: State,
state: IntendedState,
extraData: String? = null
) {
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.mcLogger
import com.viaversion.aas.util.AddressParser
import com.viaversion.aas.util.IntendedState
import com.viaversion.aas.util.StacklessException
import com.viaversion.viaversion.api.protocol.packet.State
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion
@ -36,12 +37,12 @@ class HandshakeState : ConnectionState {
override val state: State
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 limit = RateLimit.rateLimitByIp[socketAddress]
if (!limit.handshakeLimiter.tryAcquire()
|| (state == State.LOGIN && !limit.loginLimiter.tryAcquire())
|| (state == IntendedState.LOGIN && !limit.loginLimiter.tryAcquire())
) {
throw StacklessException("Rate-limited")
}
@ -50,9 +51,9 @@ class HandshakeState : ConnectionState {
private fun handleNextState(handler: MinecraftHandler, packet: Handshake) {
handler.data.frontVer = ProtocolVersion.getProtocol(packet.protocolId)
when (packet.nextState.ordinal) {
1 -> handler.data.state = StatusState()
2 -> handler.data.state = LoginState()
when (packet.intendedState) {
IntendedState.STATUS -> handler.data.state = StatusState()
IntendedState.LOGIN -> handler.data.state = LoginState()
else -> throw StacklessException("Invalid next state")
}
}
@ -117,7 +118,7 @@ class HandshakeState : ConnectionState {
if (packet !is Handshake) throw StacklessException("Invalid packet!")
handleNextState(handler, packet)
checkRateLimit(handler, packet.nextState)
checkRateLimit(handler, packet.intendedState)
handleVirtualHost(handler, packet)
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.forward
import com.viaversion.aas.handler.setCompression
import com.viaversion.aas.util.IntendedState
import com.viaversion.aas.util.StacklessException
import com.viaversion.viaversion.api.protocol.packet.State
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion
@ -291,7 +292,7 @@ class LoginState : ConnectionState {
connectBack(
handler,
HostAndPort.fromParts(backAddress!!.host, backAddress!!.port),
State.LOGIN,
IntendedState.LOGIN,
extraData
)
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.handler.MinecraftHandler
import com.viaversion.aas.handler.forward
import com.viaversion.aas.util.IntendedState
import com.viaversion.aas.util.StacklessException
import com.viaversion.viaversion.api.protocol.packet.State
import io.netty.channel.ChannelHandlerContext
@ -76,7 +77,7 @@ class StatusState : ConnectionState {
handler.coroutineScope.launch(Dispatchers.IO) {
try {
if (address != null) {
connectBack(handler, address!!, state)
connectBack(handler, address!!, IntendedState.STATUS)
} else {
handler.disconnect("VIAaaS")
}