Debug cleanup, expose session ID

This commit is contained in:
Kieran Wallbanks 2021-04-27 15:31:47 +01:00
parent c6082a35bb
commit 2968ea2513
5 changed files with 33 additions and 31 deletions

View File

@ -1,7 +1,6 @@
package net.minestom.server.extras.query;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
@ -13,6 +12,7 @@ import net.minestom.server.extras.query.response.QueryResponse;
import net.minestom.server.timer.Task;
import net.minestom.server.utils.NetworkUtils;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.time.TimeUnit;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -34,13 +34,11 @@ public class Query {
public static final Charset CHARSET = StandardCharsets.ISO_8859_1;
private static final Logger LOGGER = LoggerFactory.getLogger(Query.class);
private static final Random RANDOM = new Random();
private static final Int2ObjectMap<SocketAddress> CHALLENGE_TOKENS = Int2ObjectMaps.synchronize(new Int2ObjectOpenHashMap<>());
private static volatile boolean started;
private static volatile DatagramSocket socket;
private static volatile Thread thread;
private static final Int2ObjectMap<SocketAddress> CHALLENGE_TOKENS = Int2ObjectMaps.synchronize(new Int2ObjectOpenHashMap<>());
private static volatile Task task;
private Query() { }
@ -90,10 +88,10 @@ public class Query {
thread.start();
started = true;
/*task = MinecraftServer.getSchedulerManager()
task = MinecraftServer.getSchedulerManager()
.buildTask(CHALLENGE_TOKENS::clear)
.repeat(30, TimeUnit.SECOND)
.schedule();*/
.schedule();
return true;
}
@ -139,7 +137,6 @@ public class Query {
// try and receive the packet
try {
System.out.println("WAITING");
socket.receive(packet);
} catch (IOException e) {
if (!started) {
@ -155,13 +152,11 @@ public class Query {
// check the magic field
if (data.readUnsignedShort() != 0xFEFD) {
System.out.println("UNKNOWN");
continue;
}
// now check the query type
byte type = data.readByte();
System.out.println("READ " + type);
if (type == 9) { // handshake
int sessionID = data.readInt();
@ -179,26 +174,26 @@ public class Query {
byte[] responseData = response.toByteArray();
socket.send(new DatagramPacket(responseData, responseData.length, packet.getSocketAddress()));
} catch (IOException e) {
LOGGER.error("An error occurred whilst sending a query handshake packet.", e);
if (!started) {
LOGGER.error("An error occurred whilst sending a query handshake packet.", e);
} else {
return;
}
}
} else if (type == 0) { // stat
int sessionID = data.readInt();
int challengeToken = data.readInt();
SocketAddress sender = packet.getSocketAddress();
System.out.println(" STAT");
if (CHALLENGE_TOKENS.containsKey(challengeToken) && CHALLENGE_TOKENS.get(challengeToken).equals(sender)) {
int remaining = data.readableBytes();
System.out.println(" REMAINING " + remaining);
if (remaining == 0) { // basic
BasicQueryEvent event = new BasicQueryEvent(sender);
BasicQueryEvent event = new BasicQueryEvent(sender, sessionID);
MinecraftServer.getGlobalEventHandler().callCancellableEvent(BasicQueryEvent.class, event,
() -> sendResponse(event.getQueryResponse(), sessionID, sender));
} else if (remaining == 5) { // full
FullQueryEvent event = new FullQueryEvent(sender);
FullQueryEvent event = new FullQueryEvent(sender, sessionID);
MinecraftServer.getGlobalEventHandler().callCancellableEvent(FullQueryEvent.class, event,
() -> sendResponse(event.getQueryResponse(), sessionID, sender));
}
@ -220,12 +215,10 @@ public class Query {
byte[] responseData = response.toByteArray();
try {
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) {
LOGGER.error("An error occurred whilst sending a query handshake packet.", e);
if (!started) {
LOGGER.error("An error occurred whilst sending a query handshake packet.", e);
}
}
}
}

View File

@ -13,9 +13,10 @@ public class BasicQueryEvent extends QueryEvent<BasicQueryResponse> {
/**
* Creates a new basic query event.
*
* @param sessionID the session ID
* @param sender the sender
*/
public BasicQueryEvent(@NotNull SocketAddress sender) {
super(sender, new BasicQueryResponse());
public BasicQueryEvent(@NotNull SocketAddress sender, int sessionID) {
super(sender, sessionID, new BasicQueryResponse());
}
}

View File

@ -14,8 +14,9 @@ public class FullQueryEvent extends QueryEvent<FullQueryResponse> {
* Creates a new full query event.
*
* @param sender the sender
* @param sessionID the sessionID
*/
public FullQueryEvent(@NotNull SocketAddress sender) {
super(sender, new FullQueryResponse());
public FullQueryEvent(@NotNull SocketAddress sender, int sessionID) {
super(sender, sessionID, new FullQueryResponse());
}
}

View File

@ -15,6 +15,7 @@ import java.util.Objects;
*/
public abstract class QueryEvent<T extends QueryResponse> extends Event implements CancellableEvent {
private final SocketAddress sender;
private final int sessionID;
private T response;
private boolean cancelled;
@ -23,10 +24,12 @@ public abstract class QueryEvent<T extends QueryResponse> extends Event implemen
* Creates a new query event.
*
* @param sender the sender
* @param sessionID the session ID of the query sender
* @param response the initial response
*/
public QueryEvent(@NotNull SocketAddress sender, @NotNull T response) {
public QueryEvent(@NotNull SocketAddress sender, int sessionID, @NotNull T response) {
this.sender = sender;
this.sessionID = sessionID;
this.response = response;
this.cancelled = false;
}
@ -59,6 +62,15 @@ public abstract class QueryEvent<T extends QueryResponse> extends Event implemen
return this.sender;
}
/**
* Gets the Session ID of the initiator of the query.
*
* @return the session ID
*/
public int getSessionID() {
return this.sessionID;
}
@Override
public boolean isCancelled() {
return this.cancelled;

View File

@ -14,7 +14,6 @@ import net.minestom.server.event.server.ServerListPingEvent;
import net.minestom.server.extras.lan.OpenToLAN;
import net.minestom.server.extras.lan.OpenToLANConfig;
import net.minestom.server.extras.optifine.OptifineSupport;
import net.minestom.server.extras.query.Query;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.instance.block.rule.vanilla.RedstonePlacementRule;
import net.minestom.server.ping.ResponseData;
@ -24,7 +23,6 @@ import net.minestom.server.utils.identity.NamedAndIdentified;
import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.time.UpdateOption;
public class Main {
public static void main(String[] args) {
@ -109,10 +107,7 @@ public class Main {
// useful for testing - we don't need to worry about event calls so just set this to a long time
OpenToLAN.open(new OpenToLANConfig().setDelayBetweenEventCalls(new UpdateOption(1, TimeUnit.DAY)));
Query.start(25566);
minecraftServer.start("0.0.0.0", 25565);
//Runtime.getRuntime().addShutdownHook(new Thread(MinecraftServer::stopCleanly));
}
}