From 115f631743015048c49f54f76ce60c3a1969fbd8 Mon Sep 17 00:00:00 2001 From: Ville Rantanen Date: Fri, 13 Jan 2023 14:45:11 +0200 Subject: [PATCH] change tunnelier config format --- web/ssh-tunnelier | 61 +++++++++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/web/ssh-tunnelier b/web/ssh-tunnelier index 505800f..9071177 100755 --- a/web/ssh-tunnelier +++ b/web/ssh-tunnelier @@ -1,7 +1,7 @@ #!/bin/bash CONFDIR="$HOME/.config/ssh-tunnelier" -CONF="$CONFDIR/config" +CONF="$CONFDIR/tunnels.conf" # Just over a year in minutes MAGIC_TIME=525601 @@ -25,7 +25,7 @@ If run with arguments, runs an ssh tunnel with arguments: ssh-tunnelier hostname [local-port] remote-port If local port missing, the same port assumed in local -" + " exit 0 } @@ -37,9 +37,9 @@ done [[ -f "$CONF" ]] || { echo No config file. echo "$CONF" each line like: - echo -L 8888:localhost:8888 servername + echo "servername: 8888,8889:8989,8890:host:9090" mkdir -p "$CONFDIR" - echo -e "# example:\n -L 8888:localhost:8888 servername" > "$CONF" + echo -e "# example:\nservername: 8888,8889:8989,8890:host:9090" > "$CONF" } number_re='^[0-9]+$' # import qolop @@ -62,10 +62,31 @@ function get_id() { } function get_command() { - switches=$( echo "$1" | $UNBASE ) + switches=$( echo "$1" | $UNBASE | parse_switches ) echo -n "ssh -f -n $switches \"nice /bin/bash -c 'for ((i=1;i<$MAGIC_TIME;i++)); do cut -f4 -d \\\" \\\" /proc/\\\$PPID/stat | xargs kill -0 || exit ; sleep 60;done'; echo tunnelier $1\"" } +function parse_switches() { + python3 -c " +import sys +host, tunnels = sys.stdin.read(1024*10).split(':',1) + +for i, tunnel in enumerate(tunnels.split(',')): + tunnel = tunnel.strip().split(':') + thost='localhost' + if len(tunnel) == 1: + tport=tunnel[0] + if len(tunnel) == 2: + tport=tunnel[1] + if len(tunnel) == 3: + thost=tunnel[1] + tport=tunnel[2] + sys.stdout.write('-L {}:{}:{} '.format(tunnel[0], thost, tport)) + sys.stderr.write('Generated tunnel {}: http://localhost:{} -> {}:{}\n'.format(i+1,tunnel[0],thost, tport)) +sys.stdout.write(host) + " +} + function get_pid() { pgrep -f "$MAGIC_TIME.*echo tunnelier $1" | head -n 1 } @@ -98,13 +119,13 @@ function read_config() { } function run_args() { - HOST="$1" - LOCAL="$2" - REMOTE="$3" - [[ -z "$REMOTE" ]] && REMOTE="$LOCAL" - echo Connect to $HOST - switches=$( echo '-L "${LOCAL}:localhost:${REMOTE}" "$HOST"' | $BASE ) - run_command "$switches" + HOST="$1" + LOCAL="$2" + REMOTE="$3" + [[ -z "$REMOTE" ]] && REMOTE="$LOCAL" + echo Connect to $HOST + switches=$( echo '-L "${LOCAL}:localhost:${REMOTE}" "$HOST"' | $BASE ) + run_command "$switches" } function run_command() { @@ -137,7 +158,7 @@ function instant_entry() { } [[ -n "$2" ]] && { - run_args "$@" + run_args "$@" } while true; do @@ -147,7 +168,7 @@ while true; do printf "${colTitle} List of tunnels: (${LOCALHOSTSYMBOL}=localhost) [%s]${colZ}\n" $( date +%H:%M ) printf "${colTitle} (q)uit (e)dit (l)ist ssh (i)nstant tunnel${colZ}\n" printf "${colTitle}=============================================\n" - printf "${colRow}ID PID command\n" + printf "${colRow}ID PID host: ports\n" read_config printf "$colZ" read -t 600 input @@ -160,18 +181,18 @@ while true; do [[ "$input" =~ $number_re ]] && { j=$(( $input - 1 )) [[ $j -gt $(( $i - 1 )) ]] && { - printf "${colWarn}No such tunnel number${colZ}\n" - continue + printf "${colWarn}No such tunnel number${colZ}\n" + continue } printf "\n${colTitle}Tunnel command: %s${colZ}\n" "$( echo "${ids[$j]}" | $UNBASE )" this_pid="$( get_pid "${ids[$j]}" )" [[ -n "$this_pid" ]] && { - # PID exists, ask to kill - ask_to_kill "$this_pid" - } + # PID exists, ask to kill + ask_to_kill "$this_pid" + } [[ -z "$this_pid" ]] && { - # PID empty, run + # PID empty, run run_command "${ids[$j]}" sleep 1 }