From 9ac80842dd3e280bd1468c7261d30c21968b0da1 Mon Sep 17 00:00:00 2001 From: Roman / Linnea Gräf Date: Tue, 7 Feb 2023 06:25:16 +0100 Subject: ApiUtil: Add developer only user agent override (#590) Co-authored-by: NopoTheGamer <40329022+NopoTheGamer@users.noreply.github.com> --- .../moulberry/notenoughupdates/commands/dev/DevTestCommand.java | 5 +++++ .../io/github/moulberry/notenoughupdates/options/NEUConfig.java | 2 ++ .../java/io/github/moulberry/notenoughupdates/util/ApiUtil.java | 9 +++++++-- 3 files changed, 14 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/commands/dev/DevTestCommand.java b/src/main/java/io/github/moulberry/notenoughupdates/commands/dev/DevTestCommand.java index 0d882358..35474ff3 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/commands/dev/DevTestCommand.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/commands/dev/DevTestCommand.java @@ -254,5 +254,10 @@ public class DevTestCommand extends ClientCommandBase { MiscUtils.copyToClipboard(builder.toString()); Utils.addChatMessage("§e[NEU] Copied tablist to clipboard!"); } + if (args.length >= 1 && args[0].equalsIgnoreCase("useragent")) { + String newUserAgent = args.length == 1 ? null : String.join(" ", Arrays.copyOfRange(args, 1, args.length)); + Utils.addChatMessage("§e[NEU] Changed user agent override to: " + newUserAgent); + NotEnoughUpdates.INSTANCE.config.hidden.customUserAgent = newUserAgent; + } } } diff --git a/src/main/java/io/github/moulberry/notenoughupdates/options/NEUConfig.java b/src/main/java/io/github/moulberry/notenoughupdates/options/NEUConfig.java index 371d657e..a289ca51 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/options/NEUConfig.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/options/NEUConfig.java @@ -438,6 +438,8 @@ public class NEUConfig extends Config { @Expose public int compareMode = 0; @Expose + public String customUserAgent = null; + @Expose public int sortMode = 0; @Expose public ArrayList compareAscending = Lists.newArrayList(true, true, true); diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java b/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java index 0c590bab..023be060 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/ApiUtil.java @@ -61,7 +61,12 @@ import java.util.zip.GZIPInputStream; public class ApiUtil { private static final Gson gson = new Gson(); private static final ExecutorService executorService = Executors.newFixedThreadPool(3); - private static final String USER_AGENT = "NotEnoughUpdates/" + NotEnoughUpdates.VERSION; + private static String getUserAgent() { + if (NotEnoughUpdates.INSTANCE.config.hidden.customUserAgent != null) { + return NotEnoughUpdates.INSTANCE.config.hidden.customUserAgent; + } + return "NotEnoughUpdates/" + NotEnoughUpdates.VERSION; + } private static SSLContext ctx; private final Map> updateTasks = new HashMap<>(); @@ -170,7 +175,7 @@ public class ApiUtil { } conn.setConnectTimeout(10000); conn.setReadTimeout(10000); - conn.setRequestProperty("User-Agent", USER_AGENT); + conn.setRequestProperty("User-Agent", getUserAgent()); if (this.postContentType != null) { conn.setRequestProperty("Content-Type", this.postContentType); } -- cgit