| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- #!/usr/bin/env ash
- # shellcheck shell=dash
- set -euo pipefail
- pkg_manager() {
- local OP="$1" PM=apk
- shift
- if [ -f /etc/gentoo-release ]; then
- PM=emerge
- case "$OP" in
- add)
- OP='-v'
- ;;
- del)
- OP='-C'
- ;;
- esac
- fi
- if [ $# -eq 0 ]; then
- echo "$PM $OP"
- else
- $PM "$OP" "$@"
- fi
- }
- check_distro() {
- if [ -z "$(command -v rc-service)" ]; then
- echo "No OpenRC init-system detected."
- return 1
- fi
- if [ -f /etc/alpine-release ] || [ -f /etc/gentoo-release ]; then
- return 0
- else
- return 1
- fi
- }
- check_if_running_as_root() {
- if [ "$(id -u)" -eq 0 ]; then
- return 0
- else
- echo "error: You must run this script as root!"
- return 1
- fi
- }
- identify_architecture() {
- if [ "$(uname)" != 'Linux' ]; then
- echo "error: This operating system is not supported."
- return 1
- fi
- case "$(uname -m)" in
- 'i386' | 'i686')
- MACHINE='32'
- ;;
- 'amd64' | 'x86_64')
- MACHINE='64'
- ;;
- 'armv5tel')
- MACHINE='arm32-v5'
- ;;
- 'armv6l')
- MACHINE='arm32-v6'
- grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
- ;;
- 'armv7' | 'armv7l')
- MACHINE='arm32-v7a'
- grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
- ;;
- 'armv8' | 'aarch64')
- MACHINE='arm64-v8a'
- ;;
- 'mips')
- MACHINE='mips32'
- ;;
- 'mipsle')
- MACHINE='mips32le'
- ;;
- 'mips64')
- MACHINE='mips64'
- lscpu | grep -q "Little Endian" && MACHINE='mips64le'
- ;;
- 'mips64le')
- MACHINE='mips64le'
- ;;
- 'ppc64')
- MACHINE='ppc64'
- ;;
- 'ppc64le')
- MACHINE='ppc64le'
- ;;
- 'riscv64')
- MACHINE='riscv64'
- ;;
- 's390x')
- MACHINE='s390x'
- ;;
- *)
- echo "error: The architecture is not supported."
- return 1
- ;;
- esac
- if [ ! -f '/etc/os-release' ]; then
- echo "error: Don't use outdated Linux distributions."
- return 1
- fi
- }
- install_dependencies() {
- if [ -n "$(command -v curl)" ]; then
- return
- fi
- if [ -n "$(command -v unzip)" ]; then
- return
- fi
- if [ "$(command -v apk)" ]; then
- echo "Installing required dependencies..."
- pkg_manager add curl unzip # to generalize installation procedure
- else
- echo "error: The script does not support the package manager in this operating system."
- exit 1
- fi
- }
- download_xray() {
- echo "Downloading Xray files..."
- if ! curl -f -L -H 'Cache-Control: no-cache' -o "$ZIP_FILE" "$DOWNLOAD_LINK" -#; then
- echo 'error: Download failed! Please check your network or try again.'
- exit 1
- fi
- if ! curl -f -L -H 'Cache-Control: no-cache' -o "$ZIP_FILE.dgst" "$DOWNLOAD_LINK.dgst" -#; then
- echo 'error: Download failed! Please check your network or try again.'
- exit 1
- fi
- }
- verification_xray() {
- CHECKSUM=$(awk -F '= ' '/256=/ {print $2}' "$ZIP_FILE.dgst")
- LOCALSUM=$(sha256sum "$ZIP_FILE" | awk '{printf $1}')
- if [ "$CHECKSUM" != "$LOCALSUM" ]; then
- echo 'error: SHA256 check failed! Please check your network or try again.'
- return 1
- fi
- }
- decompression() {
- unzip -q "$ZIP_FILE" -d "$TMP_DIRECTORY"
- }
- is_it_running() {
- XRAY_RUNNING='0'
- if [ -n "$(pgrep xray)" ]; then
- rc-service xray stop
- XRAY_RUNNING='1'
- fi
- }
- install_xray() {
- install -m 755 "${TMP_DIRECTORY}xray" "/usr/local/bin/xray"
- install -d /usr/local/share/xray/
- install -m 644 "${TMP_DIRECTORY}geoip.dat" "/usr/local/share/xray/geoip.dat"
- install -m 644 "${TMP_DIRECTORY}geosite.dat" "/usr/local/share/xray/geosite.dat"
- }
- install_confdir() {
- CONFDIR='0'
- if [ ! -d '/usr/local/etc/xray/' ]; then
- install -d /usr/local/etc/xray/
- for BASE in 00_log 01_api 02_dns 03_routing 04_policy 05_inbounds 06_outbounds 07_transport 08_stats 09_reverse; do
- echo '{}' >"/usr/local/etc/xray/$BASE.json"
- done
- CONFDIR='1'
- fi
- }
- install_log() {
- LOG='0'
- if [ ! -d '/var/log/xray/' ]; then
- install -d -m 755 -o 0 -g 0 /var/log/xray/
- install -m 600 -o nobody -g nobody /dev/null /var/log/xray/access.log
- install -m 600 -o nobody -g nobody /dev/null /var/log/xray/error.log
- LOG='1'
- else
- chown 0:0 /var/log/xray/
- chmod 755 /var/log/xray/
- chown nobody:nobody /var/log/xray/*.log
- chmod 600 /var/log/xray/*.log
- fi
- }
- install_startup_service_file() {
- OPENRC='0'
- if [ ! -f '/etc/init.d/xray' ]; then
- mkdir "${TMP_DIRECTORY}init.d/"
- if ! curl -f -L -o "${TMP_DIRECTORY}init.d/xray" https://github.com/XTLS/Xray-install/raw/main/alpinelinux/init.d/xray -sS; then
- echo 'error: Failed to start service file download! Please check your network or try again.'
- exit 1
- fi
- install -m 755 "${TMP_DIRECTORY}init.d/xray" /etc/init.d/xray
- OPENRC='1'
- fi
- }
- information() {
- echo 'installed: /usr/local/bin/xray'
- echo 'installed: /usr/local/share/xray/geoip.dat'
- echo 'installed: /usr/local/share/xray/geosite.dat'
- if [ "$CONFDIR" -eq '1' ]; then
- echo 'installed: /usr/local/etc/xray/00_log.json'
- echo 'installed: /usr/local/etc/xray/01_api.json'
- echo 'installed: /usr/local/etc/xray/02_dns.json'
- echo 'installed: /usr/local/etc/xray/03_routing.json'
- echo 'installed: /usr/local/etc/xray/04_policy.json'
- echo 'installed: /usr/local/etc/xray/05_inbounds.json'
- echo 'installed: /usr/local/etc/xray/06_outbounds.json'
- echo 'installed: /usr/local/etc/xray/07_transport.json'
- echo 'installed: /usr/local/etc/xray/08_stats.json'
- echo 'installed: /usr/local/etc/xray/09_reverse.json'
- fi
- if [ "$LOG" -eq '1' ]; then
- echo 'installed: /var/log/xray/'
- fi
- if [ "$OPENRC" -eq '1' ]; then
- echo 'installed: /etc/init.d/xray'
- fi
- rm -r "$TMP_DIRECTORY"
- echo "removed: $TMP_DIRECTORY"
- echo "You may need to execute a command to remove dependent software: $(pkg_manager del) curl unzip"
- if [ "$XRAY_RUNNING" -eq '1' ]; then
- rc-service xray start
- else
- echo 'Please execute the command: rc-update add xray; rc-service xray start'
- fi
- echo "info: Xray is installed."
- }
- main() {
- check_distro || return 1
- check_if_running_as_root || return 1
- identify_architecture || return 1
- install_dependencies
- TMP_DIRECTORY="$(mktemp -d)/"
- ZIP_FILE="${TMP_DIRECTORY}Xray-linux-$MACHINE.zip"
- DOWNLOAD_LINK="https://github.com/XTLS/Xray-core/releases/latest/download/Xray-linux-$MACHINE.zip"
- download_xray
- verification_xray
- decompression
- is_it_running
- install_xray
- install_confdir
- install_log
- install_startup_service_file || return 1
- information
- }
- main
|