docker-wireguard/entrypoint.sh

62 lines
2.3 KiB
Bash
Raw Permalink Normal View History

2023-09-29 19:08:04 +02:00
#!/bin/ash
2019-02-14 21:03:36 +01:00
set -e
default_route_ip=$(ip route | grep default | awk '{print $3}')
if [[ -z "$default_route_ip" ]]; then
2023-09-29 19:08:04 +02:00
echo "No default route configured" >&2
exit 1
fi
2019-05-13 07:21:16 +02:00
configs=`find /etc/wireguard -type f -printf "%f\n"`
if [[ -z "$configs" ]]; then
2023-09-29 19:08:04 +02:00
echo "No configuration file found in /etc/wireguard" >&2
exit 1
2019-02-14 21:03:36 +01:00
fi
2019-05-13 07:21:16 +02:00
config=`echo $configs | head -n 1`
interface="${config%.*}"
2019-02-14 21:03:36 +01:00
if [[ "$(cat /proc/sys/net/ipv4/conf/all/src_valid_mark)" != "1" ]]; then
2023-09-29 19:08:04 +02:00
echo "sysctl net.ipv4.conf.all.src_valid_mark=1 is not set" >&2
exit 1
fi
2023-09-29 19:08:04 +02:00
# The net.ipv4.conf.all.src_valid_mark sysctl is set when running the container, so don't have WireGuard also set it
sed -i "s:sysctl -q net.ipv4.conf.all.src_valid_mark=1:echo Skipping setting net.ipv4.conf.all.src_valid_mark:" /usr/bin/wg-quick
2023-09-29 19:08:04 +02:00
# Start WireGuard
2019-02-14 21:03:36 +01:00
wg-quick up $interface
2023-09-29 19:08:04 +02:00
# IPv4 kill switch: traffic must be either (1) to the WireGuard interface, (2) marked as a WireGuard packet, (3) to a local address, or (4) to the container network
container_ipv4_network="$(ip -o addr show dev eth0 | awk '$3 == "inet" {print $4}')"
container_ipv4_network_rule=$([ ! -z "$container_ipv4_network" ] && echo "! -d $container_ipv4_network" || echo "")
iptables -I OUTPUT ! -o $interface -m mark ! --mark $(wg show $interface fwmark) -m addrtype ! --dst-type LOCAL $container_ipv4_network_rule -j REJECT
2019-05-13 07:21:16 +02:00
2023-09-29 19:08:04 +02:00
# IPv6 kill switch: traffic must be either (1) to the WireGuard interface, (2) marked as a WireGuard packet, (3) to a local address, or (4) to the container network
container_ipv6_network="$(ip -o addr show dev eth0 | awk '$3 == "inet6" {print $4}')"
if [[ "$container_ipv6_network" ]]; then
container_ipv6_network_rule=$([ ! -z "$container_ipv6_network" ] && echo "! -d $container_ipv6_network" || echo "")
ip6tables -I OUTPUT ! -o $interface -m mark ! --mark $(wg show $interface fwmark) -m addrtype ! --dst-type LOCAL $container_ipv6_network_rule -j REJECT
else
2023-09-29 19:08:04 +02:00
echo "IPv6 interface not found, skipping IPv6 kill switch" >&2
fi
2023-09-29 19:08:04 +02:00
# Allow traffic to local subnets
for local_subnet in ${LOCAL_SUBNETS//,/$IFS}
do
2023-09-29 19:08:04 +02:00
echo "Allowing traffic to local subnet ${local_subnet}" >&2
ip route add $local_subnet via $default_route_ip
iptables -I OUTPUT -d $local_subnet -j ACCEPT
done
2019-02-14 21:03:36 +01:00
shutdown () {
2023-09-29 19:08:04 +02:00
wg-quick down $interface
exit 0
2019-02-14 21:03:36 +01:00
}
trap shutdown SIGTERM SIGINT SIGQUIT
sleep infinity &
wait $!