Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use IO::Socket;
- use Storable;
- my $serverdirectory = './server/';
- my $logsdirectory = './server/logs/';
- my $struct ;
- my $sendstruct;
- my $username;
- my $server = IO::Socket::INET->new
- (
- LocalAddr => '127.0.0.1',
- LocalPort => 2628,
- Type => SOCK_STREAM,
- ReuseAddr => 1,
- Listen => 5
- ); die "could not open port\n" unless $server;
- warn "server ready waiting for connections..... \n";
- my $client;
- while (defined($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 logs
- {
- my $messagetolog = shift;
- open(my $fh, '>>', $logsdirectory.$username.".txt") or die "Can't open file '$username' $!";
- print $fh $messagetolog;
- close $fh;
- }
- 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";
- logs("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";
- logs("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";
- logs("pid $$ > Guest logged in"."\n");
- }
- else
- {
- print $client "error\n";
- print "pid $$ > error with login"."\n";
- logs("pid $$ > error with login"."\n");
- }
- }
- elsif ($command eq "getlist")
- {
- my $getdirectory = $struct->{directory};
- opendir my $dir, $getdirectory or die "Cannot open directory :$!";
- my @files;
- my $count = 0;
- while (my $file = readdir($dir))
- {
- next if ($file =~ m/^\./ or (-d $getdirectory.$file));
- ($count++);
- push @files, "$count. $file";
- }
- closedir $dir;
- $sendstruct= {
- cmd => "getlist",
- listoffiles => \@files,
- };
- Storable::nstore_fd($sendstruct, $client);
- logs("$username pid $$ > got the list of files"."\n");
- }
- 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";
- binmode $fh;
- while (my $row = <$fh>)
- {
- $client->syswrite($row,1024);
- }
- close($fh);
- }
- print "$username [pid $$] downloaded"." $filename ...\n";
- logs("$username [pid $$] downloaded"." $filename ...\n");
- }
- elsif ($command eq "delete")
- {
- my $filename = $struct->{filename};
- unlink $serverdirectory.$filename;
- print "File $filename was deleted\n";
- logs("$username [pid $$] deleted"." $filename ...\n");
- }
- elsif ($command eq "showlog")
- {
- my $filename = $struct->{filename};
- if ( -z $serverdirectory.$filename){
- $client->syswrite("\n");
- }
- else
- {
- open(my $fh, '<', $logsdirectory.$filename) or die "Could not open file '$filename' $!";
- print "$filename openning the log...\n";
- while (my $row = <$fh>)
- {
- $client->syswrite($row);
- }
- close($fh);
- }
- print "$username [pid $$] log"." $filename ...\n";
- logs("$username [pid $$] log"." $filename ...\n");
- }
- elsif ($command eq "upload")
- {
- my $filename = $struct->{filename};
- print "$filename\n";
- my $size = $struct->{size};
- if ( -e $serverdirectory.$filename){
- if ($username eq "User")
- {
- $client->send("0\n", 1024);
- }
- elsif ($username eq "Admin")
- {
- print "admin\n";
- $client->send("1\n",1024);
- $client->recv(my $answer,1024);
- if ($answer eq "y\n")
- {
- open(my $fh, '>', $serverdirectory.$filename) or die "Can't save file $!";
- print "$filename uploading...\n";
- while (sysread($client, my $row , 1024))
- {
- print $fh $row;
- last;
- }
- close($fh);
- print "$filename has been uploaded.\n";
- logs("$username [pid $$] uploaded(overwrite)"." $filename ...\n");
- }
- elsif($answer eq "n\n")
- {
- print "Canceling the uploading\n";
- logs("$username [pid $$] doen't want to overwrite"." $filename ...\n");
- }
- }
- }
- else
- {
- $client->send("2\n",1024);
- $client->recv(my $answer,1024);
- if ($answer eq "ok\n")
- {
- open(my $fh, '>', $serverdirectory.$filename) or die "Can't save file $!";
- print "$filename uploading...\n";
- while (sysread($client, my $row , 1024))
- {
- print $fh $row;
- last;
- }
- close($fh);
- print "$filename has been uploaded.\n";
- logs("$username [pid $$] uploaded(overwrite)"." $filename ...\n");
- }
- }
- }
- }
- exit 0;
- }
- -=-=-=-=
- #!/usr/bin/perl
- use strict;
- use IO::Socket;
- #use warnings;
- use Storable;
- use File::Basename;
- my ( $host, $port, $kidpid, $handle, $name, $pass, @listof_files, $response, $count);
- ( $host, $port ) = ('127.0.0.1',8888);
- sub login()
- {
- die "Socket is dead $!" unless $handle->connected();
- 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 = <>);
- my $message= {
- cmd => "login",
- login => "$name",
- ps => "$pass",
- };
- Storable::nstore_fd($message, $handle);
- $handle->recv($name, 1024);
- }
- sub actions
- {
- my $template = shift;
- my $action;
- $action = <>;
- print "$action\n";
- if ($action !~/$template/ and $action ne "exit")
- {
- print "Enter the number from $template:\n";
- actions($template);
- }
- else {
- if ($action eq "1\n"){
- print "download\n";
- getlist("./server/");
- downloadfile();
- }
- elsif ($action eq "2\n"){
- uploadfile();
- }
- elsif ($action eq "3\n"){
- print "logs\n";
- getlist("./server/logs/");
- showlogs();
- }
- elsif ($action eq "4\n")
- {
- getlist("./server/");
- deletefile();
- }
- elsif ($action eq "exit\n"){ shutdown($handle, 1); exit 0;
- }
- }
- }
- sub getlist
- {
- my $message= {
- cmd => "getlist",
- directory => shift,
- };
- Storable::nstore_fd($message, $handle);
- my $struct = Storable::fd_retrieve($handle);
- @listof_files = @{ $struct->{listoffiles} };
- $count = 0;
- foreach (@listof_files)
- {
- ($count++);
- print "$_\n";
- }
- }
- sub showactions()
- {
- die "Socket is dead $!" unless $handle->connected();
- if ($name eq "error\n")
- {
- login();
- }
- elsif ($name eq "admin\n") {
- print "Enter the number 1,2,3,4,5:\n 1 - download file; \n 2 - upload file; \n 3 - show logs;\n 4 - remove file.\n";
- my $range = '^[1-4]$';
- actions($range);
- }
- elsif($name eq "user\n") {
- print "Enter the number 1,2,3:\n 1 - download file; \n 2 - upload file.\n";
- my $range = '^[1-3]$';
- actions($range);
- }
- elsif($name eq "guest\n"){
- print "Enter the number 1:\n 1 - download file.\n";
- my $range = '^[1]$';
- actions($range);
- }
- else {print "error\n"; last; shutdown($handle,1); exit 0}
- }
- sub downloadfile()
- {
- $count;
- print "Enter the number of filw which you want to download [1..$count]\n";
- my $filenumber;
- my $temp = "^[1-$count]\$";
- chomp ($filenumber = <>);
- if ($filenumber !~/$temp/)
- {
- downloadfile();
- }
- else {
- my $filename = substr(@listof_files[$filenumber-1],3);
- my $message= {
- cmd => "download",
- filename => $filename,
- };
- my $buff = "true";
- print $buff."\n";
- if ($buff eq "true"){
- Storable::nstore_fd($message, $handle);
- $buff = "false";}
- print $buff."\n";
- #my $struct = Storable::fd_retrieve($handle);
- print "download\n";
- open(my $fh, '>', "./Downloads/$filename") or die "Can't save file $!";
- binmode $fh;
- print "$filename downloading...\n";
- while (sysread($handle, my $row , 1024))
- {
- print $fh $row;
- last;
- }
- close($fh);
- print "$filename has been downloaded.\n"
- }
- }
- sub uploadfile()
- {
- print "Enter the full file name that you wan't to upload\n";
- chomp (my $filename = <>);
- if (-e $filename)
- {
- my $size = -s $filename;
- my $message= {
- cmd => "upload",
- filename => basename($filename),
- size => $size,
- };
- Storable::nstore_fd($message, $handle);
- $handle->recv(my $resp, 1024);
- print "$resp";
- if ($resp eq "0\n")
- {
- print "User can't overwrite files\n";
- }
- elsif ($resp eq "1\n")
- {
- print "File already exist, do you want to overwrite it y|n?\n";
- my $answer = <>;
- $handle->send($answer, 1024);
- if ($answer eq "y\n")
- {
- open(my $fh, '<', $filename) or die "Could not open file '$filename' $!";
- print "$filename uploading...\n";
- while (my $row = <$fh>)
- {
- $handle->syswrite($row);
- }
- close($fh);
- print "$filename uploaded...\n";
- }
- }
- elsif ($resp eq "2\n")
- {
- $handle->send("ok\n", 1024);
- open(my $fh, '<', $filename) or die "Could not open file '$filename' $!";
- print "$filename uploading...\n";
- while (my $row = <$fh>)
- {
- $handle->syswrite($row);
- }
- close($fh);
- print "$filename uploaded...\n";
- }
- }
- else { print "File doesn't extis\n"; uploadfile();}
- }
- sub deletefile()
- {
- print "Enter the number of file which you want to delete [1..$count]\n";
- my $filenumber;
- my $temp = "^[1-$count]\$";
- chomp ($filenumber = <>);
- if ($filenumber !~/$temp/)
- {
- deletefile()
- }
- else {
- my $filename = substr(@listof_files[$filenumber-1],3);
- my $message= {
- cmd => "delete",
- filename => $filename,
- };
- Storable::nstore_fd($message, $handle);
- }
- print "file was deleted\n";
- }
- sub showlogs()
- {
- print "Enter the number of file which you want to see [1..$count]\n";
- my $filenumber;
- my $temp = "^[1-$count]\$";
- chomp ($filenumber = <>);
- if ($filenumber !~/$temp/)
- {
- showlogs();
- }
- else {
- my $filename = substr(@listof_files[$filenumber-1],3);
- my $message= {
- cmd => "showlog",
- filename => $filename,
- };
- Storable::nstore_fd($message, $handle);
- #my $struct = Storable::fd_retrieve($handle);
- print "show log\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;
- print "$row\n";
- last;
- }
- close($fh);
- print "$filename log.\n"
- }
- }
- # create a tcp connection to the specified host and port
- $handle = IO::Socket::INET->new(
- Proto => "tcp",
- PeerAddr => $host,
- PeerPort => 2628
- )
- or die "can't connect to port $port on $host: $!";
- $handle->autoflush(1); # so output gets there right away
- print "[Connected to $host:$port]\n";
- login();
- while(1)
- {
- showactions();
- }
- #my $action;
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement