Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- #This is a version that can read and write
- #from as many clients as your machine supports.
- use strict;
- use warnings;
- use IO::Socket;
- use Storable;
- my $serverdirectory = './server/';
- my $struct ;
- my $sendstruct;
- my $username;
- my $server = IO::Socket::INET->new
- (
- LocalAddr => '127.0.0.1',
- LocalPort => 8888,
- Type => SOCK_STREAM,
- Reuse => 1,
- Listen => 5
- ) or die "could not open port\n";
- warn "server ready waiting for connections..... \n";
- my $client;
- while ($client = $server->accept())
- {
- my $pid;
- while (not defined ($pid = fork()))
- {
- sleep 5;
- }
- if ($pid)
- {
- close $client; # Only meaningful in the client
- }
- else
- {
- $client->autoflush(1); # Always a good idea
- close $server;
- do_your_stuff();
- }
- }
- sub do_your_stuff
- {
- warn "client connected to pid $$\n";
- while(1)
- {
- my $struct = Storable::fd_retrieve($client);
- my $command = $struct->{cmd};
- if ($command eq "login")
- {
- if ($struct->{login} eq "admin" and $struct->{ps} eq "admin")
- {
- $username = "Admin";
- print $client "admin\n";
- print "pid $$ > Admin logged in"."\n";
- }
- elsif($struct->{login} eq "user" and $struct->{ps} eq "user")
- {
- $username = "User";
- print $client "user\n";
- print "pid $$ > User logged in"."\n";
- }
- elsif ($struct->{login} eq "guest" and $struct->{ps} eq "guest")
- {
- $username = "Guest";
- print $client "guest\n";
- print "pid $$ > Guest logged in"."\n";
- }
- else
- {
- print $client "error\n";
- print "pid $$ > error with login"."\n";
- }
- # my $sendmsg = "todays date\n";
- # print "$sendmsg";
- # print $client "pid $$ > ", $sendmsg;
- }
- elsif ($command eq "getlist")
- {
- opendir my $dir, "/root/server" or die "Cannot open directory :$!";
- my @files;
- my $count = 0;
- while (my $file = readdir($dir))
- {
- next if ($file =~ m/^\./);
- ($count++);
- push @files, "$count. $file";
- }
- closedir $dir;
- $sendstruct= {
- cmd => "getlist",
- listoffiles => \@files,
- };
- Storable::nstore_fd($sendstruct, $client)
- }
- elsif ($command eq "download")
- {
- my $filename = $struct->{filename};
- if ( -z $serverdirectory.$filename){
- $client->syswrite("\n");
- }
- else
- {
- open(my $fh, '<', $serverdirectory.$filename) or die "Could not open file '$filename' $!";
- print "$filename downloading...\n";
- while (my $row = <$fh>)
- {
- $client->syswrite($row);
- }
- close($fh);
- }
- print "$username [pid $$] downloaded"." $filename ...\n";
- }
- #else
- #{
- #print "$line";
- # print "fuck you\n";
- # print $client "fuck you\n";
- #}
- }
- exit 0;
- }
- -=-=-=
- #!/usr/bin/perl
- use strict;
- use IO::Socket;
- #use warnings;
- use Storable;
- my ( $host, $port, $kidpid, $handle, $line, $pass, $message, $name, @listof_files);
- ( $host, $port ) = ('127.0.0.1',8888);
- sub login()
- {
- my @array;
- push @array, "asd";
- push @array, 777;
- $name = shift || '';
- if($name eq ''){print "What's your name?\n"}
- chomp ($name = <>);
- $pass = shift || '';
- if($pass eq ''){print "What's your password?\n"}
- chomp ($pass = <>);
- $message= {
- cmd => "login",
- login => "$name",
- ps => "$pass",
- };
- Storable::nstore_fd($message, $handle);
- }
- sub actions
- {
- my $template = shift;
- my $action;
- chomp ($action = <>);
- if ($action !~/$template/)
- {
- print "Enter the number from $template:\n";
- actions($template);
- }
- else { if ($action eq "1"){
- print "download\n";
- getlist();
- downloadfile()
- }
- elsif ($action eq "2"){ print "upload\n";}
- elsif ($action eq "3"){ print "logs\n";}; }
- }
- sub getlist()
- {
- $message= {
- cmd => "getlist",
- };
- Storable::nstore_fd($message, $handle);
- my $struct = Storable::fd_retrieve($handle);
- @listof_files = @{ $struct->{listoffiles} };
- foreach (@listof_files)
- {
- print "$_\n";
- }
- }
- sub downloadfile()
- {
- my $arrlength = length(@listof_files) +1;
- print "Enter the number of filw which you want to download [1..$arrlength]\n";
- my $filenumber;
- my $temp = "^[1-$arrlength]\$";
- chomp ($filenumber = <>);
- if ($filenumber !~/$temp/)
- {
- downloadfile();
- }
- else {
- my $filename = substr(@listof_files[$filenumber-1],3);
- $message= {
- cmd => "download",
- filename => $filename,
- };
- Storable::nstore_fd($message, $handle);
- #my $struct = Storable::fd_retrieve($handle);
- print "download\n";
- open(my $fh, '>', "./Downloads/$filename") or die "Can't save file $!";
- print "$filename downloading...\n";
- while (sysread($handle, my $row , 1024))
- {
- print $fh $row;
- last;
- }
- print "$filename has been downloaded.\n"
- }
- }
- # create a tcp connection to the specified host and port
- $handle = IO::Socket::INET->new(
- Proto => "tcp",
- PeerAddr => $host,
- PeerPort => $port
- )
- or die "can't connect to port $port on $host: $!";
- $handle->autoflush(1); # so output gets there right away
- print STDERR "[Connected to $host:$port]\n";
- # split the program into two processes, identical twins
- die "can't fork: $!" unless defined( $kidpid = fork() );
- # the if{} block runs only in the parent process
- if ($kidpid) {
- my $action;
- # copy the socket to standard output
- while ( defined( $line = <$handle> ) ) {
- print STDOUT $line;
- if ($line eq "error\n")
- {
- login();
- }
- elsif ($line eq "admin\n") {
- print "Enter the number 1,2,3:\n 1 - download file; \n 2 - upload file; \n 3 - show logs.\n";
- my $range = '^[1-3]$';
- actions($range);
- }
- elsif($line eq "user\n") {
- print "Enter the number 1,2:\n 1 - download file; \n 2 - upload file.\n";
- my $range = '^[1-2]$';
- actions($range);
- }
- elsif($line eq "guest\n"){
- print "Enter the number 1:\n 1 - download file.\n";
- my $range = '^[1-1]$';
- actions($range);
- }
- else {print "error";}
- }
- kill( "TERM", $kidpid ); # send SIGTERM to child
- }
- # the else{} block runs only in the child process
- else {
- login();
- # while ( defined( $line = <STDIN> ) ) {
- while (1) {
- #print $handle "$line";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement