#!/bin/bash
set -u
h=${1:-api.baloise.de}
probe(){
  method=$1; path=$2; data=${3-}
  out=$(mktemp); hdr=$(mktemp); err=$(mktemp)
  args=(-sk --http1.1 -X "$method" -H 'X-Bug-Bounty: dwbzn' -D "$hdr" -o "$out" --max-time 12)
  [ -n "$data" ] && args+=(-H 'Content-Type: application/json' --data-binary "$data")
  curl "${args[@]}" "https://$h$path" 2>"$err" || true
  status=$(awk '/^HTTP\//{s=$2}END{print s}' "$hdr")
  size=$(wc -c < "$out")
  ct=$(awk 'BEGIN{IGNORECASE=1}/^Content-Type:/{sub(/\r$/,"",$0);print substr($0,index($0,":")+2)}' "$hdr" | tail -1)
  loc=$(awk 'BEGIN{IGNORECASE=1}/^Location:/{sub(/\r$/,"",$0);print substr($0,index($0,":")+2)}' "$hdr" | tail -1)
  ex=$(grep -o 'exchange [0-9a-f-]*' "$err" | tail -1 | cut -d' ' -f2)
  snippet=$(tr '\r\n\t' '   ' < "$out" | sed 's/  */ /g' | cut -c1-220)
  printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$method" "$path" "$status" "$size" "$ct" "$loc" "$ex" "$snippet"
  rm -f "$out" "$hdr" "$err"
}
echo -e 'method\tpath\tstatus\tsize\tcontent_type\tlocation\texchange\tsnippet'
probe GET '/ssg/ping'
probe GET '/ssg/ping/systemInfo'
probe GET '/ssg/health'
probe GET '/ssg/health/healthz'
probe GET '/ssg/health/heap'
probe GET '/ssg/health/listenPorts'
probe GET '/ssg/csr/'
probe POST '/ssg/csr/' ''
probe GET '/ssg/passwd'
probe POST '/ssg/passwd' ''
probe GET '/ssg/token'
probe POST '/ssg/token' ''
probe GET '/ssg/policy/disco/'
probe GET '/ssg/policy/disco.modulator/'
probe GET '/ssg/wsdl'
probe GET '/ssg/wsil'
probe POST '/ssg/pm2/api/session?operation=read' ''
probe POST '/ssg/pm2/api/session?operation=create' ''
probe POST '/ssg/pm2/api/graphql' '{"query":"{__typename}"}'
probe GET '/ssg/management/'
