Backend

Protocol

Listen on port whatever (23456?)
Receive 1 line of text, regexp check for up to 12 characters [A-Za-z1-9]
receive text
close the connection
open file $logs/$ip_address-$sitename
write text to $file
close the file

#!/usr/bin/perl
use strict;
use warnings;
use IO::Socket;
use threads;
 
my $port = 23456;
 
# Create the socket
my $listen_socket = IO::Socket::INET->new(LocalPort => $port,
                                          Listen => 10,
                                          Proto => 'tcp',
                                          Reuse => 1);
 
die "Cant't create a listening socket: $@" unless $listen_socket;
 
# Loop forever while waiting for connections
while (my $connection = $listen_socket->accept) {
    # spawn a thread to handle the connection
    my $child = threads->create ("read_data", $connection)->detach;
}
 
sub read_data {
    # accept data from the socket and process
    my ($socket) = @_;
    # Read one line
    my $data = <$socket>;
    # Note - in gVim use Ctrl-Q Ctrl-M to get ^M. In Unix Vim, use Ctrl-V Ctrl-M
    $data =~ s/^M//g;  # Note that if you're copying and pasting this, FIX THIS.
    chomp $data;
    if ($data !~ /^[ A-Za-z1-9:_-]{2,12}$/) {
        print $socket "ERROR: \"$data\" not valid\n";
        close $socket;
        return;
    }
    my ($port, $ip) = unpack_sockaddr_in(getpeername($socket));
    $ip = inet_ntoa $ip;
    print $socket "OK: $data from $ip\n";
    my $buf = "";
    while (<$socket>) { $buf .= $_ }
    close $socket;
    open (my $fh, ">>$ip-$data");
    print $fh $buf;
    close $fh;
    return;
 
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Noncommercial-Share Alike 2.5 License.