diff --git a/utils/bin/start b/utils/bin/start index 39f956b..6910ae2 100644 --- a/utils/bin/start +++ b/utils/bin/start @@ -5,6 +5,38 @@ usage () { 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 +} + +start_with_wsl_agent() { + if [ "$#" -gt 0 ]; then + SHLVL=0 exec ssh-agent "$SHELL" -c "$@" + else + SHLVL=0 exec ssh-agent "$SHELL" + fi +} + +start_with_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 + [ -S "$SSH_AUTH_SOCK" ] && rm "$SSH_AUTH_SOCK" + 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 "$@" + else + SHLVL=0 exec "$SHELL" + fi +} + # Use OpenSSH for Windows by default. WSL_SSH_AGENT=false @@ -29,30 +61,8 @@ while getopts ":wh" arg; do done if [ "$WSL_SSH_AGENT" = true ]; then - if [ "$#" -gt 0 ]; then - SHLVL=0 exec ssh-agent "$SHELL" -c "$@" - else - SHLVL=0 exec ssh-agent "$SHELL" - fi -fi - -require() { - if ! command -v "$@" >/dev/null 2>&1; then - >&2 echo "ERROR: required command not found: $1" - exit 1 - fi -} - -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 -[ -S "$SSH_AUTH_SOCK" ] && rm "$SSH_AUTH_SOCK" -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 "$@" + start_with_wsl_agent else - SHLVL=0 exec "$SHELL" + start_with_windows_agent fi +