-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwfh-anywhere-vpn.sh
executable file
·204 lines (171 loc) · 5.57 KB
/
wfh-anywhere-vpn.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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env bash
WIREGUARD_IP=${WIREGUARD_IP}
WIREGUARD_INTERFACE_NAME=${WIREGUARD_INTERFACE_NAME}
NS=phy
stop_network_managers() {
# Stop network managers that could get in our way
systemctl stop NetworkManager.service
systemctl stop systemd-networkd.socket
systemctl stop systemd-networkd.service
systemctl stop systemd-resolved.service
systemctl is-active systemd-networkd
if [[ ${?} != 3 ]]
then
echo "systemd-networkd is still running"
exit 1
fi
systemctl is-active NetworkManager
if [[ ${?} != 3 ]]
then
echo "Network Manager is still running"
exit 1
fi
systemctl is-active systemd-resolved.service
if [[ ${?} != 3 ]]
then
echo "Systemd resolver is still running"
exit 1
fi
}
dhcp() {
case ${1} in
up)
ip netns exec phy dhclient -pf /var/run/dhclient.pid -4 -nw
;;
down)
ip netns exec phy dhclient -pf /var/run/dhclient.pid -r
;;
restart)
ip netns exec phy dhclient -pf /var/run/dhclient.pid -r
ip netns exec phy dhclient -pf /var/run/dhclient.pid -4 -nw
;;
*)
echo "oh no, this options is not supported"
;;
esac
}
ensure_netns_exists() {
NS_EXIST=$(ip netns list|grep -c ${NS})
if [[ ${NS_EXIST} == 0 ]]
then
ip netns add ${NS}
fi
}
move_ifs_to_netns() {
case ${1} in
start)
ensure_netns_exists
while true
do
IFs=$(ls -l /sys/class/net/|grep -v -e virtual -e wlan0 | awk '{print $9}')
for IF in $IFs
do
echo ${IF}
ip link set dev ${IF} down
ip link set ${IF} netns ${NS}
ip -n ${NS} link set dev ${IF} mtu 1500
done
[[ $(echo ${IFs}|wc -w) -gt 0 ]] && dhcp restart
sleep 5
done
;;
stop)
IFs=$(ip netns exec ${NS} ls -l /sys/class/net/|grep -v -e virtual -e wlan0 | awk '{print $9}')
for IF in $IFs
do
echo ${IF}
ip -n ${NS} link set dev ${IF} down
ip -n ${NS} link set ${IF} netns 1
done
dhcp stop
;;
*)
echo "missing options for netns"
;;
esac
}
wg_vpn() {
ensure_netns_exists
#Wireguard setup
ip -n ${NS} link add ${WIREGUARD_INTERFACE_NAME} type wireguard
ip -n ${NS} link set ${WIREGUARD_INTERFACE_NAME} netns 1
wg setconf ${WIREGUARD_INTERFACE_NAME} <(wg-quick strip /etc/wireguard/${WIREGUARD_INTERFACE_NAME}.conf)
iptables -I INPUT -p udp --dport ${WIREGUARD_PORT} -j ACCEPT
iptables -I INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sleep 1
ip addr add ${WIREGUARD_IP}/32 dev ${WIREGUARD_INTERFACE_NAME}
ip link set dev ${WIREGUARD_INTERFACE_NAME} mtu 1500
ip link set ${WIREGUARD_INTERFACE_NAME} up
ip route add default dev ${WIREGUARD_INTERFACE_NAME}
sysctl -w net.ipv4.ip_forward=1
}
hotspot() {
# prep the wlan0 interface for hostapd and dnsmasq by giving it an IP
ip addr add 10.0.0.1/24 dev wlan0
hostapd -B /etc/hostapd/simple.conf
sleep 1
echo "nameserver 127.0.0.53" > /etc/resolv.conf
systemctl restart dnsmasq.service
# enable local port forward from WiFi to Wireguard
iptables -t mangle -A PREROUTING -i wlan0 -j MARK --set-mark 0x30
iptables -t nat -A POSTROUTING ! -o wlan0 -m mark --mark 0x30 -j MASQUERADE
}
down() {
IF=$(ip -n ${NS} link|grep enx|cut -d: -f2|tr -d ' ')
IP_CIDR=$(ip -n ${NS} addr show ${IF}|grep -Eo "([0-9.]{7,25})/([0-9]{1,2})")
IP=${IP_CIDR%/*}
CIDR=${IP_CIDR#*/}
sysctl -w net.ipv4.ip_forward=0
iptables -t mangle -D PREROUTING -i wlan0 -j MARK --set-mark 0x30
iptables -t nat -D POSTROUTING ! -o wlan0 -m mark --mark 0x30 -j MASQUERADE
iptables -D INPUT -p udp --dport ${WIREGUARD_PORT} -j ACCEPT
iptables -D INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
killall hostapd
ip addr del 10.0.0.1/24 dev wlan0
ip route del default dev ${WIREGUARD_INTERFACE_NAME}
ip link set ${WIREGUARD_INTERFACE_NAME} down
ip addr del ${WIREGUARD_IP}/32 dev ${WIREGUARD_INTERFACE_NAME}
ip link del ${WIREGUARD_INTERFACE_NAME} type wireguard
ip -n ${NS} link set dev ${IF} down
ip -n ${NS} link set dev eth0 down
ip -n ${NS} addr del ${IP_CIDR} dev ${IF}
ip -n ${NS} link set ${IF} netns 1
ip -n ${NS} link set eth0 netns 1
ip netns del ${NS}
systemctl stop dnsmasq.service
# This ought to be fine if the script is just stopped with
# systemctl stop ...
# but this code also is ran when rebooting or powering down
# and then it messes up the process causing timout with waiting for this
# job to stop
#systemctl restart NetworkManager.service
#systemctl restart systemd-networkd.socket
#systemctl restart systemd-networkd.service
#systemctl restart systemd-resolved.service
dhclient -x
dhclient
echo "If you whish to disable the VPN then disable its service"
echo "with systemctl and then reboot."
}
case ${1} in
start)
stop_network_managers
wg_vpn
hotspot
move_ifs_to_netns start
;;
stop)
move_ifs_to_netns stop
down
;;
restart)
down
stop_network_managers
wg_vpn
hotspot
move_ifs_to_netns start
;;
*)
echo "oh no, this option is not supported"
;;
esac