This commit is contained in:
Gordon Grant-Stuart 2023-02-20 10:52:10 +00:00
parent ac7c56527d
commit 4d35eeae37
2 changed files with 31 additions and 11 deletions

View File

@ -1,5 +1,6 @@
[settings] [settings]
debug 0 debug 0
timeout 30
[ssh] [ssh]
# Everything in this section obeys the same rules as ~/.ssh/config # Everything in this section obeys the same rules as ~/.ssh/config

View File

@ -22,28 +22,34 @@ function ruroot () {
} }
DEBUGLEVEL=$(awk '/^debug/ {print $2}' $TKCONF &>/dev/null) DEBUGLEVEL=$(awk '/^debug/ {print $2}' $TKCONF &>/dev/null)
[[ -z $DEBUGLEVEL ]] && DEBUGLEVEL='0'
case "$DEBUGLEVEL" in case "$DEBUGLEVEL" in
2) dbgopt='-v';; 2) dbgopt='-v';;
3) dbgopt='-vvv';; 3) dbgopt='-vvv';;
*) dbgopt='';; *) dbgopt='';;
esac esac
TIMEOUT=$(awk '/^timeout/ {print $2}' $TKCONF &>/dev/null)
[[ -z $TIMEOUT ]] && TIMEOUT='60'
function dbg () { function dbg () {
[[ $DEBUGLEVEL != "0" ]] && logger -t tunnelkeeper [[ $DEBUGLEVEL != "0" ]] && logger -t tunnelkeeper
} }
function connect () { function connect () {
ssh -F "${SSHCONF}" $dbgopt -N $1 '#tunnelkeeper' 2>&1 | dbg # ssh -F "${SSHCONF}" $dbgopt -N $1 '#tunnelkeeper' 2>&1 | dbg
ssh -F "${SSHCONF}" $dbgopt -o "ControlMaster auto" -S "$BASEDIR/var/$1.tksock" -N $1 '#tunnelkeeper' 2>&1 | dbg
} }
case "$1" in case "$1" in
FORKSTART ) FORKSTART )
echo -n $$ > "$BASEDIR/var/${2}.connected" # echo -n $$ > "$BASEDIR/var/${2}.connected"
if [[ $(grep -c "^$2" $PWCONF) -gt 0 ]]; then if [[ $(grep -c "^$2" $PWCONF) -gt 0 ]]; then
pass=$(awk "/^$2/ {print \$2}" ${PWCONF}) # password needed pass=$(awk "/^$2/ {print \$2}" ${PWCONF}) # password needed
screen -d -m -S "tk${2}" $0 FORKSCREEN $2 screen -d -m -S "tk${2}" $0 FORKSCREEN $2 '#tunnelkeeper'
while [[ -e "$BASEDIR/var/${2}.connected" ]]; do # while [[ -e "$BASEDIR/var/${2}.connected" ]]; do
while true; do
sleep 5 sleep 5
if [[ -f "$BASEDIR/var/${2}.screen" ]]; then if [[ -f "$BASEDIR/var/${2}.screen" ]]; then
screen -S "tk${2}" -X stuff "$pass screen -S "tk${2}" -X stuff "$pass
@ -52,29 +58,41 @@ case "$1" in
fi fi
done done
else # passwordless auth else # passwordless auth
while [[ -e "$BASEDIR/var/${2}.connected" ]]; do # while [[ -e "$BASEDIR/var/${2}.connected" ]]; do
while true; do
connect $2 connect $2
sleep 5 sleep 5
done done
fi fi
$0 FORKWATCH $2 '#tunnelkeeper' &
exit exit
;; ;;
FORKWATCH ) # makes sure the connection is still working, even if ssh doesn't drop it
while true; do
sleep $TIMEOUT
if [[ $(timeout $TIMEOUT ssh localhost -S $BASEDIR/var/$2.tksock "echo tk") != "tk" ]]; then
ssh localhost -O exit -S $BASEDIR/var/$2.tksock
fi
done
;;
FORKSCREEN ) FORKSCREEN )
while [[ -e "$BASEDIR/var/${2}.connected" ]]; do # while [[ -e "$BASEDIR/var/${2}.connected" ]]; do
while true; do
echo $$ > "$BASEDIR/var/${2}.screen" echo $$ > "$BASEDIR/var/${2}.screen"
connect $2 connect $2
sleep 5
done done
exit exit
;; ;;
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 "$SSHCONF" | awk '/^Host / {print $2}' | xargs -I% -P0 $0 FORKSTART % & cat "$SSHCONF" | awk '/^Host / {print $2}' | xargs -I% -P0 $0 FORKSTART % '#tunnelkeeper' &
;; ;;
stop ) stop )
rm -f "$BASEDIR/var/tunnelkeeper.pid" rm -f "$BASEDIR/var/tunnelkeeper.pid"
rm -f "$BASEDIR/var/*.connected" # rm -f "$BASEDIR/var/*.connected"
pkill -f 'tunnelkeeper' &>/dev/null pkill -f '#tunnelkeeper' &>/dev/null
;; ;;
restart ) restart )
if systemctl status tunnelkeeper &>/dev/null; then if systemctl status tunnelkeeper &>/dev/null; then
@ -85,7 +103,7 @@ case "$1" in
;; ;;
install ) install )
ruroot ruroot
which screen &>/dev/null || yum install -y screen || apt install -y screen || echo "Couldn't install screen" && exit which screen &>/dev/null || yum install -y screen || apt install -y screen || echo "Couldn't install screen"
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
@ -121,7 +139,8 @@ WantedBy=multi-user.target" > /lib/systemd/system/tunnelkeeper.service
;; ;;
list ) list )
echo "---" echo "---"
find "$BASEDIR/var/" -name '*.connected' | sed 's/^.*\///g; s/\.connected//g' # find "$BASEDIR/var/" -name '*.connected' | sed 's/^.*\///g; s/\.connected//g'
find "$BASEDIR/var/" -name '*.tksock' | sed 's/^.*\///g; s/\.tksock//g'
echo "---" echo "---"
;; ;;
config ) config )