Skip to content

Commit

Permalink
Use a dedicated user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Sep 14, 2024
1 parent 4c86600 commit 520d447
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
48 changes: 48 additions & 0 deletions core/src/main/java/net/smyler/terramap/http/UserAgent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package net.smyler.terramap.http;

import net.smyler.smylib.SmyLib;
import net.smyler.smylib.game.GameClient;
import net.smyler.terramap.Terramap;

import static java.lang.System.getProperty;

public final class UserAgent {

public static String buildUserAgent(GameClient client, Terramap terramap) {
StringBuilder userAgent = new StringBuilder();
terramap.version();
SmyLib.getGameClient().modLoader();

userAgent.append(modString(terramap)).append(" ");

userAgent.append(loaderString(client)).append(" ");
userAgent.append(mcString(client)).append(" ");

return userAgent.toString();
}

private static String osString() {
return getProperty("os.name", "unknown");
}

private static String javaString() {
String version = getProperty("java.version");
return "Java" + (version != null ? "/" + version : "");
}

private static String mcString(GameClient client) {
return "Minecraft/" + client.gameVersion();
}

private static String loaderString(GameClient client) {
return client.modLoader();
}

private static String modString(Terramap terramap) {
return String.format("Terramap/%s (%s; %s; +https://github.com/SmylerMC/terramap)",
terramap.version(),
osString(),
javaString()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ public void onInitialize() {
});
}, 10, 10, MINUTES);

this.httpClient.get("https://smyler.net").thenApply(String::new).thenAccept(this.logger::info);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.smyler.terramap.http;

import net.smyler.smylib.Strings;
import net.smyler.terramap.Terramap;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -24,6 +25,7 @@
import static java.util.Arrays.stream;
import static java.util.Objects.requireNonNull;
import static net.smyler.smylib.Objects.optionalBiMapSupplier;
import static net.smyler.smylib.SmyLib.getGameClient;
import static net.smyler.terramap.http.CacheStatistics.CacheType.ERROR;
import static net.smyler.terramap.http.HttpStatusCodes.*;
import static net.smyler.smylib.Strings.isNullOrEmpty;
Expand All @@ -35,7 +37,8 @@ public class TerramapHttpClient implements CachingHttpClient {
private final ForkJoinPool semaphoreAcquireExecutor = new ForkJoinPool(1, HttpWorkerThread::new, this::unhandledException, true);
private final AtomicLong workerCounter = new AtomicLong(0);

private static final String USER_AGENT = "Experimental Terramap version https://github.com/SmylerMC/terramap";
private static final String USER_AGENT = UserAgent.buildUserAgent(getGameClient(), Terramap.instance());

private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter
.ofPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.ENGLISH)
.withZone(ZoneId.of("GMT"));
Expand Down

0 comments on commit 520d447

Please sign in to comment.