curl.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env bash
  2. # Post a single abuse report to IRDB.
  3. #
  4. # Usage:
  5. # IRDB_URL=http://localhost:8081 IRDB_TOKEN=irdb_rep_... \
  6. # examples/reporters/curl.sh 203.0.113.42 brute_force
  7. #
  8. # Optional third arg: a JSON metadata object to attach to the report.
  9. # Anything in metadata is opaque to the api beyond a 4 KB size cap.
  10. set -euo pipefail
  11. if [ "$#" -lt 2 ]; then
  12. echo "Usage: $0 <ip> <category> [<metadata-json>]" >&2
  13. exit 64
  14. fi
  15. IP="$1"
  16. CATEGORY="$2"
  17. METADATA="${3:-{\}}"
  18. : "${IRDB_URL:?must be set, e.g. http://localhost:8081}"
  19. : "${IRDB_TOKEN:?must be set, e.g. irdb_rep_...}"
  20. # Build the JSON body. jq is the cleanest path; fall back to printf if
  21. # jq is unavailable.
  22. if command -v jq >/dev/null 2>&1; then
  23. BODY=$(jq -nc --arg ip "$IP" --arg cat "$CATEGORY" --argjson meta "$METADATA" \
  24. '{ip: $ip, category: $cat, metadata: $meta}')
  25. else
  26. BODY=$(printf '{"ip":"%s","category":"%s","metadata":%s}' \
  27. "$IP" "$CATEGORY" "$METADATA")
  28. fi
  29. curl -fsS -X POST \
  30. -H "Authorization: Bearer $IRDB_TOKEN" \
  31. -H "Content-Type: application/json" \
  32. --data "$BODY" \
  33. "$IRDB_URL/api/v1/report"