Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tune installation script #112

Merged
merged 12 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 68 additions & 125 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,131 +1,88 @@
#!/bin/bash

# shellcheck disable=2034,2059,2164
true
# see https://github.com/koalaman/shellcheck/wiki/Directive

# This script will do the following to install RustDesk Server Pro
# 1. Install some dependencies
# 2. Setup UFW firewall if available
# 3. Create 2 folders /var/lib/rustdesk-server and /var/log/rustdesk-server ("$RUSTDESK_LOG_DIR")
# 3. Create 2 folders /var/lib/rustdesk-server and /var/log/rustdesk-server ("$RUSTDESK_INSTALL_DIR" and "$RUSTDESK_LOG_DIR")
# 4. Download and extract RustDesk Pro Services to the above folder
# 5. Create systemd services for hbbs and hbbr
# 6. If you choose Domain, it will install Nginx and Certbot, allowing the API to be available on port 443 (https) and get an SSL certificate over port 80, it is automatically renewed

##################################################################################################################

if [[ "$EUID" -ne 0 ]]
then
echo "Sorry, you are not root. You now have two options:"
echo
echo "1. Use SUDO directly:"
echo " a) :~$ sudo bash install.sh"
echo
echo "2. Become ROOT and then type your command:"
echo " a) :~$ sudo -i"
echo " b) :~# bash install.sh"
echo
echo "More information can be found here: https://unix.stackexchange.com/a/3064"
exit 1
fi

# Identify OS
if [ -f /etc/os-release ]
then
# freedesktop.org and systemd
# shellcheck source=/dev/null
source /etc/os-release
OS=$NAME
VER=$VERSION_ID
UPSTREAM_ID=${ID_LIKE,,}

# Fallback to ID_LIKE if ID was not 'ubuntu' or 'debian'
if [ "${UPSTREAM_ID}" != "debian" ] && [ "${UPSTREAM_ID}" != "ubuntu" ]
then
UPSTREAM_ID="$(echo "${ID_LIKE,,}" | sed s/\"//g | cut -d' ' -f1)"
fi

elif type lsb_release >/dev/null 2>&1
then
# linuxbase.org
OS=$(lsb_release -si)
VER=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]
# We need curl to fetch the lib
# There are the package managers for different OS:
# osInfo[/etc/redhat-release]=yum
# osInfo[/etc/arch-release]=pacman
# osInfo[/etc/gentoo-release]=emerge
# osInfo[/etc/SuSE-release]=zypp
# osInfo[/etc/debian_version]=apt-get
# osInfo[/etc/alpine-release]=apk
NEEDED_DEPS=(curl whiptail)
if [ -x "$(command -v apt-get)" ]
then
# For some versions of Debian/Ubuntu without lsb_release command
# shellcheck source=/dev/null
source /etc/os-release
OS=$DISTRIB_ID
VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]
sudo apt-get install "${NEEDED_DEPS[@]}" -y
elif [ -x "$(command -v apk)" ]
then
# Older Debian, Ubuntu, etc.
OS=Debian
VER=$(cat /etc/debian_version)
elif [ -f /etc/SuSE-release ]
sudo apk add --no-cache "${NEEDED_DEPS[@]}"
elif [ -x "$(command -v dnf)" ]
then
# Older SuSE, etc.
OS=SuSE
VER=$(cat /etc/SuSE-release)
elif [ -f /etc/redhat-release ]
sudo dnf install "${NEEDED_DEPS[@]}"
elif [ -x "$(command -v zypper)" ]
then
# Older Red Hat, CentOS, etc.
OS=RedHat
VER=$(cat /etc/redhat-release)
else
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
OS=$(uname -s)
VER=$(uname -r)
fi

# Setup prereqs for server
# Common named prereqs
PREREQ=(curl wget unzip tar whiptail)
PREREQDEB=(dnsutils ufw)
PREREQRPM=(bind-utils)
PREREQARCH=(bind)

echo "Installing prerequisites"
if [ "${ID}" = "debian" ] || [ "$OS" = "Ubuntu" ] || [ "$OS" = "Debian" ] || [ "${UPSTREAM_ID}" = "ubuntu" ] || [ "${UPSTREAM_ID}" = "debian" ]
sudo zypper install "${NEEDED_DEPS[@]}"
elif [ -x "$(command -v pacman)" ]
then
apt-get update
apt-get install -y "${PREREQ[@]}" "${PREREQDEB[@]}"
elif [ "$OS" = "CentOS" ] || [ "$OS" = "RedHat" ] || [ "${UPSTREAM_ID}" = "rhel" ] || [ "${OS}" = "Almalinux" ] || [ "${UPSTREAM_ID}" = "Rocky*" ]
sudo pacman -S install "${NEEDED_DEPS[@]}"
elif [ -x "$(command -v yum)" ]
then
# openSUSE 15.4 fails to run the relay service and hangs waiting for it
# Needs more work before it can be enabled
# || [ "${UPSTREAM_ID}" = "suse" ]
yum update -y
yum install -y "${PREREQ[@]}" "${PREREQRPM[@]}" # git
elif [ "${ID}" = "arch" ] || [ "${UPSTREAM_ID}" = "arch" ]
sudo yum install "${NEEDED_DEPS[@]}"
elif [ -x "$(command -v emerge)" ]
then
pacman -Syu
pacman -S "${PREREQ[@]}" "${PREREQARCH[@]}"
sudo emerge -av "${NEEDED_DEPS[@]}"
else
echo "Unsupported OS!"
# Here you could ask the user for permission to try and install anyway
# If they say yes, then do the install
# If they say no, exit the script
echo "FAILED TO INSTALL PACKAGE! Package manager not found. You must manually install:" "${NEEDED_DEPS[@]}"
exit 1
fi

# Download the lib file
if ! curl -fSL https://raw.githubusercontent.com/rustdesk/rustdesk-server-pro/main/lib.sh -o lib.sh
then
echo "Failed to download the lib.sh file. Please try again"
exit 1
fi

# We need to source directly from the Github repo to be able to use the functions here
# shellcheck disable=2034,2059,2164
true
SCRIPT_NAME="Install script"
export SCRIPT_NAME
# shellcheck source=lib.sh
source lib.sh
source <(curl -sL https://raw.githubusercontent.com/rustdesk/rustdesk-server-pro/main/lib.sh)
# see https://github.com/koalaman/shellcheck/wiki/Directive
unset SCRIPT_NAME

##################################################################################################################

# This must run as root
root_check

# We need the WAN IP
get_wanip4

# Install needed dependencies
install_linux_package unzip
install_linux_package tar
install_linux_package dnsutils
install_linux_package ufw
if ! install_linux_package bind9-utils
then
install_linux_package bind-utils
fi
if ! install_linux_package bind9
then
install_linux_package bind
fi

# Select user for installation
msg_box "Rustdesk needs to be installed as root, but you can still do some parts as an unprivileged user.
msg_box "Rustdesk can be installed as an unprivileged user, but we need root for everything else.
Running with an unprivileged user enhances security, and is recommended."

if yesno_box_yes "Do you want to use an unprivileged user where it's possible?"
if yesno_box_yes "Do you want to use an unprivileged user for Rustdesk?"
then
while :
do
Expand All @@ -149,6 +106,7 @@
# Output debugging info if $DEBUG set
if [ "$DEBUG" = "true" ]
then
identify_os
print_text_in_color "$ICyan" "OS: $OS"
print_text_in_color "$ICyan" "VER: $VER"
print_text_in_color "$ICyan" "UPSTREAM_ID: $UPSTREAM_ID"
Expand All @@ -174,7 +132,7 @@
mkdir -p "$RUSTDESK_INSTALL_DIR"
if [ -d "$RUSTDESK_INSTALL_DIR" ]
then
cd "$RUSTDESK_INSTALL_DIR"

Check warning on line 135 in install.sh

View workflow job for this annotation

GitHub Actions / Shellcheck testing

[shellcheck] reported by reviewdog 🐶 Use 'cd ... || exit' or 'cd ... || return' in case cd fails. [SC2164](https://github.com/koalaman/shellcheck/wiki/SC2164) Raw Output: ./install.sh:135:13:warning:Use 'cd ... || exit' or 'cd ... || return' in case cd fails. [SC2164](https://github.com/koalaman/shellcheck/wiki/SC2164)

Check failure on line 135 in install.sh

View workflow job for this annotation

GitHub Actions / Shellcheck testing

[shellcheck (suggestion)] reported by reviewdog 🐶 Raw Output: install.sh:135:- cd "$RUSTDESK_INSTALL_DIR" install.sh:135:+ cd "$RUSTDESK_INSTALL_DIR" || exit
else
msg_box "It seems like the installation folder wasn't created, we can't continue.
Please report this to: https://github.com/rustdesk/rustdesk-server-pro/issues"
Expand Down Expand Up @@ -406,41 +364,27 @@
exit 1
fi


print_text_in_color "$IGreen" "Installing Nginx"
if [ "${ID}" = "debian" ] || [ "$OS" = "Ubuntu" ] || [ "$OS" = "Debian" ] || [ "${UPSTREAM_ID}" = "ubuntu" ] || [ "${UPSTREAM_ID}" = "debian" ]
then
if yesno_box_yes "We use Certbot to generate the free TLS certificate from Let's Encrypt.
# Install packages
print_text_in_color "$IGreen" "Installing Nginx and Cerbot..."
if yesno_box_yes "We use Certbot to generate the free TLS certificate from Let's Encrypt.
The default behavior of installing Certbot is to use the snap package which auto updates, and provides the latest version of Certbot. If you don't like snap packages, you can opt out now and we'll use regular (old) deb packages instead.

Do you want to install Certbot with snap? (recommended)"
then
install_linux_package nginx
if ! install_linux_package snapd
then
apt-get install nginx -y
apt-get install snapd -y
snap install certbot --classic
print_text_in_color "$IRed" "Sorry, snapd wasn't found on your system, reverting to python-certbot."
install_linux_package python3-certbot-nginx
else
apt-get install nginx -y
apt-get install python3-certbot-nginx -y
snap install certbot --classic
fi
elif [ "$OS" = "CentOS" ] || [ "$OS" = "RedHat" ] || [ "${UPSTREAM_ID}" = "rhel" ] || [ "${OS}" = "Almalinux" ] || [ "${UPSTREAM_ID}" = "Rocky*" ]
then
# openSUSE 15.4 fails to run the relay service and hangs waiting for it
# Needs more work before it can be enabled
# || [ "${UPSTREAM_ID}" = "suse" ]
yum -y install nginx
yum -y install python3-certbot-nginx
elif [ "${ID}" = "arch" ] || [ "${UPSTREAM_ID}" = "arch" ]
then
pacman -S install nginx
pacman -S install python3-certbot-nginx
else
msg_box "Sorry, your OS is unsupported"
if ! yesno_box_no "It might work anyway though... Do you want to give it a shot?"
then
exit 1
fi
install_linux_package nginx
install_linux_package python3-certbot-nginx
fi

# Add Nginx config
if [ ! -f "/etc/nginx/sites-available/rustdesk.conf" ]
then
rm -f "/etc/nginx/sites-available/rustdesk.conf"
Expand Down Expand Up @@ -518,4 +462,3 @@
print_text_in_color "$IGreen" "Cleaning up..."
rm -f rustdesk-server-linux-"${ACTUAL_TAR_NAME}".zip
rm -rf "${ACTUAL_TAR_NAME}"
rm -f lib.sh
31 changes: 29 additions & 2 deletions lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ RUSTDESK_INSTALL_DIR=/var/lib/rustdesk-server
RUSTDESK_LOG_DIR=/var/log/rustdesk-server
# OS
ARCH=$(uname -m)
# Network
WANIP4=$(curl -s -k -m 5 -4 https://api64.ipify.org)
get_wanip4() {
WANIP4=$(curl -s -k -m 5 -4 https://api64.ipify.org)
}
# Whiptail menus
TITLE="RustDesk - $(date +%Y)"
[ -n "$SCRIPT_NAME" ] && TITLE+=" - $SCRIPT_NAME"
Expand All @@ -22,6 +23,32 @@ MENU_GUIDE="Navigate with the [ARROW] keys and confirm by pressing [ENTER]. Canc

############ Functions

is_root() {
if [[ "$EUID" -ne 0 ]]
then
return 1
else
return 0
fi
}

root_check() {
if ! is_root
then
msg_box "Sorry, you are not root. You now have two options:

1. Use SUDO directly:
a) :~$ sudo bash name-of-script.sh

2. Become ROOT and then type your command:
a) :~$ sudo -i
b) :~# bash name-of-script.sh

More information can be found here: https://unix.stackexchange.com/a/3064"
exit 1
fi
}

print_text_in_color() {
printf "%b%s%b\n" "$1" "$2" "$Color_Off"
}
Expand Down