-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement fingerprinting for donation nags
Fingerprints consist of the Mojang UUID and installation path, hashed using a securely generated salt. The fingerprint which is saved to disk cannot be used to recover the data that was used to create it. When the fingerprint has been detected as having changed, certain options will be reset again so that donation nags are shown once more. This solves problems where (most) mod packs accidentally disable the config options to show the user our donation nags.
- Loading branch information
1 parent
a792601
commit 523c39c
Showing
14 changed files
with
462 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...ysquid/mods/sodium/mixin/MixinConfig.java → ...odium/client/data/config/MixinConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/me/jellysquid/mods/sodium/client/data/fingerprint/FingerprintMeasure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package me.jellysquid.mods.sodium.client.data.fingerprint; | ||
|
||
import net.fabricmc.loader.api.FabricLoader; | ||
import net.minecraft.client.MinecraftClient; | ||
import org.apache.commons.codec.binary.Hex; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.SecureRandom; | ||
import java.time.Instant; | ||
import java.util.Objects; | ||
|
||
public record FingerprintMeasure(@NotNull String uuid, @NotNull String path) { | ||
private static final int SALT_LENGTH = 64; | ||
|
||
public static @Nullable FingerprintMeasure create() { | ||
var uuid = MinecraftClient.getInstance().getSession().getUuidOrNull(); | ||
var path = FabricLoader.getInstance().getGameDir(); | ||
|
||
if (uuid == null || path == null) { | ||
return null; | ||
} | ||
|
||
return new FingerprintMeasure(uuid.toString(), path.toAbsolutePath().toString()); | ||
} | ||
|
||
public HashedFingerprint hashed() { | ||
var date = Instant.now(); | ||
var salt = createSalt(); | ||
|
||
var uuidHashHex = sha512(salt, this.uuid()); | ||
var pathHashHex = sha512(salt, this.path()); | ||
|
||
return new HashedFingerprint(HashedFingerprint.CURRENT_VERSION, salt, uuidHashHex, pathHashHex, date.getEpochSecond()); | ||
} | ||
|
||
public boolean looselyMatches(HashedFingerprint hashed) { | ||
var uuidHashHex = sha512(hashed.saltHex(), this.uuid()); | ||
var pathHashHex = sha512(hashed.saltHex(), this.path()); | ||
|
||
return Objects.equals(uuidHashHex, hashed.uuidHashHex()) || Objects.equals(pathHashHex, hashed.pathHashHex()); | ||
} | ||
|
||
private static String sha512(@NotNull String salt, @NotNull String message) { | ||
MessageDigest md; | ||
|
||
try { | ||
md = MessageDigest.getInstance("SHA-512"); | ||
md.update(Hex.decodeHex(salt)); | ||
md.update(message.getBytes(StandardCharsets.UTF_8)); | ||
} catch (Throwable t) { | ||
throw new RuntimeException("Failed to hash value", t); | ||
} | ||
|
||
return Hex.encodeHexString(md.digest()); | ||
} | ||
|
||
private static String createSalt() { | ||
var rng = new SecureRandom(); | ||
|
||
var salt = new byte[SALT_LENGTH]; | ||
rng.nextBytes(salt); | ||
|
||
return Hex.encodeHexString(salt); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/me/jellysquid/mods/sodium/client/data/fingerprint/HashedFingerprint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package me.jellysquid.mods.sodium.client.data.fingerprint; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.annotations.SerializedName; | ||
import me.jellysquid.mods.sodium.client.util.FileUtil; | ||
import net.fabricmc.loader.api.FabricLoader; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
|
||
public record HashedFingerprint( | ||
@SerializedName("v") | ||
int version, | ||
|
||
@NotNull | ||
@SerializedName("s") | ||
String saltHex, | ||
|
||
@NotNull | ||
@SerializedName("u") | ||
String uuidHashHex, | ||
|
||
@NotNull | ||
@SerializedName("p") | ||
String pathHashHex, | ||
|
||
@SerializedName("t") | ||
long timestamp) | ||
{ | ||
public static final int CURRENT_VERSION = 1; | ||
|
||
public static @Nullable HashedFingerprint loadFromDisk() { | ||
Path path = getFilePath(); | ||
|
||
if (!Files.exists(path)) { | ||
return null; | ||
} | ||
|
||
HashedFingerprint data; | ||
|
||
try { | ||
data = new Gson() | ||
.fromJson(Files.readString(path), HashedFingerprint.class); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to load data file", e); | ||
} | ||
|
||
if (data.version() != CURRENT_VERSION) { | ||
return null; | ||
} | ||
|
||
return data; | ||
} | ||
|
||
public static void writeToDisk(@NotNull HashedFingerprint data) { | ||
Objects.requireNonNull(data); | ||
|
||
try { | ||
FileUtil.writeTextRobustly(new Gson() | ||
.toJson(data), getFilePath()); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to save data file", e); | ||
} | ||
} | ||
|
||
private static Path getFilePath() { | ||
return FabricLoader.getInstance() | ||
.getConfigDir() | ||
.resolve("sodium-fingerprint.json"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.