Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #This is a version that can read and write
  3. #from as many clients as your machine supports.
  4. use strict;
  5. use warnings;
  6. use IO::Socket;
  7. use Storable;
  8. my $serverdirectory = './server/';
  9. my $struct ;
  10. my $sendstruct;
  11. my $username;
  12. my $server = IO::Socket::INET->new
  13. (
  14. LocalAddr => '127.0.0.1',
  15. LocalPort => 8888,
  16. Type => SOCK_STREAM,
  17. Reuse => 1,
  18. Listen => 5
  19. ) or die "could not open port\n";
  20.  
  21. warn "server ready waiting for connections..... \n";
  22.  
  23. my $client;
  24.  
  25. while ($client = $server->accept())
  26. {
  27. my $pid;
  28. while (not defined ($pid = fork()))
  29. {
  30. sleep 5;
  31. }
  32. if ($pid)
  33. {
  34. close $client; # Only meaningful in the client
  35. }
  36. else
  37. {
  38. $client->autoflush(1); # Always a good idea
  39.  
  40. close $server;
  41.  
  42. do_your_stuff();
  43. }
  44. }
  45.  
  46. sub do_your_stuff
  47. {
  48. warn "client connected to pid $$\n";
  49. while(1)
  50. {
  51. my $struct = Storable::fd_retrieve($client);
  52. my $command = $struct->{cmd};
  53.  
  54. if ($command eq "login")
  55. {
  56. if ($struct->{login} eq "admin" and $struct->{ps} eq "admin")
  57. {
  58. $username = "Admin";
  59. print $client "admin\n";
  60. print "pid $$ > Admin logged in"."\n";
  61. }
  62. elsif($struct->{login} eq "user" and $struct->{ps} eq "user")
  63. {
  64. $username = "User";
  65. print $client "user\n";
  66. print "pid $$ > User logged in"."\n";
  67. }
  68. elsif ($struct->{login} eq "guest" and $struct->{ps} eq "guest")
  69. {
  70. $username = "Guest";
  71. print $client "guest\n";
  72. print "pid $$ > Guest logged in"."\n";
  73. }
  74. else
  75. {
  76. print $client "error\n";
  77. print "pid $$ > error with login"."\n";
  78. }
  79. # my $sendmsg = "todays date\n";
  80. # print "$sendmsg";
  81. # print $client "pid $$ > ", $sendmsg;
  82.  
  83.  
  84. }
  85. elsif ($command eq "getlist")
  86. {
  87. opendir my $dir, "/root/server" or die "Cannot open directory :$!";
  88. my @files;
  89. my $count = 0;
  90. while (my $file = readdir($dir))
  91. {
  92.  
  93. next if ($file =~ m/^\./);
  94. ($count++);
  95. push @files, "$count. $file";
  96. }
  97. closedir $dir;
  98. $sendstruct= {
  99. cmd => "getlist",
  100. listoffiles => \@files,
  101. };
  102. Storable::nstore_fd($sendstruct, $client)
  103. }
  104. elsif ($command eq "download")
  105. {
  106.  
  107. my $filename = $struct->{filename};
  108. if ( -z $serverdirectory.$filename){
  109. $client->syswrite("\n");
  110. }
  111. else
  112. {
  113. open(my $fh, '<', $serverdirectory.$filename) or die "Could not open file '$filename' $!";
  114. print "$filename downloading...\n";
  115.  
  116. while (my $row = <$fh>)
  117. {
  118. $client->syswrite($row);
  119.  
  120. }
  121. close($fh);
  122. }
  123. print "$username [pid $$] downloaded"." $filename ...\n";
  124.  
  125. }
  126. #else
  127. #{
  128. #print "$line";
  129. # print "fuck you\n";
  130. # print $client "fuck you\n";
  131. #}
  132.  
  133. }
  134. exit 0;
  135. }
  136. -=-=-=
  137. #!/usr/bin/perl
  138. use strict;
  139. use IO::Socket;
  140. #use warnings;
  141. use Storable;
  142.  
  143. my ( $host, $port, $kidpid, $handle, $line, $pass, $message, $name, @listof_files);
  144.  
  145. ( $host, $port ) = ('127.0.0.1',8888);
  146. sub login()
  147. {
  148. my @array;
  149. push @array, "asd";
  150. push @array, 777;
  151. $name = shift || '';
  152. if($name eq ''){print "What's your name?\n"}
  153. chomp ($name = <>);
  154.  
  155. $pass = shift || '';
  156. if($pass eq ''){print "What's your password?\n"}
  157. chomp ($pass = <>);
  158. $message= {
  159. cmd => "login",
  160. login => "$name",
  161. ps => "$pass",
  162.  
  163. };
  164. Storable::nstore_fd($message, $handle);
  165. }
  166.  
  167. sub actions
  168. {
  169.  
  170. my $template = shift;
  171. my $action;
  172.  
  173. chomp ($action = <>);
  174.  
  175. if ($action !~/$template/)
  176. {
  177. print "Enter the number from $template:\n";
  178. actions($template);
  179. }
  180. else { if ($action eq "1"){
  181.  
  182. print "download\n";
  183. getlist();
  184. downloadfile()
  185. }
  186. elsif ($action eq "2"){ print "upload\n";}
  187. elsif ($action eq "3"){ print "logs\n";}; }
  188. }
  189.  
  190. sub getlist()
  191. {
  192.  
  193. $message= {
  194. cmd => "getlist",
  195. };
  196. Storable::nstore_fd($message, $handle);
  197. my $struct = Storable::fd_retrieve($handle);
  198. @listof_files = @{ $struct->{listoffiles} };
  199. foreach (@listof_files)
  200. {
  201. print "$_\n";
  202. }
  203. }
  204.  
  205. sub downloadfile()
  206. {
  207. my $arrlength = length(@listof_files) +1;
  208. print "Enter the number of filw which you want to download [1..$arrlength]\n";
  209. my $filenumber;
  210. my $temp = "^[1-$arrlength]\$";
  211. chomp ($filenumber = <>);
  212. if ($filenumber !~/$temp/)
  213. {
  214. downloadfile();
  215. }
  216. else {
  217. my $filename = substr(@listof_files[$filenumber-1],3);
  218. $message= {
  219. cmd => "download",
  220. filename => $filename,
  221. };
  222. Storable::nstore_fd($message, $handle);
  223. #my $struct = Storable::fd_retrieve($handle);
  224. print "download\n";
  225.  
  226. open(my $fh, '>', "./Downloads/$filename") or die "Can't save file $!";
  227. print "$filename downloading...\n";
  228.  
  229. while (sysread($handle, my $row , 1024))
  230. {
  231. print $fh $row;
  232. last;
  233. }
  234.  
  235. print "$filename has been downloaded.\n"
  236. }
  237. }
  238.  
  239. # create a tcp connection to the specified host and port
  240. $handle = IO::Socket::INET->new(
  241. Proto => "tcp",
  242. PeerAddr => $host,
  243. PeerPort => $port
  244. )
  245. or die "can't connect to port $port on $host: $!";
  246. $handle->autoflush(1); # so output gets there right away
  247. print STDERR "[Connected to $host:$port]\n";
  248.  
  249. # split the program into two processes, identical twins
  250. die "can't fork: $!" unless defined( $kidpid = fork() );
  251.  
  252. # the if{} block runs only in the parent process
  253. if ($kidpid) {
  254. my $action;
  255. # copy the socket to standard output
  256. while ( defined( $line = <$handle> ) ) {
  257. print STDOUT $line;
  258. if ($line eq "error\n")
  259. {
  260. login();
  261.  
  262. }
  263. elsif ($line eq "admin\n") {
  264. print "Enter the number 1,2,3:\n 1 - download file; \n 2 - upload file; \n 3 - show logs.\n";
  265. my $range = '^[1-3]$';
  266. actions($range);
  267. }
  268. elsif($line eq "user\n") {
  269. print "Enter the number 1,2:\n 1 - download file; \n 2 - upload file.\n";
  270. my $range = '^[1-2]$';
  271. actions($range);
  272. }
  273. elsif($line eq "guest\n"){
  274. print "Enter the number 1:\n 1 - download file.\n";
  275. my $range = '^[1-1]$';
  276. actions($range);
  277.  
  278. }
  279. else {print "error";}
  280. }
  281. kill( "TERM", $kidpid ); # send SIGTERM to child
  282. }
  283.  
  284. # the else{} block runs only in the child process
  285. else {
  286.  
  287. login();
  288.  
  289. # while ( defined( $line = <STDIN> ) ) {
  290. while (1) {
  291.  
  292.  
  293.  
  294. #print $handle "$line";
  295. }
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement