-
Notifications
You must be signed in to change notification settings - Fork 20
/
wsl-ssh-agent-relay
executable file
·183 lines (151 loc) · 4.57 KB
/
wsl-ssh-agent-relay
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/bash
#### Add following lines to your shell rc file (.zshrc .bashrc)
# ${HOME}/.local/bin/wsl-ssh-agent-relay start
# export SSH_AUTH_SOCK=${HOME}/.ssh/wsl-ssh-agent.sock
# If you do not want the ssh agent relay require your ssh agent
# to be running at the time relay is started add the option -s
# to wsl-ssh-agent-relay.
# For debugging startup problems uncomment next line
# exec 2> >(tee -a -i "$HOME/error.log")
#### Assuming ~/winhome links to %USERPROFILE on Windows side
RELAY_BIN="${HOME}/winhome/.wsl/npiperelay.exe"
PIDFILE="${HOME}/.ssh/wsl-ssh-agent-relay.pid"
WSL_AGENT_SSH_SOCK="${HOME}/.ssh/wsl-ssh-agent.sock"
log() {
echo >&2 "$@"
}
is_pid_running() {
if [[ -z "$1" ]]; then
return 1
fi
ps -p "$1" >/dev/null
return $?
}
_cleanup() {
log "Cleaning up relay to ${WSL_AGENT_SSH_SOCK}..."
if is_pid_running "${SOCAT_WSL_AGENT_SSH_PID}"; then
kill -SIGTERM "${SOCAT_WSL_AGENT_SSH_PID}" || log "Failed."
fi
}
die() {
if [[ -n "$1" ]]; then
log "$1"
fi
log "Exiting."
exit 1
}
usage() {
log "Usage: wsl-ssh-agent-relay [OPTIONS] COMMAND"
log ""
log " SUMMARY: Relay Windows openssh named pipe to local SSH socket in order to integrate WSL2 and host."
log " To debug use foreground command"
log ""
log " OPTIONS:"
log " -h|--help this page"
log ""
log " -v|--verbose verbose mode"
log ""
log " -s|--skip-test skip ssh-agent communication test"
log ""
log " COMMAND: start, stop, foreground"
}
fg_opts() {
FG_OPTS=()
# Generate opts for passing it to foreground version
if [[ -n "$VERBOSE" ]]; then
FG_OPTS+=("-v")
fi
if [[ -n "$NO_COM_TEST" ]]; then
FG_OPTS+=("-c")
fi
}
main() {
POSITIONAL=()
VERBOSE=""
SKIP_SSH_TEST=""
while (($# > 0)); do
case "$1" in
-v | --verbose)
VERBOSE="ENABLED"
shift # shift once since flags have no values
;;
-s | --skip-test)
SKIP_SSH_TEST="TRUE"
shift
;;
-h | --help)
usage
exit 0
;;
*) # unknown flag/switch
POSITIONAL+=("$1")
shift
if [[ "${#POSITIONAL[@]}" -gt 1 ]]; then
usage
die
fi
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional params
if [[ -z "$VERBOSE" ]]; then
QUIET="QUIET"
fi
case "${POSITIONAL[0]}" in
start)
fg_opts
start-stop-daemon --start --oknodo --pidfile "${PIDFILE}" --name wsl-ssh-agent-r --make-pidfile --background --startas "$0" ${VERBOSE:+--verbose} ${QUIET:+--quiet} -- foreground "${FG_OPTS[@]}"
;;
stop)
start-stop-daemon --pidfile "${PIDFILE}" --stop --remove-pidfile ${VERBOSE:+--verbose} ${QUIET:+--quiet}
;;
status)
start-stop-daemon --pidfile "${PIDFILE}" --status ${VERBOSE:+--verbose} ${QUIET:+--quiet}
local result=$?
case $result in
0) log "$0 is running" ;;
1 | 3) log "$0 is not running" ;;
4) log "$0 unable to determine status" ;;
esac
return $result
;;
foreground)
relay
;;
*)
usage
die
;;
esac
}
relay() {
trap _cleanup EXIT
[[ -f "${RELAY_BIN}" ]] || die "Unable to access ${RELAY_BIN}"
if pgrep -fx "^ssh-agent\s.+" >/dev/null; then
log "Killing previously started local ssh-agent..."
SSH_AGENT_PID="$(pidof ssh-agent)" ssh-agent -k >/dev/null 2>&1
fi
if [ -e "${WSL_AGENT_SSH_SOCK}" ]; then
log "WSL has been shutdown ungracefully, leaving garbage behind"
rm "${WSL_AGENT_SSH_SOCK}"
fi
socat UNIX-LISTEN:"\"${WSL_AGENT_SSH_SOCK}\"",fork EXEC:"\"\'${RELAY_BIN}\' -ei -s \'//./pipe/openssh-ssh-agent\'\"",nofork 1>/dev/null 2>&1 &
SOCAT_WSL_AGENT_SSH_PID="$!"
if ! is_pid_running "${SOCAT_WSL_AGENT_SSH_PID}"; then
log "Relay for ${SOCAT_WSL_AGENT_SSH_PID} failed"
return 1
fi
log "Relay is running with PID: ${SOCAT_WSL_AGENT_SSH_PID}"
if [[ -z "$SKIP_SSH_TEST" ]]; then
local res
log -n "Polling remote ssh-agent..."
SSH_AUTH_SOCK="${WSL_AGENT_SSH_SOCK}" ssh-add -L >/dev/null 2>&1
res=$?
[[ "${res}" -ge 2 ]] && die "[${res}] Failure communicating with ssh-agent"
log "OK"
fi
# Everything necessary checks, we are ready for actions
log "Entering wait..."
wait ${SOCAT_WSL_AGENT_SSH_PID}
}
main "$@"