-#!/bin/sh
+#!./busybox sh
SRVDIR=$(pwd)
print_status() {
echo "Acknowledged"
- $@
+ "$@"
echo "Done."
}
read -r COMMAND
echo "$COMMAND" >&2
+
+# Existing client
if [ "$COMMAND" = "login" ]; then
echo -n "Client name: "
read -r TMPNAME
verify_input "$TMPNAME"
+ # Check if provided credentials are accurate
if [ -e "$SRVDIR/clients/$TMPNAME" ]; then
KEY=$(cat "$SRVDIR/clients/$TMPNAME/_auth-key")
if [ "$TMPKEY" = "$KEY" ]; then
fi
unset TMPNAME
unset TMPKEY
+
+# New client
elif [ "$COMMAND" = "register" ]; then
echo -n "Hostname: "
read -r CLIENTHOSTNAME
+ # Use shortened hostname and add random suffix to prevent collisions
CLIENTHOSTNAME=$(echo "$CLIENTHOSTNAME" | sed 's/[^a-zA-Z]//g' | cut -c -16)
CLIENTNAME="client_${CLIENTHOSTNAME}_$(( RANDOM * 2**30 + RANDOM * 2**15 + RANDOM ))"
CLIENTDIR="$SRVDIR/clients/$CLIENTNAME"
echo -n "Hash: "
read -r TMPHASH
+ FILENAME="F__${TMPFILENAME}"
+
+ # Add suffix if file already exists
+ if [ -e "$FILENAME" ]; then
+ SUFFIX=1
+ while [ -e "${FILENAME}_${SUFFIX}" ]; do
+ SUFFIX=$(( SUFFIX + 1 ))
+ done
+ FILENAME="${FILENAME}_${SUFFIX}"
+ fi
+
+ FILERECVTIME="$(date '+%Y-%m-%d %H:%M:%S')"
+ echo "[${FILERECVTIME}] File received: ${FILENAME}" >> _files.log
+
# Client should send with `cat file | nc ip port`
- PORT=$(( ($RANDOM * 2 + $RANDOM % 2) % 64511 + 1024 ))
+ PORT=$(( (RANDOM * 2 + RANDOM % 2) % 64511 + 1024 ))
echo "$PORT"
- nc -w 5 -l -p "$PORT" > "F__${TMPFILENAME}"
+ nc -w 5 -l -p "$PORT" > "$FILENAME"
- HASH=$(md5sum "F__${TMPFILENAME}" | cut -d' ' -f1)
+ HASH=$(md5sum "$FILENAME" | cut -d' ' -f1)
if [ "$HASH" != "$TMPHASH" ]; then
echo "Checksum error"
else
echo "Transfer success"
fi
+
unset TMPFILENAME
+
+
elif [ "$COMMAND" = "log" ]; then
echo -n "Filename: "
read -r TMPFILENAME
verify_input "L__${TMPFILENAME}.log"
print_status write_to_file "L__${TMPFILENAME}.log"
unset TMPFILENAME
+
+
elif [ "$COMMAND" = "command" ]; then
echo -n "Filename: "
read -r TMPFILENAME
verify_input "C__${TMPFILENAME}.cmdlog"
print_status write_to_file "C__${TMPFILENAME}.cmdlog"
unset TMPFILENAME
+
+
else
echo "Command not found."
fi