From 39bb0d50ecbdd6d9669a01c7d2c170ed07291f76 Mon Sep 17 00:00:00 2001
From: Jordan Gong <jordan.gong@protonmail.com>
Date: Sat, 3 Oct 2020 10:58:11 +0800
Subject: Add basic features

- Subscription support
- V2ray-core upgrade support
---
 upgrade.sh | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
 create mode 100755 upgrade.sh

(limited to 'upgrade.sh')

diff --git a/upgrade.sh b/upgrade.sh
new file mode 100755
index 0000000..446899b
--- /dev/null
+++ b/upgrade.sh
@@ -0,0 +1,56 @@
+#!/bin/bash
+
+cd "$(dirname "$0")"
+
+V2RAY_HOME=${V2RAY_HOME:-$(pwd)}
+V2RAY="$V2RAY_HOME/v2ray"
+
+if [[ -f "$V2RAY" ]]
+then
+    CURRENT_VERSION=$("$V2RAY" -version | grep -Po "\b\d+\.\d+\.\d+\b")
+    echo "Current version: v$CURRENT_VERSION"
+fi
+
+echo "Checking latest version..."
+
+LATEST_VERSION=$(curl -H "Accept: application/vnd.github.v3+json" \
+                      -s "https://api.github.com/repos/v2fly/v2ray-core/releases/latest" | \
+                grep '"tag_name":' | \
+                sed -E 's/.*"([^"]+)".*/\1/')
+
+[[ -z "$LATEST_VERSION" ]] \
+&& { echo "Cannot check latest version, check your network." >&2; exit 1; } \
+|| echo "Lasest version found: $LATEST_VERSION"
+
+if [[ -n "$CURRENT_VERSION" && "$LATEST_VERSION" == "v$CURRENT_VERSION" ]]
+then
+    echo "Already up-to-date."
+    exit 0
+fi
+
+declare -a file_list=(
+"v2ray-linux-64.zip"
+"v2ray-linux-64.zip.dgst"
+)
+
+for file in "${file_list[@]}"
+do
+    echo "Downloading $file..."
+    curl -LO "https://github.com/v2fly/v2ray-core/releases/download/$LATEST_VERSION/$file" \
+    || { echo "Cannot download $file, check your network." >&2; exit 1; }
+done
+
+echo "Checking file integrity..."
+sha512sum "${file_list[0]}" | \
+cut -d ' ' -f 1 | grep -f - -q "${file_list[1]}" \
+|| { echo "ERROR" >&2; exit 1; }
+echo "OK"
+
+echo "Extracting ${file_list[0]}..."
+unzip -o "${file_list[0]}" -x "*.sig" "*.json" "systemd/*"
+
+echo "Removing archive..."
+rm -fv "${file_list[@]}"
+
+echo "Upgrade complete!"
+
-- 
cgit v1.2.3