#!/bin/bash SCRIPTNAME="Ultra-API" VERSION="2024-07-27" BOLD=$(tput bold) BLUE=$(tput setaf 4) RED=$(tput setaf 1) CYAN=$(tput setaf 6) YELLOW=$(tput setaf 3) MAGENTA=$(tput setaf 5) GREEN=$(tput setaf 2) STOP_COLOR=$(tput sgr0) CONFIG_DIR="$HOME/scripts/Ultra-API" BIN_DIR="$HOME/bin" TMPDIR_LOCATION="$HOME/.tmp/ultra-api-$(date +%Y%m%d-%H%M%S)" SCREEN_NAME="Ultra-API" NGINX_FILE="${HOME}/.apps/nginx/proxy.d/ultra-api.conf" print_welcome_message() { term_width=$(tput cols) welcome_message="[[ Welcome to the unofficial ${SCRIPTNAME} script ]]" padding=$(printf '%*s' $(((term_width-${#welcome_message}) / 2)) '') echo -e "\n${CYAN}${BOLD}${padding}${welcome_message}${STOP_COLOR}\n" } nginx_conf_install_ultra-api() { hostname=$HOSTNAME cat </dev/null location /ultra-api { rewrite /ultra-api(/.*) \$1 break; proxy_pass http://${hostname}-direct.usbx.me:${port}; proxy_set_header Host \$host; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto \$scheme; proxy_set_header X-Forwarded-Host \$host; proxy_set_header Range \$http_range; proxy_set_header If-Range \$http_if_range; # Allow websockets proxy_http_version 1.1; proxy_set_header Upgrade \$http_upgrade; proxy_set_header Connection "upgrade"; } EOF app-nginx restart if [[ -f "${NGINX_FILE}" ]]; then echo -e "${YELLOW}${BOLD}[INFO] ${SCRIPTNAME} Nginx file has been created at ${STOP_COLOR}'${NGINX_FILE}'" else echo -e "${RED}${BOLD}[ERROR] ${SCRIPTNAME} Nginx file NOT found at ${STOP_COLOR}'${NGINX_FILE}'${RED}${BOLD}. Terminating the script ... Bye!${STOP_COLOR}" exit 1 fi } create_auth_token_database_ultra-api() { local db_file="$CONFIG_DIR/auth_tokens.db" # Check if SQLite3 is installed if ! command -v sqlite3 &> /dev/null; then echo "SQLite3 is not installed. Please install SQLite3." return 1 fi # Create SQLite3 database and table sqlite3 "$db_file" </dev/null 2>&1 "$CONFIG_DIR"/bin/pip3 install --ignore-installed --no-cache-dir pip >/dev/null 2>&1 "$CONFIG_DIR"/bin/pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 "$CONFIG_DIR"/bin/pip install -U >/dev/null 2>&1 "$CONFIG_DIR"/bin/pip install --no-cache-dir wheel --upgrade >/dev/null 2>&1 "$CONFIG_DIR"/bin/pip3 --no-cache-dir install flask requests Flask-Limiter >/dev/null 2>&1 if [[ -d "${CONFIG_DIR}" ]]; then echo -e "${YELLOW}${BOLD}[INFO] Created Python environment and installed depnedencies at config location:${STOP_COLOR} '${CONFIG_DIR}'\n" else echo -e "${RED}${BOLD}[ERROR] Failed to create Python environment at${STOP_COLOR} '${CONFIG_DIR}'${RED}${BOLD}. Terminating the script ... Bye!" exit 1 fi echo -e "${MAGENTA}${BOLD}[STAGE-2] Download mandatory python script & Port selection${STOP_COLOR}" wget -qO ${TMPDIR_LOCATION}/stats_request.py https://scripts.usbx.me/util-v2/Ultra-API/stats_request.py >/dev/null 2>&1 mv ${TMPDIR_LOCATION}/stats_request.py "${CONFIG_DIR}"/ if [[ -f "${CONFIG_DIR}"/stats_request.py ]]; then echo -e "${YELLOW}${BOLD}[INFO] Downloaded mandatory python script at config location:${STOP_COLOR} '${CONFIG_DIR}/stats_request.py'\n" else echo -e "${RED}${BOLD}[ERROR] Failed to download mandatory python script in ${STOP_COLOR} '${CONFIG_DIR}'${RED}${BOLD}. Terminating the script ... Bye!" exit 1 fi # Call port_picker function wget -qO ${TMPDIR_LOCATION}/port-selector.sh https://scripts.usbx.me/main-v2/BaseFunctions/port-selector/main.sh source ${TMPDIR_LOCATION}/port-selector.sh port="${SELECTED_PORT}" # Sed command to update values in file sed -i "s/>port