Last active
August 29, 2015 14:04
-
-
Save andrewrjones/d606081de9510cb2899e to your computer and use it in GitHub Desktop.
Quick and dirty Perl script to email me a report of sites possibly hosting my assets. See http://andrew-jones.com/blog/prevent-other-sites-from-serving-your-assets-with-nginx/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl | |
# | |
# See http://andrew-jones.com/blog/prevent-other-sites-from-serving-your-assets-with-nginx/ | |
use strict; | |
use warnings; | |
use URI::Split qw(uri_split); | |
use Data::Dumper; | |
use Email::MIME; | |
use Email::Sender::Simple qw(sendmail); | |
open my $log, '<', 'access.log' or die $!; | |
my %skip = ( | |
'www.google.com' => 1, | |
'www.google.co.uk' => 1, | |
); | |
my %sites; | |
while (my $line = <$log>) { | |
my $rec = parse( $line ); | |
if($rec->{status} == 200 and $rec->{referer} and $rec->{request} =~ /\.(js|css)/){ | |
my ($scheme, $auth, $path, $query, $frag) = uri_split($rec->{referer}); | |
$sites{$auth}{$rec->{request}}++ if $auth and not exists $skip{$auth}; | |
} | |
} | |
my $message = Email::MIME->create( | |
header_str => [ | |
From => '...', | |
To => '...', | |
Subject => 'Sites hosting assets', | |
], | |
attributes => { | |
encoding => 'quoted-printable', | |
charset => 'ISO-8859-1', | |
}, | |
body_str => Dumper \%sites, | |
); | |
sendmail($message); | |
# from Parse::AccessLog | |
sub parse { | |
my $line = shift; | |
chomp $line; | |
my $hr; | |
# this is where the magic happens... | |
if ( $line =~ /^ (\S+) # remote_addr | |
\ \-\ (\S+) # remote_user | |
\ \[([^\]]+)\] # time_local | |
\ "(.*?)" # request | |
\ (\d+) # status | |
\ (\-|(?:\d+)) # bytes_sent | |
\ "(.*?)" # referer | |
\ "(.*?)" # user_agent | |
$ /x ) { | |
my @fields = qw(remote_addr remote_user time_local request | |
status bytes_sent referer user_agent); | |
my $c = 0; | |
{ no strict 'refs'; | |
for ( @fields ) { | |
$hr->{ $_ } = ${ ++$c }; | |
} | |
}; | |
} | |
return $hr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment