# sharefile: Share encrypted files using nc


# cat sharefile
#!/bin/bash

basename=`which basename`
cat=`which cat`
fi=`which file`
kill=`which kill`
mcrypt=`which mcrypt`
mv=`which mv`
nc=`which nc`
rm=`which rm`
sleep=`which sleep`
tput=`which tput`

function color {
        normal=`$tput sgr0`
        green=`$tput setaf 2`
        yellow=`$tput setaf 3`
        cyan=`$tput setaf 6`
        case $1 in
                green)  echo -ne "$green$2$normal"      ;;
                yellow) echo -ne "$yellow$2$normal"     ;;
                cyan)   echo -ne "$cyan$2$normal"       ;;
                *)      echo -ne "$2$normal"            ;;
        esac
}

function info {
        color yellow "[$1]: " && $fi --brief "$file"
}

function clean {
        if [ -e "$file.nc" ]; then $rm "$file.nc"; fi
}

function bar {
        color cyan $1; $sleep 0.1; printf "\b"
}

function progress {
        if [ "$1" == "s" ]; then
                color green "$2 "
                while [ true ]; do
                        bar '-'; bar '\'; bar '|'; bar '/'
                done
        elif [ "$1" == "e" ]; then
                $kill $2 && echo ''
        fi
}

function receive {
        listen=$1
        if [ -e "$file" ]; then $rm --interactive "$file"; fi
        exec 2> /dev/null
        progress s 'receiving' &
        pid=$!
        $nc $listen $ip $port > "$file"
        progress e $pid
        if $encrypt ; then
                $mv "$file" "$file.nc"
                progress s 'decrypting' &
                pid=$!
                $mcrypt --decrypt               \
                        --hash $hash            \
                        --bare                  \
                        --quiet                 \
                        --unlink                \
                        --key $password         \
                        --algorithm $algorithm  \
                        "$file.nc"
                progress e $pid
                clean
        fi
        info "$file"
}

function send {
        listen=$1
        exec 2> /dev/null
        info "$file"
        if $encrypt ; then
                clean
                progress s 'encrypting' &
                pid=$!
                $mcrypt --hash $hash            \
                        --bare                  \
                        --quiet                 \
                        --key $password         \
                        --algorithm $algorithm  \
                        "$file"
                progress e $pid
        fi
        progress s 'sending' &
        pid=$!
        if $encrypt ; then filename="$file.nc"; else filename="$file"; fi
        $cat "$filename" | $nc $listen $ip $port
        progress e $pid
        clean
}

function usage {
$cat << eof
Usage:
        `$basename $0` [-h] {-m cr|cs|lr|ls} {-i ip} {-p port} [-n] {-f file}
Options:
        -m: Mode
                cs: Connect and receive
                cs: Connect and send
                lr: Listen and receive
                ls: Listen and send
        -i: IP
        -p: Port
        -f: File
        -n: No encrypt
eof
}

password='p@ssw0rd!'
algorithm='rijndael-192'
hash='sha1'

mode=''
ip=''
port=''
file=''
encrypt=true

while getopts "hm:i:p:f:n" option; do
        case $option in
                h)      usage && exit   ;;
                m)      mode=$OPTARG    ;;
                i)      ip=$OPTARG      ;;
                p)      port=$OPTARG    ;;
                f)      file=$OPTARG    ;;
                n)      encrypt=false   ;;
        esac
done

if [ -z $ip ] || [ -z $port ] || [ -z $file ]; then
        usage && exit
fi

case $mode in
        cr)     receive                 ;;
        cs)     send                    ;;
        lr)     receive -l              ;;
        ls)     send -l                 ;;
        *)      usage && exit
esac
Listen and receive + connect and send

remote# sharefile -m lr -i 192.168.1.10 -p 1234 -f file.mp3
receiving \
decrypting |
[file.mp3]: Audio file with ID3 version 2.4.0, contains: MPEG ADTS, layer III, v1,  32 kbps, 44.1 kHz, Stereo
local# sharefile -m cs -i 192.168.1.10 -p 1234 -f file.mp3
[file.mp3]: Audio file with ID3 version 2.4.0, contains: MPEG ADTS, layer III, v1,  32 kbps, 44.1 kHz, Stereo
encrypting -
sending /
Listen and send + connect and receive

remote# sharefile -m ls -i 192.168.1.10 -p 1234 -f file.mp3
[file.mp3]: Audio file with ID3 version 2.4.0, contains: MPEG ADTS, layer III, v1,  32 kbps, 44.1 kHz, Stereo
encrypting \
sending /
local# sharefile -m cr -i 192.168.1.10 -p 1234 -f file.mp3
receiving \
decrypting /
[file.mp3]: Audio file with ID3 version 2.4.0, contains: MPEG ADTS, layer III, v1,  32 kbps, 44.1 kHz, Stereo

No comments: