install-release.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #!/usr/bin/env ash
  2. # shellcheck shell=dash
  3. set -euo pipefail
  4. pkg_manager() {
  5. local OP="$1" PM=apk
  6. shift
  7. if [ -f /etc/gentoo-release ]; then
  8. PM=emerge
  9. case "$OP" in
  10. add)
  11. OP='-v'
  12. ;;
  13. del)
  14. OP='-C'
  15. ;;
  16. esac
  17. fi
  18. if [ $# -eq 0 ]; then
  19. echo "$PM $OP"
  20. else
  21. $PM "$OP" "$@"
  22. fi
  23. }
  24. check_distro() {
  25. if [ -z "$(command -v rc-service)" ]; then
  26. echo "No OpenRC init-system detected."
  27. return 1
  28. fi
  29. if [ -f /etc/alpine-release ] || [ -f /etc/gentoo-release ]; then
  30. return 0
  31. else
  32. return 1
  33. fi
  34. }
  35. check_if_running_as_root() {
  36. if [ "$(id -u)" -eq 0 ]; then
  37. return 0
  38. else
  39. echo "error: You must run this script as root!"
  40. return 1
  41. fi
  42. }
  43. identify_architecture() {
  44. if [ "$(uname)" != 'Linux' ]; then
  45. echo "error: This operating system is not supported."
  46. return 1
  47. fi
  48. case "$(uname -m)" in
  49. 'i386' | 'i686')
  50. MACHINE='32'
  51. ;;
  52. 'amd64' | 'x86_64')
  53. MACHINE='64'
  54. ;;
  55. 'armv5tel')
  56. MACHINE='arm32-v5'
  57. ;;
  58. 'armv6l')
  59. MACHINE='arm32-v6'
  60. grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
  61. ;;
  62. 'armv7' | 'armv7l')
  63. MACHINE='arm32-v7a'
  64. grep Features /proc/cpuinfo | grep -qw 'vfp' || MACHINE='arm32-v5'
  65. ;;
  66. 'armv8' | 'aarch64')
  67. MACHINE='arm64-v8a'
  68. ;;
  69. 'mips')
  70. MACHINE='mips32'
  71. ;;
  72. 'mipsle')
  73. MACHINE='mips32le'
  74. ;;
  75. 'mips64')
  76. MACHINE='mips64'
  77. lscpu | grep -q "Little Endian" && MACHINE='mips64le'
  78. ;;
  79. 'mips64le')
  80. MACHINE='mips64le'
  81. ;;
  82. 'ppc64')
  83. MACHINE='ppc64'
  84. ;;
  85. 'ppc64le')
  86. MACHINE='ppc64le'
  87. ;;
  88. 'riscv64')
  89. MACHINE='riscv64'
  90. ;;
  91. 's390x')
  92. MACHINE='s390x'
  93. ;;
  94. *)
  95. echo "error: The architecture is not supported."
  96. return 1
  97. ;;
  98. esac
  99. if [ ! -f '/etc/os-release' ]; then
  100. echo "error: Don't use outdated Linux distributions."
  101. return 1
  102. fi
  103. }
  104. install_dependencies() {
  105. if [ -n "$(command -v curl)" ]; then
  106. return
  107. fi
  108. if [ -n "$(command -v unzip)" ]; then
  109. return
  110. fi
  111. if [ "$(command -v apk)" ]; then
  112. echo "Installing required dependencies..."
  113. pkg_manager add curl unzip # to generalize installation procedure
  114. else
  115. echo "error: The script does not support the package manager in this operating system."
  116. exit 1
  117. fi
  118. }
  119. download_xray() {
  120. echo "Downloading Xray files..."
  121. if ! curl -f -L -H 'Cache-Control: no-cache' -o "$ZIP_FILE" "$DOWNLOAD_LINK" -#; then
  122. echo 'error: Download failed! Please check your network or try again.'
  123. exit 1
  124. fi
  125. if ! curl -f -L -H 'Cache-Control: no-cache' -o "$ZIP_FILE.dgst" "$DOWNLOAD_LINK.dgst" -#; then
  126. echo 'error: Download failed! Please check your network or try again.'
  127. exit 1
  128. fi
  129. }
  130. verification_xray() {
  131. CHECKSUM=$(awk -F '= ' '/256=/ {print $2}' "$ZIP_FILE.dgst")
  132. LOCALSUM=$(sha256sum "$ZIP_FILE" | awk '{printf $1}')
  133. if [ "$CHECKSUM" != "$LOCALSUM" ]; then
  134. echo 'error: SHA256 check failed! Please check your network or try again.'
  135. return 1
  136. fi
  137. }
  138. decompression() {
  139. unzip -q "$ZIP_FILE" -d "$TMP_DIRECTORY"
  140. }
  141. is_it_running() {
  142. XRAY_RUNNING='0'
  143. if [ -n "$(pgrep xray)" ]; then
  144. rc-service xray stop
  145. XRAY_RUNNING='1'
  146. fi
  147. }
  148. install_xray() {
  149. install -m 755 "${TMP_DIRECTORY}xray" "/usr/local/bin/xray"
  150. install -d /usr/local/share/xray/
  151. install -m 644 "${TMP_DIRECTORY}geoip.dat" "/usr/local/share/xray/geoip.dat"
  152. install -m 644 "${TMP_DIRECTORY}geosite.dat" "/usr/local/share/xray/geosite.dat"
  153. }
  154. install_confdir() {
  155. CONFDIR='0'
  156. if [ ! -d '/usr/local/etc/xray/' ]; then
  157. install -d /usr/local/etc/xray/
  158. for BASE in 00_log 01_api 02_dns 03_routing 04_policy 05_inbounds 06_outbounds 07_transport 08_stats 09_reverse; do
  159. echo '{}' >"/usr/local/etc/xray/$BASE.json"
  160. done
  161. CONFDIR='1'
  162. fi
  163. }
  164. install_log() {
  165. LOG='0'
  166. if [ ! -d '/var/log/xray/' ]; then
  167. install -d -m 755 -o 0 -g 0 /var/log/xray/
  168. install -m 600 -o nobody -g nobody /dev/null /var/log/xray/access.log
  169. install -m 600 -o nobody -g nobody /dev/null /var/log/xray/error.log
  170. LOG='1'
  171. else
  172. chown 0:0 /var/log/xray/
  173. chmod 755 /var/log/xray/
  174. chown nobody:nobody /var/log/xray/*.log
  175. chmod 600 /var/log/xray/*.log
  176. fi
  177. }
  178. install_startup_service_file() {
  179. OPENRC='0'
  180. if [ ! -f '/etc/init.d/xray' ]; then
  181. mkdir "${TMP_DIRECTORY}init.d/"
  182. if ! curl -f -L -o "${TMP_DIRECTORY}init.d/xray" https://github.com/XTLS/Xray-install/raw/main/alpinelinux/init.d/xray -sS; then
  183. echo 'error: Failed to start service file download! Please check your network or try again.'
  184. exit 1
  185. fi
  186. install -m 755 "${TMP_DIRECTORY}init.d/xray" /etc/init.d/xray
  187. OPENRC='1'
  188. fi
  189. }
  190. information() {
  191. echo 'installed: /usr/local/bin/xray'
  192. echo 'installed: /usr/local/share/xray/geoip.dat'
  193. echo 'installed: /usr/local/share/xray/geosite.dat'
  194. if [ "$CONFDIR" -eq '1' ]; then
  195. echo 'installed: /usr/local/etc/xray/00_log.json'
  196. echo 'installed: /usr/local/etc/xray/01_api.json'
  197. echo 'installed: /usr/local/etc/xray/02_dns.json'
  198. echo 'installed: /usr/local/etc/xray/03_routing.json'
  199. echo 'installed: /usr/local/etc/xray/04_policy.json'
  200. echo 'installed: /usr/local/etc/xray/05_inbounds.json'
  201. echo 'installed: /usr/local/etc/xray/06_outbounds.json'
  202. echo 'installed: /usr/local/etc/xray/07_transport.json'
  203. echo 'installed: /usr/local/etc/xray/08_stats.json'
  204. echo 'installed: /usr/local/etc/xray/09_reverse.json'
  205. fi
  206. if [ "$LOG" -eq '1' ]; then
  207. echo 'installed: /var/log/xray/'
  208. fi
  209. if [ "$OPENRC" -eq '1' ]; then
  210. echo 'installed: /etc/init.d/xray'
  211. fi
  212. rm -r "$TMP_DIRECTORY"
  213. echo "removed: $TMP_DIRECTORY"
  214. echo "You may need to execute a command to remove dependent software: $(pkg_manager del) curl unzip"
  215. if [ "$XRAY_RUNNING" -eq '1' ]; then
  216. rc-service xray start
  217. else
  218. echo 'Please execute the command: rc-update add xray; rc-service xray start'
  219. fi
  220. echo "info: Xray is installed."
  221. }
  222. main() {
  223. check_distro || return 1
  224. check_if_running_as_root || return 1
  225. identify_architecture || return 1
  226. install_dependencies
  227. TMP_DIRECTORY="$(mktemp -d)/"
  228. ZIP_FILE="${TMP_DIRECTORY}Xray-linux-$MACHINE.zip"
  229. DOWNLOAD_LINK="https://github.com/XTLS/Xray-core/releases/latest/download/Xray-linux-$MACHINE.zip"
  230. download_xray
  231. verification_xray
  232. decompression
  233. is_it_running
  234. install_xray
  235. install_confdir
  236. install_log
  237. install_startup_service_file || return 1
  238. information
  239. }
  240. main