mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-19 06:32:03 +01:00
Fix query responses
This commit is contained in:
parent
17d584a2e3
commit
c6082a35bb
@ -1,12 +1,14 @@
|
|||||||
package net.minestom.server.extras.query;
|
package net.minestom.server.extras.query;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.buffer.ByteBufUtil;
|
||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
|
import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||||
import net.minestom.server.MinecraftServer;
|
import net.minestom.server.MinecraftServer;
|
||||||
import net.minestom.server.extras.query.event.BasicQueryEvent;
|
import net.minestom.server.extras.query.event.BasicQueryEvent;
|
||||||
|
import net.minestom.server.extras.query.event.FullQueryEvent;
|
||||||
import net.minestom.server.extras.query.response.QueryResponse;
|
import net.minestom.server.extras.query.response.QueryResponse;
|
||||||
import net.minestom.server.timer.Task;
|
import net.minestom.server.timer.Task;
|
||||||
import net.minestom.server.utils.NetworkUtils;
|
import net.minestom.server.utils.NetworkUtils;
|
||||||
@ -25,7 +27,7 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class to manage responses to the UT3 Query Protocol.
|
* Utility class to manage responses to the GameSpy4 Query Protocol.
|
||||||
* @see <a href="https://wiki.vg/Query">wiki.vg</a>
|
* @see <a href="https://wiki.vg/Query">wiki.vg</a>
|
||||||
*/
|
*/
|
||||||
public class Query {
|
public class Query {
|
||||||
@ -137,6 +139,7 @@ public class Query {
|
|||||||
|
|
||||||
// try and receive the packet
|
// try and receive the packet
|
||||||
try {
|
try {
|
||||||
|
System.out.println("WAITING");
|
||||||
socket.receive(packet);
|
socket.receive(packet);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (!started) {
|
if (!started) {
|
||||||
@ -152,11 +155,14 @@ public class Query {
|
|||||||
|
|
||||||
// check the magic field
|
// check the magic field
|
||||||
if (data.readUnsignedShort() != 0xFEFD) {
|
if (data.readUnsignedShort() != 0xFEFD) {
|
||||||
|
System.out.println("UNKNOWN");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// now check the query type
|
// now check the query type
|
||||||
byte type = data.readByte();
|
byte type = data.readByte();
|
||||||
|
System.out.println("READ " + type);
|
||||||
|
|
||||||
if (type == 9) { // handshake
|
if (type == 9) { // handshake
|
||||||
int sessionID = data.readInt();
|
int sessionID = data.readInt();
|
||||||
int challengeToken = RANDOM.nextInt();
|
int challengeToken = RANDOM.nextInt();
|
||||||
@ -180,16 +186,20 @@ public class Query {
|
|||||||
int challengeToken = data.readInt();
|
int challengeToken = data.readInt();
|
||||||
SocketAddress sender = packet.getSocketAddress();
|
SocketAddress sender = packet.getSocketAddress();
|
||||||
|
|
||||||
|
System.out.println(" STAT");
|
||||||
|
|
||||||
if (CHALLENGE_TOKENS.containsKey(challengeToken) && CHALLENGE_TOKENS.get(challengeToken).equals(sender)) {
|
if (CHALLENGE_TOKENS.containsKey(challengeToken) && CHALLENGE_TOKENS.get(challengeToken).equals(sender)) {
|
||||||
int remaining = data.readableBytes();
|
int remaining = data.readableBytes();
|
||||||
|
|
||||||
|
System.out.println(" REMAINING " + remaining);
|
||||||
|
|
||||||
if (remaining == 0) { // basic
|
if (remaining == 0) { // basic
|
||||||
BasicQueryEvent event = new BasicQueryEvent(sender);
|
BasicQueryEvent event = new BasicQueryEvent(sender);
|
||||||
MinecraftServer.getGlobalEventHandler().callCancellableEvent(BasicQueryEvent.class, event,
|
MinecraftServer.getGlobalEventHandler().callCancellableEvent(BasicQueryEvent.class, event,
|
||||||
() -> sendResponse(event.getQueryResponse(), sessionID, sender));
|
() -> sendResponse(event.getQueryResponse(), sessionID, sender));
|
||||||
} else if (remaining == 8) { // full
|
} else if (remaining == 5) { // full
|
||||||
BasicQueryEvent event = new BasicQueryEvent(sender);
|
FullQueryEvent event = new FullQueryEvent(sender);
|
||||||
MinecraftServer.getGlobalEventHandler().callCancellableEvent(BasicQueryEvent.class, event,
|
MinecraftServer.getGlobalEventHandler().callCancellableEvent(FullQueryEvent.class, event,
|
||||||
() -> sendResponse(event.getQueryResponse(), sessionID, sender));
|
() -> sendResponse(event.getQueryResponse(), sessionID, sender));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -210,6 +220,10 @@ public class Query {
|
|||||||
byte[] responseData = response.toByteArray();
|
byte[] responseData = response.toByteArray();
|
||||||
try {
|
try {
|
||||||
socket.send(new DatagramPacket(responseData, responseData.length, sender));
|
socket.send(new DatagramPacket(responseData, responseData.length, sender));
|
||||||
|
|
||||||
|
System.out.println(ByteBufUtil.hexDump(responseData));
|
||||||
|
|
||||||
|
System.out.println("SENT!! basic=" + (queryResponse instanceof BasicQueryEvent));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOGGER.error("An error occurred whilst sending a query handshake packet.", e);
|
LOGGER.error("An error occurred whilst sending a query handshake packet.", e);
|
||||||
}
|
}
|
||||||
|
@ -139,7 +139,7 @@ public class FullQueryResponse implements QueryResponse {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(@NotNull BinaryWriter writer) {
|
public void write(@NotNull BinaryWriter writer) {
|
||||||
writer.writeBytes(PADDING_10);
|
writer.writeBytes(PADDING_11);
|
||||||
|
|
||||||
// key-values
|
// key-values
|
||||||
for (Map.Entry<String, String> entry : this.kv.entrySet()) {
|
for (Map.Entry<String, String> entry : this.kv.entrySet()) {
|
||||||
@ -148,7 +148,7 @@ public class FullQueryResponse implements QueryResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
writer.writeNullTerminatedString("", Query.CHARSET);
|
writer.writeNullTerminatedString("", Query.CHARSET);
|
||||||
writer.writeBytes(PADDING_11);
|
writer.writeBytes(PADDING_10);
|
||||||
|
|
||||||
// players
|
// players
|
||||||
for (String player : this.players) {
|
for (String player : this.players) {
|
||||||
|
Loading…
Reference in New Issue
Block a user