Clean redundancy codes.

This commit is contained in:
huanghongxun 2015-10-26 13:02:06 +08:00
parent b9c03d6ec3
commit b65be375e7
16 changed files with 183 additions and 320 deletions

View File

@ -21,7 +21,6 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.swing.SwingUtilities;
import org.jackhuang.hellominecraft.C; import org.jackhuang.hellominecraft.C;
import org.jackhuang.hellominecraft.HMCLog; import org.jackhuang.hellominecraft.HMCLog;
import org.jackhuang.hellominecraft.launcher.utils.auth.UserProfileProvider; 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("${user_type}", lr.getUserType());
t = t.replace("${assets_index_name}", version.getAssets()); t = t.replace("${assets_index_name}", version.getAssets());
t = t.replace("${user_properties}", lr.getUserProperties()); t = t.replace("${user_properties}", lr.getUserProperties());
t = t.replace("${user_property_map}", lr.getUserPropertyMap());
res.add(t); res.add(t);
} }

View File

@ -112,7 +112,7 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler {
new Thread(() -> { new Thread(() -> {
HashMap<String, String> map = new HashMap<>(); HashMap<String, String> map = new HashMap<>();
map.put("CrashReport", text); 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(); }).start();
} }

View File

@ -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.YggdrasilAuthenticationService;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.YggdrasilUserAuthentication; 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.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.setUserName(info.username);
result.setSuccess(true); result.setSuccess(true);
result.setUserId(UUIDTypeAdapter.fromUUID(ua.getSelectedProfile().id)); 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.setUserPropertyMap(new GsonBuilder().registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()).create().toJson(ua.getUserProperties()));
result.setAccessToken(ua.getAuthenticatedToken()); result.setAccessToken(ua.getAuthenticatedToken());
result.setSession(ua.getAuthenticatedToken()); result.setSession(ua.getAuthenticatedToken());
@ -95,7 +94,7 @@ public final class YggdrasilAuthenticator extends IAuthenticator {
result.setUserName(username); result.setUserName(username);
result.setSuccess(true); result.setSuccess(true);
result.setUserId(selectedProfile == null ? OfflineAuthenticator.getUUIDFromUserName(username) : UUIDTypeAdapter.fromUUID(selectedProfile.id)); 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())); result.setUserPropertyMap(new GsonBuilder().registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()).create().toJson(ua.getUserProperties()));
String authToken = ua.getAuthenticatedToken(); String authToken = ua.getAuthenticatedToken();
if (authToken == null) authToken = "0"; if (authToken == null) authToken = "0";

View File

@ -51,11 +51,6 @@ public class GameProfile {
return result; return result;
} }
@Override
public String toString() {
return "GameProfile{" + "id=" + id + ", name=" + name + ", properties=" + properties + '}';
}
public static class GameProfileSerializer implements JsonSerializer<GameProfile>, JsonDeserializer<GameProfile> { public static class GameProfileSerializer implements JsonSerializer<GameProfile>, JsonDeserializer<GameProfile> {
@Override @Override

View File

@ -5,27 +5,16 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties.PropertyMap; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.properties.PropertyMap;
import java.io.IOException; 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.Proxy;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.jackhuang.hellominecraft.C; import org.jackhuang.hellominecraft.C;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.GameProfile.GameProfileSerializer; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.GameProfile.GameProfileSerializer;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.Response; 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.NetUtils;
import org.jackhuang.hellominecraft.utils.StrUtils; import org.jackhuang.hellominecraft.utils.StrUtils;
import org.jackhuang.hellominecraft.utils.Utils;
import org.jackhuang.hellominecraft.utils.system.IOUtils;
public class YggdrasilAuthenticationService { public class YggdrasilAuthenticationService {
private static final Logger LOGGER = new Logger("HttpAuthenticationService");
private final Proxy proxy; private final Proxy proxy;
private final String clientToken; private final String clientToken;
@ -49,129 +38,9 @@ public class YggdrasilAuthenticationService {
return this.proxy; 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<String, Object> query) {
if (query == null)
return "";
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, Object> 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 extends Response> T makeRequest(URL url, Object input, Class<T> classOfT) throws AuthenticationException { protected <T extends Response> T makeRequest(URL url, Object input, Class<T> classOfT) throws AuthenticationException {
try { 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); Response result = (Response) this.gson.fromJson(jsonResult, classOfT);
if (result == null) if (result == null)

View File

@ -1,22 +1,18 @@
package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil; package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil;
import java.net.URL; import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.jackhuang.hellominecraft.C; 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.ArrayUtils;
import org.jackhuang.hellominecraft.utils.NetUtils; import org.jackhuang.hellominecraft.utils.NetUtils;
import org.jackhuang.hellominecraft.utils.StrUtils; 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.properties.PropertyMap;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.request.AuthenticationRequest; 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.request.RefreshRequest;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.AuthenticationResponse; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.Response;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.RefreshResponse;
import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.User; import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response.User;
public class YggdrasilUserAuthentication { public class YggdrasilUserAuthentication {
@ -89,7 +85,6 @@ public class YggdrasilUserAuthentication {
this.userid = userid; this.userid = userid;
} }
private static final Logger LOGGER = new Logger("YggdrasilUserAuthentication");
private static final String BASE_URL = "https://authserver.mojang.com/"; 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_AUTHENTICATE = NetUtils.constantURL(BASE_URL + "authenticate");
private static final URL ROUTE_REFRESH = NetUtils.constantURL(BASE_URL + "refresh"); private static final URL ROUTE_REFRESH = NetUtils.constantURL(BASE_URL + "refresh");
@ -124,10 +119,10 @@ public class YggdrasilUserAuthentication {
if (StrUtils.isBlank(getPassword())) if (StrUtils.isBlank(getPassword()))
throw new AuthenticationException(C.i18n("login.invalid_password")); 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()); 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())) if (!response.clientToken.equals(getAuthenticationService().getClientToken()))
throw new AuthenticationException(C.i18n("login.changed_client_token")); throw new AuthenticationException(C.i18n("login.changed_client_token"));
@ -163,10 +158,10 @@ public class YggdrasilUserAuthentication {
if (StrUtils.isBlank(getAuthenticatedToken())) if (StrUtils.isBlank(getAuthenticatedToken()))
throw new AuthenticationException(C.i18n("login.invalid_access_token")); 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); 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())) if (!response.clientToken.equals(getAuthenticationService().getClientToken()))
throw new AuthenticationException(C.i18n("login.changed_client_token")); throw new AuthenticationException(C.i18n("login.changed_client_token"));
@ -214,7 +209,7 @@ public class YggdrasilUserAuthentication {
throw new IllegalArgumentException("Invalid profile '" + profile + "'"); throw new IllegalArgumentException("Invalid profile '" + profile + "'");
RefreshRequest request = new RefreshRequest(this, 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())) if (!response.clientToken.equals(getAuthenticationService().getClientToken()))
throw new AuthenticationException(C.i18n("login.changed_client_token")); throw new AuthenticationException(C.i18n("login.changed_client_token"));
@ -227,43 +222,20 @@ public class YggdrasilUserAuthentication {
public void loadFromStorage(Map<String, Object> credentials) { public void loadFromStorage(Map<String, Object> credentials) {
logOut(); logOut();
setUsername((String) credentials.get("username")); setUsername((String) credentials.get(STORAGE_KEY_USER_NAME));
if (credentials.containsKey("userid")) if (credentials.containsKey(STORAGE_KEY_USER_ID))
this.userid = (String) credentials.get("userid"); this.userid = (String) credentials.get(STORAGE_KEY_USER_ID);
else else
this.userid = this.username; this.userid = this.username;
if (credentials.containsKey("userProperties")) if (credentials.containsKey(STORAGE_KEY_USER_PROPERTIES))
try { getModifiableUserProperties().fromList((List<Map<String, String>>) credentials.get(STORAGE_KEY_USER_PROPERTIES));
List<Map> list = (List<Map>) credentials.get("userProperties");
for (Map propertyMap : list) { if ((credentials.containsKey(STORAGE_KEY_PROFILE_NAME)) && (credentials.containsKey(STORAGE_KEY_PROFILE_ID))) {
String name = (String) propertyMap.get("name"); GameProfile profile = new GameProfile(UUIDTypeAdapter.fromString((String) credentials.get(STORAGE_KEY_PROFILE_ID)), (String) credentials.get(STORAGE_KEY_PROFILE_NAME));
String value = (String) propertyMap.get("value"); if (credentials.containsKey(STORAGE_KEY_PROFILE_PROPERTIES))
String signature = (String) propertyMap.get("signature"); profile.properties.fromList((List<Map<String, String>>) credentials.get(STORAGE_KEY_PROFILE_PROPERTIES));
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<Map<String, String>> list = (List<Map<String, String>>) credentials.get("profileProperties");
for (Map<String, String> 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);
}
setSelectedProfile(profile); setSelectedProfile(profile);
} }
@ -274,40 +246,21 @@ public class YggdrasilUserAuthentication {
Map result = new HashMap(); Map result = new HashMap();
if (getUsername() != null) if (getUsername() != null)
result.put("username", getUsername()); result.put(STORAGE_KEY_USER_NAME, getUsername());
if (getUserID() != null) if (getUserID() != null)
result.put("userid", getUserID()); result.put(STORAGE_KEY_USER_ID, getUserID());
else if (getUsername() != null) else if (getUsername() != null)
result.put("username", getUsername()); result.put(STORAGE_KEY_USER_NAME, getUsername());
if (!getUserProperties().isEmpty()) { if (!getUserProperties().isEmpty())
List properties = new ArrayList(); result.put(STORAGE_KEY_USER_PROPERTIES, getUserProperties().list());
for (Property userProperty : getUserProperties().values()) {
Map<String, String> 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);
}
GameProfile sel = getSelectedProfile(); GameProfile sel = getSelectedProfile();
if (sel != null) { if (sel != null) {
result.put("displayName", sel.name); result.put(STORAGE_KEY_PROFILE_NAME, sel.name);
result.put("uuid", sel.id); result.put(STORAGE_KEY_PROFILE_ID, sel.id);
if (!sel.properties.isEmpty())
List properties = new ArrayList(); result.put(STORAGE_KEY_PROFILE_PROPERTIES, sel.properties.list());
for (Property profileProperty : sel.properties.values()) {
Map<String, String> 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);
} }
if (StrUtils.isNotBlank(getAuthenticatedToken())) if (StrUtils.isNotBlank(getAuthenticatedToken()))
@ -326,9 +279,4 @@ public class YggdrasilUserAuthentication {
public String getAuthenticatedToken() { public String getAuthenticatedToken() {
return this.accessToken; 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() + '\'' + '}';
}
} }

View File

@ -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<PropertyMap> {
@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;
}
}

View File

@ -25,10 +25,10 @@ public class Property {
public boolean isSignatureValid(PublicKey publicKey) { public boolean isSignatureValid(PublicKey publicKey) {
try { try {
Signature signature = Signature.getInstance("SHA1withRSA"); Signature sign = Signature.getInstance("SHA1withRSA");
signature.initVerify(publicKey); sign.initVerify(publicKey);
signature.update(this.value.getBytes()); sign.update(this.value.getBytes());
return signature.verify(Base64.decode(this.signature.toCharArray())); return sign.verify(Base64.decode(this.signature.toCharArray()));
} catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) { } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -6,14 +6,44 @@ import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer; import com.google.gson.JsonSerializer;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.jackhuang.hellominecraft.HMCLog;
public class PropertyMap extends HashMap<String, Property> { public class PropertyMap extends HashMap<String, Property> {
public List<Map<String, String>> list() {
List<Map<String, String>> properties = new ArrayList<>();
for (Property profileProperty : values()) {
Map<String, String> 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<Map<String, String>> list) {
try {
for (Map<String, String> 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<PropertyMap>, JsonDeserializer<PropertyMap> { public static class Serializer implements JsonSerializer<PropertyMap>, JsonDeserializer<PropertyMap> {
@Override @Override
@ -62,4 +92,23 @@ public class PropertyMap extends HashMap<String, Property> {
return result; return result;
} }
} }
public static class LegacySerializer
implements JsonSerializer<PropertyMap> {
@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;
}
}
} }

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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; package org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.response;
/** import org.jackhuang.hellominecraft.launcher.utils.auth.yggdrasil.GameProfile;
*
* @author huangyuhui
*/
public class Response { public class Response {
public String accessToken;
public String clientToken;
public GameProfile selectedProfile;
public GameProfile[] availableProfiles;
public User user;
public String error; public String error;
public String errorMessage; public String errorMessage;
public String cause; public String cause;

View File

@ -34,10 +34,18 @@ public class HMCLog {
logger.warn(message); logger.warn(message);
} }
public static void debug(String message) {
logger.debug(message);
}
public static void warn(String msg, Throwable t) { public static void warn(String msg, Throwable t) {
logger.warn(msg, t); logger.warn(msg, t);
} }
public static void debug(String msg, Throwable t) {
logger.debug(msg, t);
}
public static void err(String msg) { public static void err(String msg) {
logger.error(msg); logger.error(msg);
} }

View File

@ -21,13 +21,15 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.Map; import java.util.Map;
import org.jackhuang.hellominecraft.HMCLog; 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); 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 * Sends an HTTP GET request to a url
* *
@ -109,9 +119,7 @@ public final class NetUtils {
return result; return result;
} }
public static String post(String url, Map<String, String> params) { public static String post(URL u, Map<String, String> params) {
URL u;
HttpURLConnection con;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (params != null) { if (params != null) {
for (Map.Entry<String, String> e : params.entrySet()) { for (Map.Entry<String, String> e : params.entrySet()) {
@ -120,36 +128,51 @@ public final class NetUtils {
sb.append(e.getValue()); sb.append(e.getValue());
sb.append("&"); 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 { try {
u = new URL(url); HttpURLConnection con = (HttpURLConnection) u.openConnection(proxy);
con = (HttpURLConnection) u.openConnection();
con.setRequestMethod(METHOD_POST); con.setRequestMethod(METHOD_POST);
con.setDoOutput(true); con.setDoOutput(true);
con.setDoInput(true); con.setDoInput(true);
con.setUseCaches(false); con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setConnectTimeout(15000);
try (OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), DEFAULT_CHARSET)) { con.setReadTimeout(15000);
osw.write(sb.toString()); con.setRequestProperty("Content-Type", contentType + "; charset=utf-8");
osw.flush(); con.setRequestProperty("Content-Length", "" + post.getBytes(DEFAULT_CHARSET).length);
} OutputStream os = null;
StringBuilder buffer = new StringBuilder();
try { try {
BufferedReader br = new BufferedReader(new InputStreamReader(con os = con.getOutputStream();
.getInputStream(), DEFAULT_CHARSET)); IOUtils.write(post, os, DEFAULT_CHARSET);
String temp; } finally {
while ((temp = br.readLine()) != null) { if (os != null) IOUtils.closeQuietly(os);
buffer.append(temp); }
buffer.append("\n");
}
} catch (Exception e) { String result = null;
e.printStackTrace(); 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(); con.disconnect();
return buffer.toString(); return result;
} catch (Exception e) { } catch (Exception e) {
HMCLog.warn("Failed to post.", e); HMCLog.warn("Failed to post.", e);
return null; return null;

View File

@ -17,6 +17,8 @@
package org.jackhuang.hellominecraft.utils; package org.jackhuang.hellominecraft.utils;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent; import javax.swing.text.JTextComponent;
@ -26,10 +28,37 @@ import javax.swing.text.JTextComponent;
*/ */
public class TextComponentOutputStream extends OutputStream { public class TextComponentOutputStream extends OutputStream {
private static final Timer TIMER = new Timer();
private final JTextComponent txt; 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) { public TextComponentOutputStream(JTextComponent paramJTextComponent) {
txt = paramJTextComponent; txt = paramJTextComponent;
//TIMER.schedule(t, 20);
} }
@Override @Override
@ -43,6 +72,7 @@ public class TextComponentOutputStream extends OutputStream {
} }
private void append(final String newString) { private void append(final String newString) {
//t.cache(newString);
try { try {
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
String t = txt.getText() + newString.replace("\t", " "); String t = txt.getText() + newString.replace("\t", " ");
@ -58,4 +88,8 @@ public class TextComponentOutputStream extends OutputStream {
public final void write(int paramInt) { public final void write(int paramInt) {
append(new String(new byte[]{(byte) paramInt})); append(new String(new byte[]{(byte) paramInt}));
} }
public static void dispose() {
TIMER.cancel();
}
} }

View File

@ -16,6 +16,7 @@
*/ */
package org.jackhuang.hellominecraft.views; package org.jackhuang.hellominecraft.views;
import java.util.Timer;
import org.jackhuang.hellominecraft.C; import org.jackhuang.hellominecraft.C;
import org.jackhuang.hellominecraft.utils.functions.NonFunction; import org.jackhuang.hellominecraft.utils.functions.NonFunction;
import org.jackhuang.hellominecraft.utils.DoubleOutputStream; import org.jackhuang.hellominecraft.utils.DoubleOutputStream;