add custom user agent for http requests

This commit is contained in:
PiggyPiglet
2026-02-14 17:19:45 +08:00
parent 81675c1fd6
commit c8f73a5940
2 changed files with 31 additions and 12 deletions

View File

@@ -26,12 +26,9 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import java.io.File; import java.io.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.net.URL; import java.net.*;
import java.net.UnknownHostException;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@@ -64,6 +61,8 @@ public final class CloudExpansionManager {
@NotNull @NotNull
private static final String API_URL = "https://ecloud.placeholderapi.com/api/v3/?platform=bukkit"; private static final String API_URL = "https://ecloud.placeholderapi.com/api/v3/?platform=bukkit";
@NotNull
public static final String USER_AGENT = "PlaceholderAPI-Bukkit";
@NotNull @NotNull
private static final Gson GSON = new Gson(); private static final Gson GSON = new Gson();
@@ -185,8 +184,16 @@ public final class CloudExpansionManager {
// a defence tactic! use ConcurrentHashMap instead of normal HashMap // a defence tactic! use ConcurrentHashMap instead of normal HashMap
Map<String, CloudExpansion> values = new ConcurrentHashMap<>(); Map<String, CloudExpansion> values = new ConcurrentHashMap<>();
try { try {
//noinspection UnstableApiUsage final URI uri = new URI(API_URL);
String json = Resources.toString(new URL(API_URL), StandardCharsets.UTF_8); final URLConnection connection = uri.toURL().openConnection();
connection.setRequestProperty("User-Agent", USER_AGENT);
final String json;
try (final InputStream in = connection.getInputStream()) {
final BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
json = reader.lines().collect(Collectors.joining(System.lineSeparator()));
}
values.putAll(GSON.fromJson(json, TYPE)); values.putAll(GSON.fromJson(json, TYPE));
List<String> toRemove = new ArrayList<>(); List<String> toRemove = new ArrayList<>();
@@ -260,10 +267,14 @@ public final class CloudExpansionManager {
"Expansion-" + toIndexName(expansion) + ".jar"); "Expansion-" + toIndexName(expansion) + ".jar");
final CompletableFuture<File> download = CompletableFuture.supplyAsync(() -> { final CompletableFuture<File> download = CompletableFuture.supplyAsync(() -> {
try (final ReadableByteChannel source = Channels.newChannel(new URL(version.getUrl()) try {
.openStream()); final FileOutputStream target = new FileOutputStream(file)) { final URLConnection connection = new URI(version.getUrl()).toURL().openConnection();
connection.setRequestProperty("User-Agent", USER_AGENT);
try (final ReadableByteChannel source = Channels.newChannel(connection.getInputStream()); final FileOutputStream target = new FileOutputStream(file)) {
target.getChannel().transferFrom(source, 0, Long.MAX_VALUE); target.getChannel().transferFrom(source, 0, Long.MAX_VALUE);
} catch (final IOException ex) { }
} catch (final IOException | URISyntaxException ex) {
throw new CompletionException(ex); throw new CompletionException(ex);
} }
return file; return file;

View File

@@ -4,10 +4,15 @@ import com.google.common.hash.Hashing;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.common.io.Resources; import com.google.common.io.Resources;
import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.expansion.manager.CloudExpansionManager;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
@@ -47,7 +52,10 @@ public final class ExpansionSafetyCheck {
final Set<String> knownMaliciousExpansions; final Set<String> knownMaliciousExpansions;
try { try {
final String hashes = Resources.toString(new URL("https://check.placeholderapi.com"), StandardCharsets.UTF_8); final URLConnection connection = new URI("https://check.placeholderapi.com").toURL().openConnection();
connection.setRequestProperty("User-Agent", CloudExpansionManager.USER_AGENT);
final String hashes = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))
.lines().collect(Collectors.joining(System.lineSeparator()));
knownMaliciousExpansions = Arrays.stream(hashes.split("\n")).collect(Collectors.toSet()); knownMaliciousExpansions = Arrays.stream(hashes.split("\n")).collect(Collectors.toSet());
} catch (Exception e) { } catch (Exception e) {
main.getLogger().log(Level.SEVERE, "Failed to download anti malware hash check list from https://check.placeholderapi.com", e); main.getLogger().log(Level.SEVERE, "Failed to download anti malware hash check list from https://check.placeholderapi.com", e);