From b65be375e7eeb36b5559a71724f772993cdda46d Mon Sep 17 00:00:00 2001 From: huanghongxun Date: Mon, 26 Oct 2015 13:02:06 +0800 Subject: [PATCH] Clean redundancy codes. --- .../launcher/launch/MinecraftLoader.java | 2 - .../launcher/utils/CrashReporter.java | 2 +- .../utils/auth/YggdrasilAuthenticator.java | 5 +- .../utils/auth/yggdrasil/GameProfile.java | 5 - .../YggdrasilAuthenticationService.java | 135 +----------------- .../YggdrasilUserAuthentication.java | 102 ++++--------- .../LegacyPropertyMapSerializer.java | 37 ----- .../auth/yggdrasil/properties/Property.java | 8 +- .../yggdrasil/properties/PropertyMap.java | 49 +++++++ .../response/AuthenticationResponse.java | 12 -- .../yggdrasil/response/RefreshResponse.java | 12 -- .../auth/yggdrasil/response/Response.java | 18 +-- .../org/jackhuang/hellominecraft/HMCLog.java | 8 ++ .../hellominecraft/utils/NetUtils.java | 73 ++++++---- .../utils/TextComponentOutputStream.java | 34 +++++ .../hellominecraft/views/LogWindow.java | 1 + 16 files changed, 183 insertions(+), 320 deletions(-) delete mode 100644 HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/LegacyPropertyMapSerializer.java delete mode 100644 HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/response/AuthenticationResponse.java delete mode 100644 HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/response/RefreshResponse.java diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/launch/MinecraftLoader.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/launch/MinecraftLoader.java index 97f6848ee..466b665dd 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/launch/MinecraftLoader.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/launch/MinecraftLoader.java @@ -21,7 +21,6 @@ import java.io.File; import java.io.IOException; import java.util.List; import java.util.Map; -import javax.swing.SwingUtilities; import org.jackhuang.hellominecraft.C; import org.jackhuang.hellominecraft.HMCLog; import org.jackhuang.hellominecraft.launcher.utils.auth.UserProfileProvider; @@ -99,7 +98,6 @@ public class MinecraftLoader extends AbstractMinecraftLoader { t = t.replace("${user_type}", lr.getUserType()); t = t.replace("${assets_index_name}", version.getAssets()); t = t.replace("${user_properties}", lr.getUserProperties()); - t = t.replace("${user_property_map}", lr.getUserPropertyMap()); res.add(t); } diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/CrashReporter.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/CrashReporter.java index 57ae50505..0b6cc2aee 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/CrashReporter.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/CrashReporter.java @@ -112,7 +112,7 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler { new Thread(() -> { HashMap map = new HashMap<>(); map.put("CrashReport", text); - System.out.println(NetUtils.post("http://huangyuhui.duapp.com/crash.php", map)); + System.out.println(NetUtils.post(NetUtils.constantURL("http://huangyuhui.duapp.com/crash.php"), map)); }).start(); } diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/YggdrasilAuthenticator.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/YggdrasilAuthenticator.java index c706201c9..74e9b1216 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/YggdrasilAuthenticator.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/YggdrasilAuthenticator.java @@ -29,7 +29,6 @@ import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties.Pro import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.YggdrasilAuthenticationService; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.YggdrasilUserAuthentication; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.UUIDTypeAdapter; -import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties.LegacyPropertyMapSerializer; /** * @@ -53,7 +52,7 @@ public final class YggdrasilAuthenticator extends IAuthenticator { result.setUserName(info.username); result.setSuccess(true); result.setUserId(UUIDTypeAdapter.fromUUID(ua.getSelectedProfile().id)); - result.setUserProperties(new GsonBuilder().registerTypeAdapter(PropertyMap.class, new LegacyPropertyMapSerializer()).create().toJson(ua.getUserProperties())); + result.setUserProperties(new GsonBuilder().registerTypeAdapter(PropertyMap.class, new PropertyMap.LegacySerializer()).create().toJson(ua.getUserProperties())); result.setUserPropertyMap(new GsonBuilder().registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()).create().toJson(ua.getUserProperties())); result.setAccessToken(ua.getAuthenticatedToken()); result.setSession(ua.getAuthenticatedToken()); @@ -95,7 +94,7 @@ public final class YggdrasilAuthenticator extends IAuthenticator { result.setUserName(username); result.setSuccess(true); result.setUserId(selectedProfile == null ? OfflineAuthenticator.getUUIDFromUserName(username) : UUIDTypeAdapter.fromUUID(selectedProfile.id)); - result.setUserProperties(new GsonBuilder().registerTypeAdapter(PropertyMap.class, new LegacyPropertyMapSerializer()).create().toJson(ua.getUserProperties())); + result.setUserProperties(new GsonBuilder().registerTypeAdapter(PropertyMap.class, new PropertyMap.LegacySerializer()).create().toJson(ua.getUserProperties())); result.setUserPropertyMap(new GsonBuilder().registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()).create().toJson(ua.getUserProperties())); String authToken = ua.getAuthenticatedToken(); if (authToken == null) authToken = "0"; diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/GameProfile.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/GameProfile.java index 69f8130ac..15f313a02 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/GameProfile.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/GameProfile.java @@ -51,11 +51,6 @@ public class GameProfile { return result; } - @Override - public String toString() { - return "GameProfile{" + "id=" + id + ", name=" + name + ", properties=" + properties + '}'; - } - public static class GameProfileSerializer implements JsonSerializer, JsonDeserializer { @Override diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilAuthenticationService.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilAuthenticationService.java index 20af6709f..33a7a2d07 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilAuthenticationService.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilAuthenticationService.java @@ -5,27 +5,16 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonParseException; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties.PropertyMap; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.net.HttpURLConnection; import java.net.Proxy; import java.net.URL; -import java.net.URLEncoder; -import java.util.Map; import java.util.UUID; import org.jackhuang.hellominecraft.C; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.GameProfile.GameProfileSerializer; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.Response; -import org.jackhuang.hellominecraft.logging.logger.Logger; import org.jackhuang.hellominecraft.utils.NetUtils; import org.jackhuang.hellominecraft.utils.StrUtils; -import org.jackhuang.hellominecraft.utils.Utils; -import org.jackhuang.hellominecraft.utils.system.IOUtils; public class YggdrasilAuthenticationService { - - private static final Logger LOGGER = new Logger("HttpAuthenticationService"); private final Proxy proxy; private final String clientToken; @@ -48,130 +37,10 @@ public class YggdrasilAuthenticationService { public Proxy getProxy() { return this.proxy; } - - protected HttpURLConnection createUrlConnection(URL url) throws IOException { - LOGGER.debug("Opening connection to " + url); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(this.proxy); - connection.setConnectTimeout(15000); - connection.setReadTimeout(15000); - connection.setUseCaches(false); - return connection; - } - - public String performPostRequest(URL url, String post, String contentType) throws IOException { - Utils.requireNonNull(url); - Utils.requireNonNull(post); - Utils.requireNonNull(contentType); - HttpURLConnection connection = createUrlConnection(url); - byte[] postAsBytes = post.getBytes("UTF-8"); - - connection.setRequestProperty("Content-Type", contentType + "; charset=utf-8"); - connection.setRequestProperty("Content-Length", "" + postAsBytes.length); - connection.setDoOutput(true); - - LOGGER.debug("Writing POST data to " + url + ": " + post); - - OutputStream outputStream = null; - try { - outputStream = connection.getOutputStream(); - IOUtils.write(postAsBytes, outputStream); - } finally { - IOUtils.closeQuietly(outputStream); - } - - LOGGER.debug("Reading data from " + url); - - InputStream inputStream = null; - try { - inputStream = connection.getInputStream(); - String result = NetUtils.getStreamContent(inputStream, "UTF-8"); - LOGGER.debug("Successful read, server response was " + connection.getResponseCode()); - LOGGER.debug("Response: " + result); - String str1 = result; - return str1; - } catch (IOException e) { - IOUtils.closeQuietly(inputStream); - inputStream = connection.getErrorStream(); - - if (inputStream != null) { - LOGGER.debug("Reading error page from " + url); - String result = NetUtils.getStreamContent(inputStream, "UTF-8"); - LOGGER.debug("Successful read, server response was " + connection.getResponseCode()); - LOGGER.debug("Response: " + result); - String str2 = result; - return str2; - } - LOGGER.debug("Request failed", e); - throw e; - } finally { - IOUtils.closeQuietly(inputStream); - } - } - - public String performGetRequest(URL url) - throws IOException { - Utils.requireNonNull(url); - HttpURLConnection connection = createUrlConnection(url); - - LOGGER.debug("Reading data from " + url); - - InputStream inputStream = null; - try { - inputStream = connection.getInputStream(); - String result = NetUtils.getStreamContent(inputStream, "UTF-8"); - LOGGER.debug("Successful read, server response was " + connection.getResponseCode()); - LOGGER.debug("Response: " + result); - String str1 = result; - return str1; - } catch (IOException e) { - IOUtils.closeQuietly(inputStream); - inputStream = connection.getErrorStream(); - - if (inputStream != null) { - LOGGER.debug("Reading error page from " + url); - String result = NetUtils.getStreamContent(inputStream, "UTF-8"); - LOGGER.debug("Successful read, server response was " + connection.getResponseCode()); - LOGGER.debug("Response: " + result); - String str2 = result; - return str2; - } - LOGGER.debug("Request failed", e); - throw e; - } finally { - IOUtils.closeQuietly(inputStream); - } - } - - public static String buildQuery(Map query) { - if (query == null) - return ""; - StringBuilder builder = new StringBuilder(); - - for (Map.Entry entry : query.entrySet()) { - if (builder.length() > 0) - builder.append('&'); - try { - builder.append(URLEncoder.encode(entry.getKey(), "UTF-8")); - } catch (UnsupportedEncodingException e) { - LOGGER.error("Unexpected exception building query", e); - } - - if (entry.getValue() != null) { - builder.append('='); - try { - builder.append(URLEncoder.encode(entry.getValue().toString(), "UTF-8")); - } catch (UnsupportedEncodingException e) { - LOGGER.error("Unexpected exception building query", e); - } - } - } - - return builder.toString(); - } - + protected T makeRequest(URL url, Object input, Class classOfT) throws AuthenticationException { try { - String jsonResult = input == null ? performGetRequest(url) : performPostRequest(url, this.gson.toJson(input), "application/json"); + String jsonResult = input == null ? NetUtils.doGet(url) : NetUtils.post(url, this.gson.toJson(input), "application/json", proxy); Response result = (Response) this.gson.fromJson(jsonResult, classOfT); if (result == null) diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilUserAuthentication.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilUserAuthentication.java index 32c01d383..c42a2ce8e 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilUserAuthentication.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/YggdrasilUserAuthentication.java @@ -1,22 +1,18 @@ package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil; import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.jackhuang.hellominecraft.C; -import org.jackhuang.hellominecraft.logging.logger.Logger; +import org.jackhuang.hellominecraft.HMCLog; import org.jackhuang.hellominecraft.utils.ArrayUtils; import org.jackhuang.hellominecraft.utils.NetUtils; import org.jackhuang.hellominecraft.utils.StrUtils; -import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties.Property; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties.PropertyMap; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.request.AuthenticationRequest; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.request.RefreshRequest; -import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.AuthenticationResponse; -import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.RefreshResponse; +import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.Response; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.User; public class YggdrasilUserAuthentication { @@ -89,7 +85,6 @@ public class YggdrasilUserAuthentication { this.userid = userid; } - private static final Logger LOGGER = new Logger("YggdrasilUserAuthentication"); private static final String BASE_URL = "https://authserver.mojang.com/"; private static final URL ROUTE_AUTHENTICATE = NetUtils.constantURL(BASE_URL + "authenticate"); private static final URL ROUTE_REFRESH = NetUtils.constantURL(BASE_URL + "refresh"); @@ -124,10 +119,10 @@ public class YggdrasilUserAuthentication { if (StrUtils.isBlank(getPassword())) throw new AuthenticationException(C.i18n("login.invalid_password")); - LOGGER.info("Logging in with username & password"); + HMCLog.log("Logging in with username & password"); AuthenticationRequest request = new AuthenticationRequest(this, getUsername(), getPassword()); - AuthenticationResponse response = (AuthenticationResponse) getAuthenticationService().makeRequest(ROUTE_AUTHENTICATE, request, AuthenticationResponse.class); + Response response = (Response) getAuthenticationService().makeRequest(ROUTE_AUTHENTICATE, request, Response.class); if (!response.clientToken.equals(getAuthenticationService().getClientToken())) throw new AuthenticationException(C.i18n("login.changed_client_token")); @@ -163,10 +158,10 @@ public class YggdrasilUserAuthentication { if (StrUtils.isBlank(getAuthenticatedToken())) throw new AuthenticationException(C.i18n("login.invalid_access_token")); - LOGGER.info("Logging in with access token"); + HMCLog.log("Logging in with access token"); RefreshRequest request = new RefreshRequest(this); - RefreshResponse response = (RefreshResponse) getAuthenticationService().makeRequest(ROUTE_REFRESH, request, RefreshResponse.class); + Response response = (Response) getAuthenticationService().makeRequest(ROUTE_REFRESH, request, Response.class); if (!response.clientToken.equals(getAuthenticationService().getClientToken())) throw new AuthenticationException(C.i18n("login.changed_client_token")); @@ -214,7 +209,7 @@ public class YggdrasilUserAuthentication { throw new IllegalArgumentException("Invalid profile '" + profile + "'"); RefreshRequest request = new RefreshRequest(this, profile); - RefreshResponse response = (RefreshResponse) getAuthenticationService().makeRequest(ROUTE_REFRESH, request, RefreshResponse.class); + Response response = (Response) getAuthenticationService().makeRequest(ROUTE_REFRESH, request, Response.class); if (!response.clientToken.equals(getAuthenticationService().getClientToken())) throw new AuthenticationException(C.i18n("login.changed_client_token")); @@ -227,43 +222,20 @@ public class YggdrasilUserAuthentication { public void loadFromStorage(Map credentials) { logOut(); - setUsername((String) credentials.get("username")); + setUsername((String) credentials.get(STORAGE_KEY_USER_NAME)); - if (credentials.containsKey("userid")) - this.userid = (String) credentials.get("userid"); + if (credentials.containsKey(STORAGE_KEY_USER_ID)) + this.userid = (String) credentials.get(STORAGE_KEY_USER_ID); else this.userid = this.username; - if (credentials.containsKey("userProperties")) - try { - List list = (List) credentials.get("userProperties"); + if (credentials.containsKey(STORAGE_KEY_USER_PROPERTIES)) + getModifiableUserProperties().fromList((List>) credentials.get(STORAGE_KEY_USER_PROPERTIES)); - for (Map propertyMap : list) { - String name = (String) propertyMap.get("name"); - String value = (String) propertyMap.get("value"); - String signature = (String) propertyMap.get("signature"); - - getModifiableUserProperties().put(name, new Property(name, value, signature)); - } - } catch (Throwable t) { - LOGGER.warn("Couldn't deserialize user properties", t); - } - - if ((credentials.containsKey("displayName")) && (credentials.containsKey("uuid"))) { - GameProfile profile = new GameProfile(UUIDTypeAdapter.fromString((String) credentials.get("uuid")), (String) credentials.get("displayName")); - if (credentials.containsKey("profileProperties")) - try { - List> list = (List>) credentials.get("profileProperties"); - for (Map propertyMap : list) { - String name = propertyMap.get("name"); - String value = propertyMap.get("value"); - String signature = propertyMap.get("signature"); - - profile.properties.put(name, new Property(name, value, signature)); - } - } catch (Throwable t) { - LOGGER.warn("Couldn't deserialize profile properties", t); - } + if ((credentials.containsKey(STORAGE_KEY_PROFILE_NAME)) && (credentials.containsKey(STORAGE_KEY_PROFILE_ID))) { + GameProfile profile = new GameProfile(UUIDTypeAdapter.fromString((String) credentials.get(STORAGE_KEY_PROFILE_ID)), (String) credentials.get(STORAGE_KEY_PROFILE_NAME)); + if (credentials.containsKey(STORAGE_KEY_PROFILE_PROPERTIES)) + profile.properties.fromList((List>) credentials.get(STORAGE_KEY_PROFILE_PROPERTIES)); setSelectedProfile(profile); } @@ -274,40 +246,21 @@ public class YggdrasilUserAuthentication { Map result = new HashMap(); if (getUsername() != null) - result.put("username", getUsername()); + result.put(STORAGE_KEY_USER_NAME, getUsername()); if (getUserID() != null) - result.put("userid", getUserID()); + result.put(STORAGE_KEY_USER_ID, getUserID()); else if (getUsername() != null) - result.put("username", getUsername()); + result.put(STORAGE_KEY_USER_NAME, getUsername()); - if (!getUserProperties().isEmpty()) { - List properties = new ArrayList(); - for (Property userProperty : getUserProperties().values()) { - Map property = new HashMap<>(); - property.put("name", userProperty.name); - property.put("value", userProperty.value); - property.put("signature", userProperty.signature); - properties.add(property); - } - result.put("userProperties", properties); - } + if (!getUserProperties().isEmpty()) + result.put(STORAGE_KEY_USER_PROPERTIES, getUserProperties().list()); GameProfile sel = getSelectedProfile(); if (sel != null) { - result.put("displayName", sel.name); - result.put("uuid", sel.id); - - List properties = new ArrayList(); - for (Property profileProperty : sel.properties.values()) { - Map property = new HashMap<>(); - property.put("name", profileProperty.name); - property.put("value", profileProperty.value); - property.put("signature", profileProperty.signature); - properties.add(property); - } - - if (!properties.isEmpty()) - result.put("profileProperties", properties); + result.put(STORAGE_KEY_PROFILE_NAME, sel.name); + result.put(STORAGE_KEY_PROFILE_ID, sel.id); + if (!sel.properties.isEmpty()) + result.put(STORAGE_KEY_PROFILE_PROPERTIES, sel.properties.list()); } if (StrUtils.isNotBlank(getAuthenticatedToken())) @@ -326,9 +279,4 @@ public class YggdrasilUserAuthentication { public String getAuthenticatedToken() { return this.accessToken; } - - @Override - public String toString() { - return "YggdrasilAuthenticationService{profiles=" + Arrays.toString(this.profiles) + ", selectedProfile=" + getSelectedProfile() + ", username='" + getUsername() + '\'' + ", isLoggedIn=" + isLoggedIn() + ", canPlayOnline=" + canPlayOnline() + ", accessToken='" + this.accessToken + '\'' + ", clientToken='" + getAuthenticationService().getClientToken() + '\'' + '}'; - } } diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/LegacyPropertyMapSerializer.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/LegacyPropertyMapSerializer.java deleted file mode 100644 index 479b81689..000000000 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/LegacyPropertyMapSerializer.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties; - -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; -import java.lang.reflect.Type; - -/** - * - * @author huangyuhui - */ -public class LegacyPropertyMapSerializer - implements JsonSerializer { - - @Override - public JsonElement serialize(PropertyMap src, Type typeOfSrc, JsonSerializationContext context) { - JsonObject result = new JsonObject(); - - for (String key : src.keySet()) { - JsonArray values = new JsonArray(); - - values.add(new JsonPrimitive(src.get(key).value)); - - result.add(key, values); - } - - return result; - } -} diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/Property.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/Property.java index 6b2bf35ff..7b31d26d2 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/Property.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/Property.java @@ -25,10 +25,10 @@ public class Property { public boolean isSignatureValid(PublicKey publicKey) { try { - Signature signature = Signature.getInstance("SHA1withRSA"); - signature.initVerify(publicKey); - signature.update(this.value.getBytes()); - return signature.verify(Base64.decode(this.signature.toCharArray())); + Signature sign = Signature.getInstance("SHA1withRSA"); + sign.initVerify(publicKey); + sign.update(this.value.getBytes()); + return sign.verify(Base64.decode(this.signature.toCharArray())); } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) { e.printStackTrace(); } diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/PropertyMap.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/PropertyMap.java index 7ec1457b8..e4d449f67 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/PropertyMap.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/properties/PropertyMap.java @@ -6,14 +6,44 @@ import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; +import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import java.lang.reflect.Type; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; +import org.jackhuang.hellominecraft.HMCLog; public class PropertyMap extends HashMap { + public List> list() { + List> properties = new ArrayList<>(); + for (Property profileProperty : values()) { + Map property = new HashMap<>(); + property.put("name", profileProperty.name); + property.put("value", profileProperty.value); + property.put("signature", profileProperty.signature); + properties.add(property); + } + return properties; + } + + public void fromList(List> list) { + try { + for (Map propertyMap : list) { + String name = propertyMap.get("name"); + String value = propertyMap.get("value"); + String signature = propertyMap.get("signature"); + + put(name, new Property(name, value, signature)); + } + } catch (Throwable t) { + HMCLog.warn("Failed to deserialize properties", t); + } + } + public static class Serializer implements JsonSerializer, JsonDeserializer { @Override @@ -62,4 +92,23 @@ public class PropertyMap extends HashMap { return result; } } + + public static class LegacySerializer + implements JsonSerializer { + + @Override + public JsonElement serialize(PropertyMap src, Type typeOfSrc, JsonSerializationContext context) { + JsonObject result = new JsonObject(); + + for (String key : src.keySet()) { + JsonArray values = new JsonArray(); + + values.add(new JsonPrimitive(src.get(key).value)); + + result.add(key, values); + } + + return result; + } + } } diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/response/AuthenticationResponse.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/response/AuthenticationResponse.java deleted file mode 100644 index 594ce2968..000000000 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/response/AuthenticationResponse.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response; - -import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.GameProfile; - -public class AuthenticationResponse extends Response { - - public String accessToken; - public String clientToken; - public GameProfile selectedProfile; - public GameProfile[] availableProfiles; - public User user; -} diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/response/RefreshResponse.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/response/RefreshResponse.java deleted file mode 100644 index 020ad6159..000000000 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/response/RefreshResponse.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response; - -import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.GameProfile; - -public class RefreshResponse extends Response { - - public String accessToken; - public String clientToken; - public GameProfile selectedProfile; - public GameProfile[] availableProfiles; - public User user; -} diff --git a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/response/Response.java b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/response/Response.java index e14bdd1bc..2e10796e5 100644 --- a/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/response/Response.java +++ b/HMCL/src/main/java/org/jackhuang/hellominecraft/launcher/utils/auth/yggdrasil/response/Response.java @@ -1,15 +1,15 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response; -/** - * - * @author huangyuhui - */ +import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.GameProfile; + public class Response { + + public String accessToken; + public String clientToken; + public GameProfile selectedProfile; + public GameProfile[] availableProfiles; + public User user; + public String error; public String errorMessage; public String cause; diff --git a/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/HMCLog.java b/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/HMCLog.java index 09488cb12..6c6b0100e 100644 --- a/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/HMCLog.java +++ b/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/HMCLog.java @@ -34,10 +34,18 @@ public class HMCLog { logger.warn(message); } + public static void debug(String message) { + logger.debug(message); + } + public static void warn(String msg, Throwable t) { logger.warn(msg, t); } + public static void debug(String msg, Throwable t) { + logger.debug(msg, t); + } + public static void err(String msg) { logger.error(msg); } diff --git a/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/NetUtils.java b/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/NetUtils.java index 7756d8154..dbf6d5d0b 100644 --- a/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/NetUtils.java +++ b/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/NetUtils.java @@ -21,13 +21,15 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.OutputStreamWriter; +import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.net.Proxy; import java.net.URL; import java.net.URLConnection; import java.util.Map; import org.jackhuang.hellominecraft.HMCLog; +import org.jackhuang.hellominecraft.utils.system.IOUtils; /** * @@ -69,6 +71,14 @@ public final class NetUtils { return doGet(url, DEFAULT_CHARSET); } + public static String doGet(URL url) throws IOException { + return doGet(url, Proxy.NO_PROXY); + } + + public static String doGet(URL url, Proxy proxy) throws IOException { + return getStreamContent(url.openConnection(proxy).getInputStream()); + } + /** * Sends an HTTP GET request to a url * @@ -108,10 +118,8 @@ public final class NetUtils { } return result; } - - public static String post(String url, Map params) { - URL u; - HttpURLConnection con; + + public static String post(URL u, Map params) { StringBuilder sb = new StringBuilder(); if (params != null) { for (Map.Entry e : params.entrySet()) { @@ -120,36 +128,51 @@ public final class NetUtils { sb.append(e.getValue()); sb.append("&"); } - sb = new StringBuilder(sb.substring(0, sb.length() - 1)); + sb.deleteCharAt(sb.length() - 1); } + return post(u, sb.toString()); + } + + public static String post(URL u, String post) { + return post(u, post, "application/x-www-form-urlencoded"); + } + + public static String post(URL u, String post, String contentType) { + return post(u, post, contentType, Proxy.NO_PROXY); + } + + public static String post(URL u, String post, String contentType, Proxy proxy) { try { - u = new URL(url); - con = (HttpURLConnection) u.openConnection(); + HttpURLConnection con = (HttpURLConnection) u.openConnection(proxy); con.setRequestMethod(METHOD_POST); con.setDoOutput(true); con.setDoInput(true); con.setUseCaches(false); - con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); - try (OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), DEFAULT_CHARSET)) { - osw.write(sb.toString()); - osw.flush(); - } - StringBuilder buffer = new StringBuilder(); + con.setConnectTimeout(15000); + con.setReadTimeout(15000); + con.setRequestProperty("Content-Type", contentType + "; charset=utf-8"); + con.setRequestProperty("Content-Length", "" + post.getBytes(DEFAULT_CHARSET).length); + OutputStream os = null; try { - BufferedReader br = new BufferedReader(new InputStreamReader(con - .getInputStream(), DEFAULT_CHARSET)); - String temp; - while ((temp = br.readLine()) != null) { - buffer.append(temp); - buffer.append("\n"); - } - - } catch (Exception e) { - e.printStackTrace(); + os = con.getOutputStream(); + IOUtils.write(post, os, DEFAULT_CHARSET); + } finally { + if (os != null) IOUtils.closeQuietly(os); + } + + String result = null; + InputStream is = null; + try { + is = con.getInputStream(); + result = getStreamContent(is); + } catch(IOException ex) { + if (is != null) IOUtils.closeQuietly(is); + is = con.getErrorStream(); + result = getStreamContent(is); } con.disconnect(); - return buffer.toString(); + return result; } catch (Exception e) { HMCLog.warn("Failed to post.", e); return null; diff --git a/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/TextComponentOutputStream.java b/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/TextComponentOutputStream.java index f9e9afa75..26c1f1542 100644 --- a/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/TextComponentOutputStream.java +++ b/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/utils/TextComponentOutputStream.java @@ -17,6 +17,8 @@ package org.jackhuang.hellominecraft.utils; import java.io.OutputStream; +import java.util.Timer; +import java.util.TimerTask; import javax.swing.SwingUtilities; import javax.swing.text.JTextComponent; @@ -25,11 +27,38 @@ import javax.swing.text.JTextComponent; * @author huangyuhui */ public class TextComponentOutputStream extends OutputStream { + + private static final Timer TIMER = new Timer(); private final JTextComponent txt; + private final CacheTask t = new CacheTask(); + private class CacheTask extends TimerTask { + private final Object lock = new Object(); + private StringBuilder cachedString = new StringBuilder(); + + @Override + public void run() { + synchronized(lock) { + SwingUtilities.invokeLater(() -> { + String t = txt.getText() + cachedString.toString().replace("\t", " "); + txt.setText(t); + txt.setCaretPosition(t.length()); + cachedString = new StringBuilder(); + }); + } + } + + void cache(String s) { + synchronized(lock) { + cachedString.append(s); + } + } + } public TextComponentOutputStream(JTextComponent paramJTextComponent) { txt = paramJTextComponent; + + //TIMER.schedule(t, 20); } @Override @@ -43,6 +72,7 @@ public class TextComponentOutputStream extends OutputStream { } private void append(final String newString) { + //t.cache(newString); try { SwingUtilities.invokeLater(() -> { String t = txt.getText() + newString.replace("\t", " "); @@ -58,4 +88,8 @@ public class TextComponentOutputStream extends OutputStream { public final void write(int paramInt) { append(new String(new byte[]{(byte) paramInt})); } + + public static void dispose() { + TIMER.cancel(); + } } diff --git a/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/views/LogWindow.java b/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/views/LogWindow.java index ccf1eb747..610bc5a17 100644 --- a/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/views/LogWindow.java +++ b/HMCLAPI/src/main/java/org/jackhuang/hellominecraft/views/LogWindow.java @@ -16,6 +16,7 @@ */ package org.jackhuang.hellominecraft.views; +import java.util.Timer; import org.jackhuang.hellominecraft.C; import org.jackhuang.hellominecraft.utils.functions.NonFunction; import org.jackhuang.hellominecraft.utils.DoubleOutputStream;