dobroho_vechora/dobroho_vechora.bash

171 lines
2.8 KiB
Bash
Raw Normal View History

2024-12-16 17:42:18 +00:00
#!/bin/bash
set -e
2025-01-11 21:44:24 +00:00
mode="${1}"
ip="${2}"
port="${3}"
nick="${4}"
2024-12-16 17:46:50 +00:00
2024-12-16 17:42:18 +00:00
print_usage() {
echo "${1}"
2025-01-11 21:44:24 +00:00
echo "${0} <MODE> <IP> <PORT> [NICK]"
echo "Modes: interactive (intr), send, receive (rec)"
2024-12-16 17:42:18 +00:00
exit
}
2024-12-16 17:46:50 +00:00
[ -z "${ip}" ] && print_usage 'No IP given.'
[ -z "${port}" ] && print_usage 'No port given.'
2025-01-11 21:44:24 +00:00
[ -z "${5}" ] || print_usage 'Too many arguments.'
2024-12-16 17:42:18 +00:00
2025-01-11 21:44:24 +00:00
[ -z "${nick}" ] || nick="<${nick}> "
open_socket() {
2024-12-16 17:42:18 +00:00
exec 3<> "/dev/tcp/${ip}/${port}"
2025-01-11 21:44:24 +00:00
}
close_socket() {
2024-12-16 17:42:18 +00:00
exec 3>&-
}
2025-01-11 21:44:24 +00:00
send_message() {
open_socket
echo "${1}" >&3
close_socket
}
2024-12-16 17:42:18 +00:00
get_message() {
2025-01-11 21:44:24 +00:00
open_socket
2024-12-16 17:42:18 +00:00
echo "${1}" >&3
2025-01-11 21:44:24 +00:00
cat <&3 || true
close_socket
2024-12-16 17:42:18 +00:00
}
2025-01-11 21:44:24 +00:00
case "${mode}" in
2025-01-11 23:48:58 +00:00
intr | interactive)
2025-01-11 21:44:24 +00:00
;;
send)
open_socket
text="${nick}$(cat)"
send_message "0${text}"
close_socket
exit
;;
2025-01-11 23:48:58 +00:00
rec | receive)
2025-01-11 21:44:24 +00:00
length="$(get_message 1)"
open_socket
echo -n 2 >&3
head -c "${length}" <&3
close_socket
exit
;;
*)
echo Unknown mode
exit
esac
2025-01-12 13:31:18 +00:00
encrypt() {
2025-01-12 13:34:46 +00:00
if [ "${enc}" -eq 0 ] || [ -z "${DOBROHO_VECHORA_ENCRYPTION_PASSWORD}" ]; then
2025-01-12 13:31:18 +00:00
cat
return 0
fi
2025-01-12 14:01:00 +00:00
echo "ENCRYPTED"'!'"<$(openssl enc -pbkdf2 -A -aes256 -pass env:DOBROHO_VECHORA_ENCRYPTION_PASSWORD)>"
2025-01-12 13:31:18 +00:00
}
decrypt() {
2025-01-12 13:34:46 +00:00
if [ "${enc}" -eq 0 ] || [ -z "${DOBROHO_VECHORA_ENCRYPTION_PASSWORD}" ]; then
2025-01-12 13:31:18 +00:00
cat
return 0
fi
while IFS="" read -r i || [ -n "${i}" ]
do
echo "${i}"
cipher="$(echo "${i}" | grep -Po 'ENCRYPTED!<\K([a-zA-Z0-9\=\+\/]*)(?=\>)')" || :
if [ -n "${cipher}" ]; then
echo -n "Decrypted: "
2025-01-12 14:01:00 +00:00
echo "$(echo "${cipher}" | openssl enc -pbkdf2 -d -A -aes256 -pass env:DOBROHO_VECHORA_ENCRYPTION_PASSWORD || :)"
2025-01-12 13:31:18 +00:00
fi
done
}
2025-01-11 22:57:45 +00:00
print_messages() {
open_socket
echo -n 2 >&3
2025-01-12 13:31:18 +00:00
head -c "${1}" <&3 | decrypt | sed -e 's/\x1B/ESC/g'
2025-01-11 22:57:45 +00:00
close_socket
}
2025-01-11 23:11:53 +00:00
wait_for_messages() {
2025-01-12 00:05:09 +00:00
tmp=0
trap 'tmp=1' INT
2025-01-11 23:11:53 +00:00
oldlength="${length}"
while :; do
length="$(get_message 1)"
if [ "${oldlength}" -ne "${length}" ]; then
2025-01-12 00:05:09 +00:00
trap - INT
return 0
fi
if [ "${tmp}" -eq 1 ]; then
trap - INT
return 1
2025-01-11 23:11:53 +00:00
fi
echo -n .
sleep 1
done
}
2025-01-11 22:57:45 +00:00
length=0
2025-01-12 13:31:18 +00:00
enc=0
2024-12-16 17:42:18 +00:00
while :; do
read cmd
case "${cmd}" in
2025-01-11 21:44:24 +00:00
I)
2025-01-11 23:31:31 +00:00
tmp="$(mktemp)"
2025-01-12 01:51:09 +00:00
"${VISUAL:-"${EDITOR:-"$(which vi)"}"}" "${tmp}"
2025-01-12 13:31:18 +00:00
[ -s "${tmp}" ] && send_message 0"${nick}$(cat "${tmp}" | encrypt)"
2025-01-11 23:31:31 +00:00
rm "${tmp}"
2024-12-16 17:42:18 +00:00
;;
2025-01-11 21:44:24 +00:00
i)
2024-12-16 17:42:18 +00:00
read a
2025-01-12 13:31:18 +00:00
send_message 0"${nick}$(echo "${a}" | encrypt)"
2024-12-16 17:42:18 +00:00
;;
2025-01-11 23:04:30 +00:00
L)
length="$(get_message 1)"
;;
2025-01-11 22:57:45 +00:00
l)
echo "${length}"
;;
w)
2025-01-12 00:05:09 +00:00
wait_for_messages &&
{
echo
print_messages "${length}"
if [ -n "${DOBROHO_VECHORA_ON_WAIT}" ]; then
${DOBROHO_VECHORA_ON_WAIT}
fi
}
2025-01-11 22:57:45 +00:00
;;
2024-12-16 17:42:18 +00:00
p)
2025-01-11 21:44:24 +00:00
length="$(get_message 1)"
2025-01-11 22:57:45 +00:00
print_messages "${length}"
2024-12-16 17:42:18 +00:00
;;
q)
exit
;;
2025-01-12 13:31:18 +00:00
E)
read -s DOBROHO_VECHORA_ENCRYPTION_PASSWORD
export DOBROHO_VECHORA_ENCRYPTION_PASSWORD
;;
e)
if [ "${enc}" -eq 0 ]; then
enc=1
echo 1
else
enc=0
echo 0
fi
;;
2024-12-16 17:42:18 +00:00
*)
2025-01-12 00:53:55 +00:00
echo '?'
2024-12-16 17:42:18 +00:00
;;
esac
done