tasks/ae-run-basic-check/run.sh
#!/usr/bin/env bash

## 1/ Start the test pages
## 2/ Start the proxy server
## 3/ Start the task.js

check_and_kill_port() {
  local PORT=$1
  local PID=$(timeout 2 lsof -ti:$PORT 2>/dev/null)
  if [ -n "$PID" ]; then
    echo "[!] Killing process $PID on port $PORT"
    kill -9 $PID 2>/dev/null
  fi
}

# Ensure the script is run under the gadget-detection path
SCRIPT_PATH=$(realpath "$0")
TASK_PATH=$(dirname "$SCRIPT_PATH")
CONCOLIC_PATH=$(realpath $TASK_PATH/../../gadget-detection)
THEHULK_PATH=$(realpath $TASK_PATH/../../)
if [ "$(basename $CONCOLIC_PATH)" != "gadget-detection" ]; then
  echo "[!] Please run this script from the gadget-detection directory."
  exit 1
fi


RUN_TEST=true
RUN_PROXY=true
RUN_ANALYSIS=true

# Step 1: Start the Test Server for hosting the test pages
# We currently don't use this 
if [ "$RUN_TEST" = true ]; then
  check_and_kill_port 8080
  pushd $THEHULK_PATH/dataset/dom-clobbering-collection/domc-gadgets-assets > /dev/null 2>&1
  ./run.sh > /dev/null 2>&1 &
  TEST_SERVER_PID=$!
  sleep 1
  echo "[+] Test Server started with PID $TEST_SERVER_PID, at http://localhost:8080"
  popd > /dev/null 2>&1
fi

## Step 2: Start the Proxy Server for instrumenting the JS & HTML on-the-fly
if [ "$RUN_PROXY" = true ]; then
  check_and_kill_port 8899
  echo "[+] Starting the Proxy Server for instrumenting the JS & HTML on-the-fly..."

  ### Setup the python env first
  pushd $THEHULK_PATH/proxy-server > /dev/null 2>&1
  ./setup.sh > /dev/null 2>&1 &
  PROXY_SERVER_PID=$!
  
  # Wait for proxy server to be ready (max 30 seconds)
  echo "[+] Waiting for proxy server to be ready on port 8899..."
  for i in {1..30}; do
    if timeout 1 bash -c "</dev/tcp/127.0.0.1/8899" 2>/dev/null; then
      echo "[+] Proxy Server is ready with PID $PROXY_SERVER_PID"
      break
    fi
    if [ $i -eq 30 ]; then
      echo "[!] Proxy server failed to start within 30 seconds"
      exit 1
    fi
    sleep 1
  done
  popd > /dev/null 2>&1
fi

# Step 3: Refresh the latest analysis bundle
if [ "$RUN_ANALYSIS" = true ]; then
  pushd $CONCOLIC_PATH/runtime-analysis > /dev/null 2>&1
  echo "[+] Compile the lastest analysis bundle..."
  npm run deploy > /dev/null 2>&1
  sleep 1
  popd > /dev/null 2>&1
fi

## Step 4: Get back and run the task
pushd $THEHULK_PATH/crawler/src/ > /dev/null 2>&1
node ./scheduler.js --scheduler-config $TASK_PATH/config.scheduler.yml --crawler-config $TASK_PATH/config.browser.yml &
popd > /dev/null 2>&1
BROWSER_PID=$!
echo "[+] Browser started with PID $BROWSER_PID"

echo "========================== Start task running =========================="

cleanup() {
  echo "[+] Cleaning up..."
  if [ -n "$TEST_SERVER_PID" ]; then
    if kill -0 "$TEST_SERVER_PID" 2>/dev/null; then
      kill $TEST_SERVER_PID 2>/dev/null
      echo "[+] Killed TEST_SERVER_PID $TEST_SERVER_PID"
    fi
  fi
  if [ -n "$PROXY_SERVER_PID" ]; then
    check_and_kill_port 8899
    echo "[+] Killed PROXY_SERVER_PID $PROXY_SERVER_PID"
  fi
  if [ -n "$BROWSER_PID" ]; then
    if kill -0 "$BROWSER_PID" 2>/dev/null; then
      kill $BROWSER_PID 2>/dev/null
      echo "[+] Killed BROWSER_PID $BROWSER_PID"
    fi
  fi
}

trap cleanup EXIT

wait $BROWSER_PID
echo "[+] Browser process $BROWSER_PID has finished"

echo "========================== Task End Running =========================="
