This commit is contained in:
Sollace 2022-12-11 14:00:37 +00:00
parent 39c00bbfb1
commit cab2d3d952
7 changed files with 16 additions and 15 deletions

View file

@ -87,7 +87,7 @@ public class LegacySkinServer implements SkinServer {
} }
private MinecraftProfileTexture loadProfileTexture(GameProfile profile, String url) throws IOException { private MinecraftProfileTexture loadProfileTexture(GameProfile profile, String url) throws IOException {
try (MoreHttpResponses resp = MoreHttpResponses.execute(HTTP_CLIENT, new HttpHead(url))) { try (MoreHttpResponses resp = MoreHttpResponses.execute(new HttpHead(url))) {
resp.requireOk(); resp.requireOk();
logger.debug("Found skin for {} at {}", profile.getName(), url); logger.debug("Found skin for {} at {}", profile.getName(), url);

View file

@ -10,12 +10,8 @@ import net.minecraft.util.Session;
import java.io.IOException; import java.io.IOException;
import java.util.Set; import java.util.Set;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public interface SkinServer extends Exposable { public interface SkinServer extends Exposable {
CloseableHttpClient HTTP_CLIENT = HttpClients.createSystem();
/** /**
* Returns the set of features that this skin server supports. * Returns the set of features that this skin server supports.
*/ */

View file

@ -54,7 +54,7 @@ public class ValhallaSkinServer implements SkinServer {
@Override @Override
public TexturePayload loadProfileData(GameProfile profile) throws IOException, AuthenticationException { public TexturePayload loadProfileData(GameProfile profile) throws IOException, AuthenticationException {
try (MoreHttpResponses response = MoreHttpResponses.execute(HTTP_CLIENT, new HttpGet(getTexturesURI(profile)))) { try (MoreHttpResponses response = MoreHttpResponses.execute(new HttpGet(getTexturesURI(profile)))) {
return response.requireOk().json(TexturePayload.class, "Invalid texture payload"); return response.requireOk().json(TexturePayload.class, "Invalid texture payload");
} }
} }
@ -124,7 +124,7 @@ public class ValhallaSkinServer implements SkinServer {
} }
private void upload(HttpUriRequest request) throws IOException { private void upload(HttpUriRequest request) throws IOException {
try (MoreHttpResponses response = MoreHttpResponses.execute(HTTP_CLIENT, request)) { try (MoreHttpResponses response = MoreHttpResponses.execute(request)) {
response.requireOk(); response.requireOk();
} }
} }
@ -151,7 +151,7 @@ public class ValhallaSkinServer implements SkinServer {
} }
private AuthHandshake authHandshake(String name) throws IOException { private AuthHandshake authHandshake(String name) throws IOException {
try (MoreHttpResponses resp = MoreHttpResponses.execute(HTTP_CLIENT, RequestBuilder.post() try (MoreHttpResponses resp = MoreHttpResponses.execute(RequestBuilder.post()
.setUri(getHandshakeURI()) .setUri(getHandshakeURI())
.addParameter("name", name) .addParameter("name", name)
.build())) { .build())) {
@ -160,7 +160,7 @@ public class ValhallaSkinServer implements SkinServer {
} }
private AuthResponse authResponse(String name, long verifyToken) throws IOException { private AuthResponse authResponse(String name, long verifyToken) throws IOException {
try (MoreHttpResponses resp = MoreHttpResponses.execute(HTTP_CLIENT, RequestBuilder.post() try (MoreHttpResponses resp = MoreHttpResponses.execute(RequestBuilder.post()
.setUri(getResponseURI()) .setUri(getResponseURI())
.addParameter("name", name) .addParameter("name", name)
.addParameter("verifyToken", String.valueOf(verifyToken)) .addParameter("verifyToken", String.valueOf(verifyToken))

View file

@ -143,7 +143,7 @@ public class YggdrasilSkinServer implements SkinServer {
} }
private void send(RequestBuilder request) throws IOException { private void send(RequestBuilder request) throws IOException {
try (MoreHttpResponses response = MoreHttpResponses.execute(HTTP_CLIENT, request.build())) { try (MoreHttpResponses response = MoreHttpResponses.execute(request.build())) {
if (!response.ok()) { if (!response.ok()) {
throw new IOException(response.json(ErrorResponse.class, "Server error wasn't in json: {}").toString()); throw new IOException(response.json(ErrorResponse.class, "Server error wasn't in json: {}").toString());
} }

View file

@ -13,6 +13,7 @@ import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair; import org.apache.http.message.BasicNameValuePair;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -26,12 +27,13 @@ import java.util.*;
*/ */
@FunctionalInterface @FunctionalInterface
public interface MoreHttpResponses extends AutoCloseable { public interface MoreHttpResponses extends AutoCloseable {
CloseableHttpClient HTTP_CLIENT = HttpClients.createSystem();
Gson GSON = new GsonBuilder() Gson GSON = new GsonBuilder()
.registerTypeAdapter(UUID.class, new UUIDTypeAdapter()) .registerTypeAdapter(UUID.class, new UUIDTypeAdapter())
.create(); .create();
static MoreHttpResponses execute(CloseableHttpClient client, HttpUriRequest request) throws IOException { static MoreHttpResponses execute(HttpUriRequest request) throws IOException {
CloseableHttpResponse response = client.execute(request); CloseableHttpResponse response = HTTP_CLIENT.execute(request);
return () -> response; return () -> response;
} }

View file

@ -1,7 +1,5 @@
package com.voxelmodpack.hdskins.util; package com.voxelmodpack.hdskins.util;
import com.voxelmodpack.hdskins.server.SkinServer;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder; import org.apache.http.client.methods.RequestBuilder;
@ -73,7 +71,7 @@ public class NetClient {
} }
} }
return MoreHttpResponses.execute(SkinServer.HTTP_CLIENT, request); return MoreHttpResponses.execute(request);
} }
public CompletableFuture<MoreHttpResponses> async(Executor exec) { public CompletableFuture<MoreHttpResponses> async(Executor exec) {

View file

@ -14,6 +14,7 @@ import java.util.regex.Pattern;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.TrustManagerFactory;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
/** /**
@ -88,6 +89,10 @@ public class SslHelper {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
body = reader.readLine(); body = reader.readLine();
} }
try (MoreHttpResponses response = MoreHttpResponses.execute(RequestBuilder.get().setUri("https://helloworld.letsencrypt.org").build())) {
response.requireOk();
}
} catch (Exception e) { } catch (Exception e) {
mod.error("An error occurred whilst adding the Let's Encrypt root certificate. I'm afraid you wont be able to access resources with a Let's Encrypt certificate D:", e); mod.error("An error occurred whilst adding the Let's Encrypt root certificate. I'm afraid you wont be able to access resources with a Let's Encrypt certificate D:", e);
} }