From 85c6a3116cf527f53f463ed76915d2db98a36625 Mon Sep 17 00:00:00 2001 From: Whispering Wind <151555003+ReturnFI@users.noreply.github.com> Date: Tue, 11 Mar 2025 11:04:35 +0330 Subject: [PATCH] Refactor: Replace version.sh with version.py --- core/cli_api.py | 5 +-- core/scripts/hysteria2/version.py | 75 +++++++++++++++++++++++++++++++ core/scripts/hysteria2/version.sh | 57 ----------------------- 3 files changed, 77 insertions(+), 60 deletions(-) create mode 100644 core/scripts/hysteria2/version.py delete mode 100644 core/scripts/hysteria2/version.sh diff --git a/core/cli_api.py b/core/cli_api.py index 3615905..9f20d07 100644 --- a/core/cli_api.py +++ b/core/cli_api.py @@ -502,15 +502,14 @@ def get_services_status() -> dict[str, bool] | None: if res := run_cmd(['bash', Command.SERVICES_STATUS.value]): return json.loads(res) - def show_version() -> str | None: """Displays the currently installed version of the panel.""" - return run_cmd(['bash', Command.VERSION.value, 'show-version']) + return run_cmd(['python3', Command.VERSION.value, 'show-version']) def check_version() -> str | None: """Checks if the current version is up-to-date and displays changelog if not.""" - return run_cmd(['bash', Command.VERSION.value, 'check-version']) + return run_cmd(['python3', Command.VERSION.value, 'check-version']) # endregion # endregion diff --git a/core/scripts/hysteria2/version.py b/core/scripts/hysteria2/version.py new file mode 100644 index 0000000..dafe699 --- /dev/null +++ b/core/scripts/hysteria2/version.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 + +import os +import sys +import requests +from pathlib import Path + +LOCALVERSION = "/etc/hysteria/VERSION" +LATESTVERSION = "https://raw.githubusercontent.com/ReturnFI/Hysteria2/main/VERSION" +LASTESTCHANGE = "https://raw.githubusercontent.com/ReturnFI/Hysteria2/main/changelog" + +def version_greater_equal(version1, version2): + version1_parts = [int(part) for part in version1.strip().split('.')] + version2_parts = [int(part) for part in version2.strip().split('.')] + + max_length = max(len(version1_parts), len(version2_parts)) + version1_parts.extend([0] * (max_length - len(version1_parts))) + version2_parts.extend([0] * (max_length - len(version2_parts))) + + for i in range(max_length): + if version1_parts[i] > version2_parts[i]: + return True + elif version1_parts[i] < version2_parts[i]: + return False + + # If we get here, they're equal + return True + +def check_version(): + try: + with open(LOCALVERSION, 'r') as f: + local_version = f.read().strip() + + latest_version = requests.get(LATESTVERSION).text.strip() + latest_changelog = requests.get(LASTESTCHANGE).text + + print(f"Panel Version: {local_version}") + + if not version_greater_equal(local_version, latest_version): + print(f"Latest Version: {latest_version}") + print(f"{latest_version} Version Change Log:") + print(latest_changelog) + except Exception as e: + print(f"Error checking version: {e}", file=sys.stderr) + sys.exit(1) + +def show_version(): + try: + with open(LOCALVERSION, 'r') as f: + local_version = f.read().strip() + + print(f"Panel Version: {local_version}") + except Exception as e: + print(f"Error showing version: {e}", file=sys.stderr) + sys.exit(1) + +def main(): + if len(sys.argv) != 2: + print(f"Usage: {sys.argv[0]} [check-version|show-version]", file=sys.stderr) + sys.exit(1) + + command = sys.argv[1] + + if command == "check-version": + check_version() + elif command == "show-version": + show_version() + else: + print(f"Usage: {sys.argv[0]} [check-version|show-version]", file=sys.stderr) + sys.exit(1) + + sys.exit(0) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/core/scripts/hysteria2/version.sh b/core/scripts/hysteria2/version.sh deleted file mode 100644 index 2d33b0e..0000000 --- a/core/scripts/hysteria2/version.sh +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/bash - -source /etc/hysteria/core/scripts/path.sh - -version_greater_equal() { - IFS='.' read -r -a local_version_parts <<< "$1" - IFS='.' read -r -a latest_version_parts <<< "$2" - - for ((i=0; i<${#local_version_parts[@]}; i++)); do - if [[ -z ${latest_version_parts[i]} ]]; then - latest_version_parts[i]=0 - fi - - if ((10#${local_version_parts[i]} > 10#${latest_version_parts[i]})); then - return 0 - elif ((10#${local_version_parts[i]} < 10#${latest_version_parts[i]})); then - return 1 - fi - done - - return 0 -} - -check_version() { - local_version=$(cat $LOCALVERSION) - latest_version=$(curl -s $LATESTVERSION) - latest_changelog=$(curl -s $LASTESTCHANGE) - - - if version_greater_equal "$local_version" "$latest_version"; then - echo "Panel Version: $local_version" - else - echo "Panel Version: $local_version" - echo "Latest Version: $latest_version" - echo "$latest_version Version Change Log:" - echo "$latest_changelog" - fi -} - -show_version() { - - local_version=$(cat "$LOCALVERSION") - echo "Panel Version: $local_version" - -} - - -if [ "$1" == "check-version" ]; then - check_version -elif [ "$1" == "show-version" ]; then - show_version -else - echo "Usage: $0 [check-version|show-version]" >&2 - exit 1 -fi - -exit 0 \ No newline at end of file