Skip to content

Instantly share code, notes, and snippets.

@au-phiware
Last active July 26, 2017 23:06
Show Gist options
  • Save au-phiware/28acf942f77e3f2fac5b7a079bd27d62 to your computer and use it in GitHub Desktop.
Save au-phiware/28acf942f77e3f2fac5b7a079bd27d62 to your computer and use it in GitHub Desktop.
Transient file sharing bash client based on GPG and redis.
MIT License
Copyright (c) 2017 Corin Lawson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#!/bin/bash -ue
function -init {
: ${GPG_BIN:=$(which gpg)}
: ${ENCRYPT_ARGS:= -qasr $(resolve-id)}
: ${DECRYPT_ARGS:= -q}
: ${TEMPFILE:=/dev/null}
exec {DB}<> /dev/tcp/"${PIGEONHOLE_HOST:=172.17.0.2}"/"${PIGEONHOLE_PORT:=6379}"
exec {TTY}>&1
trap '( -destroy )' EXIT
}
function -destroy {
exec {DB}>&-
exec {DB}<&-
exec {TTY}>&-
}
function get {
redis-get LPOP "$(resolve-id "$*")" | decrypt
}
function put {
local rcpt
local duplex=$(tempfile)
trap "rm '$duplex'" EXIT
cat > "$duplex"
resolve-id "$@" | while read rcpt; do
cat "$duplex" | encrypt "$rcpt" | redis-put LPUSH "$rcpt"
done
}
function length {
redis-length "$(resolve-id "$*")"
}
function redis-get {
printf "%s \"%s\"\r\n" $1 $2 >&$DB
read -u $DB count
[[ "${count:1:-1}" -le 0 ]] && return
dd bs=1 count=${count:1:-1} status=none <&$DB
read <&$DB
}
function redis-put {
local line
printf "%s \"%s\" \"" $1 $2
while read line; do
printf '%s\\n' "${line//\"/\\\"}"
done
printf "\"\r\n"
read -u $DB
echo ${REPLY#:} >&$TTY
} >&$DB
function redis-length {
printf "LLEN \"%s\"\r\n" $1 >&$DB
read -u $DB count
echo ${count:1:-1}
}
function resolve-id {
if [[ ${#@} -gt 0 && ! -z "$1" ]]; then
for rcpt in "$@"; do
"${GPG_BIN}" --with-colons --list-public-keys "$rcpt" | read-first-id pub
done
else
"${GPG_BIN}" --with-colons --list-secret-keys | read-first-id sec
fi
}
function read-first-id {
local line
while read line; do
case "$line" in
$1:*)
local -a fields=(${line//:/ })
if [[ ! -z "${fields[4]}" ]]; then
echo ${fields[4]}
return
fi
;;
esac
done
}
function decrypt {
tee "$TEMPFILE" | "${GPG_BIN}"${DECRYPT_ARGS} --decrypt -
}
function encrypt {
"${GPG_BIN}"${ENCRYPT_ARGS} -r $1 --encrypt - | tee "$TEMPFILE"
}
function inspect {
set -o
type "$@"
}
function verbose {
[[ "$TEMPFILE" == "/dev/null" ]] && TEMPFILE=$(tempfile -p pigeon)
set -o xtrace
-main "$@"
}
function -main {
[[ "$(type -t $1)" =~ function ]] && "$@"
}
-init
-main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment