diff --git a/.gitignore b/.gitignore index 0f71c0a..d507042 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ target vpncloud-oldnodes ._* .~* -deb/vpncloud/vpncloud* +deb/vpncloud/vpncloud +deb/vpncloud/vpncloud.1* Stats.ods diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e9716c..80b980a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ This project follows [semantic versioning](http://semver.org). ### Unreleased +- [added] Systemd compatible startup scripts - [changed] Repeatedly resolving connect addresses to allow DynDNS - [changed] Listening on IPv4 and IPv6 - [changed] Using SO_REUSEADDR to allow frequent rebinding diff --git a/deb/vpncloud/Makefile b/deb/vpncloud/Makefile index 5c6bdd9..ee5cb7c 100644 --- a/deb/vpncloud/Makefile +++ b/deb/vpncloud/Makefile @@ -10,3 +10,4 @@ install: install -d $(DESTDIR)/run install -d $(DESTDIR)/usr/bin install -m 755 vpncloud $(DESTDIR)/usr/bin/vpncloud + install -m 755 vpncloud-control $(DESTDIR)/usr/bin/vpncloud-control diff --git a/deb/vpncloud/debian/vpncloud.init b/deb/vpncloud/debian/vpncloud.init index 1125518..65815ae 100755 --- a/deb/vpncloud/debian/vpncloud.init +++ b/deb/vpncloud/debian/vpncloud.init @@ -26,12 +26,10 @@ USER=root GROUP=root UMASK=022 -NETWORKS=$(for net in $(ls $NETCONFIGS); do basename $net .net; done); - -DAEMON=$(which $NAME) +CONTROL=$(which $NAME-control) # Exit if the package is not installed -[ -x $DAEMON ] || exit 0 +[ -x $CONTROL ] || exit 0 # Read configuration variable file if it is present [ -r /etc/default/$NAME ] && . /etc/default/$NAME @@ -43,108 +41,32 @@ DAEMON=$(which $NAME) # Depend on lsb-base (>= 3.0-6) to ensure that this file is present. . /lib/lsb/init-functions -do_status() { - for net in $NETWORKS; do - if start-stop-daemon --status --pidfile /run/$NAME-$net.pid --name $NAME; then - echo -e "\t$net" - fi - done -} - -do_start() { - # Return - # 0 if daemon has been started - # 1 if daemon was already running - # 2 if daemon could not be started - for net in $NETWORKS; do - ENABLED=0 - unset DEVICE LISTEN TYPE MODE SHARED_KEY CRYPTO IFUP IFDOWN NETWORK_ID PEER_TIMEOUT DST_TIMEOUT PEERS SUBNETS - [ -f "$NETCONFIGS/$net.net" ] && . $NETCONFIGS/$net.net - if [ $ENABLED -eq 1 ]; then - PARAMS="" - [ -z "$DEVICE" ] || PARAMS="$PARAMS --device $DEVICE" - [ -z "$LISTEN" ] || PARAMS="$PARAMS --listen $LISTEN" - [ -z "$TYPE" ] || PARAMS="$PARAMS --type $TYPE" - [ -z "$MODE" ] || PARAMS="$PARAMS --mode $MODE" - [ -z "$SHARED_KEY" ] || PARAMS="$PARAMS --shared-key '$SHARED_KEY'" - [ -z "$CRYPTO" ] || PARAMS="$PARAMS --crypto $CRYPTO" - [ -z "$IFUP" ] || PARAMS="$PARAMS --ifup '$IFUP'" - [ -z "$IFDOWN" ] || PARAMS="$PARAMS --ifdown '$IFDOWN'" - [ -z "$NETWORK_ID" ] || PARAMS="$PARAMS --network-id $NETWORK_ID" - [ -z "$PEER_TIMEOUT" ] || PARAMS="$PARAMS --peer-timeout $PEER_TIMEOUT" - [ -z "$DST_TIMEOUT" ] || PARAMS="$PARAMS --peer-timeout $DST_TIMEOUT" - for peer in $PEERS; do - PARAMS="$PARAMS --connect $peer" - done - for subnet in $SUBNETS; do - PARAMS="$PARAMS --subnet $subnet" - done - start-stop-daemon --start --pidfile /run/$NAME-$net.pid --make-pidfile --name $NAME --background --startas /bin/sh -- -c "exec $DAEMON $PARAMS >/var/log/vpncloud-$net.log 2>&1" - fi - done - return 0 -} - -do_stop() { - # Return - # 0 if daemon has been stopped - # 1 if daemon was already stopped - # 2 if daemon could not be stopped - # other if a failure occurred - for net in $NETWORKS; do - start-stop-daemon --stop --quiet --pidfile /run/$NAME-$net.pid --name $NAME --retry 60 - done - return 0 -} - -do_reload() { - # - # If the daemon can reload its configuration without - # restarting (for example, when it is sent a SIGHUP), - # then implement that here. - # - return 0 -} - case "$1" in start) log_begin_msg "Starting $DESC" "$NAME" - do_start + $CONTROL start case "$?" in 0|1) log_end_msg 0 ;; 2) log_end_msg 1 ;; esac - ;; + ;; stop) log_begin_msg "Stopping $DESC" "$NAME" - do_stop + $CONTROL stop case "$?" in 0|1) log_end_msg 0; exit 0 ;; 2) log_end_msg 1; exit 2 ;; esac ;; status) - do_status - ;; - #reload|force-reload) - # - # If do_reload() is not implemented then leave this commented out - # and leave 'force-reload' as an alias for 'restart'. - # - #log_daemon_msg "Reloading $DESC" "$NAME" - #do_reload - #log_end_msg $? - #;; + $CONTROL status + ;; restart|force-reload) - # - # If the "reload" option is implemented then remove the - # 'force-reload' alias - # log_begin_msg "Restarting $DESC" "$NAME" - do_stop + $CONTROL stop case "$?" in 0|1) - do_start + $CONTROL start case "$?" in 0) log_end_msg 0 ;; 1) log_end_msg 1 ;; # Old process is still running diff --git a/deb/vpncloud/debian/vpncloud.service b/deb/vpncloud/debian/vpncloud.service new file mode 100644 index 0000000..ee175a6 --- /dev/null +++ b/deb/vpncloud/debian/vpncloud.service @@ -0,0 +1,11 @@ +[Unit] +Description=VpnCloud networks + +[Service] +Type=forking +ExecStart=/usr/bin/vpncloud-control start +ExecStop=/usr/bin/vpncloud-control stop +RemainAfterExit=yes + +[Install] +WantedBy=multi-user.target diff --git a/deb/vpncloud/vpncloud-control b/deb/vpncloud/vpncloud-control new file mode 100755 index 0000000..f219f31 --- /dev/null +++ b/deb/vpncloud/vpncloud-control @@ -0,0 +1,93 @@ +#!/bin/bash + +NAME=vpncloud +NETCONFIGS=/etc/vpncloud + +# default settings +USER=root +GROUP=root +UMASK=022 + +NETWORKS=$(for net in $(ls $NETCONFIGS); do basename $net .net; done); + +DAEMON=$(which $NAME) + +# Exit if the package is not installed +[ -x $DAEMON ] || exit 0 + +# Read configuration variable file if it is present +[ -r /etc/default/$NAME ] && . /etc/default/$NAME + +status() { + echo "Network status" + for net in $NETWORKS; do + echo -en "\t$net: " + if start-stop-daemon --status --pidfile /run/$NAME-$net.pid --name $NAME; then + echo "running" + else + echo "not running" + fi + done +} + +start() { + # Return + # 0 if daemon has been started + # 1 if daemon was already running + # 2 if daemon could not be started + for net in $NETWORKS; do + ENABLED=0 + unset DEVICE LISTEN TYPE MODE SHARED_KEY CRYPTO IFUP IFDOWN NETWORK_ID PEER_TIMEOUT DST_TIMEOUT PEERS SUBNETS + [ -f "$NETCONFIGS/$net.net" ] && . $NETCONFIGS/$net.net + if [ $ENABLED -eq 1 ]; then + PARAMS="" + [ -z "$DEVICE" ] || PARAMS="$PARAMS --device $DEVICE" + [ -z "$LISTEN" ] || PARAMS="$PARAMS --listen $LISTEN" + [ -z "$TYPE" ] || PARAMS="$PARAMS --type $TYPE" + [ -z "$MODE" ] || PARAMS="$PARAMS --mode $MODE" + [ -z "$SHARED_KEY" ] || PARAMS="$PARAMS --shared-key '$SHARED_KEY'" + [ -z "$CRYPTO" ] || PARAMS="$PARAMS --crypto $CRYPTO" + [ -z "$IFUP" ] || PARAMS="$PARAMS --ifup '$IFUP'" + [ -z "$IFDOWN" ] || PARAMS="$PARAMS --ifdown '$IFDOWN'" + [ -z "$NETWORK_ID" ] || PARAMS="$PARAMS --network-id $NETWORK_ID" + [ -z "$PEER_TIMEOUT" ] || PARAMS="$PARAMS --peer-timeout $PEER_TIMEOUT" + [ -z "$DST_TIMEOUT" ] || PARAMS="$PARAMS --peer-timeout $DST_TIMEOUT" + for peer in $PEERS; do + PARAMS="$PARAMS --connect $peer" + done + for subnet in $SUBNETS; do + PARAMS="$PARAMS --subnet $subnet" + done + start-stop-daemon --start --pidfile /run/$NAME-$net.pid --make-pidfile --name $NAME --background --startas /bin/sh -- -c "exec $DAEMON $PARAMS >/var/log/vpncloud-$net.log 2>&1" + fi + done + return 0 +} + +stop() { + # Return + # 0 if daemon has been stopped + # 1 if daemon was already stopped + # 2 if daemon could not be stopped + # other if a failure occurred + for net in $NETWORKS; do + start-stop-daemon --stop --quiet --pidfile /run/$NAME-$net.pid --name $NAME --retry 60 + done + return 0 +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + status) + status + ;; + *) + echo "Unsupported action: '$1'" >&2 + exit 3 + ;; +esac