From abc3a187d410ad0af6ee41a97411111a573de89d Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Fri, 30 Apr 2021 12:43:37 -0400 Subject: [PATCH] Provide the Geyser file's MD5 and SHA-256 hash in Geyser dumps (#2026) --- .../org/geysermc/connector/dump/DumpInfo.java | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/dump/DumpInfo.java b/connector/src/main/java/org/geysermc/connector/dump/DumpInfo.java index e0f0391d5..d8d5d7218 100644 --- a/connector/src/main/java/org/geysermc/connector/dump/DumpInfo.java +++ b/connector/src/main/java/org/geysermc/connector/dump/DumpInfo.java @@ -27,8 +27,12 @@ package org.geysermc.connector.dump; import com.fasterxml.jackson.annotation.JsonIgnore; import com.github.steveice10.mc.protocol.MinecraftConstants; +import com.google.common.hash.Hashing; +import com.google.common.io.ByteSource; +import com.google.common.io.Files; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; +import lombok.AllArgsConstructor; import lombok.Getter; import org.geysermc.connector.GeyserConnector; import org.geysermc.connector.common.serializer.AsteriskSerializer; @@ -39,6 +43,7 @@ import org.geysermc.connector.utils.DockerCheck; import org.geysermc.connector.utils.FileUtils; import org.geysermc.floodgate.util.DeviceOS; +import java.io.File; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; @@ -55,8 +60,9 @@ public class DumpInfo { private final DumpInfo.VersionInfo versionInfo; private Properties gitInfo; private final GeyserConfiguration config; - private Object2IntMap userPlatforms; - private RamInfo ramInfo; + private final HashInfo hashInfo; + private final Object2IntMap userPlatforms; + private final RamInfo ramInfo; private final BootstrapDumpInfo bootstrapInfo; public DumpInfo() { @@ -69,9 +75,29 @@ public class DumpInfo { this.config = GeyserConnector.getInstance().getConfig(); + String md5Hash = "unknown"; + String sha256Hash = "unknown"; + try { + // https://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file + // https://stackoverflow.com/questions/304268/getting-a-files-md5-checksum-in-java + File file = new File(DumpInfo.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + ByteSource byteSource = Files.asByteSource(file); + // Jenkins uses MD5 for its hash + //noinspection UnstableApiUsage + md5Hash = byteSource.hash(Hashing.md5()).toString(); + //noinspection UnstableApiUsage + sha256Hash = byteSource.hash(Hashing.sha256()).toString(); + } catch (Exception e) { + if (GeyserConnector.getInstance().getConfig().isDebugMode()) { + e.printStackTrace(); + } + } + + this.hashInfo = new HashInfo(md5Hash, sha256Hash); + this.ramInfo = new DumpInfo.RamInfo(); - this.userPlatforms = new Object2IntOpenHashMap(); + this.userPlatforms = new Object2IntOpenHashMap<>(); for (GeyserSession session : GeyserConnector.getInstance().getPlayers()) { DeviceOS device = session.getClientData().getDeviceOS(); userPlatforms.put(device, userPlatforms.getOrDefault(device, 0) + 1); @@ -106,6 +132,13 @@ public class DumpInfo { } } + @AllArgsConstructor + @Getter + public static class HashInfo { + private final String md5Hash; + private final String sha256Hash; + } + @Getter public static class NetworkInfo {