Skip to content

Commit

Permalink
Optimize setup scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ajinabraham committed Nov 10, 2024
1 parent d83acb9 commit ebb5b86
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 106 deletions.
64 changes: 25 additions & 39 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,49 +1,35 @@
#!/bin/bash
var="$1"

function validate_ip () {
local IP=$1
local stat=1

if [[ $IP =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($IP)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
if [ "$stat" -eq 0 ]; then
return $stat
else
echo 'Bad IP'
exit 1
fi
function validate_ip() {
local IP=$1
if [[ $IP =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
for i in ${IP//./ }; do
((i >= 0 && i <= 255)) || { echo 'Bad IP'; exit 1; }
done
else
echo 'Bad IP'
exit 1
fi
}

function validate_port () {
local PORT=$1
if [ -z "$PORT" ]; then
echo 'Port can not be empty'
exit 1
fi
if [ "$PORT" -gt 1024 ] && [ "$PORT" -lt 65535 ]; then
return 0
else
echo 'Invalid Port'
exit 1
fi
function validate_port() {
local PORT=$1
if [[ -z "$PORT" || "$PORT" -le 1024 || "$PORT" -ge 65535 ]]; then
echo 'Invalid Port'
exit 1
fi
}

if [ ! -z "$var" ]; then
IP=$(echo $var | awk -F':' '{print $1}')
PORT=$(echo $var | awk -F':' '{print $2}')
validate_ip $IP
validate_port $PORT
else
if [[ -n "$var" ]]; then
IP="${var%%:*}"
PORT="${var##*:}"
validate_ip "$IP"
validate_port "$PORT"
else
IP='[::]'
PORT='8000'
fi
fi

python3 -m poetry run gunicorn -b ${IP}:${PORT} mobsf.MobSF.wsgi:application --workers=1 --threads=10 --timeout=3600 \
--log-level=citical --log-file=- --access-logfile=- --error-logfile=- --capture-output
--log-level=critical --log-file=- --access-logfile=- --error-logfile=- --capture-output
72 changes: 36 additions & 36 deletions scripts/clean.sh
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
#!/bin/bash

echo
echo '=======================MobSF Clean Script for Unix======================='
echo 'Running this script will delete the Scan database, all files uploaded and generated.'
echo 'Running this script will delete the scan database, all files uploaded and generated.'

script_path=$(dirname $0)
script_path=$(basename "$(dirname "$0")")
mobsf_home="$HOME/.MobSF"

if [ "$script_path" != "scripts" ] && [ "$script_path" != "./scripts" ]; then
echo 'Please run script from mobsf.MobSF directory '
echo './scripts/clean.sh '
exit 1
# Ensure the script is run from the correct directory
if [[ "$script_path" != "scripts" ]]; then
echo 'Please run this script from the MobSF directory:'
echo './scripts/clean.sh'
exit 1
fi

if [ "$1" != "" ]; then
VAL="$1"
else
read -p 'Continue? (Y/N): ' confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1
# Confirmation prompt
VAL=${1:-}
if [[ -z "$VAL" ]]; then
read -p 'Continue? (Y/N): ' confirm
[[ $confirm =~ ^[yY]([eE][sS])?$ ]] || exit 1
VAL=$confirm
fi
echo
if [[ $VAL =~ ^[Yy]$ ]]
then
echo 'Deleting all uploads'
rm -rf ./mobsf/uploads/*
echo 'Deleting all downloads'
rm -rf ./mobsf/downloads/*
echo 'Deleting Static Analyzer migrations'
rm -rf ./mobsf/StaticAnalyzer/migrations/*
echo 'Deleting Dynamic Analyzer migrations'
rm -rf ./mobsf/DynamicAnalyzer/migrations/*
echo 'Deleting MobSF migrations'
rm -rf ./mobsf/MobSF/migrations/*
echo 'Deleting Python byte code files'
find ./ -name "*.pyc" -exec rm -rf {} \;
find ./ | grep -E "(__pycache__|\.pyo$)" | xargs rm -rf
echo 'Deleting temp and log files'
rm -rf ./mobsf/debug.log
rm -rf ./classes*
echo 'Deleting Scan database'
rm -rf ./mobsf/db.sqlite3
echo 'Deleting Secret file'
rm -rf ./mobsf/secret
echo "Deleting MobSF data directory: $mobsf_home"
rm -rf $mobsf_home
echo 'Done'

echo
if [[ "$VAL" =~ ^[yY]$ ]]; then
echo 'Cleaning up MobSF directories and files...'

# Remove files from key directories
rm -rf ./mobsf/{uploads,downloads,StaticAnalyzer/migrations,DynamicAnalyzer/migrations,MobSF/migrations}/*

echo 'Removing Python bytecode and cache files'
find ./ -type f -name "*.pyc" -o -name "*.pyo" -delete
find ./ -type d -name "__pycache__" -exec rm -rf {} +

# Remove temporary, log, and database files
echo 'Deleting temporary, log, and database files'
rm -f ./mobsf/debug.log ./classes* ./mobsf/db.sqlite3 ./mobsf/secret

# Remove the MobSF data directory if it exists
if [[ -d "$mobsf_home" ]]; then
echo "Deleting MobSF data directory: $mobsf_home"
rm -rf "$mobsf_home"
fi

echo 'Cleanup complete.'
fi
58 changes: 27 additions & 31 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -1,50 +1,43 @@
#!/bin/bash

# Python3 Check
unamestr=$(uname)
if ! [ -x "$(command -v python3)" ]; then
# Check for Python3 and validate version
if ! command -v python3 &>/dev/null; then
echo '[ERROR] python3 is not installed.' >&2
exit 1
fi

# Python3 Version Check
python_version="$(python3 --version 2>&1 | awk '{print $2}')"
py_major=$(echo "$python_version" | cut -d'.' -f1)
py_minor=$(echo "$python_version" | cut -d'.' -f2)
if [ "$py_major" -eq "3" ] && [ "$py_minor" -gt "9" ] && [ "$py_minor" -lt "13" ]; then
echo "[INSTALL] Found Python ${python_version}"
else
echo "[ERROR] MobSF dependencies require Python 3.10 - 3.12. You have Python version ${python_version} or python3 points to Python ${python_version}."
python_version=$(python3 --version 2>&1 | awk '{print $2}')
py_major=${python_version%%.*}
py_minor=${python_version#*.}
py_minor=${py_minor%%.*}

if [[ "$py_major" -ne 3 || "$py_minor" -lt 10 || "$py_minor" -gt 12 ]]; then
echo "[ERROR] MobSF dependencies require Python 3.10 - 3.12. You have Python ${python_version}."
exit 1
fi
echo "[INSTALL] Found Python ${python_version}"

# Pip Check and Upgrade
python3 -m pip -V
if [ $? -eq 0 ]; then
# Check and upgrade pip
if python3 -m pip -V &>/dev/null; then
echo '[INSTALL] Found pip'
if [[ $unamestr == 'Darwin' ]]; then
python3 -m pip install --no-cache-dir --upgrade pip
else
python3 -m pip install --no-cache-dir --upgrade pip --user
fi
upgrade_cmd="python3 -m pip install --no-cache-dir --upgrade pip"
[[ "$(uname)" != "Darwin" ]] && upgrade_cmd+=" --user"
eval $upgrade_cmd
else
echo '[ERROR] python3-pip not installed'
exit 1
fi

# macOS Specific Checks
if [[ $unamestr == 'Darwin' ]]; then
# Check if xcode is installed
xcode-select -v
if ! [ $? -eq 0 ]; then
echo 'Please install command-line tools'
echo 'xcode-select --install'
# macOS-specific Xcode CLI tools check
if [[ "$(uname)" == "Darwin" ]]; then
if ! xcode-select -v &>/dev/null; then
echo 'Please install command-line tools with: xcode-select --install'
exit 1
else
echo '[INSTALL] Found Xcode'
fi
fi
echo '[INSTALL] Found Xcode'
fi

# Install dependencies and set up the environment
echo '[INSTALL] Installing Requirements'
python3 -m pip install --no-cache-dir wheel poetry==1.8.4
python3 -m poetry lock
Expand All @@ -53,6 +46,7 @@ python3 -m poetry install --no-root --only main --no-interaction --no-ansi
echo '[INSTALL] Clean Up'
bash scripts/clean.sh y

# Database setup and superuser creation
echo '[INSTALL] Migrating Database'
export DJANGO_SUPERUSER_USERNAME=mobsf
export DJANGO_SUPERUSER_PASSWORD=mobsf
Expand All @@ -61,8 +55,10 @@ python3 -m poetry run python manage.py makemigrations StaticAnalyzer
python3 -m poetry run python manage.py migrate
python3 -m poetry run python manage.py createsuperuser --noinput --email ""
python3 -m poetry run python manage.py create_roles
wkhtmltopdf -V
if ! [ $? -eq 0 ]; then

# Check for wkhtmltopdf
if ! command -v wkhtmltopdf &>/dev/null; then
echo 'Download and Install wkhtmltopdf for PDF Report Generation - https://wkhtmltopdf.org/downloads.html'
fi

echo '[INSTALL] Installation Complete'

0 comments on commit ebb5b86

Please sign in to comment.