#!/bin/sh
# PocketClaw security check: collector (version 1)
#
# What it does: prints, on your screen only, the facts the security check
# needs. It reads; it changes nothing, installs nothing and sends nothing.
# You copy the output and paste it into the page you got after payment.
#
#   - the version of the agents it finds (openclaw, hermes, nanobot,
#     nanoclaw, ironclaw, zeroclaw) and of Ollama
#   - the TCP addresses they listen on (ss, or lsof on macOS)
#   - the containers running with their published ports (docker ps)
#   - whether Caddy, nginx, Traefik, Tailscale or cloudflared run
#   - the lines of ~/.openclaw/openclaw.json about bind, port, auth and
#     sandbox, with every value that looks like a secret masked
#
# Everything goes through a local filter that masks keys and tokens
# before it is printed; the server filters again on reception. Read the
# output before you paste it: if anything private is left, delete it.
#
# Usage:  sh collect.sh            (as the user that runs the agent)
#         sh collect.sh > out.txt  (to read it first, then paste)
#
# Source: https://pocketclaw.dev/audit/collect.sh

LC_ALL=C
export LC_ALL

mask() {
  sed -E \
    -e 's/-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----.*/[REDACTED]/' \
    -e 's/sk-[A-Za-z0-9_-]{16,}/[REDACTED]/g' \
    -e 's/(sk|rk|pk)_(live|test)_[A-Za-z0-9]{10,}/[REDACTED]/g' \
    -e 's/(ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{20,}/[REDACTED]/g' \
    -e 's/github_pat_[A-Za-z0-9_]{20,}/[REDACTED]/g' \
    -e 's/xox[abprs]-[A-Za-z0-9-]{10,}/[REDACTED]/g' \
    -e 's/(AKIA|ASIA)[0-9A-Z]{16}/[REDACTED]/g' \
    -e 's/AIza[0-9A-Za-z_-]{30,}/[REDACTED]/g' \
    -e 's/(hf|gsk|npm)_[A-Za-z0-9]{20,}/[REDACTED]/g' \
    -e 's/tskey-[A-Za-z0-9-]{10,}/[REDACTED]/g' \
    -e 's/eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/[REDACTED]/g' \
    -e 's/[0-9]{8,10}:[A-Za-z0-9_-]{35}/[REDACTED]/g' \
    -e 's#(://[^ :/@]+:)[^ @/]+@#\1[REDACTED]@#g' \
    -e 's/(Bearer|Basic|Token) [A-Za-z0-9._~+\/=-]{12,}/\1 [REDACTED]/g' \
    -e 's/(["'"'"']?[A-Za-z0-9_.-]*([Tt][Oo][Kk][Ee][Nn]|[Ss][Ee][Cc][Rr][Ee][Tt]|[Pp][Aa][Ss][Ss][Ww]([Oo][Rr])?[Dd]|[Aa][Pp][Ii]_?[Kk][Ee][Yy]|[Cc][Rr][Ee][Dd][Ee][Nn][Tt][Ii][Aa][Ll])[A-Za-z0-9_.-]*["'"'"']?[[:space:]]*[:=][[:space:]]*["'"'"']?)[^"'"'"'[:space:],{}]{6,}/\1[REDACTED]/g' \
    -e 's/[A-Za-z0-9+\/_-]{40,}/[REDACTED]/g'
}

have() { command -v "$1" >/dev/null 2>&1; }

collect() {
  echo "# pocketclaw-collect v1"
  echo "date: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
  echo "os: $(uname -sr 2>/dev/null)"

  for bin in openclaw hermes nanobot nanoclaw ironclaw zeroclaw ollama; do
    if have "$bin"; then
      v=$("$bin" --version 2>/dev/null | head -n 1)
      echo "agent: $bin ${v:-unknown}"
    fi
  done

  if have ss; then
    ss -ltnpH 2>/dev/null | awk '{ p=$6; sub(/^users:\(\("/, "", p); sub(/".*/, "", p); print "listen: " $4 " " p }'
  elif have lsof; then
    lsof -nP -iTCP -sTCP:LISTEN 2>/dev/null | awk 'NR>1 { print "listen: " $9 " " $1 }' | sort -u
  elif have netstat; then
    netstat -ltn 2>/dev/null | awk '/LISTEN/ { print "listen: " $4 }'
  fi

  if have docker; then
    docker ps --format '{{.Image}} | {{.Ports}}' 2>/dev/null | sed 's/^/docker: /'
  fi

  for p in caddy nginx traefik haproxy; do
    if pgrep -x "$p" >/dev/null 2>&1; then echo "proxy: $p"; fi
  done
  for t in tailscaled cloudflared ngrok; do
    if pgrep -x "$t" >/dev/null 2>&1; then echo "tunnel: $t"; fi
  done

  cfg="${OPENCLAW_CONFIG_PATH:-$HOME/.openclaw/openclaw.json}"
  if [ -r "$cfg" ]; then
    grep -nE '"(bind|port|mode|auth|sandbox|allowFrom|trustedProxies|tailscale)"' "$cfg" 2>/dev/null | head -n 40 | sed 's/^/config: /'
  fi
}

collect | mask
