diff --git a/.config/zsh/functions/forward-local-port b/.config/zsh/functions/forward-local-port deleted file mode 100755 index abb433b..0000000 --- a/.config/zsh/functions/forward-local-port +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env zsh -# Forward a local port to a server via SSH tunnel. - -setopt LOCAL_OPTIONS ERR_RETURN NO_UNSET PIPE_FAIL - -zmodload zsh/zutil -local -a o_help=() -local -a o_lport=() -local -a o_rport=() -zparseopts -D -K -- h=o_help -help=o_help -local-port:=o_lport -remote-port:=o_rport -if [[ ${#o_help} -ne 0 || ! -v 1 || ${#o_lport} -ne 2 || ${#o_rport} -ne 2 ]]; then - local ret=$(( ${#o_help} ^ 1 )) - print -u $(( 1 + ${ret} )) "usage: ${0} [-h|--help]" \ - "--local-port --remote-port " - return ${ret} -fi -local host=${1} -local lport=${o_lport[2]} -local rport=${o_rport[2]} - -print -P "%F{12}Forwarding local port%f %F{13}${lport}%f %F{12}over SSH tunnel" \ - "to%f" "%F{13}${host}%f %F{12}port%f %F{13}${rport}%f %F{12}…%f" -ssh -R ${rport}:"[::1]":${lport} -N -T ${host} diff --git a/.config/zsh/functions/forward-port b/.config/zsh/functions/forward-port new file mode 100755 index 0000000..c5dd837 --- /dev/null +++ b/.config/zsh/functions/forward-port @@ -0,0 +1,41 @@ +#!/usr/bin/env zsh +# Forward a port to or from a server via SSH tunnel. + +setopt LOCAL_OPTIONS ERR_RETURN NO_UNSET PIPE_FAIL + +zmodload zsh/zutil +local -a o_help=() +local -a o_to_us=() +local -a o_from_us=() +local -a o_lport=() +local -a o_rport=() +zparseopts -D -K -- h=o_help -help=o_help -to-us=o_to_us -from-us=o_from_us \ + -local-port:=o_lport -remote-port:=o_rport + +if [[ ${#o_help} -ne 0 || ! -v 1 || ${#o_lport} -ne 2 || ${#o_rport} -ne 2 ]]; then + local ret=$(( ${#o_help} ^ 1 )) + print -u $(( 1 + ${ret} )) "usage: ${0} [-h|--help]" \ + "--local-port --remote-port " + return ${ret} +fi +if [[ ${#o_to_us} -ne 2 && ${#o_from_us} -ne 2 ]]; then + print -u 2 "no direction given, assuming --to-us" +fi + +local host=${1} +local direction="-L" # from remote to us +if [[ ${#o_from_us} -ne 2 ]]; then + direction="-R" +fi +local lport=${o_lport[2]} +local rport=${o_rport[2]} + +if [[ ${direction} == "-L" ]]; then + print -P "%F{12}forwarding remote port%f %F{13}${rport}%f %F{12}over SSH tunnel" \ + "to%f" "%F{13}${host}%f %F{12}port%f %F{13}${lport}%f %F{12}…%f" + ssh -L ${lport}:"[::1]":${rport} -N -T ${host} +elif [[ ${direction} == "-R" ]]; then + print -P "%F{12}forwarding local port%f %F{13}${lport}%f %F{12}over SSH tunnel" \ + "to%f" "%F{13}${host}%f %F{12}port%f %F{13}${rport}%f %F{12}…%f" + ssh -R ${rport}:"[::1]":${lport} -N -T ${host} +fi