MEGA-DL-3 / start.sh
megadl1's picture
Update start.sh
c73e334 verified
#!/bin/sh
# Check token
if [ -z "$GIT_TOKEN" ]; then
echo "❌ ERROR: GIT_TOKEN is not set!"
exit 1
fi
REPO_URL="https://$GIT_TOKEN@github.com/appujohn007/Mega-Downloader-API.git"
TARGET="/app/repo"
echo "📌 Cloning private repo into /app/repo ..."
git clone "$REPO_URL" "$TARGET"
echo "📌 Copying project files..."
cp -r "$TARGET"/* /app/
# Install requirements
if [ -f "/app/requirements.txt" ]; then
echo "📦 Installing requirements..."
pip install --no-cache-dir -r /app/requirements.txt
pip install uvicorn
else
echo "⚠️ No requirements.txt found."
fi
# Define Status File
STATUS_FILE="/tmp/tor_status"
echo "initializing" > "$STATUS_FILE"
# ----------------------------------------------------------------
# 1. Start Web App (Background)
# ----------------------------------------------------------------
echo "🚀 Starting Web App (Uvicorn)..."
uvicorn main:app --host 0.0.0.0 --port 7860 &
APP_PID=$!
# ----------------------------------------------------------------
# 2. Tor Management Loop
# ----------------------------------------------------------------
PIPE=/tmp/tor_log_pipe
mkfifo $PIPE
while true; do
echo "------------------------------------------------"
echo "🧅 Starting Tor Proxy..."
echo "------------------------------------------------"
echo "restarting" > "$STATUS_FILE"
tor > $PIPE 2>&1 &
TOR_PID=$!
start_time=$(date +%s)
bootstrapped=false
while read -r line; do
echo "$line"
# Check for Success
if echo "$line" | grep -q "Bootstrapped 100%"; then
if [ "$bootstrapped" = "false" ]; then
echo "✅ Tor Status: SUCCESS (100% Bootstrapped)"
echo "working" > "$STATUS_FILE"
bootstrapped=true
fi
fi
# Check Timeout
if [ "$bootstrapped" = "false" ]; then
current_time=$(date +%s)
elapsed=$((current_time - start_time))
if [ "$elapsed" -ge 180 ]; then
echo "⚠️ Timeout: Tor stuck. Restarting..."
kill -9 $TOR_PID
break
fi
fi
done < $PIPE
wait $TOR_PID
echo "🔄 Tor process ended. Re-looping..."
rm -f $PIPE
mkfifo $PIPE
done