From e6b54fd39c1e20a21d2b473bb4c158e395d11796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Endre=C3=9F?= Date: Tue, 27 Apr 2021 14:03:20 +0200 Subject: [PATCH] Fix weird bukkit thread stuff.. --- pom.xml | 2 +- .../minecraft/vcompat/skin/Mojang.java | 23 ++++++---- .../minecraft/vcompat/skin/Profile.java | 44 +++---------------- .../vcompat/utils/java/net/EasyRequest.java | 12 +++++ 4 files changed, 34 insertions(+), 47 deletions(-) diff --git a/pom.xml b/pom.xml index 8fc878b..76af456 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ 4.0.0 net.sourcewriters.minecraft vcompat - 2.1.5 + 2.1.6 vCompat diff --git a/src/main/java/net/sourcewriters/minecraft/vcompat/skin/Mojang.java b/src/main/java/net/sourcewriters/minecraft/vcompat/skin/Mojang.java index 231d434..c3c40a7 100644 --- a/src/main/java/net/sourcewriters/minecraft/vcompat/skin/Mojang.java +++ b/src/main/java/net/sourcewriters/minecraft/vcompat/skin/Mojang.java @@ -21,6 +21,7 @@ import net.sourcewriters.minecraft.vcompat.reflection.VersionControl; import net.sourcewriters.minecraft.vcompat.reflection.entity.NmsPlayer; import net.sourcewriters.minecraft.vcompat.utils.java.net.EasyRequest; +import net.sourcewriters.minecraft.vcompat.utils.java.net.EasyResponse; import net.sourcewriters.minecraft.vcompat.utils.java.net.content.EasyUrlEncodedContent; import net.sourcewriters.minecraft.vcompat.utils.minecraft.MojangProfileServer; import net.sourcewriters.minecraft.vcompat.utils.minecraft.Skin; @@ -145,9 +146,12 @@ public Skin getSkinFrom(URL url, SkinModel model, int timeout) { logger.log(LogTypeId.ERROR, "Url has to be http or https!"); return null; } - connection.setConnectTimeout(timeout); + HttpURLConnection http = (HttpURLConnection) connection; + http.setConnectTimeout(timeout); + http.setReadTimeout(timeout / 2); try { - connection.connect(); + http.connect(); + http.disconnect(); } catch (SocketTimeoutException exception) { logger.log(LogTypeId.ERROR, "Can't connect to url!"); return null; @@ -155,8 +159,8 @@ public Skin getSkinFrom(URL url, SkinModel model, int timeout) { EasyRequest request = new EasyRequest(RequestType.POST); request.header("Authorization", "Bearer " + profile.getAuthToken()); request.data("url", url.toString()).data("model", model.toString()); - int code = request.run(String.format(URL_SKIN_UPLOAD, profile.getUniqueId())).getCode(); - if (code != ResponseCode.NO_CONTENT) { + EasyResponse response = request.run(String.format(URL_SKIN_UPLOAD, profile.getUniqueId())); + if (response.getCode() != ResponseCode.NO_CONTENT) { return null; } return apply(MojangProfileServer.getSkinShorten(profile.getUniqueId())); @@ -199,9 +203,12 @@ public Skin getSkinFrom(String name, URL url, SkinModel model, int timeout) { logger.log(LogTypeId.ERROR, "Url has to be http or https!"); return null; } - connection.setConnectTimeout(timeout); + HttpURLConnection http = (HttpURLConnection) connection; + http.setConnectTimeout(timeout); + http.setReadTimeout(timeout / 2); try { - connection.connect(); + http.connect(); + http.disconnect(); } catch (SocketTimeoutException exception) { logger.log(LogTypeId.ERROR, "Can't connect to url!"); return null; @@ -209,8 +216,8 @@ public Skin getSkinFrom(String name, URL url, SkinModel model, int timeout) { EasyRequest request = new EasyRequest(RequestType.POST); request.header("Authorization", "Bearer " + profile.getAuthToken()); request.data("url", url.toString()).data("model", model.toString()); - int code = request.run(String.format(URL_SKIN_UPLOAD, profile.getUniqueId()), EasyUrlEncodedContent.URL_ENCODED).getCode(); - if (code != ResponseCode.OK && code != ResponseCode.NO_CONTENT) { + EasyResponse response = request.run(String.format(URL_SKIN_UPLOAD, profile.getUniqueId()), EasyUrlEncodedContent.URL_ENCODED); + if (response.getCode() != ResponseCode.NO_CONTENT) { return null; } return apply(MojangProfileServer.getSkinShorten(name, profile.getUniqueId())); diff --git a/src/main/java/net/sourcewriters/minecraft/vcompat/skin/Profile.java b/src/main/java/net/sourcewriters/minecraft/vcompat/skin/Profile.java index 6c93e53..9cf836b 100644 --- a/src/main/java/net/sourcewriters/minecraft/vcompat/skin/Profile.java +++ b/src/main/java/net/sourcewriters/minecraft/vcompat/skin/Profile.java @@ -65,100 +65,68 @@ public boolean validate() { if (authToken == null) { return false; } - try { - - EasyRequest request = new EasyRequest(RequestType.POST); - - request.data("accessToken", authToken).data("clientToken", provider.getClientIdentifier().toString()); - - int code = request.run(String.format(AUTH_SERVER, "validate")).getCode(); - - return code == 204 || code == 200; + EasyRequest request = new EasyRequest(RequestType.POST).data("accessToken", authToken).data("clientToken", + provider.getClientIdentifier().toString()); + return request.run(String.format(AUTH_SERVER, "validate")).getCode() == 204; } catch (IOException ignore) { return false; } - } public Profile refresh() { if (authToken == null) { return this; } - try { - - EasyRequest request = new EasyRequest(RequestType.POST); - - request.data("accessToken", authToken).data("clientToken", provider.getClientIdentifier().toString()); - + EasyRequest request = new EasyRequest(RequestType.POST).data("accessToken", authToken).data("clientToken", + provider.getClientIdentifier().toString()); JsonValue responseRaw = request.run(String.format(AUTH_SERVER, "refresh")).getDataAsJson(); - if (!responseRaw.hasType(ValueType.OBJECT)) { return this; } JsonObject response = (JsonObject) responseRaw; - authToken = null; - if (!response.has("accessToken")) { return this; } - authToken = response.get("accessToken").getValue().toString(); - return this; - } catch (IOException ignore) { return this; } - } public Profile authenticate() { - try { - EasyRequest request = new EasyRequest(RequestType.POST); - JsonObject object = new JsonObject(); JsonObject agent = new JsonObject(); agent.set("name", "Minecraft"); agent.set("version", 1); object.set("agent", agent); - object.set("username", username); object.set("password", password); object.set("clientToken", provider.getClientIdentifier().toString()); - request.data(object); - JsonValue responseRaw = request.run(String.format(AUTH_SERVER, "authenticate")).getDataAsJson(); - if (!responseRaw.hasType(ValueType.OBJECT)) { return this; } JsonObject response = (JsonObject) responseRaw; - authToken = null; - if (!response.has("selectedProfile")) { return this; } - JsonObject profile = (JsonObject) response.get("selectedProfile"); - uuid = profile.get("id").getValue().toString(); name = profile.get("name").getValue().toString(); - authToken = response.get("accessToken").getValue().toString(); - + Thread.yield(); return this; - } catch (IOException ignore) { return this; } - } } \ No newline at end of file diff --git a/src/main/java/net/sourcewriters/minecraft/vcompat/utils/java/net/EasyRequest.java b/src/main/java/net/sourcewriters/minecraft/vcompat/utils/java/net/EasyRequest.java index e6ec1f5..dd7bf25 100644 --- a/src/main/java/net/sourcewriters/minecraft/vcompat/utils/java/net/EasyRequest.java +++ b/src/main/java/net/sourcewriters/minecraft/vcompat/utils/java/net/EasyRequest.java @@ -34,6 +34,8 @@ public class EasyRequest { private int readTimeout = 20000; private int connectTimeout = 30000; + private String agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"; + public EasyRequest(RequestType requestType) { this.requestType = Objects.requireNonNull(requestType, "RequestType is needed to cast an Http request"); } @@ -69,6 +71,15 @@ public EasyRequest setConnectTimeout(long connectTimeout, TimeUnit unit) { return this; } + public String getAgent() { + return agent; + } + + public EasyRequest setAgent(String agent) { + this.agent = Objects.requireNonNull(agent, "UserAgent cant be null!"); + return this; + } + public String[] header(String name) { return headers.containsKey(name) ? headers.get(name).toArray(EMPTY) : EMPTY; } @@ -170,6 +181,7 @@ public EasyResponse run(URL url, IEasyContent content) throws IOException { for (Entry> header : headers.entrySet()) { connection.setRequestProperty(header.getKey(), String.join("; ", header.getValue())); } + connection.setRequestProperty("User-Agent", agent); if (requestType.hasOutput()) { connection.setRequestProperty("Content-Type", content.type()); connection.setFixedLengthStreamingMode(output ? data.length : 0);