-
Notifications
You must be signed in to change notification settings - Fork 0
/
wg_namespace_cli
executable file
·68 lines (56 loc) · 1.99 KB
/
wg_namespace_cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
# A simple script that manages network namespace routed through a wireguard interface
# Usage:
# $0 [up|down]
# Some configuration is available through environment variables
# See manual for details
#
# Author: Vladimir Timofeenko <[email protected]>
# Bugs-to: https://github.com/VTimofeenko/wireguard-namespace-service/issues
# License: Unlicense
set -euo pipefail
set -o errtrace
set -x
# STOCK_SERVICE variable should be set by the accompanying systemd service
if [ -z ${STOCK_SERVICE+x} ]; then
BASE_CONF_DIR=${CREDENTIALS_DIRECTORY:-'/etc/wireguard'}
else
BASE_CONF_DIR=${BASE_CONF_DIR:-'/etc/wireguard'}
fi
command="$1"
conf_file="$2"
# The default name to be used for link name, config file name, etc.
# Useful to override all of them at the same time
DEFAULT_NAME="wg0"
NAMESPACE_NAME=${NAMESPACE_NAME:-${DEFAULT_NAME}}
LINK_NAME=${LINK_NAME:-${DEFAULT_NAME}}
WG_CONF_FILE=${WG_CONF_FILE:-"${BASE_CONF_DIR}/${conf_file:-"${DEFAULT_NAME}"}.conf"}
trap down ERR
up() {
if [ -z ${MY_IP+x} ]; then
echo "MY_IP variable is unset, please set it"
exit 1;
fi
ip netns add "$NAMESPACE_NAME"
# create the interface
ip link add "${LINK_NAME}" type wireguard
# move the ${LINK_NAME} device into netns
ip link set "${LINK_NAME}" netns "$NAMESPACE_NAME"
# set device config
ip netns exec "${NAMESPACE_NAME}" wg setconf "${LINK_NAME}" "${WG_CONF_FILE}"
ip -n "${NAMESPACE_NAME}" addr add "${MY_IP}" dev "${LINK_NAME}"
ip -n "${NAMESPACE_NAME}" link set mtu 1420 up dev "${LINK_NAME}"
# all routing will go through "${LINK_NAME}"
ip -j -n "${NAMESPACE_NAME}" route add default dev "${LINK_NAME}"
echo "Namespace ${NAMESPACE_NAME} is up"
}
down() {
ip -n "${NAMESPACE_NAME}" link set down dev "${LINK_NAME}"
ip -n "${NAMESPACE_NAME}" link del dev "${LINK_NAME}"
ip netns del "${NAMESPACE_NAME}"
echo "Namespace ${NAMESPACE_NAME} is down"
}
case "$command" in
up) up "$@" ;;
down) down "$@" ;;
esac