-
Notifications
You must be signed in to change notification settings - Fork 33
/
setup-netns.sh
executable file
·32 lines (27 loc) · 1.19 KB
/
setup-netns.sh
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
#!/bin/sh
. ./config.sh
# Create netns
ip netns add ${NETNS}
# Create veth
ip link add ${NIC} type veth peer name ${NIC}-ns
ip link set ${NIC}-ns netns ${NETNS}
ip addr add ${HOST_VETH_IP}/30 dev ${NIC}
ip netns exec ${NETNS} ip addr add ${NS_VETH_IP}/30 dev ${NIC}-ns
ip link set ${NIC} up
ip netns exec ${NETNS} ip link set ${NIC}-ns up
# Setup WG for host
wg genkey > host-privkey 2> /dev/null
wg genkey > ns-privkey 2> /dev/null
ip link add ${NIC}-wg type wireguard
wg set ${NIC}-wg listen-port ${HOST_PORT} private-key host-privkey
ip addr add ${HOST_WG_IP}/32 dev ${NIC}-wg peer ${NS_WG_IP}
wg set ${NIC}-wg peer $(wg pubkey < ns-privkey) allowed-ips ${NS_WG_IP}/32 endpoint ${NS_VETH_IP}:${NS_PORT}
ip link set ${NIC}-wg up
# Setup WG for netns
ip netns exec ${NETNS} ip link add ${NIC}-wg type wireguard
ip netns exec ${NETNS} wg set ${NIC}-wg listen-port ${NS_PORT} private-key $PWD/ns-privkey
ip netns exec ${NETNS} ip addr add ${NS_WG_IP}/32 dev ${NIC}-wg peer ${HOST_WG_IP}
ip netns exec ${NETNS} wg set ${NIC}-wg peer $(wg pubkey < $PWD/host-privkey) allowed-ips ${HOST_WG_IP}/32 endpoint ${HOST_VETH_IP}:${HOST_PORT}
ip netns exec ${NETNS} ip link set ${NIC}-wg up
rm host-privkey
rm ns-privkey