2020-05-20 17:48:13 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# ###
|
|
|
|
# Simple testeur d'envoi de message via la messagerie de Cesium ou de Gchange.
|
|
|
|
# ###
|
|
|
|
|
2020-11-05 13:46:07 +01:00
|
|
|
[[ -z $(which jq) || -z $(which curl) ]] && echo "Installation de jq et curl ..." && sudo apt update && sudo apt install jq curl -y
|
|
|
|
|
2020-11-05 02:19:04 +01:00
|
|
|
[[ ! -f .env ]] && cp .env.template .env
|
2020-11-04 15:16:03 +01:00
|
|
|
source .env
|
2020-05-20 17:48:13 +02:00
|
|
|
|
2020-11-04 15:16:03 +01:00
|
|
|
# Help display
|
|
|
|
helpOpt() {
|
2020-11-10 11:38:08 +01:00
|
|
|
echo -e "Cesium+ messages sender
|
|
|
|
Default: ask title, content and recipient in interactive mode.
|
|
|
|
Advice: Fill your .env file for more fun.
|
2020-11-17 01:11:30 +01:00
|
|
|
Example: $0 -f <Path of file content message> -r <recipient pubkey> -k <path of pubsec keychain of issuer>
|
2020-11-05 02:10:04 +01:00
|
|
|
|
|
|
|
\rOptions:
|
|
|
|
-t\t\t\t\tTest mode: Uses the \"test.txt\" file as well as the same recipient as the sender.
|
|
|
|
-f,--file <file>\t\tRead the file <file> with title in first line and content in rest of the file for the message.
|
|
|
|
-r,--recipient <pubkey>\tUses <pubkey> as recipient of the message.
|
2020-11-10 11:38:08 +01:00
|
|
|
-k,--key <key>\t\tPath <key> to the pubsec keychain file of the issuer.
|
|
|
|
-h,--help\t\t\tDisplay this help"
|
2020-11-04 15:16:03 +01:00
|
|
|
}
|
|
|
|
|
2020-11-05 01:31:40 +01:00
|
|
|
REGEX_PUBKEYS="[a-zA-Z0-9]{42,44}"
|
|
|
|
|
2020-11-04 15:16:03 +01:00
|
|
|
# Parse options
|
|
|
|
declare -a args=($@)
|
|
|
|
for ((i=0; i<${#args[*]}; ++i))
|
|
|
|
do
|
|
|
|
case ${args[$i]} in
|
2020-11-05 01:31:40 +01:00
|
|
|
-f|--file) file="${args[$i+1]}"
|
|
|
|
[[ ! -f $file ]] && echo "Le fichier $file n'existe pas." && exit 1;;
|
2020-11-05 02:10:04 +01:00
|
|
|
-t|--test) file="test.txt"
|
2020-11-17 01:11:30 +01:00
|
|
|
issuer=$(./natools.py pk -f pubsec -k $dunikey)
|
2020-11-05 02:10:04 +01:00
|
|
|
recipient=$issuer;;
|
2020-11-05 01:31:40 +01:00
|
|
|
-r|--recipient) recipient="${args[$i+1]}"
|
|
|
|
[[ -z $recipient ]] && echo "Veuillez préciser un destinataire." && exit 1;;
|
|
|
|
-k|--key) dunikey="${args[$i+1]}"
|
|
|
|
[[ -z $dunikey ]] && echo "Veuillez préciser un fichier de trousseau." && exit 1;;
|
2020-11-04 15:16:03 +01:00
|
|
|
-h|--help) helpOpt && exit 0;;
|
2020-11-05 04:32:22 +01:00
|
|
|
*) [[ "${args[$i]}" == "-"* ]] && echo "Option inconnue." && exit 1;;
|
2020-11-04 15:16:03 +01:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-11-17 01:11:30 +01:00
|
|
|
if [[ -z $dunikey ]]; then
|
|
|
|
read -p "Fichier de trousseau: " dunikey
|
|
|
|
fi
|
|
|
|
issuer=$(./natools.py pk -f pubsec -k $dunikey)
|
|
|
|
|
2020-11-05 01:31:40 +01:00
|
|
|
if [[ -z $file ]]; then
|
|
|
|
read -p "Objet du message: " title
|
|
|
|
read -p "Corps du message: " content
|
|
|
|
message="$title"$'\n'"$content"
|
|
|
|
else
|
|
|
|
message=$(cat $file)
|
|
|
|
fi
|
2020-11-05 02:10:04 +01:00
|
|
|
if [[ -z $recipient ]]; then
|
|
|
|
read -p "Destinataire: " recipient
|
|
|
|
fi
|
|
|
|
|
|
|
|
[[ -z $(grep -Eo $REGEX_PUBKEYS <<<$recipient) ]] && echo "Le format de la clé publique du destinataire est invalide." && exit 1
|
2020-11-05 02:57:54 +01:00
|
|
|
[[ -z $(grep -Eo $REGEX_PUBKEYS <<<$issuer) ]] && echo "Le format de la clé publique de l'émetteur est invalide." && exit 1
|
2020-11-05 02:10:04 +01:00
|
|
|
|
2020-05-20 17:48:13 +02:00
|
|
|
# Récupération et chiffrement du titre et du message
|
2020-11-10 11:08:07 +01:00
|
|
|
nonce=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
|
|
|
|
b58nonce=$(echo $nonce | base64 -d | base58)
|
|
|
|
title=$(head -n1 <<<$message | ./natools.py box-encrypt -n $nonce -f pubsec -k $dunikey -p $recipient -O 64)
|
|
|
|
content=$(tail -n+2 <<<$message | ./natools.py box-encrypt -n $nonce -f pubsec -k $dunikey -p $recipient -O 64)
|
2020-05-20 17:48:13 +02:00
|
|
|
|
|
|
|
times=$(date -u +'%s')
|
|
|
|
|
|
|
|
# Fabrication du hash
|
2020-11-10 11:08:07 +01:00
|
|
|
hashBrut="{\"issuer\":\"$issuer\",\"recipient\":\"$recipient\",\"title\":\"$title\",\"content\":\"$content\",\"time\":$times,\"nonce\":\"$b58nonce\",\"version\":2}"
|
2020-11-05 13:46:07 +01:00
|
|
|
hash=$(echo -n "$hashBrut" | sha256sum | cut -d ' ' -f1 | awk '{ print toupper($0) }')
|
2020-05-20 17:48:13 +02:00
|
|
|
|
|
|
|
# Fabrication de la signature
|
2020-11-05 01:31:40 +01:00
|
|
|
signature=$(echo -n "$hash" | ./natools.py sign -f pubsec -k $dunikey --noinc -O 64)
|
2020-05-20 17:48:13 +02:00
|
|
|
|
|
|
|
# Affichage du JSON final
|
2020-11-05 13:46:07 +01:00
|
|
|
document="{\"hash\":\"$hash\",\"signature\":\"$signature\",${hashBrut:1}"
|
|
|
|
jq . <<<$document
|
2020-05-20 17:48:13 +02:00
|
|
|
|
2020-11-05 02:57:54 +01:00
|
|
|
# Envoi du document
|
2020-11-05 13:46:07 +01:00
|
|
|
#curl -s -i -X OPTIONS "$pod/message/inbox?pubkey=$issuer" -d "pubkey=$issuer"
|
2020-11-10 11:08:07 +01:00
|
|
|
msgID=$(curl -s -X POST "$pod/message/inbox?pubkey=$recipient" -d "$document")
|
2020-11-05 13:46:07 +01:00
|
|
|
echo -e "\nMessage ID: $msgID"
|
|
|
|
|
2020-11-10 11:08:07 +01:00
|
|
|
|
|
|
|
### Tests mode ###
|
|
|
|
|
2020-11-05 13:46:07 +01:00
|
|
|
# Delete the message 1 second later, just for test
|
2020-11-10 11:08:07 +01:00
|
|
|
#sleep 1 && ./deletemsg.sh -id $msgID
|
2020-11-05 04:09:22 +01:00
|
|
|
|
2020-11-05 13:46:07 +01:00
|
|
|
# To put the message in outbox too
|
2020-11-10 11:08:07 +01:00
|
|
|
#curl -s -X POST "$pod/message/outbox?pubkey=$issuer" -d "$document"
|
2020-11-05 04:09:22 +01:00
|
|
|
|
2020-11-17 01:11:30 +01:00
|
|
|
# To put the message as read, add this at the end of document
|
2020-11-10 11:08:07 +01:00
|
|
|
#,\"read_signature\":\"$signature\"
|