mirror of
https://github.com/Suiranoil/SkinRestorer.git
synced 2026-01-16 04:42:12 +00:00
add timeout and proxy config
This commit is contained in:
@@ -6,10 +6,10 @@ import net.lionarius.skinrestorer.skin.SkinIO;
|
||||
import net.lionarius.skinrestorer.skin.SkinStorage;
|
||||
import net.lionarius.skinrestorer.skin.SkinValue;
|
||||
import net.lionarius.skinrestorer.skin.provider.*;
|
||||
import net.lionarius.skinrestorer.translation.Translation;
|
||||
import net.lionarius.skinrestorer.util.FileUtils;
|
||||
import net.lionarius.skinrestorer.util.PlayerUtils;
|
||||
import net.lionarius.skinrestorer.util.Result;
|
||||
import net.lionarius.skinrestorer.translation.Translation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.level.storage.LevelResource;
|
||||
@@ -56,6 +56,7 @@ public final class SkinRestorer {
|
||||
|
||||
public static void onInitialize(Path rootConfigDir) {
|
||||
SkinRestorer.configDir = rootConfigDir.resolve(SkinRestorer.MOD_ID);
|
||||
SkinRestorer.reloadConfig();
|
||||
|
||||
SkinRestorer.providersRegistry.register(EmptySkinProvider.PROVIDER_NAME, SkinProvider.EMPTY, false);
|
||||
SkinRestorer.providersRegistry.register(MojangSkinProvider.PROVIDER_NAME, SkinProvider.MOJANG);
|
||||
@@ -67,7 +68,6 @@ public final class SkinRestorer {
|
||||
Path worldSkinDirectory = server.getWorldPath(LevelResource.ROOT).resolve(SkinRestorer.MOD_ID);
|
||||
FileUtils.tryMigrateOldSkinDirectory(worldSkinDirectory);
|
||||
|
||||
SkinRestorer.reloadConfig();
|
||||
SkinRestorer.skinStorage = new SkinStorage(new SkinIO(worldSkinDirectory));
|
||||
}
|
||||
|
||||
@@ -133,7 +133,6 @@ public final class SkinRestorer {
|
||||
|
||||
return Result.<Collection<ServerPlayer>, String>success(acceptedPlayers);
|
||||
}, server)
|
||||
.orTimeout(10, TimeUnit.SECONDS)
|
||||
.exceptionally(e -> {
|
||||
SkinRestorer.LOGGER.error(e.toString());
|
||||
return Result.error(e.getMessage());
|
||||
|
||||
@@ -15,13 +15,24 @@ public final class Config {
|
||||
|
||||
private boolean fetchSkinOnFirstJoin = true;
|
||||
|
||||
private String proxy = "";
|
||||
|
||||
private long requestTimeout = 10;
|
||||
|
||||
public String getLanguage() {
|
||||
return language;
|
||||
return this.language;
|
||||
}
|
||||
|
||||
public boolean fetchSkinOnFirstJoin() {
|
||||
return fetchSkinOnFirstJoin;
|
||||
return this.fetchSkinOnFirstJoin;
|
||||
}
|
||||
|
||||
public String getProxy() {
|
||||
return this.proxy;
|
||||
}
|
||||
|
||||
public long getRequestTimeout() {
|
||||
return this.requestTimeout;
|
||||
}
|
||||
|
||||
public static Config load(Path path) {
|
||||
@@ -34,9 +45,8 @@ public final class Config {
|
||||
SkinRestorer.LOGGER.warn("Could not load config", e);
|
||||
}
|
||||
|
||||
if (config == null) {
|
||||
if (config == null)
|
||||
config = new Config();
|
||||
}
|
||||
|
||||
FileUtils.writeFile(path.resolve(Config.CONFIG_FILENAME), JsonUtils.toJson(config));
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ public final class MojangSkinProvider implements SkinProvider {
|
||||
return profile.get("id").getAsString();
|
||||
}
|
||||
|
||||
private static JsonObject getMojangProfile(final String uuid) throws IOException {
|
||||
private static JsonObject getMojangProfile(String uuid) throws IOException {
|
||||
var request = HttpRequest.newBuilder()
|
||||
.uri(MojangSkinProvider.SESSION_SERVER_URI
|
||||
.resolve("session/minecraft/profile/")
|
||||
|
||||
@@ -57,6 +57,9 @@ public final class FileUtils {
|
||||
|
||||
public static String readFile(Path file) {
|
||||
try {
|
||||
if (!Files.exists(file))
|
||||
return null;
|
||||
|
||||
return Files.readString(file);
|
||||
} catch (Exception e) {
|
||||
SkinRestorer.LOGGER.error("failed to read file", e);
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
package net.lionarius.skinrestorer.util;
|
||||
|
||||
import net.lionarius.skinrestorer.SkinRestorer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ProxySelector;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Date;
|
||||
|
||||
public final class WebUtils {
|
||||
@@ -15,9 +21,33 @@ public final class WebUtils {
|
||||
static {
|
||||
var date = new Date();
|
||||
USER_AGENT = String.format("SkinRestorer/%d", date.getTime() % 65535);
|
||||
|
||||
var builder = HttpClient.newBuilder();
|
||||
var proxy = SkinRestorer.getConfig().getProxy();
|
||||
try {
|
||||
if (proxy != null) {
|
||||
var colonIndex = proxy.lastIndexOf(':');
|
||||
if (colonIndex != -1) {
|
||||
var host = proxy.substring(0, colonIndex);
|
||||
var port = Integer.parseInt(proxy.substring(colonIndex + 1));
|
||||
|
||||
builder.proxy(ProxySelector.of(InetSocketAddress.createUnresolved(host, port)));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
SkinRestorer.LOGGER.error("failed to parse proxy", e);
|
||||
}
|
||||
try {
|
||||
builder.connectTimeout(Duration.of(SkinRestorer.getConfig().getRequestTimeout(), ChronoUnit.SECONDS));
|
||||
} catch (IllegalArgumentException e) {
|
||||
SkinRestorer.LOGGER.error("failed to set request timeout", e);
|
||||
builder.connectTimeout(Duration.of(10, ChronoUnit.SECONDS));
|
||||
}
|
||||
|
||||
HTTP_CLIENT = builder.build();
|
||||
}
|
||||
|
||||
private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder().build();
|
||||
private static final HttpClient HTTP_CLIENT;
|
||||
|
||||
public static HttpResponse<String> executeRequest(HttpRequest request) throws IOException {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user