From b4526f94ccac6e505d363a2b9ce1a1783046af55 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Fri, 5 May 2023 09:27:10 +0200 Subject: [PATCH] Use WSL ssh-agent by default --- utils/bin/start | 70 +++++++++++++++++++++++-------------------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/utils/bin/start b/utils/bin/start index 93cb968..2b52000 100644 --- a/utils/bin/start +++ b/utils/bin/start @@ -1,18 +1,20 @@ #!/bin/sh -usage () { +usage() { echo "usage: $0 [-w] [-h]" - echo " -w use the WSL ssh-agent instead of OpenSSH for Windows" + echo " -w use OpenSSH for Windows instead of WSL ssh-agent" echo " -h show help and exit" } -require() { - if ! command -v "$@" >/dev/null 2>&1; then - >&2 echo "ERROR: required command not found: $1" - exit 1 - fi +error() { + printf "ERROR: %s\n" "$1" >&2 + exit 1 } -start_with_wsl_agent() { +require() { + command -v "$@" >/dev/null 2>&1 || error "'$1' not found" +} + +start_wsl_agent() { if [ "$#" -gt 0 ]; then SHLVL=0 exec ssh-agent "$SHELL" -c "$@" else @@ -20,15 +22,18 @@ start_with_wsl_agent() { fi } -start_with_windows_agent() { +start_windows_agent() { require socat require npiperelay # Forward SSH Agent requests from withitn WSL to OpenSSH for Windows, based on: # https://stuartleeks.com/posts/wsl-ssh-key-forward-to-windows/ - export SSH_AUTH_SOCK=$HOME/.ssh/agent.sock + export SSH_AUTH_SOCK="$HOME/.ssh/agent.sock" [ -S "$SSH_AUTH_SOCK" ] && rm "$SSH_AUTH_SOCK" - socat UNIX-LISTEN:$SSH_AUTH_SOCK,fork EXEC:"npiperelay -ei -s //./pipe/openssh-ssh-agent",nofork & + socat \ + UNIX-LISTEN:$SSH_AUTH_SOCK,fork \ + EXEC:"npiperelay -ei -s //./pipe/openssh-ssh-agent",nofork \ + & if [ "$#" -gt 0 ]; then SHLVL=0 exec "$SHELL" -c "$@" @@ -37,32 +42,23 @@ start_with_windows_agent() { fi } -# Use OpenSSH for Windows by default. -WSL_SSH_AGENT=false +main() { + WSL_SSH_AGENT=yes + while getopts ":wh" arg; do + case "$arg" in + w) WSL_SSH_AGENT=no;; + h) usage; exit 0;; + ?) usage >&2; error "unknown option -$OPTARG";; + esac + shift + done -while getopts ":wh" arg; do - case "$arg" in - w) - WSL_SSH_AGENT=true - ;; + if [ "$WSL_SSH_AGENT" = "yes" ]; then + start_wsl_agent "$@" + else + start_windows_agent "$@" + fi +} - h) - usage - exit 0 - ;; - - ?) - >&2 echo "ERROR: unknown option -$OPTARG" - >&2 usage - exit 1 - ;; - esac - shift -done - -if [ "$WSL_SSH_AGENT" = true ]; then - start_with_wsl_agent "$@" -else - start_with_windows_agent "$@" -fi +main "$@"