diff --git a/bin/version.bat b/bin/version.bat new file mode 100644 index 00000000..d01a1de5 --- /dev/null +++ b/bin/version.bat @@ -0,0 +1,30 @@ +echo OFF +REM ------------------------------------------------------------------------------ +REM +REM beetRoot Update Check +REM Version: 1.0 +REM +REM ------------------------------------------------------------------------------ + + +pushd %cd% + +REM +REM ROOT path +REM +for %%d in (%~dp0..) do set ROOT=%%~fd +cd %ROOT% + +REM +REM Build the classpath +REM +set CLASSPATH=%ROOT%/lib/* + + + +REM +REM Encode +REM +java -DROOTPATH="%ROOT%" -classpath "%CLASSPATH%" ch.autumo.beetroot.utils.system.Update %* + +popd diff --git a/bin/version.sh b/bin/version.sh new file mode 100755 index 00000000..9334e91e --- /dev/null +++ b/bin/version.sh @@ -0,0 +1,57 @@ +#!/bin/bash + + +#------------------------------------------------------------------------------ +# +# beetRoot Update Check +# Version: 1.0 +# +#------------------------------------------------------------------------------ + + +pushd() { + command pushd "$@" > /dev/null +} +popd() { + command popd > /dev/null +} + +pushd `pwd` + +# +# ROOT path +# +cd "$(dirname "$0")/.." +ROOT=`pwd` + +# +# Base classpath +# +CLASSPATH=${ROOT} + +# +# Dynamically build the classpath +# +COUNT=0 +LIB_CLASSPATH= +for i in `ls ${ROOT}/lib/*.jar` +do + if [ $COUNT -eq 0 ]; then + LIB_CLASSPATH=${i} + else + LIB_CLASSPATH=${LIB_CLASSPATH}:${i} + fi +COUNT=$((c+1)) +done +CLASSPATH=${CLASSPATH}:${LIB_CLASSPATH} + + + +# +# Encode +# +java \ + -cp "${CLASSPATH}" \ + ch.autumo.beetroot.utils.system.Update $* + +popd diff --git a/db/h2/db/test/beetroot.mv.db b/db/h2/db/test/beetroot.mv.db index cf48b5a1..1c656920 100644 Binary files a/db/h2/db/test/beetroot.mv.db and b/db/h2/db/test/beetroot.mv.db differ diff --git a/lib/repo/ch/autumo/beetroot/autumo-beetroot/3.1.0/autumo-beetroot-3.1.0.jar b/lib/repo/ch/autumo/beetroot/autumo-beetroot/3.1.0/autumo-beetroot-3.1.0.jar index 68278013..8ba2d3b5 100644 Binary files a/lib/repo/ch/autumo/beetroot/autumo-beetroot/3.1.0/autumo-beetroot-3.1.0.jar and b/lib/repo/ch/autumo/beetroot/autumo-beetroot/3.1.0/autumo-beetroot-3.1.0.jar differ diff --git a/src/main/java/ch/autumo/beetroot/BeetRootConfigurationManager.java b/src/main/java/ch/autumo/beetroot/BeetRootConfigurationManager.java index 2b84c324..1d0ca2c4 100644 --- a/src/main/java/ch/autumo/beetroot/BeetRootConfigurationManager.java +++ b/src/main/java/ch/autumo/beetroot/BeetRootConfigurationManager.java @@ -46,7 +46,7 @@ import ch.autumo.beetroot.security.SecureApplication; import ch.autumo.beetroot.utils.Helper; import ch.autumo.beetroot.utils.security.Security; -import ch.autumo.beetroot.utils.systen.OS; +import ch.autumo.beetroot.utils.system.OS; /** diff --git a/src/main/java/ch/autumo/beetroot/BeetRootDatabaseManager.java b/src/main/java/ch/autumo/beetroot/BeetRootDatabaseManager.java index cc448d3e..60f29503 100644 --- a/src/main/java/ch/autumo/beetroot/BeetRootDatabaseManager.java +++ b/src/main/java/ch/autumo/beetroot/BeetRootDatabaseManager.java @@ -40,7 +40,7 @@ import ch.autumo.beetroot.utils.database.DB; import ch.autumo.beetroot.utils.database.DBField; import ch.autumo.beetroot.utils.database.H2Url; -import ch.autumo.beetroot.utils.systen.OS; +import ch.autumo.beetroot.utils.system.OS; /** * Database manager. diff --git a/src/main/java/ch/autumo/beetroot/BeetRootWebServer.java b/src/main/java/ch/autumo/beetroot/BeetRootWebServer.java index 65789b29..ed42046d 100644 --- a/src/main/java/ch/autumo/beetroot/BeetRootWebServer.java +++ b/src/main/java/ch/autumo/beetroot/BeetRootWebServer.java @@ -72,7 +72,7 @@ import ch.autumo.beetroot.utils.common.MIME; import ch.autumo.beetroot.utils.database.DB; import ch.autumo.beetroot.utils.security.Security; -import ch.autumo.beetroot.utils.systen.OS; +import ch.autumo.beetroot.utils.system.OS; import ch.autumo.beetroot.utils.web.TwoFA; import ch.autumo.beetroot.utils.web.Web; diff --git a/src/main/java/ch/autumo/beetroot/Session.java b/src/main/java/ch/autumo/beetroot/Session.java index 128a5cb2..e412f1c3 100644 --- a/src/main/java/ch/autumo/beetroot/Session.java +++ b/src/main/java/ch/autumo/beetroot/Session.java @@ -29,7 +29,7 @@ import ch.autumo.beetroot.handler.users.User; import ch.autumo.beetroot.utils.common.LowerCaseList; -import ch.autumo.beetroot.utils.systen.GUIDGenerator; +import ch.autumo.beetroot.utils.system.GUIDGenerator; /** * User session. diff --git a/src/main/java/ch/autumo/beetroot/handler/users/ResetHandler.java b/src/main/java/ch/autumo/beetroot/handler/users/ResetHandler.java index 60635696..a9b2672a 100644 --- a/src/main/java/ch/autumo/beetroot/handler/users/ResetHandler.java +++ b/src/main/java/ch/autumo/beetroot/handler/users/ResetHandler.java @@ -36,7 +36,7 @@ import ch.autumo.beetroot.handler.HandlerResponse; import ch.autumo.beetroot.mailing.MailerFactory; import ch.autumo.beetroot.utils.common.Time; -import ch.autumo.beetroot.utils.systen.GUIDGenerator; +import ch.autumo.beetroot.utils.system.GUIDGenerator; /** * Default login handler. diff --git a/src/main/java/ch/autumo/beetroot/plant/Fertilizer.java b/src/main/java/ch/autumo/beetroot/plant/Fertilizer.java index 12fd8e2a..a678d88a 100644 --- a/src/main/java/ch/autumo/beetroot/plant/Fertilizer.java +++ b/src/main/java/ch/autumo/beetroot/plant/Fertilizer.java @@ -36,7 +36,7 @@ import ch.autumo.beetroot.BeetRootDatabaseManager; import ch.autumo.beetroot.utils.bean.Beans; import ch.autumo.beetroot.utils.database.DBField; -import ch.autumo.beetroot.utils.systen.OS; +import ch.autumo.beetroot.utils.system.OS; /** * Fertilizer for plant. diff --git a/src/main/java/ch/autumo/beetroot/plant/Plant.java b/src/main/java/ch/autumo/beetroot/plant/Plant.java index 7a32dc7f..fc8350d6 100644 --- a/src/main/java/ch/autumo/beetroot/plant/Plant.java +++ b/src/main/java/ch/autumo/beetroot/plant/Plant.java @@ -41,7 +41,7 @@ import ch.autumo.beetroot.BeetRootDatabaseManager; import ch.autumo.beetroot.utils.Helper; import ch.autumo.beetroot.utils.common.Colors; -import ch.autumo.beetroot.utils.systen.OS; +import ch.autumo.beetroot.utils.system.OS; /** * PLANT - beetRoot CRUD Generator. diff --git a/src/main/java/ch/autumo/beetroot/server/BaseServer.java b/src/main/java/ch/autumo/beetroot/server/BaseServer.java index 9712fa74..99d63c94 100644 --- a/src/main/java/ch/autumo/beetroot/server/BaseServer.java +++ b/src/main/java/ch/autumo/beetroot/server/BaseServer.java @@ -69,7 +69,7 @@ import ch.autumo.beetroot.utils.UtilsException; import ch.autumo.beetroot.utils.common.Colors; import ch.autumo.beetroot.utils.security.SSL; -import ch.autumo.beetroot.utils.systen.OS; +import ch.autumo.beetroot.utils.system.OS; import ch.autumo.beetroot.utils.web.Web; /** diff --git a/src/main/java/ch/autumo/beetroot/server/communication/FileTransfer.java b/src/main/java/ch/autumo/beetroot/server/communication/FileTransfer.java index 55f12ef4..129968c4 100644 --- a/src/main/java/ch/autumo/beetroot/server/communication/FileTransfer.java +++ b/src/main/java/ch/autumo/beetroot/server/communication/FileTransfer.java @@ -30,7 +30,7 @@ import ch.autumo.beetroot.BeetRootConfigurationManager; import ch.autumo.beetroot.Constants; import ch.autumo.beetroot.server.action.Download; -import ch.autumo.beetroot.utils.systen.OS; +import ch.autumo.beetroot.utils.system.OS; /** * Client/Server file transfer. diff --git a/src/main/java/ch/autumo/beetroot/server/message/file/PingUploadRequest.java b/src/main/java/ch/autumo/beetroot/server/message/file/PingUploadRequest.java index e2cdbdbf..0ab0117a 100644 --- a/src/main/java/ch/autumo/beetroot/server/message/file/PingUploadRequest.java +++ b/src/main/java/ch/autumo/beetroot/server/message/file/PingUploadRequest.java @@ -19,7 +19,7 @@ import java.io.IOException; -import ch.autumo.beetroot.utils.systen.OS; +import ch.autumo.beetroot.utils.system.OS; /** * Ping upload request. diff --git a/src/main/java/ch/autumo/beetroot/server/modules/file/DummyFileStorage.java b/src/main/java/ch/autumo/beetroot/server/modules/file/DummyFileStorage.java index e92d09d8..30ab2491 100644 --- a/src/main/java/ch/autumo/beetroot/server/modules/file/DummyFileStorage.java +++ b/src/main/java/ch/autumo/beetroot/server/modules/file/DummyFileStorage.java @@ -32,7 +32,7 @@ import ch.autumo.beetroot.BeetRootConfigurationManager; import ch.autumo.beetroot.server.action.Download; import ch.autumo.beetroot.server.modules.FileStorage; -import ch.autumo.beetroot.utils.systen.OS; +import ch.autumo.beetroot.utils.system.OS; /** diff --git a/src/main/java/ch/autumo/beetroot/utils/Helper.java b/src/main/java/ch/autumo/beetroot/utils/Helper.java index 2d0922e5..1d83d92f 100644 --- a/src/main/java/ch/autumo/beetroot/utils/Helper.java +++ b/src/main/java/ch/autumo/beetroot/utils/Helper.java @@ -20,7 +20,7 @@ import com.diogonunes.jcolor.Ansi; import com.diogonunes.jcolor.Attribute; -import ch.autumo.beetroot.utils.systen.OS; +import ch.autumo.beetroot.utils.system.OS; /** * Helper methods. diff --git a/src/main/java/ch/autumo/beetroot/utils/security/PWEncoder.java b/src/main/java/ch/autumo/beetroot/utils/security/PWEncoder.java index 2ae04399..ff637b4d 100644 --- a/src/main/java/ch/autumo/beetroot/utils/security/PWEncoder.java +++ b/src/main/java/ch/autumo/beetroot/utils/security/PWEncoder.java @@ -119,7 +119,7 @@ public static void main(String args[]) { private static final class Help { private static final String SHELL_EXT = SystemUtils.IS_OS_UNIX ? "sh" : "bat"; private static final String USAGEA = Colors.darkYellow("pwencoder."+SHELL_EXT+" \"\""); - private static final String USAGE0 = Colors.darkYellow("pwencoder.sh 1 \"mySecretPass\""); + private static final String USAGE0 = Colors.darkYellow("pwencoder."+SHELL_EXT+" \"mySecretPass\""); public static final String TEXT = "" + LINE_SEPARATOR + "" + LINE_SEPARATOR + diff --git a/src/main/java/ch/autumo/beetroot/utils/security/Security.java b/src/main/java/ch/autumo/beetroot/utils/security/Security.java index 2b583826..7642b2be 100644 --- a/src/main/java/ch/autumo/beetroot/utils/security/Security.java +++ b/src/main/java/ch/autumo/beetroot/utils/security/Security.java @@ -38,7 +38,7 @@ import ch.autumo.beetroot.security.SecureApplication; import ch.autumo.beetroot.security.password.PasswordHashProvider; import ch.autumo.beetroot.utils.UtilsException; -import ch.autumo.beetroot.utils.systen.GUIDGenerator; +import ch.autumo.beetroot.utils.system.GUIDGenerator; /** diff --git a/src/main/java/ch/autumo/beetroot/utils/systen/GUIDGenerator.java b/src/main/java/ch/autumo/beetroot/utils/system/GUIDGenerator.java similarity index 99% rename from src/main/java/ch/autumo/beetroot/utils/systen/GUIDGenerator.java rename to src/main/java/ch/autumo/beetroot/utils/system/GUIDGenerator.java index 726ddea3..e2bbef1c 100644 --- a/src/main/java/ch/autumo/beetroot/utils/systen/GUIDGenerator.java +++ b/src/main/java/ch/autumo/beetroot/utils/system/GUIDGenerator.java @@ -15,7 +15,7 @@ * limitations under the License. * */ -package ch.autumo.beetroot.utils.systen; +package ch.autumo.beetroot.utils.system; import java.net.InetAddress; import java.net.NetworkInterface; diff --git a/src/main/java/ch/autumo/beetroot/utils/systen/OS.java b/src/main/java/ch/autumo/beetroot/utils/system/OS.java similarity index 99% rename from src/main/java/ch/autumo/beetroot/utils/systen/OS.java rename to src/main/java/ch/autumo/beetroot/utils/system/OS.java index bce4707a..2d321b5c 100644 --- a/src/main/java/ch/autumo/beetroot/utils/systen/OS.java +++ b/src/main/java/ch/autumo/beetroot/utils/system/OS.java @@ -15,7 +15,7 @@ * limitations under the License. * */ -package ch.autumo.beetroot.utils.systen; +package ch.autumo.beetroot.utils.system; import java.io.IOException; import java.nio.file.Files; diff --git a/src/main/java/ch/autumo/beetroot/utils/system/Update.java b/src/main/java/ch/autumo/beetroot/utils/system/Update.java new file mode 100644 index 00000000..bd12f590 --- /dev/null +++ b/src/main/java/ch/autumo/beetroot/utils/system/Update.java @@ -0,0 +1,167 @@ +/** + * + * Copyright (c) 2024 autumo Ltd. Switzerland, Michael Gasche + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package ch.autumo.beetroot.utils.system; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +import org.json.JSONObject; + +import ch.autumo.beetroot.BeetRootConfigurationManager; +import ch.autumo.beetroot.utils.Helper; +import ch.autumo.beetroot.utils.common.Colors; + + +/** + * Update Check. + */ +public class Update { + + private static final String HEAD; + private static final String GITHUB_API_URL; + private static final String GITHUB_REL_URL; + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + static { + try { + BeetRootConfigurationManager.getInstance().initialize(); + } catch (Exception e) { + System.err.println(Colors.red("Couldn't initialize configuration 'cfg/beetroot.cfg' !")); + System.err.println(Colors.red("ERROR") + ": " + e.getMessage()); + e.printStackTrace(); + Helper.fatalExit(); + } + HEAD = + "" + LINE_SEPARATOR + + "" + LINE_SEPARATOR + + Colors.darkCyan("Update Check") + LINE_SEPARATOR + + "------------" + LINE_SEPARATOR; + GITHUB_API_URL = BeetRootConfigurationManager.getInstance().getString("github_api_url", "https://api.github.com/repos/autumoswitzerland/autumo-beetroot/releases/latest"); + GITHUB_REL_URL = BeetRootConfigurationManager.getInstance().getString("github_rel_url", "https://github.com/autumoswitzerland/autumo-beetroot/releases"); + } + + /** + * Fetches the latest release version from the GitHub repository. + * + * @return the latest release version as a string + * @throws Exception if an error occurs during the request + */ + public static String getLatestRelease() throws Exception { + // Create HTTP client + HttpClient client = HttpClient.newHttpClient(); + + // Create the request + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI(GITHUB_API_URL)) + .header("Accept", "application/vnd.github.v3+json") // Set GitHub API version + .GET() + .build(); + + // Send the request + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + + // Check if the request was successful + if (response.statusCode() != 200) { + throw new RuntimeException("Failed to fetch latest release. HTTP status: " + response.statusCode()); + } + + // Parse the JSON response + JSONObject jsonResponse = new JSONObject(response.body()); + + // Get the 'tag_name' field, which usually holds the release version + return jsonResponse.getString("tag_name"); + } + + /** + * Checks if the latest version is newer than the current version. + * + * @param latestRelease the latest release version (e.g., "1.2.0") + * @param currentReleaseVersion the current release version (e.g., "1.0.0") + * @return true if latestRelease is newer, false otherwise + */ + public static boolean isNewerVersion2(String latestRelease, String currentReleaseVersion) { + // Split versions into major, minor, and patch numbers + final String latestParts[] = latestRelease.split("\\."); + final String currentParts[] = currentReleaseVersion.split("\\."); + int length = Math.max(latestParts.length, currentParts.length); + for (int i = 0; i < length; i++) { + int latest = i < latestParts.length ? Integer.parseInt(latestParts[i]) : 0; + int current = i < currentParts.length ? Integer.parseInt(currentParts[i]) : 0; + if (latest > current) { + return true; // A newer version is available + } else if (latest < current) { + return false; // Current version is newer + } + } + // Versions are equal + return false; + } + + public static int isNewerVersion(String latestRelease, String currentReleaseVersion) { + // Remove leading 'v' if present + latestRelease = latestRelease.startsWith("v") ? latestRelease.substring(1) : latestRelease; + currentReleaseVersion = currentReleaseVersion.startsWith("v") ? currentReleaseVersion.substring(1) : currentReleaseVersion; + + // Split versions into major, minor, and patch + String[] v1Parts = latestRelease.split("\\."); + String[] v2Parts = currentReleaseVersion.split("\\."); + + // Compare major, minor, and patch numbers + for (int i = 0; i < Math.max(v1Parts.length, v2Parts.length); i++) { + int v1 = i < v1Parts.length ? Integer.parseInt(v1Parts[i]) : 0; + int v2 = i < v2Parts.length ? Integer.parseInt(v2Parts[i]) : 0; + + if (v1 != v2) { + return v1 - v2; // If different, return comparison result + } + } + + return 0; // If versions are equal + } + + public static void main(String args[]) { + try { + //String currentReleaseVersion = "v2.0.1"; + String currentReleaseVersion = "v" + BeetRootConfigurationManager.getAppVersion(); + + System.out.println(HEAD); + + System.out.println("Current release: " + Colors.cyan(currentReleaseVersion)); + String latestRelease = getLatestRelease(); + System.out.println("Latest release: " + Colors.cyan(latestRelease)); + System.out.println(""); + + int res = isNewerVersion(latestRelease, currentReleaseVersion); + + if (res > 0) { + System.out.println("A new release is available: " + Colors.green(latestRelease)); + System.out.println("Download new release here: " + Colors.green(GITHUB_REL_URL)); + } else if (res < 0) { + System.out.println("You are using a newer version " + Colors.darkYellow(currentReleaseVersion) + " than the latest release!"); + } else { + System.out.println(Colors.green("You are using the latest release.")); + } + } catch (Exception e) { + System.err.println(Colors.red("ERROR") + ": " + e.getMessage()); + e.printStackTrace(); + } + System.out.println(""); + } + +} diff --git a/src/main/java/ch/autumo/beetroot/utils/system/package-info.java b/src/main/java/ch/autumo/beetroot/utils/system/package-info.java new file mode 100644 index 00000000..52603203 --- /dev/null +++ b/src/main/java/ch/autumo/beetroot/utils/system/package-info.java @@ -0,0 +1,4 @@ +/** + * System tools. + */ +package ch.autumo.beetroot.utils.system; diff --git a/src/main/java/ch/autumo/beetroot/utils/systen/package-info.java b/src/main/java/ch/autumo/beetroot/utils/systen/package-info.java deleted file mode 100644 index 941d4cd8..00000000 --- a/src/main/java/ch/autumo/beetroot/utils/systen/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * System tools. - */ -package ch.autumo.beetroot.utils.systen; diff --git a/src/main/java/ch/autumo/beetroot/utils/web/TwoFA.java b/src/main/java/ch/autumo/beetroot/utils/web/TwoFA.java index 014f247b..72d3bcef 100644 --- a/src/main/java/ch/autumo/beetroot/utils/web/TwoFA.java +++ b/src/main/java/ch/autumo/beetroot/utils/web/TwoFA.java @@ -35,7 +35,7 @@ import ch.autumo.beetroot.BeetRootConfigurationManager; import ch.autumo.beetroot.Constants; import ch.autumo.beetroot.utils.UtilsException; -import ch.autumo.beetroot.utils.systen.OS; +import ch.autumo.beetroot.utils.system.OS; import de.taimos.totp.TOTP; /**