change tunnelier config format

This commit is contained in:
Ville Rantanen
2023-01-13 14:45:11 +02:00
parent 8b475a5409
commit 115f631743

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
CONFDIR="$HOME/.config/ssh-tunnelier" CONFDIR="$HOME/.config/ssh-tunnelier"
CONF="$CONFDIR/config" CONF="$CONFDIR/tunnels.conf"
# Just over a year in minutes # Just over a year in minutes
MAGIC_TIME=525601 MAGIC_TIME=525601
@@ -25,7 +25,7 @@ If run with arguments, runs an ssh tunnel with arguments:
ssh-tunnelier hostname [local-port] remote-port ssh-tunnelier hostname [local-port] remote-port
If local port missing, the same port assumed in local If local port missing, the same port assumed in local
" "
exit 0 exit 0
} }
@@ -37,9 +37,9 @@ done
[[ -f "$CONF" ]] || { [[ -f "$CONF" ]] || {
echo No config file. echo No config file.
echo "$CONF" each line like: echo "$CONF" each line like:
echo -L 8888:localhost:8888 servername echo "servername: 8888,8889:8989,8890:host:9090"
mkdir -p "$CONFDIR" 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]+$' number_re='^[0-9]+$'
# import qolop # import qolop
@@ -62,10 +62,31 @@ function get_id() {
} }
function get_command() { 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\"" 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() { function get_pid() {
pgrep -f "$MAGIC_TIME.*echo tunnelier $1" | head -n 1 pgrep -f "$MAGIC_TIME.*echo tunnelier $1" | head -n 1
} }
@@ -98,13 +119,13 @@ function read_config() {
} }
function run_args() { function run_args() {
HOST="$1" HOST="$1"
LOCAL="$2" LOCAL="$2"
REMOTE="$3" REMOTE="$3"
[[ -z "$REMOTE" ]] && REMOTE="$LOCAL" [[ -z "$REMOTE" ]] && REMOTE="$LOCAL"
echo Connect to $HOST echo Connect to $HOST
switches=$( echo '-L "${LOCAL}:localhost:${REMOTE}" "$HOST"' | $BASE ) switches=$( echo '-L "${LOCAL}:localhost:${REMOTE}" "$HOST"' | $BASE )
run_command "$switches" run_command "$switches"
} }
function run_command() { function run_command() {
@@ -137,7 +158,7 @@ function instant_entry() {
} }
[[ -n "$2" ]] && { [[ -n "$2" ]] && {
run_args "$@" run_args "$@"
} }
while true; do 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} 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} (q)uit (e)dit (l)ist ssh (i)nstant tunnel${colZ}\n"
printf "${colTitle}=============================================\n" printf "${colTitle}=============================================\n"
printf "${colRow}ID PID command\n" printf "${colRow}ID PID host: ports\n"
read_config read_config
printf "$colZ" printf "$colZ"
read -t 600 input read -t 600 input
@@ -160,18 +181,18 @@ while true; do
[[ "$input" =~ $number_re ]] && { [[ "$input" =~ $number_re ]] && {
j=$(( $input - 1 )) j=$(( $input - 1 ))
[[ $j -gt $(( $i - 1 )) ]] && { [[ $j -gt $(( $i - 1 )) ]] && {
printf "${colWarn}No such tunnel number${colZ}\n" printf "${colWarn}No such tunnel number${colZ}\n"
continue continue
} }
printf "\n${colTitle}Tunnel command: %s${colZ}\n" "$( echo "${ids[$j]}" | $UNBASE )" printf "\n${colTitle}Tunnel command: %s${colZ}\n" "$( echo "${ids[$j]}" | $UNBASE )"
this_pid="$( get_pid "${ids[$j]}" )" this_pid="$( get_pid "${ids[$j]}" )"
[[ -n "$this_pid" ]] && { [[ -n "$this_pid" ]] && {
# PID exists, ask to kill # PID exists, ask to kill
ask_to_kill "$this_pid" ask_to_kill "$this_pid"
} }
[[ -z "$this_pid" ]] && { [[ -z "$this_pid" ]] && {
# PID empty, run # PID empty, run
run_command "${ids[$j]}" run_command "${ids[$j]}"
sleep 1 sleep 1
} }