#!/usr/bin/perl -w
use strict;
use Term::ANSIColor qw(:constants);
$Term::ANSIColor::AUTORESET = 1;

if (!defined $ARGV[0]) {
	print "Usage: $0 [http or https list] [directory] [ssl?] [port]\n";
	exit;
}

open(IN, $ARGV[0]) or die "no\n";
my @ary = <IN>;
require LWP::UserAgent;
my $dir = $ARGV[1];
my ($scheme,$port);
$scheme =  defined $ARGV[2] ? 'https' : 'http'; 

if (defined $ARGV[3]) {
	$port = $ARGV[3];	
}
else {
	if ($scheme eq 'https') {
		$port = 443;
	}
	else {
		$port = 80;
	}
}

my $ua = LWP::UserAgent->new;
$ua->timeout(.5);
$ua->env_proxy;

sub wildcard_check {
	my ($ip,$port,$scheme) = @_;
	my $rand = 1e11 - int( rand(1e10) );
        my $response_1 = $ua->get("$scheme://$ip:$port/this_cant_exist_$rand");
        my $wildcard = 0;
        if ($response_1->is_success or $response_1->is_redirect) {
                $wildcard = 1;
        }
	return $wildcard;
}

sub grab {
	my ($location) = @_;
	print "Requesting $location\n";
	my $response_2 = $ua->get("$location");
	return $response_2;
}

sub print_results {
	my ($response_2,$location,$wildcard) = @_;
        my $output = " [ " . $response_2->code . " ] - $location\n";
        if ($response_2->is_success or $response_2->is_redirect) {
                if ( $wildcard == 1 ) {
                        print BOLD RED "SUCCESS [ WILDCARD ]" . $output;

                }
                else {
                        print BOLD YELLOW "SUCCESS" . $output;
                }
        }
        else {
                print BOLD RED "FAIL $output";
        }
	return;
}

foreach my $ip (@ary) {
    	chomp($ip);
	my $location = "$scheme://$ip:$port/$dir";
	my $wildcard = wildcard_check($ip,$port,$scheme);
        my $response_2 = grab("$location");
	print_results($response_2,$location,$wildcard);
	if ($location !~ /\.\w{3,4}$/) {
		$location .= '/';
		$response_2 = grab("$location");
		print_results($response_2,$location,$wildcard);
	}
}
