diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java index 1f0aae81..248a802c 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/AuthHandler.java @@ -44,7 +44,6 @@ public static void registerHandlers() { registerHandler("binaryFile", BinaryFileAuthHandler::new); registerHandler("textFile", TextFileAuthHandler::new); registerHandler("mysql", MySQLAuthHandler::new); - registerHandler("json", JsonAuthHandler::new); registredHandl = true; } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/JsonAuthHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/JsonAuthHandler.java deleted file mode 100644 index 4e85b2c1..00000000 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/JsonAuthHandler.java +++ /dev/null @@ -1,123 +0,0 @@ -package ru.gravit.launchserver.auth.handler; - -import java.io.IOException; -import java.net.URL; -import java.util.UUID; - -import com.eclipsesource.json.Json; -import com.eclipsesource.json.JsonObject; -import com.eclipsesource.json.JsonValue; - -import ru.gravit.launchserver.auth.provider.AuthProviderResult; -import ru.gravit.utils.HTTPRequest; -import ru.gravit.utils.helper.IOHelper; -import ru.gravit.utils.helper.VerifyHelper; -import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry; -import ru.gravit.launcher.serialize.config.entry.StringConfigEntry; - -@SuppressWarnings("unused") -public class JsonAuthHandler extends AuthHandler { - - private final URL url; - private final URL urlCheckServer; - private final URL urlJoinServer; - private final URL urlUsernameToUUID; - private final URL urlUUIDToUsername; - private final String userKeyName; - private final String serverIDKeyName; - private final String accessTokenKeyName; - private final String uuidKeyName; - private final String responseErrorKeyName; - private final String responseOKKeyName; - - protected JsonAuthHandler(BlockConfigEntry block) { - super(block); - String configUrl = block.getEntryValue("url", StringConfigEntry.class); - String configUrlCheckServer = block.getEntryValue("urlCheckServer", StringConfigEntry.class); - String configUrlJoinServer = block.getEntryValue("urlJoinServer", StringConfigEntry.class); - String configUrlUsernameUUID = block.getEntryValue("urlUsernameToUUID", StringConfigEntry.class); - String configUrlUUIDUsername = block.getEntryValue("urlUUIDToUsername", StringConfigEntry.class); - userKeyName = VerifyHelper.verify(block.getEntryValue("userKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "Username key name can't be empty"); - serverIDKeyName = VerifyHelper.verify(block.getEntryValue("serverIDKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "ServerID key name can't be empty"); - uuidKeyName = VerifyHelper.verify(block.getEntryValue("UUIDKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "UUID key name can't be empty"); - accessTokenKeyName = VerifyHelper.verify(block.getEntryValue("accessTokenKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "AccessToken key name can't be empty"); - responseErrorKeyName = VerifyHelper.verify(block.getEntryValue("responseErrorKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "Response error key can't be empty"); - responseOKKeyName = VerifyHelper.verify(block.getEntryValue("responseOKKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "Response okay key can't be empty"); - url = IOHelper.convertToURL(configUrl); - urlCheckServer = IOHelper.convertToURL(configUrlCheckServer); - urlJoinServer = IOHelper.convertToURL(configUrlJoinServer); - urlUsernameToUUID = IOHelper.convertToURL(configUrlUsernameUUID); - urlUUIDToUsername = IOHelper.convertToURL(configUrlUUIDUsername); - } - - @Override - public UUID auth(AuthProviderResult authResult) throws IOException { - JsonObject request = Json.object().add(userKeyName, authResult.username).add(accessTokenKeyName, authResult.accessToken); - JsonObject result = jsonRequestChecked(request, url); - String value; - if ((value = result.getString(uuidKeyName, null)) != null) - return UUID.fromString(value); - throw new IOException("Service error"); - } - - @Override - public UUID checkServer(String username, String serverID) throws IOException { - JsonObject request = Json.object().add(userKeyName, username).add(serverIDKeyName, serverID); - JsonObject result = jsonRequestChecked(request, urlCheckServer); - String value; - if ((value = result.getString(uuidKeyName, null)) != null) - return UUID.fromString(value); - throw new IOException("Service error"); - } - - @Override - public void close() { - - } - - @Override - public boolean joinServer(String username, String accessToken, String serverID) throws IOException { - JsonObject request = Json.object().add(userKeyName, username).add(serverIDKeyName, serverID).add(accessTokenKeyName, accessToken); - HTTPRequest.jsonRequest(request, urlJoinServer); - return request.getString(responseOKKeyName, null).equals("OK"); - } - - @Override - public UUID usernameToUUID(String username) throws IOException { - JsonObject request = Json.object().add(userKeyName, username); - JsonObject result = jsonRequestChecked(request, urlUsernameToUUID); - String value; - if ((value = result.getString(uuidKeyName, null)) != null) - return UUID.fromString(value); - throw new IOException("Service error"); - } - - @Override - public String uuidToUsername(UUID uuid) throws IOException { - JsonObject request = Json.object().add(uuidKeyName, uuid.toString()); - JsonObject result = jsonRequestChecked(request, urlUUIDToUsername); - String value; - if ((value = result.getString(userKeyName, null)) != null) - return value; - throw new IOException("Service error"); - } - - public JsonObject jsonRequestChecked(JsonObject object, URL url) throws IOException { - JsonValue result = HTTPRequest.jsonRequest(object, url); - if (!result.isObject()) - authError("Authentication server response is malformed"); - - JsonObject response = result.asObject(); - String value; - - if ((value = response.getString(responseErrorKeyName, null)) != null) - authError(value); - return response; - } -} diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonHWIDHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonHWIDHandler.java index 38f4087f..523f5a70 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonHWIDHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/JsonHWIDHandler.java @@ -1,38 +1,79 @@ package ru.gravit.launchserver.auth.hwid; -import com.eclipsesource.json.Json; -import com.eclipsesource.json.JsonArray; -import com.eclipsesource.json.JsonObject; -import com.eclipsesource.json.JsonValue; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import ru.gravit.utils.HTTPRequest; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; -import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry; import ru.gravit.launcher.serialize.config.entry.StringConfigEntry; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.net.HttpURLConnection; import java.net.URL; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; public final class JsonHWIDHandler extends HWIDHandler { private static final int TIMEOUT = Integer.parseInt( System.getProperty("launcher.connection.timeout", Integer.toString(1500))); + private static final Gson gson = new Gson(); private final URL url; private final URL urlBan; private final URL urlUnBan; private final URL urlGet; - private final String loginKeyName; - private final String hddKeyName; - private final String cpuKeyName; - private final String biosKeyName; - private final String isBannedKeyName; + + public class banRequest + { + long hwid_hdd; + + public banRequest(long hwid_hdd, long hwid_cpu, long hwid_bios) { + this.hwid_hdd = hwid_hdd; + this.hwid_cpu = hwid_cpu; + this.hwid_bios = hwid_bios; + } + + long hwid_cpu; + long hwid_bios; + } + public class checkRequest + { + public checkRequest(String username, long hwid_hdd, long hwid_cpu, long hwid_bios) { + this.username = username; + this.hwid_hdd = hwid_hdd; + this.hwid_cpu = hwid_cpu; + this.hwid_bios = hwid_bios; + } + + String username; + long hwid_hdd; + long hwid_cpu; + long hwid_bios; + + } + public class Result + { + String error; + } + public class BannedResult + { + boolean isBanned; + String error; + } + public class HWIDResult + { + long hwid_hdd; + long hwid_cpu; + long hwid_bios; + } + public class HWIDRequest + { + public HWIDRequest(String username) { + this.username = username; + } + + String username; + } JsonHWIDHandler(BlockConfigEntry block) { super(block); @@ -40,16 +81,6 @@ public final class JsonHWIDHandler extends HWIDHandler { String configUrlBan = block.getEntryValue("urlBan", StringConfigEntry.class); String configUrlUnBan = block.getEntryValue("urlUnBan", StringConfigEntry.class); String configUrlGet = block.getEntryValue("urlGet", StringConfigEntry.class); - loginKeyName = VerifyHelper.verify(block.getEntryValue("loginKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "Login key name can't be empty"); - hddKeyName = VerifyHelper.verify(block.getEntryValue("hddKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "HDD key name can't be empty"); - cpuKeyName = VerifyHelper.verify(block.getEntryValue("cpuKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "CPU key can't be empty"); - biosKeyName = VerifyHelper.verify(block.getEntryValue("biosKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "Bios key can't be empty"); - isBannedKeyName = VerifyHelper.verify(block.getEntryValue("isBannedKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "Response username key can't be empty"); url = IOHelper.convertToURL(configUrl); urlBan = IOHelper.convertToURL(configUrlBan); urlUnBan = IOHelper.convertToURL(configUrlUnBan); @@ -59,9 +90,11 @@ public final class JsonHWIDHandler extends HWIDHandler { @Override public void ban(List l_hwid) throws HWIDException { for (HWID hwid : l_hwid) { - JsonObject request = Json.object().add(hddKeyName, hwid.getHwid_hdd()).add(cpuKeyName, hwid.getHwid_cpu()).add(biosKeyName, hwid.getHwid_bios()); + banRequest request = new banRequest(hwid.getHwid_hdd(),hwid.getHwid_cpu(),hwid.getHwid_bios()); try { - request(request, urlBan); + JsonElement result = HTTPRequest.jsonRequest(gson.toJsonTree(request), urlBan); + Result r = gson.fromJson(result,Result.class); + if(r.error != null) throw new HWIDException(r.error); } catch (IOException e) { LogHelper.error(e); throw new HWIDException("HWID service error"); @@ -69,48 +102,19 @@ public void ban(List l_hwid) throws HWIDException { } } - public JsonObject request(JsonObject request, URL url) throws HWIDException, IOException { - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setDoInput(true); - connection.setDoOutput(true); - connection.setRequestMethod("POST"); - connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); - connection.setRequestProperty("Accept", "application/json"); - if (TIMEOUT > 0) - connection.setConnectTimeout(TIMEOUT); - - OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), Charset.forName("UTF-8")); - writer.write(request.toString()); - writer.flush(); - writer.close(); - - InputStreamReader reader; - int statusCode = connection.getResponseCode(); - - if (200 <= statusCode && statusCode < 300) - reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8); - else - reader = new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8); - JsonValue content = Json.parse(reader); - if (!content.isObject()) - throw new HWIDException("HWID server response is malformed"); - - JsonObject response = content.asObject(); - return response; - } - @Override public void check0(HWID hwid, String username) throws HWIDException { - JsonObject request = Json.object().add(loginKeyName, username).add(hddKeyName, hwid.getHwid_hdd()).add(cpuKeyName, hwid.getHwid_cpu()).add(biosKeyName, hwid.getHwid_bios()); - JsonObject response; + checkRequest request = new checkRequest(username,hwid.getHwid_hdd(),hwid.getHwid_cpu(),hwid.getHwid_bios()); try { - response = request(request, url); + JsonElement result = HTTPRequest.jsonRequest(gson.toJsonTree(request), urlBan); + BannedResult r = gson.fromJson(result,BannedResult.class); + if(r.error != null) throw new HWIDException(r.error); + boolean isBanned = r.isBanned; + if (isBanned) throw new HWIDException("You will BANNED!"); } catch (IOException e) { LogHelper.error(e); throw new HWIDException("HWID service error"); } - boolean isBanned = response.getBoolean(isBannedKeyName, false); - if (isBanned) throw new HWIDException("You will BANNED!"); } @Override @@ -120,34 +124,30 @@ public void close() { @Override public List getHwid(String username) throws HWIDException { - JsonObject request = Json.object().add(loginKeyName, username); - JsonObject responce; + ArrayList hwids = new ArrayList<>(); + HWIDRequest request = new HWIDRequest(username); try { - responce = request(request, urlGet); + JsonElement result = HTTPRequest.jsonRequest(gson.toJsonTree(request), urlBan); + HWIDResult[] r = gson.fromJson(result,HWIDResult[].class); + for( HWIDResult hw : r) + { + hwids.add(HWID.gen(hw.hwid_hdd,hw.hwid_bios,hw.hwid_cpu)); + } } catch (IOException e) { LogHelper.error(e); throw new HWIDException("HWID service error"); } - JsonArray array = responce.get("hwids").asArray(); - ArrayList hwids = new ArrayList<>(); - for (JsonValue i : array) { - long hdd, cpu, bios; - JsonObject object = i.asObject(); - hdd = object.getLong(hddKeyName, 0); - cpu = object.getLong(cpuKeyName, 0); - bios = object.getLong(biosKeyName, 0); - HWID hwid = HWID.gen(hdd, cpu, bios); - hwids.add(hwid); - } return hwids; } @Override public void unban(List l_hwid) throws HWIDException { for (HWID hwid : l_hwid) { - JsonObject request = Json.object().add(hddKeyName, hwid.getHwid_hdd()).add(cpuKeyName, hwid.getHwid_cpu()).add(biosKeyName, hwid.getHwid_bios()); + banRequest request = new banRequest(hwid.getHwid_hdd(),hwid.getHwid_cpu(),hwid.getHwid_bios()); try { - request(request, urlUnBan); + JsonElement result = HTTPRequest.jsonRequest(gson.toJsonTree(request), urlUnBan); + Result r = gson.fromJson(result,Result.class); + if(r.error != null) throw new HWIDException(r.error); } catch (IOException e) { LogHelper.error(e); throw new HWIDException("HWID service error"); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java index 6e6258c8..23fa2c2a 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java @@ -3,60 +3,57 @@ import java.io.IOException; import java.net.URL; -import com.eclipsesource.json.Json; -import com.eclipsesource.json.JsonObject; -import com.eclipsesource.json.JsonValue; - +import com.google.gson.Gson; +import com.google.gson.JsonElement; import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.auth.ClientPermissions; import ru.gravit.utils.HTTPRequest; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.SecurityHelper; -import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry; import ru.gravit.launcher.serialize.config.entry.StringConfigEntry; public final class JsonAuthProvider extends AuthProvider { + private final Gson gson = new Gson(); private final URL url; - private final String userKeyName; - private final String passKeyName; - private final String ipKeyName; - private final String responseUserKeyName; - private final String responsePermissionKeyName; - private final String responseErrorKeyName; + public class authResult + { + String username; + String error; + long permissions; + } + public class authRequest + { + public authRequest(String username, String password, String ip) { + this.username = username; + this.password = password; + this.ip = ip; + } + + String username; + String password; + String ip; + } JsonAuthProvider(BlockConfigEntry block, LaunchServer server) { super(block, server); String configUrl = block.getEntryValue("url", StringConfigEntry.class); - userKeyName = VerifyHelper.verify(block.getEntryValue("userKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "Username key name can't be empty"); - passKeyName = VerifyHelper.verify(block.getEntryValue("passKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "Password key name can't be empty"); - ipKeyName = VerifyHelper.verify(block.getEntryValue("ipKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "IP key can't be empty"); - responseUserKeyName = VerifyHelper.verify(block.getEntryValue("responseUserKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "Response username key can't be empty"); - responseErrorKeyName = VerifyHelper.verify(block.getEntryValue("responseErrorKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "Response error key can't be empty"); - responsePermissionKeyName = VerifyHelper.verify(block.getEntryValue("responsePermissionKeyName", StringConfigEntry.class), - VerifyHelper.NOT_EMPTY, "Response error key can't be empty"); url = IOHelper.convertToURL(configUrl); } @Override public AuthProviderResult auth(String login, String password, String ip) throws IOException { - JsonObject request = Json.object().add(userKeyName, login).add(passKeyName, password).add(ipKeyName, ip); - JsonValue content = HTTPRequest.jsonRequest(request, url); - if (!content.isObject()) + authRequest authRequest = new authRequest(login,password,ip); + JsonElement request = gson.toJsonTree(authRequest); + JsonElement content = HTTPRequest.jsonRequest(request, url); + if (!content.isJsonObject()) return authError("Authentication server response is malformed"); - JsonObject response = content.asObject(); - String value; - - if ((value = response.getString(responseUserKeyName, null)) != null) - return new AuthProviderResult(value, SecurityHelper.randomStringToken(), new ClientPermissions(response.getLong(responsePermissionKeyName, 0))); - else if ((value = response.getString(responseErrorKeyName, null)) != null) - return authError(value); + authResult result = gson.fromJson(content, authResult.class); + if (result.username != null) + return new AuthProviderResult(result.username, SecurityHelper.randomStringToken(), new ClientPermissions(result.permissions)); + else if (result.error != null) + return authError(result.error); else return authError("Authentication server response is malformed"); } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MojangAuthProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MojangAuthProvider.java index e02f27ef..d799cc16 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MojangAuthProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MojangAuthProvider.java @@ -11,18 +11,18 @@ import java.util.UUID; import java.util.regex.Pattern; -import com.eclipsesource.json.Json; -import com.eclipsesource.json.JsonObject; -import com.eclipsesource.json.JsonValue; -import com.eclipsesource.json.WriterConfig; - +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import ru.gravit.launchserver.LaunchServer; +import ru.gravit.utils.HTTPRequest; import ru.gravit.utils.helper.IOHelper; import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry; public final class MojangAuthProvider extends AuthProvider { private static final Pattern UUID_REGEX = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})"); private static final URL URL; + private final Gson gson = new Gson(); static { try { @@ -32,52 +32,43 @@ public final class MojangAuthProvider extends AuthProvider { } } - public static JsonObject makeJSONRequest(URL url, JsonObject request) throws IOException { - // Make authentication request - HttpURLConnection connection = IOHelper.newConnectionPost(url); - connection.setRequestProperty("Content-Type", "application/json"); - try (OutputStream output = connection.getOutputStream()) { - output.write(request.toString(WriterConfig.MINIMAL).getBytes(StandardCharsets.UTF_8)); - } - connection.getResponseCode(); // Actually make request - - // Read response - InputStream errorInput = connection.getErrorStream(); - try (InputStream input = errorInput == null ? connection.getInputStream() : errorInput) { - String charset = connection.getContentEncoding(); - Charset charsetObject = charset == null ? - IOHelper.UNICODE_CHARSET : Charset.forName(charset); - - // Parse response - String json = new String(IOHelper.read(input), charsetObject); - return json.isEmpty() ? null : Json.parse(json).asObject(); - } - } - public MojangAuthProvider(BlockConfigEntry block, LaunchServer server) { super(block, server); } + public class mojangAuth + { + public mojangAuth(String username, String password) { + this.username = username; + this.password = password; + name = "Minecraft"; + version = 1; + } + String name; + int version; + String username; + String password; + + } @Override public AuthProviderResult auth(String login, String password, String ip) throws Exception { - JsonObject request = Json.object(). - add("agent", Json.object().add("name", "Minecraft").add("version", 1)). - add("username", login).add("password", password); + mojangAuth mojangAuth = new mojangAuth(login,password); + JsonElement request = gson.toJsonTree(mojangAuth); // Verify there's no error - JsonObject response = makeJSONRequest(URL, request); + JsonObject response = HTTPRequest.jsonRequest(request,URL).getAsJsonObject(); if (response == null) authError("Empty com.mojang response"); - JsonValue errorMessage = response.get("errorMessage"); + JsonElement errorMessage = response.get("errorMessage"); if (errorMessage != null) - authError(errorMessage.asString()); + authError(errorMessage.getAsString()); // Parse JSON data - JsonObject selectedProfile = response.get("selectedProfile").asObject(); - String username = selectedProfile.get("name").asString(); - String accessToken = response.get("clientToken").asString(); - UUID uuid = UUID.fromString(UUID_REGEX.matcher(selectedProfile.get("id").asString()).replaceFirst("$1-$2-$3-$4-$5")); - String launcherToken = response.get("accessToken").asString(); + JsonObject selectedProfile = response.get("selectedProfile").getAsJsonObject(); + String username = selectedProfile.get("name").getAsString(); + String accessToken = response.get("clientToken").getAsString(); + UUID uuid = UUID.fromString(UUID_REGEX.matcher(selectedProfile.get("id").getAsString()).replaceFirst("$1-$2-$3-$4-$5")); + String launcherToken = response.get("accessToken").getAsString(); // We're done return new MojangAuthProviderResult(username, accessToken, uuid, launcherToken); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/IndexAssetCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/IndexAssetCommand.java index 041a6b45..131d5c3b 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/IndexAssetCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/IndexAssetCommand.java @@ -9,10 +9,9 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.Collections; -import com.eclipsesource.json.Json; -import com.eclipsesource.json.JsonObject; -import com.eclipsesource.json.WriterConfig; - +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonParser; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.SecurityHelper; @@ -22,12 +21,25 @@ import ru.gravit.launchserver.command.CommandException; public final class IndexAssetCommand extends Command { + private static Gson gson = new Gson(); + private static JsonParser parser = new JsonParser(); + public static class IndexObject + { + long size; + + public IndexObject(long size, String hash) { + this.size = size; + this.hash = hash; + } + + String hash; + } private static final class IndexAssetVisitor extends SimpleFileVisitor { - private final JsonObject objects; + private final JsonArray objects; private final Path inputAssetDir; private final Path outputAssetDir; - private IndexAssetVisitor(JsonObject objects, Path inputAssetDir, Path outputAssetDir) { + private IndexAssetVisitor(JsonArray objects, Path inputAssetDir, Path outputAssetDir) { this.objects = objects; this.inputAssetDir = inputAssetDir; this.outputAssetDir = outputAssetDir; @@ -40,7 +52,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO // Add to index and copy file String digest = SecurityHelper.toHex(SecurityHelper.digest(DigestAlgorithm.SHA1, file)); - objects.add(name, Json.object().add("size", attrs.size()).add("hash", digest)); + IndexObject obj = new IndexObject(attrs.size(),digest); + objects.add(gson.toJsonTree(obj)); IOHelper.copy(file, resolveObjectFile(outputAssetDir, digest)); // Continue visiting @@ -93,14 +106,15 @@ public void invoke(String... args) throws Exception { Files.createDirectory(outputAssetDir); // Index objects - JsonObject objects = Json.object(); + JsonArray objects = new JsonArray(); LogHelper.subInfo("Indexing objects"); IOHelper.walk(inputAssetDir, new IndexAssetVisitor(objects, inputAssetDir, outputAssetDir), false); // Write index file LogHelper.subInfo("Writing asset index file: '%s'", indexFileName); + try (BufferedWriter writer = IOHelper.newWriter(resolveIndexFile(outputAssetDir, indexFileName))) { - Json.object().add(OBJECTS_DIR, objects).writeTo(writer, WriterConfig.MINIMAL); + writer.write(gson.toJson(objects)); } // Finished diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/UnindexAssetCommand.java b/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/UnindexAssetCommand.java index 46c09b85..670e8e1a 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/UnindexAssetCommand.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/command/hash/UnindexAssetCommand.java @@ -4,11 +4,9 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; +import java.util.Map; -import com.eclipsesource.json.Json; -import com.eclipsesource.json.JsonObject; -import com.eclipsesource.json.JsonObject.Member; - +import com.google.gson.*; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; import ru.gravit.launchserver.LaunchServer; @@ -16,6 +14,8 @@ import ru.gravit.launchserver.command.CommandException; public final class UnindexAssetCommand extends Command { + private static Gson gson = new Gson(); + private static JsonParser parser = new JsonParser(); public UnindexAssetCommand(LaunchServer server) { super(server); } @@ -49,17 +49,17 @@ public void invoke(String... args) throws Exception { JsonObject objects; LogHelper.subInfo("Reading asset index file: '%s'", indexFileName); try (BufferedReader reader = IOHelper.newReader(IndexAssetCommand.resolveIndexFile(inputAssetDir, indexFileName))) { - objects = Json.parse(reader).asObject().get(IndexAssetCommand.OBJECTS_DIR).asObject(); + objects = parser.parse(reader).getAsJsonObject().get("objects").getAsJsonObject(); } // Restore objects LogHelper.subInfo("Unindexing %d objects", objects.size()); - for (Member member : objects) { - String name = member.getName(); + for (Map.Entry member : objects.entrySet()) { + String name = member.getKey(); LogHelper.subInfo("Unindexing: '%s'", name); // Copy hashed file to target - String hash = member.getValue().asObject().get("hash").asString(); + String hash = member.getValue().getAsJsonObject().get("hash").getAsString(); Path source = IndexAssetCommand.resolveObjectFile(inputAssetDir, hash); IOHelper.copy(source, outputAssetDir.resolve(name)); } diff --git a/Launcher/runtime/engine/api.js b/Launcher/runtime/engine/api.js index 92ccf4a7..785f8f96 100644 --- a/Launcher/runtime/engine/api.js +++ b/Launcher/runtime/engine/api.js @@ -101,11 +101,11 @@ function newTask(r) { } function newRequestTask(request) { - return newTask(function() request.request()); + FunctionalBridge.makeRequest(request); } function startTask(task) { - CommonHelper.newThread("FX Task Thread", true, task).start(); + FunctionalBridge.startTask(task); } function openURL(url) { diff --git a/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java b/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java index 883e7ad8..93dc8385 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java +++ b/Launcher/src/main/java/ru/gravit/launcher/LauncherEngine.java @@ -212,6 +212,8 @@ public void start(String... args) throws Throwable { throw new IllegalStateException("Launcher has been already started"); Launcher.modulesManager.initModules(); // Load init.js script + FunctionalBridge.worker = new RequestWorker(); + CommonHelper.newThread("FX Task Worker", true, FunctionalBridge.worker); loadScript(Launcher.API_SCRIPT_FILE); loadScript(Launcher.CONFIG_SCRIPT_FILE); loadScript(Launcher.INIT_SCRIPT_FILE); diff --git a/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java b/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java index 2aeb3672..0ec1d416 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java +++ b/Launcher/src/main/java/ru/gravit/launcher/client/ClientLauncher.java @@ -17,10 +17,8 @@ import javax.swing.JOptionPane; -import com.eclipsesource.json.Json; -import com.eclipsesource.json.JsonObject; -import com.eclipsesource.json.WriterConfig; - +import com.google.gson.Gson; +import com.google.gson.JsonObject; import ru.gravit.launcher.*; import ru.gravit.launcher.hasher.DirWatcher; import ru.gravit.launcher.hasher.FileNameMatcher; @@ -42,6 +40,7 @@ import ru.gravit.launcher.serialize.stream.StreamObject; public final class ClientLauncher { + private static Gson gson = new Gson(); private static final class ClassPathFileVisitor extends SimpleFileVisitor { private final Collection result; @@ -170,7 +169,6 @@ public void write(HOutput output) throws IOException { private static final Path NATIVES_DIR = IOHelper.toPath("natives"); private static final Path RESOURCEPACKS_DIR = IOHelper.toPath("resourcepacks"); private static PublicURLClassLoader classLoader; - private static void addClientArgs(Collection args, ClientProfile profile, Params params) { PlayerProfile pp = params.pp; @@ -185,16 +183,16 @@ private static void addClientArgs(Collection args, ClientProfile profile if (version.compareTo(ClientProfile.Version.MC1710) >= 0) { // Add user properties Collections.addAll(args, "--userType", "mojang"); - JsonObject properties = Json.object(); + JsonObject properties = new JsonObject(); if (pp.skin != null) { - properties.add(Launcher.SKIN_URL_PROPERTY, Json.array(pp.skin.url)); - properties.add(Launcher.SKIN_DIGEST_PROPERTY, Json.array(SecurityHelper.toHex(pp.skin.digest))); + properties.addProperty(Launcher.SKIN_URL_PROPERTY, pp.skin.url); + properties.addProperty(Launcher.SKIN_DIGEST_PROPERTY, SecurityHelper.toHex(pp.skin.digest)); } if (pp.cloak != null) { - properties.add(Launcher.CLOAK_URL_PROPERTY, Json.array(pp.cloak.url)); - properties.add(Launcher.CLOAK_DIGEST_PROPERTY, Json.array(SecurityHelper.toHex(pp.cloak.digest))); + properties.addProperty(Launcher.CLOAK_URL_PROPERTY, pp.cloak.url); + properties.addProperty(Launcher.CLOAK_DIGEST_PROPERTY, SecurityHelper.toHex(pp.cloak.digest)); } - Collections.addAll(args, "--userProperties", properties.toString(WriterConfig.MINIMAL)); + Collections.addAll(args, "--userProperties", ClientLauncher.gson.toJson(properties)); // Add asset index Collections.addAll(args, "--assetIndex", profile.getAssetIndex()); diff --git a/Launcher/src/main/java/ru/gravit/launcher/client/FunctionalBridge.java b/Launcher/src/main/java/ru/gravit/launcher/client/FunctionalBridge.java index 7169ff5b..6cfd4c3a 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/client/FunctionalBridge.java +++ b/Launcher/src/main/java/ru/gravit/launcher/client/FunctionalBridge.java @@ -1,11 +1,15 @@ package ru.gravit.launcher.client; +import javafx.concurrent.Task; import ru.gravit.launcher.LauncherAPI; import ru.gravit.launcher.hasher.FileNameMatcher; import ru.gravit.launcher.hasher.HashedDir; import ru.gravit.launcher.request.Request; import ru.gravit.launcher.request.update.LegacyLauncherRequest; +import ru.gravit.launcher.request.websockets.RequestInterface; import ru.gravit.launcher.serialize.signed.SignedObjectHolder; +import ru.gravit.utils.helper.CommonHelper; +import ru.gravit.utils.helper.LogHelper; import java.io.IOException; import java.nio.file.Path; @@ -14,6 +18,8 @@ public class FunctionalBridge { @LauncherAPI public static LauncherSettings settings; + @LauncherAPI + public static RequestWorker worker; @LauncherAPI public HashedDirRunnable offlineUpdateRequest(String dirName, Path dir, SignedObjectHolder hdir, FileNameMatcher matcher, boolean digest) throws Exception { @@ -45,4 +51,18 @@ public LegacyLauncherRequest.Result offlineLauncherRequest() throws IOException, public interface HashedDirRunnable { SignedObjectHolder run() throws Exception; } + @LauncherAPI + public void makeJsonRequest(RequestInterface request, Runnable callback) + { + + } + @LauncherAPI + public void startTask(Task task) + { + try { + worker.queue.put(task); + } catch (InterruptedException e) { + LogHelper.error(e); + } + } } diff --git a/Launcher/src/main/java/ru/gravit/launcher/client/RequestWorker.java b/Launcher/src/main/java/ru/gravit/launcher/client/RequestWorker.java new file mode 100644 index 00000000..90850b22 --- /dev/null +++ b/Launcher/src/main/java/ru/gravit/launcher/client/RequestWorker.java @@ -0,0 +1,29 @@ +package ru.gravit.launcher.client; + +import javafx.concurrent.Task; +import ru.gravit.utils.helper.LogHelper; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +public class RequestWorker implements Runnable { + public RequestWorker() + { + queue = new LinkedBlockingQueue<>(64); + } + public BlockingQueue queue; + @Override + public void run() { + while (!Thread.interrupted()) + { + try { + Runnable task; + task = queue.take(); + task.run(); + } catch (InterruptedException e) { + LogHelper.error(e); + return; + } + } + } +} diff --git a/Launcher/src/main/java/ru/gravit/launcher/client/ServerPinger.java b/Launcher/src/main/java/ru/gravit/launcher/client/ServerPinger.java index fdb7e7e5..a0b18c1e 100644 --- a/Launcher/src/main/java/ru/gravit/launcher/client/ServerPinger.java +++ b/Launcher/src/main/java/ru/gravit/launcher/client/ServerPinger.java @@ -10,9 +10,8 @@ import java.util.Objects; import java.util.regex.Pattern; -import com.eclipsesource.json.Json; -import com.eclipsesource.json.JsonObject; - +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import ru.gravit.launcher.LauncherAPI; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.LogHelper; @@ -22,6 +21,7 @@ import ru.gravit.launcher.profiles.ClientProfile; public final class ServerPinger { + private JsonParser parser = new JsonParser(); public static final class Result { @LauncherAPI public final int onlinePlayers; @@ -184,10 +184,10 @@ private Result modernPing(HInput input, HOutput output) throws IOException { } // Parse JSON response - JsonObject object = Json.parse(response).asObject(); - JsonObject playersObject = object.get("players").asObject(); - int online = playersObject.get("online").asInt(); - int max = playersObject.get("max").asInt(); + JsonObject object = parser.parse(response).getAsJsonObject(); + JsonObject playersObject = object.get("players").getAsJsonObject(); + int online = playersObject.get("online").getAsInt(); + int max = playersObject.get("max").getAsInt(); // Return ping status return new Result(online, max, response); diff --git a/libLauncher/build.gradle b/libLauncher/build.gradle index c5ee7b1e..41b686dd 100644 --- a/libLauncher/build.gradle +++ b/libLauncher/build.gradle @@ -4,6 +4,5 @@ dependencies { compileOnly 'org.fusesource.jansi:jansi:1.17.1' compileOnly 'org.javassist:javassist:3.23.1-GA' - compile 'com.eclipsesource.minimal-json:minimal-json:0.9.4' compile 'com.google.code.gson:gson:2.8.5' } diff --git a/libLauncher/src/main/java/ru/gravit/launcher/profiles/ClientProfile.java b/libLauncher/src/main/java/ru/gravit/launcher/profiles/ClientProfile.java index 6c7d38f7..18c94a69 100644 --- a/libLauncher/src/main/java/ru/gravit/launcher/profiles/ClientProfile.java +++ b/libLauncher/src/main/java/ru/gravit/launcher/profiles/ClientProfile.java @@ -66,15 +66,15 @@ public String toString() { private static final FileNameMatcher ASSET_MATCHER = new FileNameMatcher( new String[0], new String[]{"indexes", "objects"}, new String[0]); // Version - private final StringConfigEntry version; + private String version; - private final StringConfigEntry assetIndex; + private final String assetIndex; // Client - private final IntegerConfigEntry sortIndex; - private final StringConfigEntry title; - private final StringConfigEntry serverAddress; + private final int sortIndex; + private String title; + private final String serverAddress; - private final IntegerConfigEntry serverPort; + private final int serverPort; public static class MarkedString { @LauncherAPI @@ -112,30 +112,30 @@ public int hashCode() { private final List updateShared = new ArrayList<>(); private final List updateVerify = new ArrayList<>(); private final Set updateOptional = new HashSet<>(); - private final BooleanConfigEntry updateFastCheck; + private final boolean updateFastCheck; - private final BooleanConfigEntry useWhitelist; + private final boolean useWhitelist; // Client launcher - private final StringConfigEntry mainClass; - private final ListConfigEntry jvmArgs; - private final ListConfigEntry classPath; - private final ListConfigEntry clientArgs; + private final String mainClass; + private final List jvmArgs = new ArrayList<>(); + private final List classPath = new ArrayList<>(); + private final List clientArgs = new ArrayList<>(); - private final ListConfigEntry whitelist; + private final List whitelist = new ArrayList<>(); @LauncherAPI public ClientProfile(BlockConfigEntry block) { super(block); // Version - version = block.getEntry("version", StringConfigEntry.class); - assetIndex = block.getEntry("assetIndex", StringConfigEntry.class); + version = block.getEntryValue("version", StringConfigEntry.class); + assetIndex = block.getEntryValue("assetIndex", StringConfigEntry.class); // Client - sortIndex = block.getEntry("sortIndex", IntegerConfigEntry.class); - title = block.getEntry("title", StringConfigEntry.class); - serverAddress = block.getEntry("serverAddress", StringConfigEntry.class); - serverPort = block.getEntry("serverPort", IntegerConfigEntry.class); + sortIndex = block.getEntryValue("sortIndex", IntegerConfigEntry.class); + title = block.getEntryValue("title", StringConfigEntry.class); + serverAddress = block.getEntryValue("serverAddress", StringConfigEntry.class); + serverPort = block.getEntryValue("serverPort", IntegerConfigEntry.class); // Updater and client watch service block.getEntry("update", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(update::add); @@ -144,15 +144,15 @@ public ClientProfile(BlockConfigEntry block) { block.getEntry("updateOptional", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(e -> updateOptional.add(new MarkedString(e))); block.getEntry("updateExclusions", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(updateExclusions::add); block.getEntry("enabledOptional", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(e -> updateOptional.stream().anyMatch(e1 -> e.equals(e1.string) && (e1.mark = true))); - updateFastCheck = block.getEntry("updateFastCheck", BooleanConfigEntry.class); - useWhitelist = block.getEntry("useWhitelist", BooleanConfigEntry.class); + updateFastCheck = block.getEntryValue("updateFastCheck", BooleanConfigEntry.class); + useWhitelist = block.getEntryValue("useWhitelist", BooleanConfigEntry.class); // Client launcher - mainClass = block.getEntry("mainClass", StringConfigEntry.class); - classPath = block.getEntry("classPath", ListConfigEntry.class); - jvmArgs = block.getEntry("jvmArgs", ListConfigEntry.class); - clientArgs = block.getEntry("clientArgs", ListConfigEntry.class); - whitelist = block.getEntry("whitelist", ListConfigEntry.class); + mainClass = block.getEntryValue("mainClass", StringConfigEntry.class); + block.getEntry("classPath", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(classPath::add); + block.getEntry("jvmArgs", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(jvmArgs::add); + block.getEntry("clientArgs", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(clientArgs::add); + block.getEntry("whitelist", ListConfigEntry.class).stream(StringConfigEntry.class).forEach(whitelist::add); } @LauncherAPI @@ -167,7 +167,7 @@ public int compareTo(ClientProfile o) { @LauncherAPI public String getAssetIndex() { - return assetIndex.getValue(); + return assetIndex; } @LauncherAPI @@ -177,12 +177,12 @@ public FileNameMatcher getAssetUpdateMatcher() { @LauncherAPI public String[] getClassPath() { - return classPath.stream(StringConfigEntry.class).toArray(String[]::new); + return classPath.toArray(new String[0]); } @LauncherAPI public String[] getClientArgs() { - return clientArgs.stream(StringConfigEntry.class).toArray(String[]::new); + return clientArgs.toArray(new String[0]); } @LauncherAPI @@ -204,17 +204,17 @@ public FileNameMatcher getClientUpdateMatcher(/*boolean excludeOptional*/) { @LauncherAPI public String[] getJvmArgs() { - return jvmArgs.stream(StringConfigEntry.class).toArray(String[]::new); + return jvmArgs.toArray(new String[0]); } @LauncherAPI public String getMainClass() { - return mainClass.getValue(); + return mainClass; } @LauncherAPI public String getServerAddress() { - return serverAddress.getValue(); + return serverAddress; } @LauncherAPI @@ -248,7 +248,7 @@ public void pushOptional(HashedDir dir, boolean digest) throws IOException { @LauncherAPI public int getServerPort() { - return serverPort.getValue(); + return serverPort; } @LauncherAPI @@ -258,43 +258,43 @@ public InetSocketAddress getServerSocketAddress() { @LauncherAPI public int getSortIndex() { - return sortIndex.getValue(); + return sortIndex; } @LauncherAPI public String getTitle() { - return title.getValue(); + return title; } @LauncherAPI public Version getVersion() { - return Version.byName(version.getValue()); + return Version.byName(version); } @LauncherAPI public boolean isUpdateFastCheck() { - return updateFastCheck.getValue(); + return updateFastCheck; } @LauncherAPI public boolean isWhitelistContains(String username) { - if (!useWhitelist.getValue()) return true; - return whitelist.stream(StringConfigEntry.class).anyMatch(e -> e.equals(username)); + if (!useWhitelist) return true; + return whitelist.stream().anyMatch(e -> e.equals(username)); } @LauncherAPI public void setTitle(String title) { - this.title.setValue(title); + this.title = title; } @LauncherAPI public void setVersion(Version version) { - this.version.setValue(version.name); + this.version = version.name; } @Override public String toString() { - return title.getValue(); + return title; } @LauncherAPI @@ -309,9 +309,6 @@ public void verify() { VerifyHelper.verifyInt(getServerPort(), VerifyHelper.range(0, 65535), "Illegal server port: " + getServerPort()); // Client launcher - jvmArgs.verifyOfType(ConfigEntry.Type.STRING); - classPath.verifyOfType(ConfigEntry.Type.STRING); - clientArgs.verifyOfType(ConfigEntry.Type.STRING); VerifyHelper.verify(getTitle(), VerifyHelper.NOT_EMPTY, "Main class can't be empty"); } } diff --git a/libLauncher/src/main/java/ru/gravit/utils/HTTPRequest.java b/libLauncher/src/main/java/ru/gravit/utils/HTTPRequest.java index bcfd2352..812a0ad8 100644 --- a/libLauncher/src/main/java/ru/gravit/utils/HTTPRequest.java +++ b/libLauncher/src/main/java/ru/gravit/utils/HTTPRequest.java @@ -9,13 +9,13 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import com.eclipsesource.json.Json; -import com.eclipsesource.json.JsonObject; -import com.eclipsesource.json.JsonValue; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; import ru.gravit.utils.helper.IOHelper; public class HTTPRequest { private static final int TIMEOUT = 10; + private static final JsonParser parser = new JsonParser(); public static int sendCrashreport(String strurl, byte[] data) throws IOException { URL url = new URL(strurl); @@ -36,7 +36,7 @@ public static int sendCrashreport(String strurl, String data) throws IOException return sendCrashreport(strurl, data.getBytes(IOHelper.UNICODE_CHARSET)); } - public static JsonValue jsonRequest(JsonObject request, URL url) throws IOException { + public static JsonElement jsonRequest(JsonElement request, URL url) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); @@ -58,7 +58,7 @@ public static JsonValue jsonRequest(JsonObject request, URL url) throws IOExcept reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8); else reader = new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8); - JsonValue content = Json.parse(reader); + JsonElement content = parser.parse(reader); return content; } }