From: mivirl <> Date: Wed, 7 Feb 2024 16:22:52 +0000 (-0600) Subject: client: Add comments explaining behavior X-Git-Url: http://mivirl.dev/git/?a=commitdiff_plain;h=23f6ad789639e60670a62e86dd876a2fa2e441ec;p=remote-logging.git client: Add comments explaining behavior --- diff --git a/src/client.pl b/src/client.pl index 16bae4c..6999ee6 100644 --- a/src/client.pl +++ b/src/client.pl @@ -88,6 +88,8 @@ sub register { my $socket = connect_to_server; $socket->send("register\n"); $socket->send("$hostname\n"); + + # Wait for connection to be established, try up to 5 times my $response; foreach (1..5) { sleep $_; @@ -107,7 +109,9 @@ sub register { sub login { my $socket = connect_to_server; - my $response = ""; + + # Wait for connection to be established, try up to 5 times + my $response; $socket->send("login\n"); $socket->send("$clientName\n"); $socket->send("$clientKey\n"); @@ -127,7 +131,9 @@ sub login { sub send_info { my $socket = login($clientName, $clientKey); + my $info = join "", ns_system('./busybox', 'sh', '-c', 'hostname; date; uname -a; lspci; lsusb; ifconfig'); + $socket->send("info\n"); $socket->send($info); $socket->send("⟃---EOF---⟄\n"); @@ -142,9 +148,16 @@ sub send_log { return; } + # Check that log exists and is readable by current user exit if (! -e $file || ! -r _); + my $socket = login($clientName, $clientKey); + + # Replace / character with similar-looking character that is valid + # for filenames. Used to show full path to file my $fileName = $file =~ s/\////gr; + + # Upload tailed log continuously $socket->send("log\n"); $socket->send("$fileName\n"); print_log "Log: Uploading $file"; @@ -166,6 +179,8 @@ sub send_processes { } my $socket = login($clientName, $clientKey); + + # Upload process log continuously $socket->send("processes\n"); print_log "Processes: Started"; my $commandLog = ns_systemFH('./pspy64', '-f'); @@ -187,6 +202,8 @@ sub send_command_output { } my $socket = login($clientName, $clientKey); + + # Upload command output continously with provided filename my ($fileName) = $name; $socket->send("command\n"); $socket->send("$fileName\n"); @@ -209,13 +226,20 @@ sub send_file { return; } + # Check that log exists and is readable by current user exit if (! -e $file || ! -r _); + + # Replace / character with similar-looking character that is valid + # for filenames. Used to show full path to file my $fileName = $file =~ s/\////gr; my ($fileHash) = ns_system('./busybox', 'md5sum', "$file"); chomp $fileName; chomp $fileHash; ($fileHash) = $fileHash =~ m/([0-9a-f]+)/; my $socket = login($clientName, $clientKey); + + # Send filename and hash to server, wait for a response with the port to + # upload the file to $socket->send("file\n"); $socket->send("$fileName\n"); $socket->send("$fileHash\n"); @@ -223,6 +247,7 @@ sub send_file { $socket->recv(my $port, 128); ($port) = $port =~ m/(\d+)/; + # Send file once print_log "File: upload port is $port ($file)"; open(my $fileFH, '<', "$file") || die "Failed to open $file"; my $fileSocket = connect_to_server $port; @@ -232,10 +257,11 @@ sub send_file { close($fileFH); close($fileSocket); + # Server checks that the uploaded hash matches and informs on error + # No retry is attempted on failed upload $socket->recv(my $response, 128); if ($response =~ m/Transfer success/) { print_log "File: upload success ($file)"; - exit; } else { print_log "File: upload failure ($file)"; } @@ -249,6 +275,8 @@ sub watch_directory { push @child_processes, $pid; return; } + + # Recursively monitor directory for files that are written to. Uploads files when found my $monitor = ns_systemFH('./inotifywait', '-r', '-m', '-e', 'close_write', '--format', '%w%f', $dir); while (<$monitor>) { chomp; @@ -272,8 +300,8 @@ sub capture_packets { send_info(); send_processes(); -# Files, logs, and commands to send to the server # ------------------------------------------------------------------------------ +# Files, logs, and commands to send to the server # These files will have their contents sent as they are updated send_log('/var/log/secure');