Add tags to license usage logs

This commit is contained in:
Braydon 2023-05-31 23:09:48 -04:00
parent 98b67aba4f
commit 01a356a09d
2 changed files with 20 additions and 9 deletions

@ -7,7 +7,6 @@ import lombok.ToString;
import me.braydon.license.exception.APIException; import me.braydon.license.exception.APIException;
import me.braydon.license.exception.LicenseHwidLimitExceededException; import me.braydon.license.exception.LicenseHwidLimitExceededException;
import me.braydon.license.exception.LicenseIpLimitExceededException; import me.braydon.license.exception.LicenseIpLimitExceededException;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
@ -129,13 +128,10 @@ public class License {
/** /**
* Invoked when this license is used. * Invoked when this license is used.
* *
* @param ip the ip used * @param hashedIp the hashed ip used
* @param ipSalt the IP salt to use * @param hwid the hardware id used
* @param hwid the hardware id used
*/ */
public void use(@NonNull String ip, @NonNull String ipSalt, @NonNull String hwid) throws APIException { public void use(@NonNull String hashedIp, @NonNull String hwid) throws APIException {
String hashedIp = BCrypt.hashpw(ip, ipSalt); // Hash the IP
// IP limit has been exceeded // IP limit has been exceeded
if (!ips.contains(hashedIp) && ips.size() >= ipLimit) { if (!ips.contains(hashedIp) && ips.size() >= ipLimit) {
throw new LicenseIpLimitExceededException(); throw new LicenseIpLimitExceededException();

@ -106,14 +106,29 @@ public final class LicenseService {
throw new LicenseNotFoundException(); throw new LicenseNotFoundException();
} }
License license = optionalLicense.get(); // The license found License license = optionalLicense.get(); // The license found
String hashedIp = BCrypt.hashpw(ip, ipsSalt); // Hash the IP
// Log the license being used, if enabled // Log the license being used, if enabled
if (discordService.isLogUses()) { if (discordService.isLogUses()) {
// god i hate sending discord embeds, it's so big and ugly :( // god i hate sending discord embeds, it's so big and ugly :(
boolean newIp = !license.getIps().contains(hashedIp); // If the IP is new
boolean newHwid = !license.getHwids().contains(hwid);
// Constructing tags
StringBuilder tags = new StringBuilder();
if (newIp) { // New IP
tags.append("New IP");
}
if (newHwid) { // New HWID
if (tags.length() > 0) {
tags.append(" & ");
}
tags.append("New HWID");
}
long expirationDate = (license.getCreated().getTime() + license.getDuration()) / 1000L; long expirationDate = (license.getCreated().getTime() + license.getDuration()) / 1000L;
discordService.sendLog(new EmbedBuilder() discordService.sendLog(new EmbedBuilder()
.setColor(Color.BLUE) .setColor(Color.BLUE)
.setTitle("License Used") .setTitle("License Used" + (!tags.isEmpty() ? " (" + tags + ")" : ""))
.addField("License", .addField("License",
"`" + MiscUtils.obfuscateKey(key) + "`", "`" + MiscUtils.obfuscateKey(key) + "`",
true true
@ -169,7 +184,7 @@ public final class LicenseService {
throw new LicenseExpiredException(); throw new LicenseExpiredException();
} }
try { try {
license.use(ip, ipsSalt, hwid); // Use the license license.use(hashedIp, hwid); // Use the license
repository.save(license); // Save the used license repository.save(license); // Save the used license
log.info("License key {} for product {} was used by {} ({})", key, product, ip, hwid); log.info("License key {} for product {} was used by {} ({})", key, product, ip, hwid);
return license; return license;