mirror of
https://github.com/Suiranoil/SkinRestorer.git
synced 2026-01-16 04:42:12 +00:00
extract proxy parsing logic to Proxy class
This commit is contained in:
@@ -5,6 +5,7 @@ import net.lionarius.skinrestorer.util.FileUtils;
|
||||
import net.lionarius.skinrestorer.util.JsonUtils;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
|
||||
public final class Config {
|
||||
|
||||
@@ -18,6 +19,7 @@ public final class Config {
|
||||
private FirstJoinSkinProvider firstJoinSkinProvider = FirstJoinSkinProvider.MOJANG;
|
||||
|
||||
private String proxy = "";
|
||||
private transient Proxy parsedProxy = null;
|
||||
|
||||
private long requestTimeout = 10;
|
||||
|
||||
@@ -33,8 +35,8 @@ public final class Config {
|
||||
return this.firstJoinSkinProvider;
|
||||
}
|
||||
|
||||
public String getProxy() {
|
||||
return this.proxy;
|
||||
public Optional<Proxy> getProxy() {
|
||||
return Optional.ofNullable(this.parsedProxy);
|
||||
}
|
||||
|
||||
public long getRequestTimeout() {
|
||||
@@ -71,6 +73,15 @@ public final class Config {
|
||||
if (this.proxy == null)
|
||||
this.proxy = "";
|
||||
|
||||
if (!this.proxy.isEmpty()) {
|
||||
try {
|
||||
this.parsedProxy = Proxy.parse(this.proxy);
|
||||
} catch (Exception e) {
|
||||
SkinRestorer.LOGGER.warn("Could not parse proxy config", e);
|
||||
this.parsedProxy = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.requestTimeout <= 0)
|
||||
this.requestTimeout = 10;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package net.lionarius.skinrestorer.config;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public record Proxy(@NotNull String host, int port) {
|
||||
|
||||
public static Proxy parse(@NotNull String proxy) {
|
||||
var colonIndex = proxy.lastIndexOf(':');
|
||||
if (colonIndex == -1)
|
||||
throw new IllegalArgumentException("no port in hostname");
|
||||
|
||||
var port = Integer.parseInt(proxy.substring(colonIndex + 1));
|
||||
|
||||
if (port < 0 || port > 0xFFFF)
|
||||
throw new IllegalArgumentException("port out of range: " + port);
|
||||
|
||||
var host = proxy.substring(0, colonIndex);
|
||||
|
||||
return new Proxy(host, port);
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,6 @@ 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 {
|
||||
|
||||
@@ -18,24 +17,16 @@ public final class WebUtils {
|
||||
|
||||
public static final String USER_AGENT;
|
||||
|
||||
private static final HttpClient HTTP_CLIENT;
|
||||
|
||||
static {
|
||||
USER_AGENT = String.format("SkinRestorer/%d", System.currentTimeMillis() % 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);
|
||||
}
|
||||
proxy.ifPresent(value -> builder.proxy(ProxySelector.of(InetSocketAddress.createUnresolved(value.host(), value.port()))));
|
||||
|
||||
try {
|
||||
builder.connectTimeout(Duration.of(SkinRestorer.getConfig().getRequestTimeout(), ChronoUnit.SECONDS));
|
||||
} catch (IllegalArgumentException e) {
|
||||
@@ -46,8 +37,6 @@ public final class WebUtils {
|
||||
HTTP_CLIENT = builder.build();
|
||||
}
|
||||
|
||||
private static final HttpClient HTTP_CLIENT;
|
||||
|
||||
public static HttpResponse<String> executeRequest(HttpRequest request) throws IOException {
|
||||
try {
|
||||
var modifiedRequest = HttpRequest.newBuilder(request, (name, value) -> true)
|
||||
|
||||
Reference in New Issue
Block a user