Merge branch 'better-hwid' into 'master'

Improve HWIDs to be more human-readable

See merge request sheepitrenderfarm/client!330
This commit is contained in:
Sheepit Renderfarm
2024-09-22 21:09:29 +00:00

View File

@@ -8,6 +8,8 @@ import java.math.BigInteger;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Optional;
public class HWIdentifier { public class HWIdentifier {
private final BasicHWInfoStrategy strategy; private final BasicHWInfoStrategy strategy;
@@ -18,52 +20,23 @@ public class HWIdentifier {
this.log = log; this.log = log;
} }
public String getMAC() {
return strategy.getMAC().orElse("");
}
public String getHarddriveSerial() {
return strategy.getHarddriveID().orElse("");
}
public String getProcessorName() {
return strategy.getProcessorName().orElse("");
}
public String getHardwareHash() { public String getHardwareHash() {
byte[] hash;
String mac;
String cpuName;
String hdSerial;
try { try {
MessageDigest digest = MessageDigest.getInstance("md5"); String hwid = String.format("%1.16s-%1.16s-%1.16s", hashHWIdPart(strategy.getMAC()), hashHWIdPart(strategy.getHarddriveID()),
if ((hdSerial = getHarddriveSerial()).length() > 0) { hashHWIdPart(strategy.getProcessorName()));
hash = digest.digest(hdSerial.getBytes(StandardCharsets.UTF_8));
log.debug("Hardware::loaded Hardware definitions ver 1.1"); //Fallback: computing a hash out of homepath+jarFileLocation
} if ("null-null-null".equals(hwid)) {
else if ((mac = getMAC()).length() > 0) { log.debug("Hardware::loaded Hardware definitions fallback");
hash = digest.digest(mac.getBytes(StandardCharsets.UTF_8));
log.debug("Hardware::loaded Hardware definitions ver 1.2");
}
else { //Fallback: computing a hash out of homepath+jarFileLocation+cpuName
log.debug("Hardware::loaded Hardware definitions ver 1.3");
cpuName = getProcessorName();
if (cpuName.isEmpty()) {
log.error("Hardware::failed to load Hardware definitions");
throw new UnsupportedOperationException("Unable to load CPU information!");
}
String homeDir = System.getProperty("user.home"); //get home path String homeDir = System.getProperty("user.home"); //get home path
URL clientURL = getClass().getProtectionDomain().getCodeSource().getLocation(); URL clientURL = getClass().getProtectionDomain().getCodeSource().getLocation();
String clientPath = new File(clientURL.toString()).getParent(); //get jar file location String clientPath = new File(clientURL.toString()).getParent(); //get jar file location
hash = digest.digest((homeDir + clientPath + cpuName).getBytes(StandardCharsets.UTF_8)); hwid = hashHWIdPart(Optional.of(homeDir + clientPath));
} }
BigInteger num = new BigInteger(1, hash); return hwid;
return num.toString(16);
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@@ -71,4 +44,13 @@ public class HWIdentifier {
return "unknown"; return "unknown";
} }
} }
private String hashHWIdPart(Optional<String> part) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("md5");
return part //
.map(s -> digest.digest(s.getBytes(StandardCharsets.UTF_8))) //
.map(b -> new BigInteger(1, b).toString(16)) //
.orElse("null");
}
} }