#!/bin/bash
# Allow outbound traffic only to GitHub and the hosts in allowed-domains.txt.
# Adapted from the firewall script in Anthropic's Claude Code reference
# container (.devcontainer/init-firewall.sh in anthropics/claude-code).
# Needs the NET_ADMIN and NET_RAW capabilities (see devcontainer.json).
set -euo pipefail
IFS=$'\n\t'

LIST=/usr/local/etc/allowed-domains.txt

DOCKER_DNS_RULES=$(iptables-save -t nat | grep "127\.0\.0\.11" || true)
iptables -F; iptables -X
iptables -t nat -F; iptables -t nat -X
iptables -t mangle -F; iptables -t mangle -X
ipset destroy allowed-domains 2>/dev/null || true

if [ -n "$DOCKER_DNS_RULES" ]; then
  iptables -t nat -N DOCKER_OUTPUT 2>/dev/null || true
  iptables -t nat -N DOCKER_POSTROUTING 2>/dev/null || true
  echo "$DOCKER_DNS_RULES" | xargs -L 1 iptables -t nat
fi

# DNS and localhost first.
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

ipset create allowed-domains hash:net

echo "Adding GitHub address ranges..."
gh_ranges=$(curl -s https://api.github.com/meta)
echo "$gh_ranges" | jq -e '.web and .api and .git' >/dev/null \
  || { echo "ERROR: could not read GitHub address ranges"; exit 1; }
while read -r cidr; do
  [[ "$cidr" =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}/[0-9]{1,2}$ ]] || { echo "ERROR: bad range $cidr"; exit 1; }
  ipset add -exist allowed-domains "$cidr"
done < <(echo "$gh_ranges" | jq -r '(.web + .api + .git)[]' | aggregate -q)

echo "Adding hosts from $LIST..."
while read -r domain; do
  [[ -z "$domain" || "$domain" == \#* ]] && continue
  ips=$(dig +noall +answer A "$domain" | awk '$4 == "A" {print $5}')
  # A host that does not resolve stays blocked, which is the safe outcome, so
  # skip it with a warning instead of leaving the firewall off.
  if [ -z "$ips" ]; then
    echo "WARNING: cannot resolve $domain, so it stays blocked"
    continue
  fi
  while read -r ip; do
    ipset add -exist allowed-domains "$ip"
  done < <(echo "$ips")
done < "$LIST"

# The Docker host network, so the editor can reach the container.
HOST_IP=$(ip route | awk '/default/ {print $3; exit}')
HOST_NETWORK=$(echo "$HOST_IP" | sed "s/\.[0-9]*$/.0\/24/")
iptables -A INPUT -s "$HOST_NETWORK" -j ACCEPT
iptables -A OUTPUT -d "$HOST_NETWORK" -j ACCEPT

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m set --match-set allowed-domains dst -j ACCEPT
iptables -A OUTPUT -j REJECT --reject-with icmp-admin-prohibited

echo "Checking the firewall..."
if curl --connect-timeout 5 -s https://example.com >/dev/null 2>&1; then
  echo "ERROR: example.com is reachable, the firewall is not working"; exit 1
fi
curl --connect-timeout 5 -s https://api.github.com/zen >/dev/null \
  || { echo "ERROR: GitHub is not reachable"; exit 1; }
echo "Firewall ready."
