mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI
synced 2026-02-26 05:51:12 +01:00
add custom user agent for http requests
This commit is contained in:
@@ -26,12 +26,9 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Type;
|
||||
import java.net.URL;
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.*;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -64,6 +61,8 @@ public final class CloudExpansionManager {
|
||||
|
||||
@NotNull
|
||||
private static final String API_URL = "https://ecloud.placeholderapi.com/api/v3/?platform=bukkit";
|
||||
@NotNull
|
||||
public static final String USER_AGENT = "PlaceholderAPI-Bukkit";
|
||||
|
||||
@NotNull
|
||||
private static final Gson GSON = new Gson();
|
||||
@@ -185,8 +184,16 @@ public final class CloudExpansionManager {
|
||||
// a defence tactic! use ConcurrentHashMap instead of normal HashMap
|
||||
Map<String, CloudExpansion> values = new ConcurrentHashMap<>();
|
||||
try {
|
||||
//noinspection UnstableApiUsage
|
||||
String json = Resources.toString(new URL(API_URL), StandardCharsets.UTF_8);
|
||||
final URI uri = new URI(API_URL);
|
||||
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));
|
||||
|
||||
List<String> toRemove = new ArrayList<>();
|
||||
@@ -260,10 +267,14 @@ public final class CloudExpansionManager {
|
||||
"Expansion-" + toIndexName(expansion) + ".jar");
|
||||
|
||||
final CompletableFuture<File> download = CompletableFuture.supplyAsync(() -> {
|
||||
try (final ReadableByteChannel source = Channels.newChannel(new URL(version.getUrl())
|
||||
.openStream()); final FileOutputStream target = new FileOutputStream(file)) {
|
||||
target.getChannel().transferFrom(source, 0, Long.MAX_VALUE);
|
||||
} catch (final IOException ex) {
|
||||
try {
|
||||
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);
|
||||
}
|
||||
} catch (final IOException | URISyntaxException ex) {
|
||||
throw new CompletionException(ex);
|
||||
}
|
||||
return file;
|
||||
|
||||
@@ -4,10 +4,15 @@ import com.google.common.hash.Hashing;
|
||||
import com.google.common.io.Files;
|
||||
import com.google.common.io.Resources;
|
||||
import me.clip.placeholderapi.PlaceholderAPIPlugin;
|
||||
import me.clip.placeholderapi.expansion.manager.CloudExpansionManager;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
@@ -47,7 +52,10 @@ public final class ExpansionSafetyCheck {
|
||||
final Set<String> knownMaliciousExpansions;
|
||||
|
||||
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());
|
||||
} catch (Exception e) {
|
||||
main.getLogger().log(Level.SEVERE, "Failed to download anti malware hash check list from https://check.placeholderapi.com", e);
|
||||
|
||||
Reference in New Issue
Block a user