Guest User

Upside-Down-Ternet script

a guest
Feb 22nd, 2011
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.94 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use threads;
  7.  
  8. use Getopt::Long;
  9.  
  10. use HTTP::Daemon;
  11. use HTTP::Status;
  12. use LWP::UserAgent;
  13.  
  14. use String::Random;
  15.  
  16. my %options;
  17. my %mode;
  18.  
  19. my $default_blur = '-blur 0x6';
  20. my $default_prefix = 'img';
  21.  
  22. my $result = GetOptions (
  23.     "p|port=s"    => \$options{'port'},
  24.     "c|imagemagick=s" => \$mode{'imagemagic'},
  25.     "s|substitute=s"  => \$mode{'substitute'},
  26.     "b|blur"      => \$mode{'blur'},
  27.     "f|flip"      => \$mode{'flip'},
  28.     "v|verbose+"      => \$options{'verbose'},
  29.     "h|help"      => \$options{'help'},
  30.     "r|remove"    => \$options{'remove'},
  31.     );
  32.    
  33. if (defined $options{'help'}){
  34.     print "-p|port <port>\n";
  35.     print "-c|imagemagick <comand>\n";
  36.     print "-s|substitute <image file>\n";
  37.     print "-b|blur\n";
  38.     print "-f|flip\n";
  39.     print "-v|verbose\n";
  40.     print "-h|help\n";
  41.     print "-r|remove - remove temp images...(does not work on subs)\n";
  42.     exit(0);
  43. }
  44.  
  45. if (!defined $options{'port'}){
  46.     $options{'port'} = "8000";
  47.     print "Port not defined. ";
  48. }
  49. print "Using port ".$options{'port'}."\n";
  50.  
  51. #clean modes =/
  52. foreach my $key(keys %mode){
  53.     if(!defined $mode{$key}){
  54.         delete($mode{$key});
  55.         next;
  56.     }
  57. }
  58. if ( (keys %mode) == 0){
  59.     print "Error: no mode chosen\n";
  60.     exit(1);
  61. }
  62. if ( (keys %mode)  > 1){
  63.     print "Error: c|s|b|f, not at the same time!\n";
  64.     exit(1);
  65. }
  66.  
  67. sub verbose{
  68.     if ($options{'verbose'}){
  69.         print @_;
  70.     }
  71.     print "\n";
  72. }
  73.  
  74. my $rand = new String::Random;
  75.  
  76. my $ua = LWP::UserAgent->new;
  77. my $d = new HTTP::Daemon(LocalPort=>$options{'port'});
  78.  
  79. my $newImage = "";
  80. if(defined $mode{'substitute'}){
  81.     verbose("Loading substitute image: ".$mode{'substitute'});
  82.     open IMAGE, $mode{'substitute'} or die "Error opening image: $!\n";
  83.     while(<IMAGE>){
  84.         $newImage .= $_;
  85.     }
  86.     close(IMAGE);
  87. }
  88.  
  89. sub conHandler($) {
  90.     my ($c) = @_;
  91.     verbose("New connection! Total:".threads->list());
  92.    
  93.     while (my $r = $c->get_request) {
  94.         verbose("GET: ".$r->uri);
  95.         #verbose("REQUEST:\n".$r->as_string."\n-------------");
  96.        
  97.         my $response = $ua -> request( HTTP::Request->new(
  98.                     $r->method,
  99.                     $r->url,
  100.                     $r->headers,
  101.                     $r->content));
  102.         #verbose("RESPONSE:\n".$response->headers_as_string."\n");
  103.  
  104.         my $goodToGo = 0;
  105.         if($r->method eq "GET"){
  106.             if(defined $response->header('Content-Type') && $response->header('Content-Type') =~ m/image/){
  107.                 $goodToGo = 1;
  108.             }
  109.         }
  110.         if($goodToGo){
  111.             if($response->header('Content-Type') =~ m/image\/x-icon/){
  112.                 $goodToGo = 0;
  113.             }elsif(defined($response->header('Transfer-Encoding'))){
  114.                 $goodToGo = 0;
  115.             }elsif(defined($response->header('Client-Transfer-Encoding'))){
  116.                 $goodToGo = 0;
  117.             }
  118.         }
  119.  
  120.         if($goodToGo){
  121.             verbose("Modifying image...");
  122.             my $name = $response->header('Content-Type');
  123.             $name =~ s/image\/(.+)$/$1/gi;
  124.             $name = $default_prefix.$rand->randregex('\w\w\w\w\w\w\w\w').'.'.$name;
  125.  
  126.             my $action = "";
  127.             if(defined $mode{'blur'}){
  128.                 $action = "convert $name $default_blur $name.mod";
  129.             }elsif(defined $mode{'flip'}){
  130.                 $action = "convert $name -flip $name.mod";
  131.             }elsif(defined $mode{'imagemagick'}){
  132.                 $action = "convert $name ".$mode{'imagemagick'}. " $name.mod";
  133.             }
  134.  
  135.             if($action ne ""){
  136.                 #verbose("Saving image as $name");
  137.                 open IMAGE, ">>".$name or die "erro:$!\n";
  138.                 print IMAGE $response->content;
  139.                 close IMAGE;
  140.  
  141.                 #verbose("operation: '$action'");
  142.                 system($action);
  143.  
  144.                 #verbose("Loading Modification");
  145.                 open IMAGE, $name.".mod" or die "erro:$!\n";
  146.                
  147.                 $newImage = "";
  148.                 while (<IMAGE>){
  149.                     $newImage .= $_;
  150.                 }
  151.                 close(IMAGE);
  152.                 if(defined $options{'remove'} && $options{'remove'}){
  153.                     unlink $name;
  154.                     unlink $name.".mod";
  155.                 }
  156.             }
  157.             verbose("Sending Image");
  158.             $response->content($newImage);
  159.             $response->header('Content-Length' => length($newImage));
  160.  
  161.         }
  162.  
  163.         $c->send_response($response);
  164.     }
  165.     $c->close();
  166.     undef($c);
  167. }
  168.  
  169. while (my $c = $d->accept) {
  170.     threads->create('conHandler',$c)->detach();
  171. }
  172.  
  173. print "Daemon crashed?\n";
Add Comment
Please, Sign In to add comment