summaryrefslogtreecommitdiff
path: root/entrypoint.sh
diff options
context:
space:
mode:
Diffstat (limited to 'entrypoint.sh')
-rw-r--r--entrypoint.sh82
1 files changed, 82 insertions, 0 deletions
diff --git a/entrypoint.sh b/entrypoint.sh
new file mode 100644
index 0000000..2316e01
--- /dev/null
+++ b/entrypoint.sh
@@ -0,0 +1,82 @@
1#!/bin/bash
2set -e
3
4WG_CONF="/etc/wireguard/wg0.conf"
5
6# Parse the config file
7PRIVATE_KEY=$(grep -oP 'PrivateKey\s*=\s*\K.*' "$WG_CONF" | tr -d ' ')
8ADDRESS_V4=$(grep -oP 'Address\s*=\s*\K[^,]+' "$WG_CONF" | grep -v ':' | tr -d ' ')
9ADDRESS_V6=$(grep -oP 'Address\s*=\s*\K.*' "$WG_CONF" | grep -oP '[^,]*::[^,]*' | tr -d ' ')
10DNS_SERVERS=$(grep -oP 'DNS\s*=\s*\K.*' "$WG_CONF" | tr ',' '\n' | tr -d ' ')
11PEER_PUBKEY=$(grep -oP 'PublicKey\s*=\s*\K.*' "$WG_CONF" | tr -d ' ')
12ENDPOINT=$(grep -oP 'Endpoint\s*=\s*\K.*' "$WG_CONF" | tr -d ' ')
13WG_ENDPOINT=$(echo "$ENDPOINT" | cut -d: -f1)
14WG_PORT=$(echo "$ENDPOINT" | cut -d: -f2)
15
16# Set DNS manually
17if [ -n "$DNS_SERVERS" ]; then
18 : > /etc/resolv.conf
19 for dns in $DNS_SERVERS; do
20 echo "nameserver $dns" >> /etc/resolv.conf
21 done
22fi
23
24DEFAULT_IF=$(ip route | awk '/default/ {print $5; exit}')
25DEFAULT_GW=$(ip route | awk '/default/ {print $3; exit}')
26
27# --- IPv4 kill switch ---
28iptables -A INPUT -i lo -j ACCEPT
29iptables -A OUTPUT -o lo -j ACCEPT
30iptables -A OUTPUT -d "$WG_ENDPOINT" -p udp --dport "$WG_PORT" -j ACCEPT
31iptables -A INPUT -s "$WG_ENDPOINT" -p udp --sport "$WG_PORT" -j ACCEPT
32iptables -A INPUT -i wg0 -j ACCEPT
33iptables -A OUTPUT -o wg0 -j ACCEPT
34iptables -A INPUT -i "$DEFAULT_IF" -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
35# Allow container-to-container traffic
36iptables -A INPUT -i eth0 -j ACCEPT
37iptables -A OUTPUT -o eth0 -d 172.16.0.0/12 -j ACCEPT
38iptables -A OUTPUT -o eth0 -d 10.0.0.0/8 -j ACCEPT
39iptables -A OUTPUT -o eth0 -d 192.168.0.0/16 -j ACCEPT
40iptables -A INPUT -j DROP
41iptables -A OUTPUT -j DROP
42
43# --- IPv6 kill switch ---
44ip6tables -A INPUT -i lo -j ACCEPT
45ip6tables -A OUTPUT -o lo -j ACCEPT
46ip6tables -A INPUT -i wg0 -j ACCEPT
47ip6tables -A OUTPUT -o wg0 -j ACCEPT
48ip6tables -A INPUT -j DROP
49ip6tables -A OUTPUT -j DROP
50
51# --- Bring up WireGuard manually (no wg-quick) ---
52ip link add wg0 type wireguard
53echo "$PRIVATE_KEY" | wg set wg0 private-key /dev/stdin peer "$PEER_PUBKEY" endpoint "$ENDPOINT" allowed-ips 0.0.0.0/0,::/0
54
55[ -n "$ADDRESS_V4" ] && ip addr add "$ADDRESS_V4" dev wg0
56[ -n "$ADDRESS_V6" ] && ip addr add "$ADDRESS_V6" dev wg0
57
58ip link set wg0 up
59
60# Route the WireGuard endpoint via the real gateway (avoid routing loop)
61ip route add "$WG_ENDPOINT"/32 via "$DEFAULT_GW" dev "$DEFAULT_IF"
62
63# Route all other traffic through the tunnel
64ip route add 0.0.0.0/1 dev wg0
65ip route add 128.0.0.0/1 dev wg0
66
67# IPv6 routes through the tunnel
68if [ -n "$ADDRESS_V6" ]; then
69 ip -6 route add ::/1 dev wg0
70 ip -6 route add 8000::/1 dev wg0
71fi
72
73echo "VPN is up. Checking connection..."
74curl -s --max-time 10 https://am.i.mullvad.net/connected || echo "Warning: could not verify Mullvad connection"
75echo "Public IP: $(curl -s --max-time 10 https://am.i.mullvad.net/ip) | Location: $(curl -s --max-time 10 https://am.i.mullvad.net/country), $(curl -s --max-time 10 https://am.i.mullvad.net/city)"
76
77echo "VPN gateway ready."
78
79# Keep the container running, exit gracefully on SIGTERM
80trap 'echo "Shutting down VPN..."; ip link del wg0 2>/dev/null; exit 0' SIGTERM SIGINT
81sleep infinity &
82wait $!