Skip to content

Instantly share code, notes, and snippets.

@twilligon
Created May 16, 2024 22:43
Show Gist options
  • Save twilligon/4a6dca48422acc8f8c623cbd0921a3f4 to your computer and use it in GitHub Desktop.
Save twilligon/4a6dca48422acc8f8c623cbd0921a3f4 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -eu
NETNS="$(printf '%s' "$1" | sed -n 's#.*/\([^.]*\)\..*#\1#p')"
mkdir -p "/etc/netns/${NETNS}"
sed -nr 's/^DNS = (.*)/\1/p' "$1" | tr ',' '\n' | sed 's/^/nameserver /' > "/etc/netns/${NETNS}/resolv.conf"
ip netns del "${NETNS}" 2>/dev/null || :
ip netns add "${NETNS}"
ip link add "${NETNS}" type wireguard
sed 's/^Address =/#Address =/g;s/^DNS =/#DNS =/' "$1" | wg setconf "${NETNS}" /dev/stdin
ip link set "${NETNS}" netns "${NETNS}"
sed -nr 's/^Address = (.*)/\1/p' "$1" | tr ',' '\n' | sed "s/^/ip -n ${NETNS} addr add /;s/\$/ dev ${NETNS}/" | sh
ip -n "${NETNS}" link set "${NETNS}" up
ip -n "${NETNS}" route add default dev "${NETNS}"
ip -n "${NETNS}" link set lo up
@twilligon
Copy link
Author

License: CC0-1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment