#!/bin/sh

run_sweep() {
  for port in $(seq 13000 1 13020); do
    result=$(echo Q | timeout 5 openssl s_client -connect 64.233.177.84:443 -bind 0.0.0.0:$port 2>&1)
    if echo "$result" | grep -q "CONNECTED"; then
      echo "OK:     src port $port"
    else
      echo "BROKEN: src port $port"
    fi
  done
}

run_detail_sweep() {
  for port in $(seq 12000 1 12020); do
    result=$(echo Q | timeout 5 openssl s_client -connect 64.233.177.84:443 -bind 0.0.0.0:$port 2>&1)
    if echo "$result" | grep -q "CONNECTED"; then
      echo "OK:     src port $port"
    else
      echo "BROKEN: src port $port"
    fi
    echo $result
  done
}

run_connect() {
  result=$(echo Q | timeout 5 openssl s_client -connect 64.233.177.84:443 2>&1)
  if echo "$result" | grep -q "CONNECTED"; then
    echo "OK"
  else
    echo "BROKEN"
  fi
}

case "${1}" in
  sweep)
    run_sweep
    ;;
  detail_sweep)
    run_detail_sweep
    ;;
  connect)
    run_connect
    ;;
  *)
    echo "Usage: $0 [sweep|connect]"
    exit 1
    ;;
esac
