Skip to content

Commit

Permalink
Added a few comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Zi1mo5zo committed Nov 22, 2018
1 parent 6ae3a56 commit 49b4d38
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions ipfs-wormhole.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,60 @@ function checkdep() {
case "${1:-}" in

send)

# Get path to deps
PWGENCMD="$(checkdep pwgen)"
TARCMD="$(checkdep tar)"
GPGCMD="$(checkdep gpg)"
IPFSCMD="$(checkdep ipfs)"
PASSWORD=$($PWGENCMD -1 $IWPASSWORDLENGTH)

# Handle user input
USERINPUT=${2:-}
FILE=${USERINPUT%/}

# Choose between symmetric/asymmetric encryption
if [ -z "${IWKEYBASEDENCRYPTION-}" ]; then
GPGCMDOPTS="-c"
else
GPGCMDOPTS="-e"
fi

# Check if ipfs is running and if no, start it
if ! pgrep ipfs 1>/dev/null 2>&1; then
echo "IPFS is not running, starting the daemon and sleep 5 seconds"
$IPFSCMD daemon &
sleep 5
fi

# Set passphrase for asymmetric encryption, if required
if [ "$GPGCMDOPTS" == "-c" ]; then
GPGCMDOPTS="--batch --passphrase=$PASSWORD $GPGCMDOPTS"
fi

if [ -d "$FILE" ]; then
# If FILE is a directory (!): compress, encrypt and add to IFPS
IFS=' '
TAG=$($TARCMD -Jc "$FILE" | $GPGCMD $GPGCMDOPTS -o - | $IPFSCMD add -Q)
IFS=$'\n\t'
FILE="$FILE".tar.xz
elif [ -f "$FILE" ]; then
# If FILE is a file :) : encrypt and add to IFPS
IFS=' '
TAG=$($GPGCMD $GPGCMDOPTS -o - "$FILE" | $IPFSCMD add -Q)
IFS=$'\n\t'
else
# If FILE is neither a file or a directory, exit
echo "error: $FILE is neither a file, nor a directory"
exit 1
fi

# Create the tag: <IPFSHASH>-<PASSWORD>-<BASE64 ENCODED FILENAME>
FILENAME="$(echo "$FILE" | base64)"
FULLTAG="$TAG-$PASSWORD-$FILENAME"
RECEIVECMD="$0 receive $FULLTAG"

# Check if xclip is available, and send the tag to the clipboard
set +e
XCLIPCMD="$(command -v xclip)"
set -e
Expand All @@ -71,44 +89,59 @@ send)
echo "$FULLTAG" | $XCLIPCMD
EXTRA="(copied to clipboard)"
fi

# Output
echo
echo "$FILE sent, tag: $FULLTAG $EXTRA"
echo
echo "Retrieve it with $RECEIVECMD"
echo

exit 0
;;

receive)

# Get path to deps
GPGCMD="$(checkdep gpg)"

# Handle user input
USERINPUT=${2:-}
IPFSHASH=${USERINPUT%%-*}
DSTFILENAME="$(echo "${USERINPUT##*-}" | base64 -d)"
PASSWORD=${USERINPUT%-*}
PASSWORD=${PASSWORD#*-}

# Check if the file already exists, protect it if its size is >0
if [ -s "$DSTFILENAME" ]; then
echo "File $DSTFILENAME already exists, aborting..."
exit 1
fi

# Check whether IPFS is running
if pgrep ipfs 1>/dev/null 2>&1; then
# If yes, download from IPFS
IPFSCMD="$(checkdep ipfs)"
echo "Receiving $DSTFILENAME over IPFS..."
$IPFSCMD cat "$IPFSHASH" |
$GPGCMD --batch --passphrase="$PASSWORD" -d \
>"$DSTFILENAME" \
2>/dev/null
else
# If no, download from an IPFS gateway
echo "Receiving $DSTFILENAME over HTTPS..."
WGETCMD="$(checkdep wget)"
$WGETCMD -qO - "$IWIPFSGATEWAY"/"$IPFSHASH" |
$GPGCMD --batch --passphrase="$PASSWORD" -d \
>"$DSTFILENAME" \
2>/dev/null
fi

exit 0
;;

checkdeps)
# Check if all dependencies are installed
PWGENCMD="$(checkdep pwgen)"
TARCMD="$(checkdep tar)"
GPGCMD="$(checkdep gpg)"
Expand All @@ -119,6 +152,7 @@ checkdeps)
;;

update)
# Update directly from github
WGETCMD="$(checkdep wget)"
echo Update...
$WGETCMD -O "${0:-}" \
Expand All @@ -128,6 +162,7 @@ update)
;;

*)
# Show usage, i.e. download the readme from github.
WGETCMD="$(checkdep wget)"
$WGETCMD -O- -q \
https://raw.githubusercontent.com/aurelg/ipfs-wormhole/master/README.md
Expand Down

0 comments on commit 49b4d38

Please sign in to comment.