| #!/bin/bash |
| |
| |
| |
| |
| |
| |
| |
| |
| set -uo pipefail |
|
|
| |
| API_HOSTS=( |
| api.fireworks.ai |
| openrouter.ai openrouter.io |
| api.openai.com api.anthropic.com generativelanguage.googleapis.com |
| api.together.xyz integrate.api.nvidia.com api.moonshot.cn api.kimi.com |
| ) |
| |
| CUSTOM=() |
| while IFS='=' read -r n v; do |
| case "$n" in |
| *_BASE_URL|*_URL|*_ENDPOINT) |
| [ -n "$v" ] && CUSTOM+=("$(echo "$v" | sed 's|.*://||; s|[:/].*||')") ;; |
| esac |
| done < <(env) |
| if [ -n "${ALLOWED_HOSTS:-}" ]; then IFS=',' read -ra E <<< "$ALLOWED_HOSTS"; CUSTOM+=("${E[@]}"); fi |
| ALL_HOSTS=("${API_HOSTS[@]}" ${CUSTOM[@]+"${CUSTOM[@]}"}) |
|
|
| if iptables -L OUTPUT -n >/dev/null 2>&1; then |
| echo "restrict-network: applying outbound allowlist firewall..." |
| iptables -F OUTPUT 2>/dev/null || true |
| iptables -A OUTPUT -o lo -j ACCEPT |
| iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT |
| iptables -A OUTPUT -p udp --dport 53 -j ACCEPT |
| iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT |
| iptables -A OUTPUT -d 10.0.0.0/8 -j ACCEPT |
| iptables -A OUTPUT -d 172.16.0.0/12 -j ACCEPT |
| iptables -A OUTPUT -d 192.168.0.0/16 -j ACCEPT |
| iptables -A OUTPUT -d 127.0.0.11 -j ACCEPT |
| for h in "${ALL_HOSTS[@]}"; do |
| for ip in $(getent ahosts "$h" 2>/dev/null | awk '{print $1}' | sort -u); do |
| iptables -A OUTPUT -d "$ip" -j ACCEPT |
| done |
| done |
| iptables -A OUTPUT -j REJECT --reject-with icmp-admin-prohibited |
| echo "restrict-network: outbound locked to DNS + LLM endpoints; pypi/github/HF blocked." |
| else |
| echo "restrict-network: WARNING — iptables unavailable (need cap_add NET_ADMIN); NOT firewalled." >&2 |
| fi |
|
|
| exec "$@" |
|
|