1#!/usr/bin/env bash
 2
 3set -euo pipefail
 4
 5if command -v docker >/dev/null 2>&1; then
 6    ENGINE="docker"
 7elif command -v podman >/dev/null 2>&1; then
 8    ENGINE="podman"
 9else
10    echo "Neither Docker nor Podman found. Please install one of them."
11    exit 1
12fi
13if [ ! -d ~/.mitmproxy ]; then
14    mkdir -p ~/.mitmproxy
15fi
16
17CONTAINER_ID="$(${ENGINE} run -d --rm -it -v ~/.mitmproxy:/home/mitmproxy/.mitmproxy -p 9876:8080 mitmproxy/mitmproxy mitmdump)"
18
19trap "${ENGINE} stop \"$CONTAINER_ID\" 1> /dev/null || true; exit 1" SIGINT
20
21echo "Add the root certificate created in ~/.mitmproxy to your certificate chain for HTTP"
22echo "on macOS:"
23echo "sudo security add-trusted-cert -d -p ssl -p basic -k /Library/Keychains/System.keychain ~/.mitmproxy/mitmproxy-ca-cert.pem"
24echo "Press enter to continue"
25read
26
27http_proxy=http://localhost:9876 cargo run
28
29# Clean up detached proxy after running
30${ENGINE} stop "${CONTAINER_ID}" 2>/dev/null || true