minor: docs
[vpn-setup] / vpn-server-setup
index 797594620aa416921054df9076af86959ed7bdba..69ce5416c47fec3c13dad237930962fd1cfdddaa 100755 (executable)
@@ -33,13 +33,15 @@ usage: ${0##*/} [OPTIONS] [IPV6_ADDR/BITS]
 -s   Do not start openvpn
 -h --help print help
 
+IPV6_ADDR/BITS    Ipv6 address of the vpn interface.
+
 Sets up a vpn server which pushes gateway route and dns server so all
 traffic goes through the vpn. requires systemd, and might have some
 debian specific paths.
 
 For ipv6, we assume ipv6_addr routes to the server.
 
-You can save all the keys by storing /etc/openvpn/easy-rsa/keys, and
+You can save all the keys by storing /etc/openvpn/easy-rsa-NAME/keys, and
 the script will not generate them if it sees they exist already.
 
 For future updates to this script, this is a good place to
@@ -77,7 +79,7 @@ read -r ip6 ip6route <<<"$@"
 
 source /a/bin/distro-functions/src/package-manager-abstractions
 
-pi-nostart openvpn openssl resolvconf easy-rsa uuid-runtime
+pi-nostart openvpn openssl easy-rsa uuid-runtime
 
 if [[ -e /lib/systemd/system/openvpn-server@.service ]]; then
   vpn_service=openvpn-server@$name
@@ -261,31 +263,23 @@ sysctl -p /etc/sysctl.conf
 
 gw=$(ip route | sed -rn 's/^default via .* dev (\S+).*/\1/p' | head -n1)
 
-cat >/etc/systemd/system/vpnnat.service <<EOF
-[Unit]
-Description=Turns on nat iptables setting
-
-[Install]
-WantedBy=$vpn_service.service
-
+d=/etc/systemd/system/$vpn_service.service.d
+mkdir -p $d
+f=$d/nat.conf
+cat >$f <<EOF
 [Service]
-Type=oneshot
-RemainAfterExit=yes
-ExecStart=/sbin/iptables -t nat -A POSTROUTING -s $ip4.0/24 -o $gw -j MASQUERADE
-ExecStop=/sbin/iptables -t nat -D POSTROUTING -s $ip4.0/24 -o $gw -j MASQUERADE
+ExecStartPre=/sbin/iptables -t nat -A POSTROUTING -s $ip4.0/24 -o $gw -j MASQUERADE
+ExecStopPost=/sbin/iptables -t nat -D POSTROUTING -s $ip4.0/24 -o $gw -j MASQUERADE
 EOF
-
-
 if [[ $ip6net ]]; then
-  cat >>/etc/systemd/system/vpnnat.service <<EOF
-ExecStart=/sbin/ip6tables -t nat -A POSTROUTING -s $ip6net -o $gw -j MASQUERADE
-ExecStop=/sbin/ip6tables -t nat -D POSTROUTING -s $ip6net -o $gw -j MASQUERADE
+  cat >>$f <<EOF
+ExecStartPre=/sbin/ip6tables -t nat -A POSTROUTING -s $ip6net -o $gw -j MASQUERADE
+ExecStopPost=/sbin/ip6tables -t nat -D POSTROUTING -s $ip6net -o $gw -j MASQUERADE
 EOF
-systemctl daemon-reload # needed if the file was already there
-# note, no need to start it, the vpn_service does that.
-systemctl enable vpnnat
+  systemctl daemon-reload # needed if the file was already there
 
-if $start; then
-  systemctl enable $vpn_service
-  systemctl restart $vpn_service
+  if $start; then
+    systemctl enable $vpn_service
+    systemctl restart $vpn_service
+  fi
 fi