Compare commits

...

6 commits

Author SHA1 Message Date
Hippolyte Chauvin
6a905e39cf ssh-fwd : prendre en compte les redirections en dehors d'un control
master
2023-11-11 17:57:40 +01:00
Hippolyte Chauvin
1f68407d9b ssh-fwd : remaniement du système d'analyse des arguments 2023-11-11 17:32:08 +01:00
Hippolyte Chauvin
7297666d6c Suppression de la fonction parse_variable 2023-11-11 17:01:48 +01:00
Hippolyte Chauvin
f1395291d7 ssh-fwd : changement de la pipeline pour checker l'existence de la
redirection
2023-11-11 16:56:32 +01:00
Hippolyte Chauvin
fcdfc805a2 Merge branch 'master' into reecriture/ssh-fwd 2023-11-11 16:37:43 +01:00
Hippolyte Chauvin
9ff68dda52 Réécriture en cours de ssh-fwd 2023-10-29 17:28:47 +01:00

View file

@ -1,68 +1,73 @@
#!/bin/bash #!/bin/bash
# Alias si le terminal est kitty step() {
if [ "$TERM" = xterm-kitty ]; then printf '\033[1;32m*\033[0m \033[1m%s...\033[0m\n' "$*"
ssh=(kitty +kitten ssh) }
else
ssh=(ssh)
fi
# Variables
declare -a args
# Foncitons
error() { error() {
>&2 printf '\033[1;31m%s\033[0m \033[1m%s\033[0m\n' ERROR: "$*" >&2 printf '\033[1;31mERROR:\033[0m \033[1m%s\033[0m\n' "$error"
return "$1"
} }
help() { error_null_arg() {
local name local error
name="$(basename "$0")" error="Argument \"${arg_name}\" can't be null"
cat << EOF error 1
${name}: utilisation:
${name} SERVEUR PORT_LOCAL PORT_DISTANT [CIBLE] [-- ARGUMENTS_SSH...]
EOF
} }
error_help() { # Lancer la redirection SSH
error "Invalid usage" start_forwarding() {
>&2 help ssh \
exit 1 -f -N \
-L "${local_port}:${target}:${distant_port}" \
"$@" \
"$ssh_host"
} }
requested_port_bridge_pid() { show_redirection_info() {
lsof -nP -i TCP -s TCP:LISTEN | awk '($1 == "ssh" && $9 ~ /.*:'"${1}"'/) { print $2 }' | uniq | head -n 1 lsof -i -P -n | awk '($1 == "ssh" && $10 == "(LISTEN)" && $9 ~ /.*:'"${local_port}"'/) { print }'
} }
# Analyser les arguments get_ssh_pid() {
while [ -n "$1" ]; do show_redirection_info | awk '{ print $2 }' | sort | uniq
case "$1" in }
--)
shift
break
;;
*)
args+=("$1")
shift
;;
esac
done
# Il doit y avoir au moins 3 arguments dans le tableau # Supprimer la redirection SSH
if [ "${#args[@]}" -lt 3 ]; then stop_forwarding() {
error_help start_forwarding -O cancel
if [ "$?" -eq 255 ]; then
echo "Okay then, brutally killing SSH..."
kill "$(get_ssh_pid)"
fi
}
# Retourner 0 si le port de la redirection est déjà utilisé par SSH
is_forwarding_active() {
[ -n "$(show_redirection_info)" ]
}
# Parser les variables
trap error_null_arg EXIT
set -e
arg_name='SSH host' ; ssh_host="$1" ; shift
arg_name='local port' ; local_port="$1" ; shift
arg_name='distant port'; distant_port="$1"; shift
trap - EXIT
unset arg_name
if [ -n "$1" ]
then target="$1"
else target=localhost
fi fi
pid="$(requested_port_bridge_pid "${args[1]}")" set +e
if [ -z "$pid" ]; then
exec "${ssh[@]}" \ # Branchement principal
-f \ if ! is_forwarding_active; then
-N \ step "Starting the redirection"
-L "${args[1]}:${args[3]:-localhost}:${args[2]}" \ start_forwarding
"${args[0]}" \
"$@"
else else
echo "Suppression de la redirection..." step "Stopping the redirection"
kill "$pid" stop_forwarding
fi fi