Added some more logging

This commit is contained in:
Adrian Bergqvist 2022-07-23 19:53:31 +02:00
parent 364f3d2cb2
commit ae44082336
5 changed files with 42 additions and 14 deletions

View File

@ -4,7 +4,7 @@ plugins {
} }
group 'org.adde0109' group 'org.adde0109'
version '0.2.2' version '0.2.3'
repositories { repositories {
maven { maven {

View File

@ -22,7 +22,7 @@ import org.slf4j.Logger;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
@Plugin(id = "ambassador", name = "Ambassador", version = "0.2.2", authors = {"adde0109"}) @Plugin(id = "ambassador", name = "Ambassador", version = "0.2.3", authors = {"adde0109"})
public class Ambassador { public class Ambassador {
private final ProxyServer server; private final ProxyServer server;
@ -82,6 +82,7 @@ public class Ambassador {
continuation.resume(); continuation.resume();
} else if (forgeConnection.isPresent()) { } else if (forgeConnection.isPresent()) {
if (forgeConnection.get().getTransmittedHandshake().isPresent() if (forgeConnection.get().getTransmittedHandshake().isPresent()
&& forgeConnection.get().getRecivedClientModlist().isPresent()
&& msg.equals(forgeConnection.get().getTransmittedHandshake().get())) { && msg.equals(forgeConnection.get().getTransmittedHandshake().get())) {
//The client's registry is the same as the server's //The client's registry is the same as the server's
continuation.resume(); continuation.resume();

View File

@ -6,6 +6,8 @@ import com.velocitypowered.api.event.player.ServerLoginPluginMessageEvent;
import com.velocitypowered.api.proxy.LoginPhaseConnection; import com.velocitypowered.api.proxy.LoginPhaseConnection;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import com.velocitypowered.api.proxy.server.RegisteredServer; import com.velocitypowered.api.proxy.server.RegisteredServer;
import org.slf4j.Logger;
import java.io.EOFException; import java.io.EOFException;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -13,26 +15,30 @@ import java.util.concurrent.CompletableFuture;
public class ForgeConnection { public class ForgeConnection {
private final Logger logger;
private final LoginPhaseConnection connection; private final LoginPhaseConnection connection;
private byte[] recivedClientModlist; private Optional<byte[]> recivedClientModlist = Optional.empty();
private static byte[] recivedClientACK; private static byte[] recivedClientACK;
private boolean ignoreSyncExepction = false;
private Optional<ForgeHandshakeUtils.CachedServerHandshake> transmittedHandshake = Optional.empty(); private Optional<ForgeHandshakeUtils.CachedServerHandshake> transmittedHandshake = Optional.empty();
private Optional<RegisteredServer> syncedTo = Optional.empty(); private Optional<RegisteredServer> syncedTo = Optional.empty();
public ForgeConnection(LoginPhaseConnection connection) { public ForgeConnection(LoginPhaseConnection connection, Logger logger) {
this.connection = connection; this.connection = connection;
this.logger = logger;
} }
public static CompletableFuture<Boolean> testIfForge(LoginPhaseConnection connection) { public CompletableFuture<Boolean> testIfForge(LoginPhaseConnection connection) {
CompletableFuture<Boolean> future = new CompletableFuture<>(); CompletableFuture<Boolean> future = new CompletableFuture<>();
byte[] testPacket = ForgeHandshakeUtils.generateTestPacket(); byte[] testPacket = ForgeHandshakeUtils.generateTestPacket();
connection.sendLoginPluginMessage(MinecraftChannelIdentifier.create("fml", "loginwrapper"), testPacket, connection.sendLoginPluginMessage(MinecraftChannelIdentifier.create("fml", "loginwrapper"), testPacket,
responseBody -> { responseBody -> {
future.complete(responseBody != null); future.complete(responseBody != null);
ignoreSyncExepction = responseBody == null;
}); });
return future; return future;
} }
@ -44,16 +50,24 @@ public class ForgeConnection {
forgeServerConnection.getHandshake().whenComplete((msg,ex) -> { forgeServerConnection.getHandshake().whenComplete((msg,ex) -> {
if (ex != null) { if (ex != null) {
future.complete(false); future.complete(false);
} logger.warn("Sync Exception: " + ex);
} else {
sendModlist(msg.modListPacket).thenAccept((response) -> { sendModlist(msg.modListPacket).thenAccept((response) -> {
recivedClientModlist = response; if (!ignoreSyncExepction && response == null) {
logger.warn("Sync Exception: Client responded with an empty body.");
}
recivedClientModlist = Optional.ofNullable(response);
}); });
sendOther(msg.otherPackets).thenAccept((response) -> { sendOther(msg.otherPackets).thenAccept((response) -> {
if (!ignoreSyncExepction && response == null) {
logger.warn("Sync Exception: Client responded with an empty body.");
}
ForgeConnection.recivedClientACK = response; ForgeConnection.recivedClientACK = response;
transmittedHandshake = Optional.of(msg); transmittedHandshake = Optional.of(msg);
syncedTo = Optional.of(forgeServerConnection.getServer()); syncedTo = Optional.of(forgeServerConnection.getServer());
}); });
future.complete(true); future.complete(true);
}
}); });
return future; return future;
} }
@ -85,9 +99,20 @@ public class ForgeConnection {
int packetID = ForgeHandshakeUtils.readVarInt(data); int packetID = ForgeHandshakeUtils.readVarInt(data);
if (packetID == 1) { if (packetID == 1) {
event.setResult(ServerLoginPluginMessageEvent.ResponseResult.reply(getRecivedClientModlist())); if (getRecivedClientModlist().isPresent()) {
event.setResult(ServerLoginPluginMessageEvent.ResponseResult.reply(getRecivedClientModlist().get()));
} else {
continuation.resumeWithException(new Exception("Client isn't synced. This should have been caught" +
" during serverPreConnect"));
return;
}
} else { } else {
event.setResult(ServerLoginPluginMessageEvent.ResponseResult.reply(getRecivedClientACK())); if (getRecivedClientACK() != null) {
event.setResult(ServerLoginPluginMessageEvent.ResponseResult.reply(getRecivedClientACK()));
} else {
continuation.resumeWithException(new Exception("No response available."));
return;
}
} }
continuation.resume(); continuation.resume();
} }
@ -100,7 +125,7 @@ public class ForgeConnection {
return transmittedHandshake; return transmittedHandshake;
} }
public byte[] getRecivedClientModlist() { public Optional<byte[]> getRecivedClientModlist() {
return recivedClientModlist; return recivedClientModlist;
} }

View File

@ -50,8 +50,8 @@ public class ForgeHandshakeHandler {
} }
RegisteredServer defaultServer = config.getServer(event.getConnection().getProtocolVersion().getProtocol()); RegisteredServer defaultServer = config.getServer(event.getConnection().getProtocolVersion().getProtocol());
ForgeConnection forgeConnection = new ForgeConnection((LoginPhaseConnection) event.getConnection()); ForgeConnection forgeConnection = new ForgeConnection((LoginPhaseConnection) event.getConnection(), logger);
ForgeConnection.testIfForge((LoginPhaseConnection) event.getConnection()) forgeConnection.testIfForge((LoginPhaseConnection) event.getConnection())
.thenAccept((isForge) -> { .thenAccept((isForge) -> {
registerForgeConnection(forgeConnection); registerForgeConnection(forgeConnection);
}); });
@ -113,6 +113,8 @@ public class ForgeHandshakeHandler {
incomingForgeConnections.get(event.getConnection().getPlayer().getRemoteAddress()) incomingForgeConnections.get(event.getConnection().getPlayer().getRemoteAddress())
.handleServerHandshakePacket(event,continuation); .handleServerHandshakePacket(event,continuation);
} else { } else {
//This will lead to "multiplayer.disconnect.unexpected_query_response"
//and will be handled during KickFromServerEvent.
continuation.resume(); continuation.resume();
} }
} }

View File

@ -77,7 +77,7 @@ public class ForgeHandshakeUtils {
private HandshakeReceiver(ServerPing serverPing) throws Exception { private HandshakeReceiver(ServerPing serverPing) throws Exception {
if ((serverPing.getModinfo().isEmpty()) || (!Objects.equals(serverPing.getModinfo().get().getType(), "ambassador"))) { if ((serverPing.getModinfo().isEmpty()) || (!Objects.equals(serverPing.getModinfo().get().getType(), "ambassador"))) {
throw new HandshakeNotAvailableException("The specified Forge server is not running the Forge-side version of this plugin!"); throw new HandshakeNotAvailableException("The specified Forge server is not running the Ambassador-Forge mod!");
} }
ModInfo.Mod pair = serverPing.getModinfo().orElseThrow(IllegalAccessError::new).getMods().get(0); ModInfo.Mod pair = serverPing.getModinfo().orElseThrow(IllegalAccessError::new).getMods().get(0);