Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
masahide committed Oct 5, 2024
1 parent b800ca0 commit 46969ef
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions hack/ubuntu.wsl2-ssh-agent-proxy-fish.sh
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
#!/bin/sh
#!/usr/bin/env fish

NAME=wsl2-ssh-agent-proxy
export SSH_AUTH_SOCK="${HOME}/.ssh/${NAME}/${NAME}.sock"
PROXYCMD_DIR="${HOME}/${NAME}"
CMD="${PROXYCMD_DIR}/${NAME}"
set NAME "wsl2-ssh-agent-proxy"
set -x SSH_AUTH_SOCK "$HOME/.ssh/$NAME/$NAME.sock"
set PROXYCMD_DIR "$HOME/$NAME"
set CMD "$PROXYCMD_DIR/$NAME"

RELEASE_NAME=$1
REPO_URL=https://github.com/masahide/OmniSSHAgent
if [ -z "${RELEASE_NAME}" ]; then
VER_PATH="releases/latest"
set RELEASE_NAME $argv[1]
set REPO_URL "https://github.com/masahide/OmniSSHAgent"

if test -z "$RELEASE_NAME"
set VER_PATH "releases/latest"
else
VER_PATH="download/${RELEASE_NAME}"
fi

__get_proxy() {
echo "Downloading ${NAME}.gz"
mkdir -p "${PROXYCMD_DIR}"
curl "${REPO_URL}/releases/${VER_PATH}/${NAME}.gz" -sL | gunzip >"${CMD}"
chmod +x "${CMD}"
}

setup_proxy() {
[ -f "${CMD}" ] || __get_proxy

# Checks wether $SSH_AUTH_SOCK is a socket or not
(ps aux | grep "${CMD}" | grep -qv "grep") && [ -S "${SSH_AUTH_SOCK}" ] && return

# Create directory for the socket, if it is missing
SSH_AUTH_SOCK_DIR="$(dirname "${SSH_AUTH_SOCK}")"
mkdir -p "${SSH_AUTH_SOCK_DIR}"
# Applying best-practice permissions if we are creating ${HOME}/.ssh
if [ "${SSH_AUTH_SOCK_DIR}" = "${HOME}/.ssh" ]; then
chmod 700 "${SSH_AUTH_SOCK_DIR}"
fi

(setsid "${CMD}" >>"${PROXYCMD_DIR}/${NAME}.log" 2>&1 &)
}
set VER_PATH "download/$RELEASE_NAME"
end

function __get_proxy
echo "Downloading $NAME.gz"
mkdir -p "$PROXYCMD_DIR"
curl "$REPO_URL/releases/$VER_PATH/$NAME.gz" -sL | gunzip > "$CMD"
chmod +x "$CMD"
end

function setup_proxy
if not test -f "$CMD"
__get_proxy
end

# Checks whether $SSH_AUTH_SOCK is a socket or not
if pgrep -f "$CMD" > /dev/null; and test -S "$SSH_AUTH_SOCK"
return
end

# Create directory for the socket, if it is missing
set SSH_AUTH_SOCK_DIR (dirname "$SSH_AUTH_SOCK")
mkdir -p "$SSH_AUTH_SOCK_DIR"

# Applying best-practice permissions if we are creating $HOME/.ssh
if test "$SSH_AUTH_SOCK_DIR" = "$HOME/.ssh"
chmod 700 "$SSH_AUTH_SOCK_DIR"
end

setsid "$CMD" >> "$PROXYCMD_DIR/$NAME.log" 2>&1 &
end

setup_proxy

0 comments on commit 46969ef

Please sign in to comment.