From: mivirl <> Date: Fri, 26 Jan 2024 15:26:10 +0000 (-0600) Subject: Add suffix to duplicate files; add comments X-Git-Url: http://mivirl.dev/git/?a=commitdiff_plain;h=6167fc51f737571872a3ed25000ec1ee6da8e523;p=remote-logging.git Add suffix to duplicate files; add comments - Add more comments to server.sh - Change server.sh shebang to busybox - Add suffix to received files when duplicated --- diff --git a/src/server.sh b/src/server.sh index da3a4a1..b732d08 100644 --- a/src/server.sh +++ b/src/server.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!./busybox sh SRVDIR=$(pwd) @@ -28,7 +28,7 @@ write_to_file() { print_status() { echo "Acknowledged" - $@ + "$@" echo "Done." } @@ -38,6 +38,8 @@ print_status() { read -r COMMAND echo "$COMMAND" >&2 + +# Existing client if [ "$COMMAND" = "login" ]; then echo -n "Client name: " read -r TMPNAME @@ -46,6 +48,7 @@ if [ "$COMMAND" = "login" ]; then 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 @@ -60,9 +63,12 @@ if [ "$COMMAND" = "login" ]; 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" @@ -101,30 +107,51 @@ while read -r COMMAND; do 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