#!/usr/bin/perl
# youBoob

use LWP::UserAgent;
use HTTP::Request::Common;
use Parallel::ForkManager;

my %config;
our (%iptracking,%proxytrack);

%config = (
		   video_url => 'http://www.youtube.com/watch?v=ID',  				# Video URL...
		   use_random_ips => 1, 																							# If you have a server with many IPS set to 1.
		   use_proxies => 0, 																									# Use Proxy list 1 = yes...
		   proxy_list =>'proxylist.txt', 																			# List Format  IP:PORT
		   max_threads => 50,  																								# Max processes
		   max_views_per_host => '200',																				# Max views per IP / Proxy
		   ifconfig_path => '/sbin/ifconfig',																	# Path to ifconfig [to find local IPs]
		   debug => 1,																												# Print out infoz..
			);


	
main();

sub main {
		
		my $pm = new Parallel::ForkManager();
		$pm = new Parallel::ForkManager($config{max_threads});
		
		if ($config{use_random_ips}) { our (@localaddrs) = get_local_ips(); print "Found: $#localaddrs local ips\n" if ($config{debug});}
		if ($config{use_proxies}) { our (@proxies) = loadfile($config{proxy_list}); print "Loaded: $#proxies proxies from $confg{proxy_list}\n" if ($config{debug});}
		
		if ($config{debug}) {
				print "Configured Options:\n"; 
				while ( my ($opt,$value) = each (%config)) { print "\t$opt => $value\n";}
				}
				
		# Get busy!
		
		if ($config{use_random_ips}) { $doViews = $#localaddrs * $config{max_views_per_host}; }
		if ($config{use_proxies}) { $doViews = $#proxies * $config{max_views_per_host}; }
				
		print "Max Views: $doViews\n";
		$cView=0;
		
		for (1...$doViews) {
		$cView++;
		print "Loading view: $cView\n";
		
		if ($config{use_random_ips}) { 
				$localaddr = $localaddrs[int(rand(@localaddrs))];
				if ($iptracking{$localaddr} >= $config{max_views_per_host}) { $localaddr = $localaddr[int(rand(@localaddrs))]; } 
				print "Using LocalAddr => $localaddr TimesUsed => $iptracking{$localaddr} \n" if $config{debug};
				$iptracking{$localaddr}++; 
				}
		
		if ($config{use_proxies}) { 
		$proxy = $proxies[int(rand(@proxies))];
		if ($proxytrack{$proxy} >= $config{max_views_per_host}) { $proxy = $proxies[int(rand(@proxies))]; }
		print "Using Proxy => $proxy TimesUsed => $proxytrack{$proxy} \n" if $config{debug};
		$proxytrack{$proxy}++; 
		}
		my $pid = $pm->start and next;
				
		view_video();
		
		$pm->finish;
		
		}
		
		$pm->wait_all_children;
		

}


sub view_video {
		my $ua = LWP::UserAgent->new;
		$ua->agent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/2006120418 Firefox/2.0.0.1');
		$ua->timeout('180');
		
		if ($config{use_proxies}) {
				$ua->proxy(['http'], 'http://$proxy');
		}

		if ($config{use_random_ips}) {
				@LWP::Protocol::http::EXTRA_SOCK_OPTS = (LocalAddr => $localaddr,Reuse => 1 ); 
		}
		
		my $req = HTTP::Request->new(GET => $config{video_url});
		$req->content_type('application/x-www-form-urlencoded');
		my $res = $ua->request($req);
		
#		if ($config{debug}) {
#				if ($res->is_success) {
#						print $res->content;
#						}
#		}
		
		if ($res->is_success) {
		print "OK! Loaded\n";
		print "Used: $localaddr\n" if ($config{use_random_ips});
		print "Used: $proxy\n" if ($config{use_proxies});
		}
	
		
		
}


sub load_file {
my ($fname) = @_;
my (@fhin);
			open(FH,"$fname") || die "can't open $fname: $0";
				
				while (<FH>) {
						s/\n//g;
						s/\r//g;
						push(@fhin,$_);
				}
				
			close(FH);
			return(@fhin);
}

sub get_local_ips {
my @iflines = `$config{ifconfig_path}`;
		for (@iflines) { 
				if (/\s*inet addr:([\d.]+)/) {
						push(@local_addrs,$1);
					}
				}
		return @local_addrs;
}