#!/usr/bin/perl
# HTTP fingerprinting tool

use strict;
use warnings;
use Getopt::Long;
use lib './lib';
use lbmap::lbmap;
use Data::Dump qw(dump);

my $VERSION = '0.4';
my $AUTHOR = 'Eldar "Wireghoul" Marcussen';
my $batch = 0;
my $debug = 0;
my $timeout = 30;
my $opts = GetOptions(
    'batch' => \$batch,
    'debug' => \$debug,
    'timeout' => \$timeout,
    'version' => sub { print "lbmap version: $VERSION\n"; exit 0; }
  );
my @uris;
&banner;
if (! $ARGV[0]) {
    &show_help;
} elsif ($ARGV[0] eq '-') {
    @uris = <STDIN>;
} elsif ( -e $ARGV[0] ) {
    open my $ifh, '<', $ARGV[0];
    @uris = <$ifh>;
    close $ifh;
} else {
    push @uris, $ARGV[0];
}
chomp(@uris);
for my $uri (@uris) {
    my $lbmap = lbmap::lbmap->new('debug' => $debug, 'timeout' => $timeout);
    &scan_status($uri);
    my %result = $lbmap->scan($uri);
    print dump(\%result)."\n";
}

sub show_help { # This should really be a qq block or similar
    print "Usage $0 <options> target\n";
    print "Valid options:\n";
    print "\t--batch\t batch mode supresses banner output\n";
    print "\t--debug\tprints debug information\n";
    print "\t--timeout\tsets timeout in seconds (defult: 30)\n";
    print "\t--version\tprints the lbmap version\n";
    print "Valid target is a single uri (http://example.com), a file (targets.txt) or - to read targets from STDIN\n";
    exit;
}

sub banner {
    return if $batch;
    print "lbmap - HTTP fingerprinting tool v$VERSION by $AUTHOR\n";
    print "====================================[ www.justanotherhacker.com ]===\n";
}

sub scan_status {
    my $target = shift;
    return if $batch;
    print "[*] Scanning $target\n";
}
