Merge branch 'better-hwid' into 'master'
Improve HWIDs to be more human-readable See merge request sheepitrenderfarm/client!330
This commit is contained in:
@@ -8,67 +8,49 @@ 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;
|
||||||
private Log log;
|
private Log log;
|
||||||
|
|
||||||
public HWIdentifier(Log log) {
|
public HWIdentifier(Log log) {
|
||||||
strategy = new BaseHWInfoImpl();
|
strategy = new BaseHWInfoImpl();
|
||||||
this.log = log;
|
this.log = log;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMAC() {
|
public String getHardwareHash() {
|
||||||
return strategy.getMAC().orElse("");
|
try {
|
||||||
}
|
String hwid = String.format("%1.16s-%1.16s-%1.16s", hashHWIdPart(strategy.getMAC()), hashHWIdPart(strategy.getHarddriveID()),
|
||||||
|
hashHWIdPart(strategy.getProcessorName()));
|
||||||
|
|
||||||
public String getHarddriveSerial() {
|
//Fallback: computing a hash out of homepath+jarFileLocation
|
||||||
return strategy.getHarddriveID().orElse("");
|
if ("null-null-null".equals(hwid)) {
|
||||||
}
|
log.debug("Hardware::loaded Hardware definitions fallback");
|
||||||
|
|
||||||
public String getProcessorName() {
|
String homeDir = System.getProperty("user.home"); //get home path
|
||||||
return strategy.getProcessorName().orElse("");
|
URL clientURL = getClass().getProtectionDomain().getCodeSource().getLocation();
|
||||||
}
|
String clientPath = new File(clientURL.toString()).getParent(); //get jar file location
|
||||||
|
|
||||||
public String getHardwareHash() {
|
hwid = hashHWIdPart(Optional.of(homeDir + clientPath));
|
||||||
byte[] hash;
|
}
|
||||||
String mac;
|
|
||||||
String cpuName;
|
|
||||||
String hdSerial;
|
|
||||||
|
|
||||||
try {
|
return hwid;
|
||||||
MessageDigest digest = MessageDigest.getInstance("md5");
|
}
|
||||||
if ((hdSerial = getHarddriveSerial()).length() > 0) {
|
|
||||||
hash = digest.digest(hdSerial.getBytes(StandardCharsets.UTF_8));
|
|
||||||
log.debug("Hardware::loaded Hardware definitions ver 1.1");
|
|
||||||
}
|
|
||||||
else if ((mac = getMAC()).length() > 0) {
|
|
||||||
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
|
|
||||||
URL clientURL = getClass().getProtectionDomain().getCodeSource().getLocation();
|
|
||||||
String clientPath = new File(clientURL.toString()).getParent(); //get jar file location
|
|
||||||
|
|
||||||
hash = digest.digest((homeDir + clientPath + cpuName).getBytes(StandardCharsets.UTF_8));
|
|
||||||
}
|
|
||||||
|
|
||||||
BigInteger num = new BigInteger(1, hash);
|
|
||||||
return num.toString(16);
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
log.error("HWIdentifier::getHardwareHash could not retrieve hash: " + e);
|
log.error("HWIdentifier::getHardwareHash could not retrieve hash: " + e);
|
||||||
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user