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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
#!/usr/bin/env sh
set -e
OPTS=$(getopt -o hvs:a:x: --long help,verbose,sock:,args:,exec:,ssh,gpg,gpg-extra,gpg-ssh,gpg-browser,discord -n 'javawrap' -- "$@")
eval set -- "$OPTS"
GPG_AGENT_SOCK="${GPG_AGENT_SOCK:-$HOME/.gnupg/S.gpg-agent}"
# SSH_AUTH_SOCK="${SSH_AUTH_SOCK:-$HOME/.ssh/agent.sock}"
SSH_AUTH_SOCK="$GPG_AGENT_SOCK.ssh"
DISCORD_IPC_SOCK="${DISCORD_IPC_SOCK:-/var/run/discord-ipc-0}"
PAGEANT="$HOME/.ssh/wsl2-ssh-pageant.exe"
NPIPE="$(command -v npiperelay.exe)"
SCRIPT="$(basename "$0")"
help() {
cat <<-EOF
$SCRIPT
Toby Vincent <tobyv13@gmail.com>
$SCRIPT description
USAGE:
$SCRIPT [OPTIONS] <COMMAND>
$SCRIPT [OPTIONS]
OPTIONS:
-h, --help Display this message
-s, --sock <SOCKET> Path to the linux socket
-x, --exec <CMD> Command to run for socat's EXEC: arg
--ssh Relay the ssh agent's socket
--gpg Relay the gpg agent's socket
--gpg-extra Relay the gpg agent's extra socket
ARGS:
status
Check the status of the socat process
start (default)
Start the socat process
stop
Kill the socat process
restart
Same as $($SCRIPT stop && $SCRIPT start)
EOF
}
say() {
if ! $quiet; then
echo "$SCRIPT: $1"
fi
}
say_verbose() {
if $verbose; then
say "$@"
fi
}
say_err() {
say "$1" >&2
}
err() {
# shellcheck disable=2145
say_err "ERROR: $@"
exit 1
}
need() {
for need_cmd in "$@"; do
if ! command -v "$need_cmd" >/dev/null 2>&1; then
err "need $need_cmd (command not found)"
fi
done
}
start() {
say_verbose "SOCKET: $sock"
say_verbose "ARGS: $args"
say_verbose "EXEC: $exec"
if [ -z "$exec" ]; then
err "No EXEC provided. Must supply either one of the preset options, or provide an explicit value with --exec"
fi
if [ -z "$sock" ]; then
err "No socket provided."
fi
if ! command -v "$relay" >/dev/null 2>&1; then
err "$relay is not executable."
fi
if ! pgrep -fa "socat.*$sock.*$relay,"; then
rm -rf "$sock"
(setsid nohup socat UNIX-LISTEN:"${sock},${args}" EXEC:"$exec" >/dev/null 2>&1 &)
fi
if $gpg; then
gpg-connect-agent.exe /bye >"$v_stdout" 2>"$v_stderr"
fi
}
stop() {
if [ -z "$sock" ]; then
err "No socket provided."
fi
if [ -z "$relay" ]; then
err "No exec provided"
fi
if $gpg; then
gpg-connect-agent.exe KILLAGENT /bye >"$v_stdout" 2>"$v_stderr"
fi
pkill -fe "socat.*$sock.*$relay" >"$v_stdout"
rm -rf "$sock"
}
status() {
proc="socat.*${sock:-}.*${relay:-}"
if ! pgrep -fa "$proc"; then
say_err "No process found"
return 1
fi
}
quiet=false
verbose=false
sock=""
exec=""
gpg=false
gpg_extra=false
gpg_ssh=false
gpg_browser=false
ssh=false
discord=false
v_stdout=/dev/null
v_stderr=/dev/null
while test $# -gt 0; do
case $1 in
-h | --help)
help
return 0
;;
-q | --quiet)
quiet=true
shift
;;
-v | --verbose)
verbose=true
v_stdout=/dev/stdout
v_stderr=/dev/stderr
shift
;;
-s | --sock)
sock=$2
shift
shift
;;
-l | --args)
args=$2
shift
shift
;;
-x | --exec)
exec=$2
shift
shift
;;
--ssh)
ssh=true
shift
;;
--gpg)
gpg=true
shift
;;
--gpg-extra)
gpg=true
gpg_extra=true
shift
;;
--gpg-ssh)
gpg=true
gpg_ssh=true
shift
;;
--gpg-browser)
gpg=true
gpg_browser=true
shift
;;
--discord)
discord=true
shift
;;
--)
shift
break
;;
*)
help
exit 1
;;
esac
done
need ss
need socat
if $ssh; then
need "$PAGEANT"
sock="${ssh_sock:-$SSH_AUTH_SOCK}"
args="${ssh_args:-fork}"
exec="${ssh_exec:-$PAGEANT}"
elif $gpg; then
need "$PAGEANT"
need "gpg-connect-agent.exe"
config_path="C\:/Users/$USER/AppData/Local/gnupg"
case ${1:-'start'} in
"$gpg_extra")
sock="${gpg_sock:-$GPG_AGENT_SOCK}.extra"
;;
"$gpg_ssh")
sock="${gpg_sock:-$GPG_AGENT_SOCK}.ssh"
;;
"$gpg_browser")
sock="${gpg_sock:-$GPG_AGENT_SOCK}.browser"
;;
*)
sock="${gpg_sock:-$GPG_AGENT_SOCK}"
;;
esac
args="${gpg_args:-fork}"
exec="${gpg_exec:-$PAGEANT --gpgConfigBasepath ${config_path} --gpg $(basename "$sock")}"
elif $discord; then
need "$NPIPE"
sock="${DISCORD_IPC_SOCK:-/var/run/discord-ipc-0}"
exec="$NPIPE -ep -s //./pipe/$(basename "$sock")"
args="fork,group=discord,umask=007"
fi
relay="$(echo "$exec" | head -n1 | awk '{print $1;}')"
case ${1:-'start'} in
status)
status
;;
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
help
exit 1
;;
esac
|