This commit is contained in:
Gordon Grant-Stuart 2022-12-05 21:15:19 +00:00
parent ec3e1196b3
commit 255040e009
4 changed files with 32 additions and 28 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
var/*

15
etc/tunnelkeeper.conf Normal file
View File

@ -0,0 +1,15 @@
[tunnelkeeper]
debug 0
[ssh] # Everything below here obeys the same rules as ~/.ssh/config
# Examples
# Host server1
# Hostname 123.45.67.89
# User ubuntu
# LocalForward 10001 127.0.0.1:22
#
# Host server1
# Hostname 123.45.67.90
# User ubuntu
# LocalForward 10002 127.0.0.1:22

View File

@ -1,15 +0,0 @@
#
# TunnelKeeper config file
#
#: debug 0
#
Host server1
Hostname 123.45.67.89
User ubuntu
LocalForward 10001 127.0.0.1:22
Host server1
Hostname 123.45.67.90
User ubuntu
LocalForward 10002 127.0.0.1:22

View File

@ -3,12 +3,16 @@
REALPATH="$(realpath $0)" REALPATH="$(realpath $0)"
BASEDIR="${REALPATH%/*}" BASEDIR="${REALPATH%/*}"
mkdir -p "$BASEDIR/var" rm $BASEDIR/var/*.conf &>/dev/null # Create separate .conf files in var/ from etc/tunnelkeeper.conf
mkdir -p "$BASEDIR/etc" awk -v "dir=$BASEDIR/var" '
CONFFILE="$BASEDIR/etc/tunnels.conf" /^\[/ {sec=$1; gsub("[\[\]]","",sec)}
!/^\[/ {print >> dir"/"sec".conf"}' < $BASEDIR/etc/tunnelkeeper.conf
if [[ ! -f "$CONFFILE" ]]; then SSHCONF="$BASEDIR/var/ssh.conf"
echo "Config file \"$CONFFILE\" does not exist" TKCONF="$BASEDIR/var/tunnelkeeper.conf"
if [[ ! -f "$SSHCONF" ]]; then
echo "Config file \"$SSHCONF\" does not exist"
exit 1 exit 1
fi fi
@ -19,7 +23,7 @@ function ruroot () {
fi fi
} }
DEBUGLEVEL=$(cat "$CONFFILE" | grep -i "#: *debug" | grep -o [0-9]) DEBUGLEVEL=$(cat $TKCONF | grep -i "debug" | grep -o [0-9])
case "$DEBUGLEVEL" in case "$DEBUGLEVEL" in
2) dbgopt='-v';; 2) dbgopt='-v';;
3) dbgopt='-vvv';; 3) dbgopt='-vvv';;
@ -36,7 +40,7 @@ case "$1" in
FORKSTART ) FORKSTART )
touch "$BASEDIR/var/${2}.connected" touch "$BASEDIR/var/${2}.connected"
while [[ -e "$BASEDIR/var/${2}.connected" ]]; do while [[ -e "$BASEDIR/var/${2}.connected" ]]; do
ssh -F "${CONFFILE}" $dbgopt -N $2 2>&1 | dbg ssh -F "${SSHCONF}" $dbgopt -N $2 2>&1 | dbg
sleep 5 sleep 5
done done
;; ;;
@ -45,12 +49,12 @@ case "$1" in
start) start)
[[ -e "$BASEDIR/var/tunnelkeeper.pid" ]] && exit [[ -e "$BASEDIR/var/tunnelkeeper.pid" ]] && exit
echo $$ > "$BASEDIR/var/tunnelkeeper.pid" echo $$ > "$BASEDIR/var/tunnelkeeper.pid"
cat "$CONFFILE" | awk '/^Host / {print $2}' | xargs -I% -P0 $0 FORKSTART % & cat "$SSHCONF" | awk '/^Host / {print $2}' | xargs -I% -P0 $0 FORKSTART % &
;; ;;
stop) stop)
rm "$BASEDIR/var/tunnelkeeper.pid" rm "$BASEDIR/var/tunnelkeeper.pid"
cat "$CONFFILE" | awk '/^Host / {print $2}' | sed 's/^Host //' | xargs -I% -P0 $0 FORKDEL % cat "$SSHCONF" | awk '/^Host / {print $2}' | sed 's/^Host //' | xargs -I% -P0 $0 FORKDEL %
cat "$CONFFILE" | grep -o 'LocalForward [0-9]* ' | grep -o '[0-9]*' | xargs -I% -P0 $0 FORKKILL % cat "$SSHCONF" | grep -o 'LocalForward [0-9]* ' | grep -o '[0-9]*' | xargs -I% -P0 $0 FORKKILL %
;; ;;
restart) $0 stop; sleep 2; $0 start ;; restart) $0 stop; sleep 2; $0 start ;;
install ) install )
@ -58,8 +62,7 @@ case "$1" in
mkdir -p /opt/tunnelkeeper/var mkdir -p /opt/tunnelkeeper/var
mkdir -p /opt/tunnelkeeper/etc mkdir -p /opt/tunnelkeeper/etc
if [[ "$BASEDIR" != '/opt/tunnelkeeper/' ]]; then if [[ "$BASEDIR" != '/opt/tunnelkeeper/' ]]; then
cp -n "$BASEDIR/etc/tunnels.conf" /opt/tunnelkeeper/etc cp -n "$BASEDIR/etc/tunnelkeeper.conf" /opt/tunnelkeeper/etc/
cp "$BASEDIR/etc/tunnels.conf.example" /opt/tunnelkeeper/etc
cp "$REALPATH" "/opt/tunnelkeeper/tunnelkeeper" cp "$REALPATH" "/opt/tunnelkeeper/tunnelkeeper"
fi fi
ln -f -s /opt/tunnelkeeper/tunnelkeeper /usr/local/bin/tunnelkeeper ln -f -s /opt/tunnelkeeper/tunnelkeeper /usr/local/bin/tunnelkeeper
@ -96,7 +99,7 @@ WantedBy=multi-user.target" > /lib/systemd/system/tunnelkeeper.service
;; ;;
config ) config )
ruroot ruroot
vi "$CONFFILE" vi "$BASEDIR/etc/tunnelkeeper.conf"
;; ;;
* ) * )
echo -e "\nUsage: $(basename $0) start|stop|restart|install|uninstall|config|list\n" echo -e "\nUsage: $(basename $0) start|stop|restart|install|uninstall|config|list\n"