Some bug fixes

This commit is contained in:
Adrian Bergqvist 2022-10-30 19:18:50 +01:00
parent b723d0ae6d
commit 91e44b9c21
No known key found for this signature in database
GPG Key ID: FAE7D8EDE225E686
5 changed files with 10 additions and 162 deletions

View File

@ -4,7 +4,7 @@ plugins {
}
group 'org.adde0109'
version '1.1.0-alpha'
version '1.1.2-alpha'
repositories {
maven {

View File

@ -22,7 +22,7 @@ import org.slf4j.Logger;
import java.nio.file.Path;
@Plugin(id = "ambassador", name = "Ambassador", version = "1.1.0-alpha", authors = {"adde0109"})
@Plugin(id = "ambassador", name = "Ambassador", version = "1.1.2-alpha", authors = {"adde0109"})
public class Ambassador {
public ProxyServer server;

View File

@ -83,6 +83,7 @@ public class FML2CRPMClientConnectionPhase extends VelocityForgeClientConnection
connection.setSessionHandler(((VelocityForgeHandshakeSessionHandler) connection.getSessionHandler()).getOriginal());
connection.getChannel().pipeline().remove(OUTBOUND_CATCHER_NAME);
backupServer = null;
}
public void handleKick(KickedFromServerEvent event) {

View File

@ -117,159 +117,4 @@ public class ForgeHandshakeUtils {
stream.write(dataAndPacketId);
return stream.toByteArray();
}
public static class HandshakeReceiver {
private int partLength;
public static Logger logger;
private int numberOfRecivedParts;
private int recivedBytes;
private final int numberOfParts;
private final long checksum;
private final int[] separators;
private final byte[] recivedParts;
private HandshakeReceiver(ServerPing serverPing) throws Exception {
if ((serverPing.getModinfo().isEmpty()) || (!Objects.equals(serverPing.getModinfo().get().getType(), "ambassador"))) {
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);
this.separators = Arrays.stream(pair.getVersion().substring(pair.getVersion().indexOf(":") + 1).split(":")).map(Integer::parseInt).mapToInt(x -> x).toArray();
this.checksum = Long.parseUnsignedLong((pair.getVersion().split(":")[0].split("-"))[3],16);
this.numberOfParts = Integer.parseInt((pair.getVersion().split(":")[0].split("-"))[1]);
int totalLength = Integer.parseInt((pair.getVersion().split(":")[0].split("-"))[2]);
this.recivedParts = new byte[totalLength];
}
public static CompletableFuture<CachedServerHandshake> downloadHandshake(RegisteredServer forgeServer) {
CompletableFuture<CachedServerHandshake> future = new CompletableFuture<>();
forgeServer.ping().whenComplete((msg,ex) -> {
if (ex != null) {
future.completeExceptionally(ex);
} else {
try {
HandshakeReceiver handshakeReceiver = new HandshakeReceiver(msg);
handshakeReceiver.handle(msg);
handshakeReceiver.downloadLoop(forgeServer,future);
} catch (Exception e) {
future.completeExceptionally(e);
}
}
});
return future;
}
public static CompletableFuture<CachedServerHandshake> downloadHandshake(RegisteredServer forgeServer, CachedServerHandshake oldHandshake) {
CompletableFuture<CachedServerHandshake> future = new CompletableFuture<>();
forgeServer.ping().whenComplete((msg,ex) -> {
if (ex != null) {
future.completeExceptionally(ex);
} else {
try {
HandshakeReceiver handshakeReceiver = new HandshakeReceiver(msg);
if (handshakeReceiver.getChecksum() == oldHandshake.fingerprint) {
future.complete(oldHandshake);
} else {
handshakeReceiver.handle(msg);
handshakeReceiver.downloadLoop(forgeServer,future);
}
} catch (Exception e) {
future.completeExceptionally(e);
}
}
});
return future;
}
private long getChecksum() {
return checksum;
}
private void downloadLoop(RegisteredServer server, CompletableFuture<CachedServerHandshake> future) {
if (numberOfRecivedParts < numberOfParts) {
server.ping().whenComplete((msg,ex) -> {
if (ex != null) {
future.completeExceptionally(ex);
} else {
handle(msg);
downloadLoop(server, future);
}
});
} else {
List<byte[]> packets = splitPackets(recivedParts,separators);
future.complete(new CachedServerHandshake(checksum,packets.get(0),packets.subList(1,packets.size()-1)));
}
}
private void handle(ServerPing status) {
numberOfRecivedParts++;
ModInfo.Mod pair = status.getModinfo().orElseThrow(IllegalAccessError::new).getMods().get(0);
int recivedPartNr = Integer.parseInt((pair.getVersion().split(":")[0].split("-"))[0]);
placePartInArray(pair.getId().getBytes(StandardCharsets.ISO_8859_1), recivedPartNr - 1);
logger.info("Downloaded part " + numberOfRecivedParts + " out of " + numberOfParts);
}
private void placePartInArray(byte[] temp, int partNr) {
int head = (partNr == numberOfParts-1) ? recivedParts.length-temp.length : partNr*temp.length;
for (byte b : temp) {
recivedParts[head] = b;
head++;
recivedBytes++;
}
}
private byte[] getPacket(byte[] data, int startByteIndex, int lastByteIndex) {
byte[] temp = new byte[lastByteIndex - startByteIndex + 1];
if (lastByteIndex + 1 - startByteIndex >= 0)
System.arraycopy(data, startByteIndex, temp, 0,
lastByteIndex + 1 - startByteIndex);
return temp;
}
private List<byte[]> splitPackets(byte[] data, int[] startPacketMarkers) {
List<byte[]> list = new ArrayList<>();
for (int i = 0; i < startPacketMarkers.length - 1; i++) {
list.add(getPacket(data, startPacketMarkers[i], startPacketMarkers[i + 1] - 1));
}
list.add(getPacket(data, startPacketMarkers[startPacketMarkers.length - 1], recivedBytes - 1));
return list;
}
public static class HandshakeNotAvailableException extends Exception {
HandshakeNotAvailableException(String errorMessage) {
super(errorMessage);
}
}
}
public static class CachedServerHandshake {
private final long fingerprint;
public byte[] modListPacket;
public List<byte[]> otherPackets;
private CachedServerHandshake(long fingerprint,byte[] modListPacket,List<byte[]> otherPackets) {
this.fingerprint = fingerprint;
this.modListPacket = modListPacket;
this.otherPackets = otherPackets;
}
public boolean equals(CachedServerHandshake cachedServerHandshake) {
return this.fingerprint == cachedServerHandshake.fingerprint;
}
}
}

View File

@ -22,11 +22,13 @@ public class VelocityHandshakeSessionHandler implements MinecraftSessionHandler
@Override
public boolean handle(Handshake handshake) {
handshake.handle(original);
if (connection.getType() == ConnectionTypes.VANILLA && connection.getState() == StateRegistry.LOGIN) {
final String marker = handshake.getServerAddress().split("\0")[1];
switch (marker) {
case "FML2" -> connection.setType(ForgeConstants.ForgeFML2);
case "FML3" -> connection.setType(ForgeConstants.ForgeFML3);
if (connection.getType() == ConnectionTypes.VANILLA) {
final String[] markerSplit = handshake.getServerAddress().split("\0");
if (connection.getState() == StateRegistry.LOGIN && markerSplit.length > 1) {
switch (markerSplit[1]) {
case "FML2" -> connection.setType(ForgeConstants.ForgeFML2);
case "FML3" -> connection.setType(ForgeConstants.ForgeFML3);
}
}
}
return true;