laconia/laconia

72 lines
1.8 KiB
Plaintext
Raw Permalink Normal View History

2021-07-06 15:46:53 +00:00
#!/bin/bash
2021-07-05 17:01:04 +00:00
PROGNAME=${0##*/}
VERSION="0.0.1"
error_exit() {
printf "%s: %s\n" "$PROGNAME" "${1:-"Unknown Error"}" >&2
exit 1
}
usage() {
2021-07-06 08:11:28 +00:00
printf "usage: %s [OPTIONS] URI [ARGS]\n" "$PROGNAME"
2021-07-05 17:01:04 +00:00
printf "Simple client for spartan protocol\n"
printf "Options:
-h, --help - display this help
-p, --port - change port number (default is 300)
-m, --message - input as string
-i, --input-file - input as file
-u, --url - url to the capsule\n"
2021-07-05 17:01:04 +00:00
}
2021-07-06 15:46:53 +00:00
request(){
2021-07-09 14:25:17 +00:00
page=$(printf "$host /$path $post_len\r\n$post\r\n" | nc $host $port)
2021-07-08 08:51:19 +00:00
status=$(echo $page | awk 'NR == 1 {print $1}')
2021-07-08 12:07:57 +00:00
meta=$(echo $page | awk 'NR == 1 {$1=="";print}')
2021-07-08 08:51:19 +00:00
if [[ $status == 2 ]]; then
2021-07-09 14:25:17 +00:00
echo -e "$page" | awk 'NR > 1 {print}'
2021-07-08 08:51:19 +00:00
elif [[ $status == 3 ]]; then
#path=$meta
2021-07-08 14:47:39 +00:00
printf "%s %s %s\r\n%s\r\n" "$host" "/$path/" "$post_len" "$post" | nc $host $port | awk 'NR > 1 {print}'
2021-07-08 08:51:19 +00:00
elif [[ $status == 4 ]]; then
printf "Client error: %s\n" "$meta"
elif [[ $status == 5 ]]; then
2021-07-08 12:07:57 +00:00
printf "Server error: %s\n" "$meta"
2021-07-08 08:51:19 +00:00
fi
2021-07-06 15:46:53 +00:00
}
2021-07-16 11:12:45 +00:00
while getopts ":h:p:i:m:u:" arg; do
2021-07-06 15:46:53 +00:00
case $arg in
h | --help)
usage; exit ;;
p | --port)
export port=$OPTARG
;;
i | --input-file)
export post=$(cat $OPTARG)
export post_len=$(echo $post | wc -c )
;;
m | --message)
export post=$OPTARG
export post_len=$(echo $post | wc -c )
2021-07-06 15:46:53 +00:00
;;
u | --url)
url_unparsed=$OPTARG
#pattern=^((spartan)://)?([^/:]+)(:([0-9]+))?/(.*)$
#echo $url_unparsed
if [[ "$url_unparsed" =~ ^((spartan)://)?([^/:]+)(:([0-9]+))?/(.*)$ ]]; then
url=$(echo $url_unparsed | sed -e 's,spartan://,,g')
host=$(echo "$url" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
path="$(echo $url | grep / | cut -d/ -f2-)"
port=${port:-300}
2021-07-08 14:47:39 +00:00
post=${post:-""}
post_len=${post_len:-0}
2021-07-06 15:46:53 +00:00
request
else
echo $url_unparsed is not a Spartan URL
fi
;;
*)
2021-07-06 15:46:53 +00:00
usage; error_exit "unknown option $1" ;;
esac
done