diff options
author | mdxd44 <ogurec332@mail.ru> | 2021-12-17 19:31:55 +0900 |
---|---|---|
committer | mdxd44 <ogurec332@mail.ru> | 2021-12-17 19:31:55 +0900 |
commit | cff1b4a22bb47c8bcf064d5e8da8c7d7ef67ea52 (patch) | |
tree | 9fc8614b144288af2749c2376c8ca523cd2a0cc0 /src/main/java/net/elytrium/limboauth/utils | |
parent | f11b09654cc33f4c3d9239c04be5978cbe3cad2d (diff) | |
download | LimboAuth-cff1b4a22bb47c8bcf064d5e8da8c7d7ef67ea52.tar.gz LimboAuth-cff1b4a22bb47c8bcf064d5e8da8c7d7ef67ea52.tar.bz2 LimboAuth-cff1b4a22bb47c8bcf064d5e8da8c7d7ef67ea52.zip |
Split projects.
Diffstat (limited to 'src/main/java/net/elytrium/limboauth/utils')
-rw-r--r-- | src/main/java/net/elytrium/limboauth/utils/UpdatesChecker.java | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/main/java/net/elytrium/limboauth/utils/UpdatesChecker.java b/src/main/java/net/elytrium/limboauth/utils/UpdatesChecker.java new file mode 100644 index 0000000..71af1bb --- /dev/null +++ b/src/main/java/net/elytrium/limboauth/utils/UpdatesChecker.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2021 Elytrium + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package net.elytrium.limboauth.utils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.TimeUnit; +import net.elytrium.limboauth.Settings; +import org.slf4j.Logger; + +public class UpdatesChecker { + + public static void checkForUpdates(Logger logger) { + try { + URLConnection conn = new URL("https://raw.githubusercontent.com/Elytrium/LimboAuth/master/VERSION").openConnection(); + int timeout = (int) TimeUnit.SECONDS.toMillis(5); + conn.setConnectTimeout(timeout); + conn.setReadTimeout(timeout); + try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { + String latestVersion = in.readLine(); + if (latestVersion == null) { + logger.warn("Unable to check for updates."); + return; + } + String latestVersion0 = getCleanVersion(latestVersion.trim()); + String currentVersion0 = getCleanVersion(Settings.IMP.VERSION); + int latestVersionId = Integer.parseInt(latestVersion0.replace(".", "").replace("$", "")); + int currentVersionId = Integer.parseInt(currentVersion0.replace(".", "").replace("$", "")); + if (latestVersion0.endsWith("$")) { + --latestVersionId; + } + if (currentVersion0.endsWith("$")) { + --currentVersionId; + } + + if (currentVersionId < latestVersionId) { + logger.error("****************************************"); + logger.warn("The new LimboAuth update was found, please update."); + logger.error("https://github.com/Elytrium/LimboAuth/releases/"); + logger.error("****************************************"); + } + } + } catch (IOException e) { + logger.warn("Unable to check for updates.", e); + } + } + + private static String getCleanVersion(String version) { + int indexOf = version.indexOf("-"); + if (indexOf > 0) { + return version.substring(0, indexOf) + "$"; // "$" - Indicates that the version is snapshot + } else { + return version; + } + } +} |