Advertisement
R4kashy

Untitled

May 25th, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.54 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -e
  4.  
  5. ######################################################################################
  6. # #
  7. # Project 'pterodactyl-installer' #
  8. # #
  9. # Copyright (C) 2018 - 2023, Vilhelm Prytz, <vilhelm@prytznet.se> #
  10. # #
  11. # This program is free software: you can redistribute it and/or modify #
  12. # it under the terms of the GNU General Public License as published by #
  13. # the Free Software Foundation, either version 3 of the License, or #
  14. # (at your option) any later version. #
  15. # #
  16. # This program is distributed in the hope that it will be useful, #
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of #
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  19. # GNU General Public License for more details. #
  20. # #
  21. # You should have received a copy of the GNU General Public License #
  22. # along with this program. If not, see <https://www.gnu.org/licenses/>. #
  23. # #
  24. # https://github.com/pterodactyl-installer/pterodactyl-installer/blob/master/LICENSE #
  25. # #
  26. # This script is not associated with the official Pterodactyl Project. #
  27. # https://github.com/pterodactyl-installer/pterodactyl-installer #
  28. # #
  29. ######################################################################################
  30.  
  31. # ------------------ Variables ----------------- #
  32.  
  33. # Versioning
  34. export GITHUB_SOURCE=${GITHUB_SOURCE:-master}
  35. export SCRIPT_RELEASE=${SCRIPT_RELEASE:-canary}
  36.  
  37. # Pterodactyl versions
  38. export PTERODACTYL_PANEL_VERSION=""
  39. export PTERODACTYL_WINGS_VERSION=""
  40.  
  41. # Path (export everything that is possible, doesn't matter that it exists already)
  42. export PATH="$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
  43.  
  44. # OS
  45. export OS=""
  46. export OS_VER_MAJOR=""
  47. export CPU_ARCHITECTURE=""
  48. export ARCH=""
  49. export SUPPORTED=false
  50.  
  51. # download URLs
  52. export PANEL_DL_URL="https://github.com/pterodactyl/panel/releases/latest/download/panel.tar.gz"
  53. export WINGS_DL_BASE_URL="https://github.com/pterodactyl/wings/releases/latest/download/wings_linux_"
  54. export MARIADB_URL="https://downloads.mariadb.com/MariaDB/mariadb_repo_setup"
  55. export GITHUB_BASE_URL=${GITHUB_BASE_URL:-"https://raw.githubusercontent.com/pterodactyl-installer/pterodactyl-installer"}
  56. export GITHUB_URL="$GITHUB_BASE_URL/$GITHUB_SOURCE"
  57.  
  58. # Colors
  59. COLOR_YELLOW='\033[1;33m'
  60. COLOR_GREEN='\033[0;32m'
  61. COLOR_RED='\033[0;31m'
  62. COLOR_NC='\033[0m'
  63.  
  64. # email input validation regex
  65. email_regex="^(([A-Za-z0-9]+((\.|\-|\_|\+)?[A-Za-z0-9]?)*[A-Za-z0-9]+)|[A-Za-z0-9]+)@(([A-Za-z0-9]+)+((\.|\-|\_)?([A-Za-z0-9]+)+)*)+\.([A-Za-z]{2,})+$"
  66.  
  67. # Charset used to generate random passwords
  68. password_charset='A-Za-z0-9!"#%&()*+,-./:;<=>?@[\]^_`{|}~'
  69.  
  70. # --------------------- Lib -------------------- #
  71.  
  72. lib_loaded() {
  73. return 0
  74. }
  75.  
  76. # -------------- Visual functions -------------- #
  77.  
  78. output() {
  79. echo -e "* $1"
  80. }
  81.  
  82. success() {
  83. echo ""
  84. output "${COLOR_GREEN}SUCCESS${COLOR_NC}: $1"
  85. echo ""
  86. }
  87.  
  88. error() {
  89. echo ""
  90. echo -e "* ${COLOR_RED}ERROR${COLOR_NC}: $1" 1>&2
  91. echo ""
  92. }
  93.  
  94. warning() {
  95. echo ""
  96. output "${COLOR_YELLOW}WARNING${COLOR_NC}: $1"
  97. echo ""
  98. }
  99.  
  100. print_brake() {
  101. for ((n = 0; n < $1; n++)); do
  102. echo -n "#"
  103. done
  104. echo ""
  105. }
  106.  
  107. print_list() {
  108. print_brake 30
  109. for word in $1; do
  110. output "$word"
  111. done
  112. print_brake 30
  113. echo ""
  114. }
  115.  
  116. hyperlink() {
  117. echo -e "\e]8;;${1}\a${1}\e]8;;\a"
  118. }
  119.  
  120. # First argument is wings / panel / neither
  121. welcome() {
  122. get_latest_versions
  123.  
  124. print_brake 70
  125. output "Pterodactyl panel installation script @ $SCRIPT_RELEASE"
  126. output ""
  127. output "Copyright (C) 2018 - 2023, Vilhelm Prytz, <vilhelm@prytznet.se>"
  128. output "https://github.com/pterodactyl-installer/pterodactyl-installer"
  129. output ""
  130. output "This script is not associated with the official Pterodactyl Project."
  131. output ""
  132. output "Running $OS version $OS_VER."
  133. if [ "$1" == "panel" ]; then
  134. output "Latest pterodactyl/panel is $PTERODACTYL_PANEL_VERSION"
  135. elif [ "$1" == "wings" ]; then
  136. output "Latest pterodactyl/wings is $PTERODACTYL_WINGS_VERSION"
  137. fi
  138. print_brake 70
  139. }
  140.  
  141. # ---------------- Lib functions --------------- #
  142.  
  143. get_latest_release() {
  144. curl -sL "https://api.github.com/repos/$1/releases/latest" | # Get latest release from GitHub api
  145. grep '"tag_name":' | # Get tag line
  146. sed -E 's/.*"([^"]+)".*/\1/' # Pluck JSON value
  147. }
  148.  
  149. get_latest_versions() {
  150. output "Retrieving release information..."
  151. PTERODACTYL_PANEL_VERSION=$(get_latest_release "pterodactyl/panel")
  152. PTERODACTYL_WINGS_VERSION=$(get_latest_release "pterodactyl/wings")
  153. }
  154.  
  155. update_lib_source() {
  156. GITHUB_URL="$GITHUB_BASE_URL/$GITHUB_SOURCE"
  157. rm -rf /tmp/lib.sh
  158. curl -sSL -o /tmp/lib.sh "$GITHUB_URL"/lib/lib.sh
  159. # shellcheck source=lib/lib.sh
  160. source /tmp/lib.sh
  161. }
  162.  
  163. run_installer() {
  164. bash <(curl -sSL "$GITHUB_URL/installers/$1.sh")
  165. }
  166.  
  167. run_ui() {
  168. bash <(curl -sSL "$GITHUB_URL/ui/$1.sh")
  169. }
  170.  
  171. array_contains_element() {
  172. local e match="$1"
  173. shift
  174. for e; do [[ "$e" == "$match" ]] && return 0; done
  175. return 1
  176. }
  177.  
  178. valid_email() {
  179. [[ $1 =~ ${email_regex} ]]
  180. }
  181.  
  182. invalid_ip() {
  183. ip route get "$1" >/dev/null 2>&1
  184. echo $?
  185. }
  186.  
  187. gen_passwd() {
  188. local length=$1
  189. local password=""
  190. while [ ${#password} -lt "$length" ]; do
  191. password=$(echo "$password""$(head -c 100 /dev/urandom | LC_ALL=C tr -dc "$password_charset")" | fold -w "$length" | head -n 1)
  192. done
  193. echo "$password"
  194. }
  195.  
  196. # -------------------- MYSQL ------------------- #
  197.  
  198. create_db_user() {
  199. local db_user_name="$1"
  200. local db_user_password="$2"
  201. local db_host="${3:-127.0.0.1}"
  202.  
  203. output "Creating database user $db_user_name..."
  204.  
  205. mysql -u root -e "CREATE USER '$db_user_name'@'$db_host' IDENTIFIED BY '$db_user_password';"
  206. mysql -u root -e "FLUSH PRIVILEGES;"
  207.  
  208. output "Database user $db_user_name created"
  209. }
  210.  
  211. grant_all_privileges() {
  212. local db_name="$1"
  213. local db_user_name="$2"
  214. local db_host="${3:-127.0.0.1}"
  215.  
  216. output "Granting all privileges on $db_name to $db_user_name..."
  217.  
  218. mysql -u root -e "GRANT ALL PRIVILEGES ON $db_name.* TO '$db_user_name'@'$db_host' WITH GRANT OPTION;"
  219. mysql -u root -e "FLUSH PRIVILEGES;"
  220.  
  221. output "Privileges granted"
  222.  
  223. }
  224.  
  225. create_db() {
  226. local db_name="$1"
  227. local db_user_name="$2"
  228. local db_host="${3:-127.0.0.1}"
  229.  
  230. output "Creating database $db_name..."
  231.  
  232. mysql -u root -e "CREATE DATABASE $db_name;"
  233. grant_all_privileges "$db_name" "$db_user_name" "$db_host"
  234.  
  235. output "Database $db_name created"
  236. }
  237.  
  238. # --------------- Package Manager -------------- #
  239.  
  240. # Argument for quite mode
  241. update_repos() {
  242. local args=""
  243. [[ $1 == true ]] && args="-qq"
  244. case "$OS" in
  245. ubuntu | debian)
  246. apt-get -y $args update
  247. ;;
  248. *)
  249. # Do nothing as AlmaLinux and RockyLinux update metadata before installing packages.
  250. ;;
  251. esac
  252. }
  253.  
  254. # First argument list of packages to install, second argument for quite mode
  255. install_packages() {
  256. local args=""
  257. if [[ $2 == true ]]; then
  258. case "$OS" in
  259. ubuntu | debian) args="-qq" ;;
  260. *) args="-q" ;;
  261. esac
  262. fi
  263.  
  264. # Eval needed for proper expansion of arguments
  265. case "$OS" in
  266. ubuntu | debian)
  267. eval apt-get -y $args install "$1"
  268. ;;
  269. rocky | almalinux)
  270. eval dnf -y $args install "$1"
  271. ;;
  272. esac
  273. }
  274.  
  275. # ------------ User input functions ------------ #
  276.  
  277. required_input() {
  278. local __resultvar=$1
  279. local result=''
  280.  
  281. while [ -z "$result" ]; do
  282. echo -n "* ${2}"
  283. read -r result
  284.  
  285. if [ -z "${3}" ]; then
  286. [ -z "$result" ] && result="${4}"
  287. else
  288. [ -z "$result" ] && error "${3}"
  289. fi
  290. done
  291.  
  292. eval "$__resultvar="'$result'""
  293. }
  294.  
  295. email_input() {
  296. local __resultvar=$1
  297. local result=''
  298.  
  299. while ! valid_email "$result"; do
  300. echo -n "* ${2}"
  301. read -r result
  302.  
  303. valid_email "$result" || error "${3}"
  304. done
  305.  
  306. eval "$__resultvar="'$result'""
  307. }
  308.  
  309. password_input() {
  310. local __resultvar=$1
  311. local result=''
  312. local default="$4"
  313.  
  314. while [ -z "$result" ]; do
  315. echo -n "* ${2}"
  316.  
  317. # modified from https://stackoverflow.com/a/22940001
  318. while IFS= read -r -s -n1 char; do
  319. [[ -z $char ]] && {
  320. printf '\n'
  321. break
  322. } # ENTER pressed; output \n and break.
  323. if [[ $char == $'\x7f' ]]; then # backspace was pressed
  324. # Only if variable is not empty
  325. if [ -n "$result" ]; then
  326. # Remove last char from output variable.
  327. [[ -n $result ]] && result=${result%?}
  328. # Erase '*' to the left.
  329. printf '\b \b'
  330. fi
  331. else
  332. # Add typed char to output variable. [ -z "$result" ] && [ -n "
  333. result+=$char
  334. # Print '*' in its stead.
  335. printf '*'
  336. fi
  337. done
  338. [ -z "$result" ] && [ -n "$default" ] && result="$default"
  339. [ -z "$result" ] && error "${3}"
  340. done
  341.  
  342. eval "$__resultvar="'$result'""
  343. }
  344.  
  345. # ------------------ Firewall ------------------ #
  346.  
  347. ask_firewall() {
  348. local __resultvar=$1
  349.  
  350. case "$OS" in
  351. ubuntu | debian)
  352. echo -e -n "* Do you want to automatically configure UFW (firewall)? (y/N): "
  353. read -r CONFIRM_UFW
  354.  
  355. if [[ "$CONFIRM_UFW" =~ [Yy] ]]; then
  356. eval "$__resultvar="'true'""
  357. fi
  358. ;;
  359. rocky | almalinux)
  360. echo -e -n "* Do you want to automatically configure firewall-cmd (firewall)? (y/N): "
  361. read -r CONFIRM_FIREWALL_CMD
  362.  
  363. if [[ "$CONFIRM_FIREWALL_CMD" =~ [Yy] ]]; then
  364. eval "$__resultvar="'true'""
  365. fi
  366. ;;
  367. esac
  368. }
  369.  
  370. install_firewall() {
  371. case "$OS" in
  372. ubuntu | debian)
  373. output ""
  374. output "Installing Uncomplicated Firewall (UFW)"
  375.  
  376. if ! [ -x "$(command -v ufw)" ]; then
  377. update_repos true
  378. install_packages "ufw" true
  379. fi
  380.  
  381. ufw --force enable
  382.  
  383. success "Enabled Uncomplicated Firewall (UFW)"
  384.  
  385. ;;
  386. rocky | almalinux)
  387.  
  388. output ""
  389. output "Installing FirewallD"+
  390.  
  391. if ! [ -x "$(command -v firewall-cmd)" ]; then
  392. install_packages "firewalld" true
  393. fi
  394.  
  395. systemctl --now enable firewalld >/dev/null
  396.  
  397. success "Enabled FirewallD"
  398.  
  399. ;;
  400. esac
  401. }
  402.  
  403. firewall_allow_ports() {
  404. case "$OS" in
  405. ubuntu | debian)
  406. for port in $1; do
  407. ufw allow "$port"
  408. done
  409. ufw --force reload
  410. ;;
  411. rocky | almalinux)
  412. for port in $1; do
  413. firewall-cmd --zone=public --add-port="$port"/tcp --permanent
  414. done
  415. firewall-cmd --reload -q
  416. ;;
  417. esac
  418. }
  419.  
  420. # ---------------- System checks --------------- #
  421.  
  422. # panel x86_64 check
  423. check_os_x86_64() {
  424. if [ "${ARCH}" != "amd64" ]; then
  425. warning "Detected CPU architecture $CPU_ARCHITECTURE"
  426. warning "Using any other architecture than 64 bit (x86_64) will cause problems."
  427.  
  428. echo -e -n "* Are you sure you want to proceed? (y/N):"
  429. read -r choice
  430.  
  431. if [[ ! "$choice" =~ [Yy] ]]; then
  432. error "Installation aborted!"
  433. exit 1
  434. fi
  435. fi
  436. }
  437.  
  438. # wings virtualization check
  439. check_virt() {
  440. output "Installing virt-what..."
  441.  
  442. update_repos true
  443. install_packages "virt-what" true
  444.  
  445. # Export sbin for virt-what
  446. export PATH="$PATH:/sbin:/usr/sbin"
  447.  
  448. virt_serv=$(virt-what)
  449.  
  450. case "$virt_serv" in
  451. *openvz* | *lxc*)
  452. warning "Unsupported type of virtualization detected. Please consult with your hosting provider whether your server can run Docker or not. Proceed at your own risk."
  453. echo -e -n "* Are you sure you want to proceed? (y/N): "
  454. read -r CONFIRM_PROCEED
  455. if [[ ! "$CONFIRM_PROCEED" =~ [Yy] ]]; then
  456. error "Installation aborted!"
  457. exit 1
  458. fi
  459. ;;
  460. *)
  461. [ "$virt_serv" != "" ] && warning "Virtualization: $virt_serv detected."
  462. ;;
  463. esac
  464.  
  465. if uname -r | grep -q "xxxx"; then
  466. error "Unsupported kernel detected."
  467. exit 1
  468. fi
  469.  
  470. success "System is compatible with docker"
  471. }
  472.  
  473. # Exit with error status code if user is not root
  474. if [[ $EUID -ne 0 ]]; then
  475. error "This script must be executed with root privileges."
  476. exit 1
  477. fi
  478.  
  479. # Detect OS
  480. if [ -f /etc/os-release ]; then
  481. # freedesktop.org and systemd
  482. . /etc/os-release
  483. OS=$(echo "$ID" | awk '{print tolower($0)}')
  484. OS_VER=$VERSION_ID
  485. elif type lsb_release >/dev/null 2>&1; then
  486. # linuxbase.org
  487. OS=$(lsb_release -si | awk '{print tolower($0)}')
  488. OS_VER=$(lsb_release -sr)
  489. elif [ -f /etc/lsb-release ]; then
  490. # For some versions of Debian/Ubuntu without lsb_release command
  491. . /etc/lsb-release
  492. OS=$(echo "$DISTRIB_ID" | awk '{print tolower($0)}')
  493. OS_VER=$DISTRIB_RELEASE
  494. elif [ -f /etc/debian_version ]; then
  495. # Older Debian/Ubuntu/etc.
  496. OS="debian"
  497. OS_VER=$(cat /etc/debian_version)
  498. elif [ -f /etc/SuSe-release ]; then
  499. # Older SuSE/etc.
  500. OS="SuSE"
  501. OS_VER="?"
  502. elif [ -f /etc/redhat-release ]; then
  503. # Older Red Hat, CentOS, etc.
  504. OS="Red Hat/CentOS"
  505. OS_VER="?"
  506. else
  507. # Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
  508. OS=$(uname -s)
  509. OS_VER=$(uname -r)
  510. fi
  511.  
  512. OS=$(echo "$OS" | awk '{print tolower($0)}')
  513. OS_VER_MAJOR=$(echo "$OS_VER" | cut -d. -f1)
  514. CPU_ARCHITECTURE=$(uname -m)
  515.  
  516. case "$OS" in
  517. ubuntu)
  518. [ "$OS_VER_MAJOR" == "18" ] && SUPPORTED=true
  519. [ "$OS_VER_MAJOR" == "20" ] && SUPPORTED=true
  520. [ "$OS_VER_MAJOR" == "22" ] && SUPPORTED=true
  521. export DEBIAN_FRONTEND=noninteractive
  522. ;;
  523. debian)
  524. [ "$OS_VER_MAJOR" == "10" ] && SUPPORTED=true
  525. [ "$OS_VER_MAJOR" == "11" ] && SUPPORTED=true
  526. export DEBIAN_FRONTEND=noninteractive
  527. ;;
  528. rocky | almalinux)
  529. [ "$OS_VER_MAJOR" == "8" ] && SUPPORTED=true
  530. [ "$OS_VER_MAJOR" == "9" ] && SUPPORTED=true
  531. ;;
  532. *)
  533. SUPPORTED=false
  534. ;;
  535. esac
  536.  
  537. # exit if not supported
  538. if [ "$SUPPORTED" == false ]; then
  539. output "$OS $OS_VER is not supported"
  540. error "Unsupported OS"
  541. exit 1
  542. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement