From 55d13c551a9c7f31b63fbb19a1272107ded48c2d Mon Sep 17 00:00:00 2001 From: Peng Ding Date: Mon, 20 Jan 2025 05:27:58 -0600 Subject: [PATCH 1/2] Enhance proxy handling in install scripts for GitHub mirrors --- supplemental/scripts/install-agent.sh | 27 ++++++++--- supplemental/scripts/install-hub.sh | 65 ++++++++++++++++++--------- 2 files changed, 63 insertions(+), 29 deletions(-) diff --git a/supplemental/scripts/install-agent.sh b/supplemental/scripts/install-agent.sh index 4aba731..c654cea 100755 --- a/supplemental/scripts/install-agent.sh +++ b/supplemental/scripts/install-agent.sh @@ -8,12 +8,22 @@ is_openwrt() { cat /etc/os-release | grep -q "OpenWrt" } +# Function to ensure the proxy URL ends with a / +ensure_trailing_slash() { + if [ -n "$1" ] && [ "${1: -1}" != "/" ]; then + echo "$1/" + else + echo "$1" + fi +} + # Define default values PORT=45876 UNINSTALL=false CHINA_MAINLAND=false GITHUB_URL="https://github.com" GITHUB_API_URL="https://api.github.com" +GITHUB_PROXY_URL="https://ghfast.top/" # Default proxy URL KEY="" # Check for help flag first @@ -25,7 +35,8 @@ case "$1" in printf " -k : SSH key (required, or interactive if not provided)\n" printf " -p : Port (default: $PORT)\n" printf " -u : Uninstall Beszel Agent\n" - printf " --china-mirrors : Using GitHub mirror sources to resolve network timeout issues in mainland China\n" + printf " --china-mirrors [URL] : Using GitHub mirror sources to resolve network timeout issues in mainland China\n" + printf " (optional: specify a custom proxy URL, e.g., https://ghfast.top/)\n" printf " -h, --help : Display this help message\n" exit 0 ;; @@ -73,6 +84,10 @@ while [ $# -gt 0 ]; do ;; --china-mirrors) CHINA_MAINLAND=true + if [ "$2" != "" ] && ! echo "$2" | grep -q '^-'; then + GITHUB_PROXY_URL=$(ensure_trailing_slash "$2") + shift + fi ;; *) echo "Invalid option: $1" >&2 @@ -147,14 +162,12 @@ if [ "$UNINSTALL" = true ]; then fi if [ "$CHINA_MAINLAND" = true ]; then - printf "\nConfirmed to use GitHub mirrors (ghp.ci) for download beszel-agent?\nThis helps to install Agent properly in mainland China. (Y/n): " + printf "\nConfirmed to use GitHub mirrors (%s) for download beszel-agent?\nThis helps to install Agent properly in mainland China. (Y/n): " "$GITHUB_PROXY_URL" read USE_MIRROR USE_MIRROR=${USE_MIRROR:-Y} if [ "$USE_MIRROR" = "Y" ] || [ "$USE_MIRROR" = "y" ]; then - GITHUB_URL="https://ghp.ci/https://github.com" - # In China, only github.com is blocked, while api.github.com is not (for now). - # GITHUB_API_URL="https://api.github.com" - echo "Using GitHub Mirror for downloads..." + GITHUB_URL="${GITHUB_PROXY_URL}https://github.com" + echo "Using GitHub Mirror ($GITHUB_PROXY_URL) for downloads..." else echo "GitHub mirrors will not be used for installation." fi @@ -385,7 +398,7 @@ start_service() { procd_set_param env PORT="$PORT" procd_set_param env KEY="$KEY" procd_set_param stdout 1 - procd_set_param stdout 1 + procd_set_param stderr 1 procd_close_instance } diff --git a/supplemental/scripts/install-hub.sh b/supplemental/scripts/install-hub.sh index 113792f..7df1727 100755 --- a/supplemental/scripts/install-hub.sh +++ b/supplemental/scripts/install-hub.sh @@ -2,32 +2,53 @@ # Check if running as root if [ "$(id -u)" != "0" ]; then - if command -v sudo >/dev/null 2>&1; then - exec sudo "$0" "$@" - else - echo "This script must be run as root. Please either:" - echo "1. Run this script as root (su root)" - echo "2. Install sudo and run with sudo" - exit 1 - fi + if command -v sudo >/dev/null 2>&1; then + exec sudo "$0" "$@" + else + echo "This script must be run as root. Please either:" + echo "1. Run this script as root (su root)" + echo "2. Install sudo and run with sudo" + exit 1 + fi fi +# Define default values version=0.0.1 -PORT=8090 # Default port +PORT=8090 # Default port +GITHUB_PROXY_URL="https://ghfast.top/" # Default proxy URL + +# Function to ensure the proxy URL ends with a / +ensure_trailing_slash() { + if [ -n "$1" ] && [ "${1: -1}" != "/" ]; then + echo "$1/" + else + echo "$1" + fi +} + +# Ensure the proxy URL ends with a / +GITHUB_PROXY_URL=$(ensure_trailing_slash "$GITHUB_PROXY_URL") # Read command line options -while getopts ":uhp:" opt; do +while getopts ":uhp:c:" opt; do case $opt in - u) UNINSTALL="true";; - h) printf "Beszel Hub installation script\n\n"; - printf "Usage: ./install-hub.sh [options]\n\n"; - printf "Options: \n" - printf " -u : Uninstall the Beszel Hub\n"; - printf " -p : Specify a port number (default: 8090)\n"; - echo " -h : Display this help message"; - exit 0;; - p) PORT=$OPTARG;; - \?) echo "Invalid option: -$OPTARG"; exit 1;; + u) UNINSTALL="true" ;; + h) + printf "Beszel Hub installation script\n\n" + printf "Usage: ./install-hub.sh [options]\n\n" + printf "Options: \n" + printf " -u : Uninstall the Beszel Hub\n" + printf " -p : Specify a port number (default: 8090)\n" + printf " -c : Use a custom GitHub mirror URL (e.g., https://ghfast.top/)\n" + echo " -h : Display this help message" + exit 0 + ;; + p) PORT=$OPTARG ;; + c) GITHUB_PROXY_URL=$(ensure_trailing_slash "$OPTARG") ;; + \?) + echo "Invalid option: -$OPTARG" + exit 1 + ;; esac done @@ -81,14 +102,14 @@ else fi # Create a dedicated user for the service if it doesn't exist -if ! id -u beszel > /dev/null 2>&1; then +if ! id -u beszel >/dev/null 2>&1; then echo "Creating a dedicated user for the Beszel Hub service..." useradd -M -s /bin/false beszel fi # Download and install the Beszel Hub echo "Downloading and installing the Beszel Hub..." -curl -sL "https://github.com/henrygd/beszel/releases/latest/download/beszel_$(uname -s)_$(uname -m | sed 's/x86_64/amd64/' | sed 's/armv7l/arm/' | sed 's/aarch64/arm64/').tar.gz" | tar -xz -O beszel | tee ./beszel >/dev/null && chmod +x beszel +curl -sL "${GITHUB_PROXY_URL}https://github.com/henrygd/beszel/releases/latest/download/beszel_$(uname -s)_$(uname -m | sed 's/x86_64/amd64/' | sed 's/armv7l/arm/' | sed 's/aarch64/arm64/').tar.gz" | tar -xz -O beszel | tee ./beszel >/dev/null && chmod +x beszel mkdir -p /opt/beszel/beszel_data mv ./beszel /opt/beszel/beszel chown -R beszel:beszel /opt/beszel From ddb4f1c8f8653f4c579f0a8da783f4a1eff20761 Mon Sep 17 00:00:00 2001 From: Peng Ding Date: Mon, 20 Jan 2025 05:38:50 -0600 Subject: [PATCH 2/2] fix compatibility issue with sh. --- supplemental/scripts/install-agent.sh | 9 ++++++--- supplemental/scripts/install-hub.sh | 7 +++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/supplemental/scripts/install-agent.sh b/supplemental/scripts/install-agent.sh index c654cea..f9caa4e 100755 --- a/supplemental/scripts/install-agent.sh +++ b/supplemental/scripts/install-agent.sh @@ -10,8 +10,11 @@ is_openwrt() { # Function to ensure the proxy URL ends with a / ensure_trailing_slash() { - if [ -n "$1" ] && [ "${1: -1}" != "/" ]; then - echo "$1/" + if [ -n "$1" ]; then + case "$1" in + */) echo "$1" ;; + *) echo "$1/" ;; + esac else echo "$1" fi @@ -23,7 +26,7 @@ UNINSTALL=false CHINA_MAINLAND=false GITHUB_URL="https://github.com" GITHUB_API_URL="https://api.github.com" -GITHUB_PROXY_URL="https://ghfast.top/" # Default proxy URL +GITHUB_PROXY_URL="https://ghfast.top/" # Default proxy URL KEY="" # Check for help flag first diff --git a/supplemental/scripts/install-hub.sh b/supplemental/scripts/install-hub.sh index 7df1727..eb71655 100755 --- a/supplemental/scripts/install-hub.sh +++ b/supplemental/scripts/install-hub.sh @@ -19,8 +19,11 @@ GITHUB_PROXY_URL="https://ghfast.top/" # Default proxy URL # Function to ensure the proxy URL ends with a / ensure_trailing_slash() { - if [ -n "$1" ] && [ "${1: -1}" != "/" ]; then - echo "$1/" + if [ -n "$1" ]; then + case "$1" in + */) echo "$1" ;; + *) echo "$1/" ;; + esac else echo "$1" fi