From 81f50a57f3ab842698eb68a1e69c4d24197f1a58 Mon Sep 17 00:00:00 2001 From: Gravit Date: Mon, 30 Sep 2019 10:43:41 +0700 Subject: [PATCH] =?UTF-8?q?[FEATURE]=20=D0=9D=D0=BE=D0=B2=D1=8B=D0=B5=20?= =?UTF-8?q?=D1=84=D0=B8=D1=82=D1=87=D0=B8=20MirrorManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../command/hash/DownloadAssetCommand.java | 6 +- .../command/hash/DownloadClientCommand.java | 13 ++- .../launchserver/manangers/MirrorManager.java | 91 ++++++++++++++++--- .../java/pro/gravit/utils/HTTPRequest.java | 19 ++-- 4 files changed, 101 insertions(+), 28 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadAssetCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadAssetCommand.java index c6f6e708..78317457 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadAssetCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadAssetCommand.java @@ -30,7 +30,8 @@ public String getUsageDescription() { @Override public void invoke(String... args) throws Exception { verifyArgs(args, 2); - Version version = Version.byName(args[0]); + //Version version = Version.byName(args[0]); + String versionName = args[0]; String dirName = IOHelper.verifyFileName(args[1]); Path assetDir = server.updatesDir.resolve(dirName); @@ -40,7 +41,8 @@ public void invoke(String... args) throws Exception { // Download required asset LogHelper.subInfo("Downloading asset, it may take some time"); - HttpDownloader.downloadZip(server.mirrorManager.getDefaultMirror().getAssetsURL(version.name), assetDir); + //HttpDownloader.downloadZip(server.mirrorManager.getDefaultMirror().getAssetsURL(version.name), assetDir); + server.mirrorManager.downloadZip(assetDir,"assets/%s.zip", versionName); // Finished server.syncUpdatesDir(Collections.singleton(dirName)); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadClientCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadClientCommand.java index 26807bc4..bc89c1c6 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadClientCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/hash/DownloadClientCommand.java @@ -7,6 +7,7 @@ import java.nio.file.Path; import java.util.Collections; +import com.google.gson.JsonElement; import pro.gravit.launcher.Launcher; import pro.gravit.launcher.profiles.ClientProfile; import pro.gravit.launcher.profiles.ClientProfile.Version; @@ -36,7 +37,8 @@ public String getUsageDescription() { @Override public void invoke(String... args) throws IOException, CommandException { verifyArgs(args, 2); - Version version = Version.byName(args[0]); + //Version version = Version.byName(args[0]); + String versionName = args[0]; String dirName = IOHelper.verifyFileName(args[1]); Path clientDir = server.updatesDir.resolve(args[1]); @@ -46,14 +48,19 @@ public void invoke(String... args) throws IOException, CommandException { // Download required client LogHelper.subInfo("Downloading client, it may take some time"); - HttpDownloader.downloadZip(server.mirrorManager.getDefaultMirror().getClientsURL(version.name), clientDir); + //HttpDownloader.downloadZip(server.mirrorManager.getDefaultMirror().getClientsURL(version.name), clientDir); + server.mirrorManager.downloadZip(clientDir,"clients/%s.zip", versionName); // Create profile file LogHelper.subInfo("Creaing profile file: '%s'", dirName); ClientProfile client; - String profilePath = String.format("pro/gravit/launchserver/defaults/profile%s.cfg", version.name); + String profilePath = String.format("pro/gravit/launchserver/defaults/profile%s.cfg", versionName); try (BufferedReader reader = IOHelper.newReader(IOHelper.getResourceURL(profilePath))) { client = Launcher.gsonManager.configGson.fromJson(reader, ClientProfile.class); + } catch (IOException e) + { + JsonElement clientJson = server.mirrorManager.jsonRequest(null, "GET", "clients/%s.json", versionName); + client = Launcher.gsonManager.configGson.fromJson(clientJson, ClientProfile.class); } client.setTitle(dirName); client.setDir(dirName); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/MirrorManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/MirrorManager.java index 684accb3..592a5fc6 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/MirrorManager.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/MirrorManager.java @@ -1,33 +1,36 @@ package pro.gravit.launchserver.manangers; +import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; +import java.nio.file.Path; import java.util.ArrayList; +import java.util.Arrays; +import com.google.gson.JsonElement; +import pro.gravit.utils.HTTPRequest; +import pro.gravit.utils.HttpDownloader; import pro.gravit.utils.helper.IOHelper; +import pro.gravit.utils.helper.LogHelper; public class MirrorManager { - public class Mirror { - URL url; - String assetsURLMask; - String clientsURLMask; + public static class Mirror { + String baseUrl; boolean enabled; Mirror(String url) { - assetsURLMask = url.concat("assets/%s.zip"); - clientsURLMask = url.concat("clients/%s.zip"); + //assetsURLMask = url.concat("assets/%s.zip"); + //clientsURLMask = url.concat("clients/%s.zip"); + baseUrl = url; } - private URL formatArg(String mask, String arg) throws MalformedURLException { - return new URL(String.format(mask, IOHelper.urlEncode(arg))); + private URL formatArgs(String mask, Object... args) throws MalformedURLException { + Object[] data = Arrays.stream(args).map(e -> IOHelper.urlEncode(e.toString())).toArray(); + return new URL(baseUrl.concat(String.format(mask, data))); } - public URL getAssetsURL(String assets) throws MalformedURLException { - return formatArg(assetsURLMask, assets); - } - - public URL getClientsURL(String client) throws MalformedURLException { - return formatArg(clientsURLMask, client); + public URL getURL(String mask, Object... args) throws MalformedURLException { + return formatArgs(mask, args); } } @@ -38,13 +41,14 @@ public void addMirror(String mirror) { Mirror m = new Mirror(mirror); m.enabled = true; if (defaultMirror == null) defaultMirror = m; + list.add(m); } public void addMirror(String mirror, boolean enabled) throws MalformedURLException { Mirror m = new Mirror(mirror); - m.url = new URL(mirror); m.enabled = enabled; if (defaultMirror == null && enabled) defaultMirror = m; + list.add(m); } public Mirror getDefaultMirror() { @@ -66,4 +70,61 @@ public void enableMirror(int index) { public int size() { return list.size(); } + + public boolean downloadZip(Mirror mirror, Path path, String mask, Object... args) throws IOException + { + if(!mirror.enabled) return false; + URL url = mirror.getURL(mask, args); + LogHelper.debug("Try download %s", url.toString()); + try { + HttpDownloader.downloadZip(url, path); + } catch (IOException e) + { + LogHelper.error("Download %s failed(%s: %s)", url.toString(), e.getClass().getName(), e.getMessage()); + return false; + } + return true; + } + + public void downloadZip(Path path, String mask, Object... args) throws IOException + { + if(downloadZip(defaultMirror, path, mask, args)) + { + return; + } + for(Mirror mirror : list) + { + if(mirror != defaultMirror) + { + if(downloadZip(mirror, path, mask, args)) return; + } + } + throw new IOException(String.format("Error download %s. All mirrors return error", path.toString())); + } + public JsonElement jsonRequest(Mirror mirror, JsonElement request, String method, String mask, Object... args) throws IOException + { + if(!mirror.enabled) return null; + URL url = mirror.getURL(mask, args); + try { + return HTTPRequest.jsonRequest(request, method, url); + } catch (IOException e) + { + LogHelper.error("JsonRequest %s failed(%s: %s)", url.toString(), e.getClass().getName(), e.getMessage()); + return null; + } + } + public JsonElement jsonRequest(JsonElement request, String method, String mask, Object... args) throws IOException + { + JsonElement result = jsonRequest(defaultMirror, request, method, mask, args); + if(result != null) return result; + for(Mirror mirror : list) + { + if(mirror != defaultMirror) + { + result = jsonRequest(mirror, request, method, mask, args); + if(result != null) return result; + } + } + throw new IOException("Error jsonRequest. All mirrors return error"); + } } diff --git a/LauncherCore/src/main/java/pro/gravit/utils/HTTPRequest.java b/LauncherCore/src/main/java/pro/gravit/utils/HTTPRequest.java index 662ad81c..59077f26 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/HTTPRequest.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/HTTPRequest.java @@ -39,19 +39,22 @@ public static int sendCrashreport(String strurl, String data) throws IOException } public static JsonElement jsonRequest(JsonElement request, URL url) throws IOException { + return jsonRequest(request, "POST", url); + } + + public static JsonElement jsonRequest(JsonElement request, String method, URL url) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); - connection.setDoOutput(true); - connection.setRequestMethod("POST"); - connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); + if(request != null) connection.setDoOutput(true); + connection.setRequestMethod(method); + if(request != null) 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(); + if(request != null) try (OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8)) { + writer.write(request.toString()); + writer.flush(); + } InputStreamReader reader; int statusCode = connection.getResponseCode();