feat: default user agent (#389)

* feat: default user agent

* fix: Fabric version detection

* refactor: remove junit test

* refactor: read mod loader info in bootstrap

* Optimize mod loader version retrieval

---------

Co-authored-by: ZekerZhayard <[email protected]>
This commit is contained in:
Steven QiuandZekerZhayard authored and GitHub committed 2026-08-12 20:19:00 +08:00
1 parent ebea66d261
commit 873705c4fb
14 files changed
+194 -16

No files matched your search

@@ -218,7 +218,7 @@ public class CustomSkinLoader {
logger.info("Operating System: " + System.getProperty("os.name") + " (" + System.getProperty("os.arch") + ") version " + System.getProperty("os.version"));
logger.info("Java Version: " + System.getProperty("java.version") + ", " + System.getProperty("java.vendor"));
logger.info("Java VM Version: " + System.getProperty("java.vm.name") + " (" + System.getProperty("java.vm.info") + "), " + System.getProperty("java.vm.vendor"));
logger.info("Minecraft: " + MinecraftUtil.getMinecraftMainVersion());
logger.info("Minecraft: " + MinecraftUtil.getMinecraftMainVersion() + " (" + MinecraftUtil.getLoaderName() + " " + MinecraftUtil.getLoaderVersion() + ")");
return logger;
}
@@ -97,7 +97,8 @@ public class HttpRequestUtil {
CustomSkinLoader.logger.debug("Try to read cache '" + request.cacheFile + "'.");
return loadFromCache(request, new HttpResponce());
}
CustomSkinLoader.logger.debug("Try to request '" + request.url + (request.userAgent == null ? "'." : "' with user agent '" + request.userAgent + "'."));
String userAgent = UserAgentUtil.expandUserAgent(request.userAgent);
CustomSkinLoader.logger.debug("Try to request '" + request.url + "' with user agent '" + userAgent + "'.");
//Check Cache
if (StringUtils.isNotEmpty(request.payload) || CustomSkinLoader.config.forceDisableCache) {
request.cacheTime = -1;//No Cache
@@ -156,9 +157,7 @@ public class HttpRequestUtil {
c.setRequestProperty("If-None-Match", cacheInfo.etag);
}
c.setRequestProperty("Accept-Encoding", "gzip");
if (request.userAgent != null) {
c.setRequestProperty("User-Agent", request.userAgent);
}
c.setRequestProperty("User-Agent", userAgent);
if (StringUtils.isNotEmpty(request.payload)) {
CustomSkinLoader.logger.info("Payload: " + request.payload);
c.setRequestMethod("POST");
@@ -12,8 +12,6 @@ import com.google.gson.JsonParser;
import com.mojang.authlib.GameProfile;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.resources.SkinManager;
/**
* @author Alexander Xia
@@ -24,13 +22,6 @@ public class MinecraftUtil {
return Minecraft.getInstance().gameDirectory;
}
public static TextureManager getTextureManager() {
return Minecraft.getInstance().getTextureManager();
}
public static SkinManager getSkinManager() {
return Minecraft.getInstance().getSkinManager();
}
private static String minecraftMainVersion = null;
public static String getMinecraftMainVersion() {
@@ -67,6 +58,17 @@ public class MinecraftUtil {
return "unknown";
}
private static final String MOD_LOADER_NAME_PROPERTY = "customskinloader.modLoader.name";
private static final String MOD_LOADER_VERSION_PROPERTY = "customskinloader.modLoader.version";
public static String getLoaderName() {
return System.getProperty(MOD_LOADER_NAME_PROPERTY, "unknown");
}
public static String getLoaderVersion() {
return System.getProperty(MOD_LOADER_VERSION_PROPERTY, "unknown");
}
// (domain|ip)(:port)
public static String getServerAddress() {
ServerData data = Minecraft.getInstance().getCurrentServer();
@@ -0,0 +1,55 @@
package customskinloader.utils;
import customskinloader.CustomSkinLoader;
public final class UserAgentUtil {
public static final String DEFAULT_USER_AGENT_PLACEHOLDER = "{DEFAULT_USER_AGENT}";
public static final String CSL_VERSION_PLACEHOLDER = "{CSL_VERSION}";
public static final String MINECRAFT_VERSION_PLACEHOLDER = "{MINECRAFT_VERSION}";
public static final String MOD_LOADER_NAME_PLACEHOLDER = "{MOD_LOADER_NAME}";
public static final String MOD_LOADER_VERSION_PLACEHOLDER = "{MOD_LOADER_VERSION}";
public static final String JAVA_VERSION_PLACEHOLDER = "{JAVA_VERSION}";
private UserAgentUtil() {
}
public static String expandUserAgent(String userAgent) {
String modVersion = sanitizeComponent(CustomSkinLoader.CustomSkinLoader_FULL_VERSION);
String minecraftVersion = sanitizeComponent(MinecraftUtil.getMinecraftMainVersion());
String loaderName = sanitizeComponent(MinecraftUtil.getLoaderName());
String loaderVersion = sanitizeComponent(MinecraftUtil.getLoaderVersion());
String javaVersion = sanitizeComponent(System.getProperty("java.version", "unknown"));
String defaultUserAgent = "CustomSkinLoader/" + modVersion
+ " (Minecraft/" + minecraftVersion
+ "; " + loaderName + "/" + loaderVersion
+ "; Java/" + javaVersion + ")";
if (userAgent == null) {
return defaultUserAgent;
}
return userAgent
.replace(DEFAULT_USER_AGENT_PLACEHOLDER, defaultUserAgent)
.replace(CSL_VERSION_PLACEHOLDER, modVersion)
.replace(MINECRAFT_VERSION_PLACEHOLDER, minecraftVersion)
.replace(MOD_LOADER_NAME_PLACEHOLDER, loaderName)
.replace(MOD_LOADER_VERSION_PLACEHOLDER, loaderVersion)
.replace(JAVA_VERSION_PLACEHOLDER, javaVersion);
}
private static String sanitizeComponent(String value) {
if (value == null || value.trim().isEmpty()) {
return "unknown";
}
String component = value.trim();
StringBuilder sanitized = new StringBuilder(component.length());
for (int i = 0; i < component.length(); i++) {
char c = component.charAt(i);
sanitized.append(c >= '0' && c <= '9'
|| c >= 'A' && c <= 'Z'
|| c >= 'a' && c <= 'z'
|| "!#$%&'*+-.^_`|~".indexOf(c) >= 0 ? c : '_');
}
return sanitized.toString();
}
}