-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient.sh
40 lines (36 loc) · 949 Bytes
/
client.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env bash
if [[ $# != 2 ]]; then
echo "Usage:"
echo " $0 hostname message"
exit 1
fi
StartDate=$(date -u +"%s.%N")
# create message, remove padding
stripped_b32=$(echo "$2" | base32 | tr -d =)
# create domain
crafted_domain="${stripped_b32}.$1"
# make the DNS query and retrieve the answers
answer=$(dig @$1 "$crafted_domain" TXT)
# decode answer
message=$(echo "$answer" | grep -A 1 ";; ANSWER SECTION:" | tail -n 1 | grep -E -o "\".+\"" | cut -c 2- | rev | cut -c 2- | rev)
length=$((4 - $(echo -n "$message" | wc -c) % 4))
# add padding back accordingly
case "$length" in
"1")
message="${message}="
;;
"2")
message="${message}=="
;;
"3")
message="${message}==="
;;
*)
;;
esac
# decode
decoded=$(echo "$message" | base64 -d)
FinalDate=$(date -u +"%s.%N")
elapsed=$(date -u -d "0 $FinalDate sec - $StartDate sec" +"%S.%N")
echo "Received in $elapsed seconds"
echo "$decoded" | tr "|" "\n"