-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgpg-transceiver
executable file
·70 lines (62 loc) · 1.56 KB
/
gpg-transceiver
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
#!/bin/sh
usage() {
cat >&2 <<EOF
usage: `basename $0` [-l] <host> <port> -r <user> [<args>...]
-l specifies "listen" mode as opposed to "connect" mode.
<user> is the user/key ID of the recipient on the other end.
<host> is the hostname to connect to or listen on.
<port> is the port to connect to or listen on.
<arg> specify additional arguments to gpg used during encryption.
EOF
exit 1
}
listen=n
if [ "$1" = -l ]; then
listen=y
shift
fi
if [ $# -lt 2 ]; then
usage
fi
host=$1
shift
port=$1
shift
# only render these characters in the message;
# this prevents weird control characters from being sent to stdout
chars='a-zA-Z0-9 ,./<>?:;"'\''\\|][{}[-]=_+`~!@#$%^&*()
'
# get the escape prefix for later use
esc=`printf '\033['`
send_lines() {
while read -r line; do
printf '%s\n' "$line" | gpg -e -s "$@";
echo .
done
}
run_netcat() {
if [ $listen = y ]; then
nc -l -p "$port" "$host"
else
nc "$host" "$port"
fi
}
recv_lines() {
while
while :; do
read -r line || exit
if [ "$line" = . ]; then
break
else
printf '%s\n' "$line"
fi
done | gpg -d; do
:;
done
}
{ { send_lines "$@" | run_netcat | recv_lines |
# post-process stdout and stderr;
# loop is needed to ensure line-buffering
while read -r line; do printf '%s\n' "$line" | tr -d -c "$chars"; done |
sed 's/^\(.*\)$/'"$esc"'34m> \1'"$esc"'0m/'; } 3>&1 1>&2 2>&3; } |
sed 's/^\(.*\)$/'"$esc"'33m| \1'"$esc"'0m/'; 3>&1 1>&2 2>&3