2019-05-03 05:38:02 +02:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
# This Irssi script automatically check incoming http/https links
|
|
|
|
# and replace it to archive one if it is MITMed.
|
2019-05-04 16:03:22 +02:00
|
|
|
#
|
|
|
|
# Irssi /set Options
|
|
|
|
# you can view your current settigns by running "/set cflarealt" in Irssi
|
|
|
|
#
|
|
|
|
# /set cflarealt_debug <on|off> -- (off) if you have a problem try turning this on to debug
|
|
|
|
# /set cflarealt_send2channel <on|off> -- (off) send the converted URL publicly to everyone in your channels
|
|
|
|
# /set cflarealt_channels <"#channel1, #channel2, etc"> -- Channels to automatically convert. Empty Defaults to all
|
2019-05-07 01:18:04 +02:00
|
|
|
#
|
2019-05-04 16:03:22 +02:00
|
|
|
# /set cflarealt_shorturl_activate <on|off> -- (off) set it 'on' to use shortner
|
|
|
|
# /set cflarealt_shorturl_min <40> -- (40) How long a url has to be to trigger automatic url shortening
|
2019-05-07 01:18:04 +02:00
|
|
|
# /set cflarealt_shorturl_useonion <on|off> -- (off) set it 'on' to use .onion
|
2019-05-05 03:14:26 +02:00
|
|
|
#
|
|
|
|
# /set cflarealt_localdbpath <"string to path"> -- () '/path/database/split/'
|
|
|
|
# /set cflarealt_uselocaldb <on|off> -- (off) if 'on', please set path to local database (or the script will die)
|
2019-05-05 12:14:27 +02:00
|
|
|
#
|
|
|
|
# /set cflarealt_printurl <on|off> -- (off) if 'on' print converted URL
|
|
|
|
# /set cflarealt_donotsend <on|off> -- (off) if 'on' do not send converted URL
|
2019-05-03 05:38:02 +02:00
|
|
|
#---------------------------------------------------------------------
|
2019-05-04 16:12:21 +02:00
|
|
|
|
2019-05-04 16:03:22 +02:00
|
|
|
##use strict;
|
2019-05-04 16:12:21 +02:00
|
|
|
|
2019-05-03 05:38:02 +02:00
|
|
|
use vars qw($VERSION %IRSSI);
|
|
|
|
|
2019-05-05 03:14:26 +02:00
|
|
|
$VERSION = "20190506";
|
2019-05-04 16:03:22 +02:00
|
|
|
%IRSSI = (
|
2019-05-05 12:14:27 +02:00
|
|
|
|
2019-05-04 16:03:22 +02:00
|
|
|
# Special thanks to: "eo, tsaavik"
|
2019-05-03 05:38:02 +02:00
|
|
|
authors => "Anonymous",
|
2019-05-04 16:03:22 +02:00
|
|
|
contact => 'anonymous@cloudflare-tor.nab',
|
2019-05-03 05:38:02 +02:00
|
|
|
name => "irssi_cf_alturl.pl",
|
|
|
|
description => "Cloudflare URL replacer",
|
|
|
|
license => "WTFPL",
|
|
|
|
changed => "$VERSION"
|
|
|
|
);
|
|
|
|
|
|
|
|
use Irssi;
|
|
|
|
use Irssi::Irc;
|
|
|
|
use LWP::Simple;
|
|
|
|
use LWP::UserAgent;
|
|
|
|
|
2019-05-05 12:14:27 +02:00
|
|
|
my (
|
2019-05-07 01:18:04 +02:00
|
|
|
$cfg_minurllen, $cfg_send2chan, $cfg_useshort, $cfg_shortonion,
|
|
|
|
$cfg_isdebug, $cfg_uselocaldb, $cfg_localdbpath, $cfg_chanlist
|
2019-05-05 12:14:27 +02:00
|
|
|
);
|
|
|
|
my ( $cfg_printurl, $cfg_donotsendurl );
|
2019-05-04 16:03:22 +02:00
|
|
|
my @cached = ();
|
2019-05-03 05:38:02 +02:00
|
|
|
|
|
|
|
sub setuphandler {
|
2019-05-04 16:03:22 +02:00
|
|
|
Irssi::settings_add_bool( "cflarealt", "cflarealt_send2channel", 0 );
|
|
|
|
if ( Irssi::settings_get_bool("cflarealt_send2channel") ) {
|
|
|
|
print "cflarealt: sending of shorturl's to public channels enabled";
|
|
|
|
$cfg_send2chan = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Irssi::settings_add_bool( "cflarealt", "cflarealt_shorturl_activate", 0 );
|
|
|
|
if ( Irssi::settings_get_bool("cflarealt_shorturl_activate") ) {
|
|
|
|
print "cflarealt: URL shortner enabled";
|
|
|
|
$cfg_useshort = 1;
|
|
|
|
}
|
|
|
|
|
2019-05-07 01:18:04 +02:00
|
|
|
Irssi::settings_add_bool( "cflarealt", "cflarealt_shorturl_useonion", 0 );
|
|
|
|
if ( Irssi::settings_get_bool("cflarealt_shorturl_useonion") ) {
|
|
|
|
print "cflarealt: URL onion enabled";
|
|
|
|
$cfg_shortonion = 1;
|
|
|
|
}
|
|
|
|
|
2019-05-04 16:03:22 +02:00
|
|
|
Irssi::settings_add_str( "cflarealt", "cflarealt_channels", "" );
|
|
|
|
$cfg_chanlist = Irssi::settings_get_str("cflarealt_channels");
|
|
|
|
if ($cfg_chanlist) {
|
|
|
|
print "cflarealt: Following channels are now parsed $cfg_chanlist";
|
|
|
|
}
|
|
|
|
|
|
|
|
Irssi::settings_add_int( "cflarealt", "cflarealt_shorturl_min", 40 );
|
|
|
|
my $old_min_url_length = $cfg_minurllen;
|
|
|
|
$cfg_minurllen = Irssi::settings_get_int("cflarealt_shorturl_min");
|
|
|
|
if ( $cfg_minurllen != $old_min_url_length ) {
|
|
|
|
print "cflarealt: min_url_length sucessfully set to $cfg_minurllen";
|
|
|
|
}
|
|
|
|
|
|
|
|
Irssi::settings_add_bool( "cflarealt", "cflarealt_debug", 0 );
|
|
|
|
my $old_debug = $cfg_isdebug;
|
|
|
|
$cfg_isdebug = Irssi::settings_get_bool("cflarealt_debug");
|
|
|
|
if ( $cfg_isdebug != $old_debug ) {
|
|
|
|
if ($cfg_isdebug) {
|
|
|
|
print "cflarealt: Debug Mode Enabled";
|
|
|
|
$cfg_isdebug = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print "cflarealt: Debug Mode Disabled";
|
|
|
|
$cfg_isdebug = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-05 03:14:26 +02:00
|
|
|
Irssi::settings_add_bool( "cflarealt", "cflarealt_uselocaldb", 0 );
|
|
|
|
if ( Irssi::settings_get_bool("cflarealt_uselocaldb") ) {
|
|
|
|
print "cflarealt: Lookup Local DB enabled";
|
|
|
|
$cfg_uselocaldb = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Irssi::settings_add_str( "cflarealt", "cflarealt_localdbpath", "" );
|
|
|
|
$cfg_localdbpath = Irssi::settings_get_str("cflarealt_localdbpath");
|
|
|
|
if ($cfg_localdbpath) {
|
|
|
|
print "cflarealt: DB path set to $cfg_localdbpath";
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:14:27 +02:00
|
|
|
Irssi::settings_add_bool( "cflarealt", "cflarealt_printurl", 0 );
|
|
|
|
if ( Irssi::settings_get_bool("cflarealt_printurl") ) {
|
|
|
|
print "cflarealt: print URL enabled";
|
|
|
|
$cfg_printurl = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Irssi::settings_add_bool( "cflarealt", "cflarealt_donotsend", 0 );
|
|
|
|
if ( Irssi::settings_get_bool("cflarealt_donotsend") ) {
|
|
|
|
print "cflarealt: dont-send enabled";
|
|
|
|
$cfg_donotsendurl = 1;
|
|
|
|
}
|
|
|
|
|
2019-05-03 05:38:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub GotUrl {
|
|
|
|
my ( $server, $data, $nick, $addr, $target ) = @_;
|
|
|
|
if ( !$server || !$server->{connected} ) {
|
|
|
|
Irssi::print("Not connected to server");
|
|
|
|
return;
|
|
|
|
}
|
2019-05-04 16:03:22 +02:00
|
|
|
return unless ( goodchan($target) );
|
2019-05-03 05:38:02 +02:00
|
|
|
$data =~ s/^\s+//;
|
|
|
|
$data =~ s/\s+$//;
|
|
|
|
my @urls = ();
|
2019-05-05 12:14:27 +02:00
|
|
|
my @knownShortFQDN = ( 'tinyurl.com', 'bit.ly' );
|
2019-05-05 03:39:06 +02:00
|
|
|
my ( $url, $a, $return, $char, $ch ) = "";
|
2019-05-05 12:14:27 +02:00
|
|
|
my $same = 0;
|
2019-05-04 16:03:22 +02:00
|
|
|
|
2019-05-03 05:38:02 +02:00
|
|
|
return unless ( ( $data =~ /\bhttp\:/ ) || ( $data =~ /\bhttps\:/ ) );
|
|
|
|
deb("$target triggered GotUrl() with url: $data");
|
|
|
|
|
2019-05-04 16:03:22 +02:00
|
|
|
# split on whitespace and get the url(s) out
|
|
|
|
# done this way in case there are more than
|
|
|
|
# one url per line.
|
2019-05-03 05:38:02 +02:00
|
|
|
foreach ( split( /\s/, $data ) ) {
|
|
|
|
if ( ( $_ =~ /^http\:/ ) || ( $_ =~ /^https\:/ ) ) {
|
|
|
|
foreach $a (@urls) {
|
|
|
|
if ( $_ eq $a ) {
|
2019-05-05 03:14:26 +02:00
|
|
|
|
2019-05-04 16:03:22 +02:00
|
|
|
# incase they use the same url on the line.
|
2019-05-03 05:38:02 +02:00
|
|
|
$same = 1;
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $same == 0 ) {
|
|
|
|
$same = 0;
|
|
|
|
push( @urls, $_ );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-04 16:03:22 +02:00
|
|
|
|
2019-05-05 12:14:27 +02:00
|
|
|
my ( $myurl, $fqdn, $junk, $mytype );
|
|
|
|
my ( $url, $browser, $response, $answer );
|
|
|
|
my ( $line, $ifoundit );
|
2019-05-04 16:03:22 +02:00
|
|
|
|
2019-05-03 05:38:02 +02:00
|
|
|
foreach (@urls) {
|
2019-05-04 16:03:22 +02:00
|
|
|
$myurl = $_;
|
|
|
|
( $junk, $fqdn ) = split( /\/\//, $myurl, 2 );
|
|
|
|
( $fqdn, $junk ) = split( /\//, $fqdn, 2 );
|
2019-05-05 09:46:40 +02:00
|
|
|
$mytype = '';
|
2019-05-04 16:03:22 +02:00
|
|
|
|
|
|
|
if ( length($fqdn) >= 4 ) {
|
|
|
|
## Start of Act
|
2019-05-05 12:14:27 +02:00
|
|
|
|
|
|
|
## ACT0. If ShortURL, expand it. (knownShortFQDN)
|
|
|
|
if ( grep( /^$fqdn$/, @knownShortFQDN ) ) {
|
|
|
|
deb("$target Expand $fqdn");
|
|
|
|
$browser = LWP::UserAgent->new;
|
|
|
|
$answer = HTTP::Request->new( GET => $myurl );
|
|
|
|
$response = $browser->request($answer);
|
|
|
|
|
|
|
|
if ( $response->is_success and $response->previous ) {
|
|
|
|
if ( $myurl ne $response->request->uri ) {
|
|
|
|
$junk = $response->request->uri;
|
|
|
|
if ( index( $junk, 'http' ) == 0 ) {
|
|
|
|
deb("$target Expanded $fqdn");
|
|
|
|
$myurl = $junk;
|
|
|
|
( $junk, $fqdn ) = split( /\/\//, $myurl, 2 );
|
|
|
|
( $fqdn, $junk ) = split( /\//, $fqdn, 2 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-04 16:03:22 +02:00
|
|
|
## ACT1: Update URL if Cloudflared
|
|
|
|
if ( grep( /^$fqdn$/, @cached ) ) {
|
|
|
|
deb("$target Found in Cache $fqdn");
|
2019-05-05 09:52:07 +02:00
|
|
|
$mytype = '^B^C3[Archive]^O ';
|
2019-05-05 12:14:27 +02:00
|
|
|
$myurl = 'https://web.archive.org/web/' . $myurl;
|
2019-05-03 05:38:02 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-05-05 03:14:26 +02:00
|
|
|
if ( $cfg_uselocaldb == 1 ) {
|
|
|
|
deb("$target Lookup local DB about $fqdn");
|
2019-05-05 12:14:27 +02:00
|
|
|
open( CFSFILE,
|
|
|
|
$cfg_localdbpath
|
|
|
|
. "cloudflare_"
|
|
|
|
. substr( $fqdn, 0, 1 )
|
|
|
|
. ".txt" )
|
|
|
|
or die "file not found for $fqdn";
|
2019-05-05 03:14:26 +02:00
|
|
|
$ifoundit = 0;
|
|
|
|
while (<CFSFILE>) {
|
|
|
|
$line = $_;
|
|
|
|
$line =~ s/\R//g;
|
|
|
|
if ( $line eq $fqdn ) {
|
|
|
|
$ifoundit = 1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close CFSFILE;
|
|
|
|
|
|
|
|
if ( $ifoundit == 1 ) {
|
|
|
|
push( @cached, $fqdn );
|
2019-05-05 09:52:07 +02:00
|
|
|
$mytype = '^B^C3[Archive]^O ';
|
2019-05-05 12:14:27 +02:00
|
|
|
$myurl = 'https://web.archive.org/web/' . $myurl;
|
2019-05-05 03:14:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
deb("$target Asking API about $fqdn");
|
|
|
|
$answer = '';
|
2019-05-07 01:18:04 +02:00
|
|
|
$url =
|
2019-05-18 12:56:40 +02:00
|
|
|
'https://searxes.eu.org/collab/open/ismitm.php?f='
|
2019-05-07 01:18:04 +02:00
|
|
|
. $fqdn;
|
2019-05-05 03:14:26 +02:00
|
|
|
$browser = LWP::UserAgent->new;
|
2019-06-04 02:31:53 +02:00
|
|
|
$browser->agent("Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0");
|
2019-05-05 03:14:26 +02:00
|
|
|
$response = $browser->get($url);
|
|
|
|
$answer = $response->content;
|
|
|
|
if ( $answer eq '[true,true]' ) {
|
|
|
|
push( @cached, $fqdn );
|
2019-05-05 09:52:07 +02:00
|
|
|
$mytype = '^B^C3[Archive]^O ';
|
2019-05-05 12:14:27 +02:00
|
|
|
$myurl = 'https://web.archive.org/web/' . $myurl;
|
2019-05-05 03:14:26 +02:00
|
|
|
}
|
2019-05-04 16:03:22 +02:00
|
|
|
}
|
2019-05-03 05:38:02 +02:00
|
|
|
}
|
|
|
|
|
2019-05-04 16:03:22 +02:00
|
|
|
## ACT2: Short URL __if__ enabled and long
|
|
|
|
if ( $cfg_useshort == 1 ) {
|
|
|
|
if ( length($myurl) > $cfg_minurllen ) {
|
2019-05-07 01:18:04 +02:00
|
|
|
if ( $cfg_shortonion == 1 ) {
|
|
|
|
deb("$target Creating Short Onion for $myurl");
|
|
|
|
$url = 'https://url.danwin1210.me/?i=new&url=' . $myurl;
|
|
|
|
$browser = LWP::UserAgent->new;
|
2019-06-04 02:31:53 +02:00
|
|
|
$browser->agent("Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0");
|
2019-05-07 01:18:04 +02:00
|
|
|
$response = $browser->get($url);
|
|
|
|
$answer = $response->content;
|
|
|
|
if (
|
|
|
|
index( $answer,
|
2019-05-07 01:55:13 +02:00
|
|
|
'http://dt2tq5y2ccowebjo.onion/?' ) == 0
|
2019-05-07 01:18:04 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if ( $mytype eq '' ) {
|
|
|
|
$mytype = '^B^C7[Onion]^O ';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$mytype = '^B^C2[Onion,Archive]^O ';
|
|
|
|
}
|
|
|
|
$myurl = $answer;
|
2019-05-05 12:14:27 +02:00
|
|
|
}
|
2019-05-07 01:18:04 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
deb("$target Creating Short URL for $myurl");
|
|
|
|
$url =
|
|
|
|
'https://ux.nu/api/short?format=plain&url=' . $myurl;
|
|
|
|
$browser = LWP::UserAgent->new;
|
2019-06-04 02:31:53 +02:00
|
|
|
$browser->agent("Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0");
|
2019-05-07 01:18:04 +02:00
|
|
|
$response = $browser->get($url);
|
|
|
|
$answer = $response->content;
|
|
|
|
if ( index( $answer, 'https://ux.nu/' ) == 0 ) {
|
|
|
|
if ( $mytype eq '' ) {
|
|
|
|
$mytype = '^B^C7[Short]^O ';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$mytype = '^B^C2[Short,Archive]^O ';
|
|
|
|
}
|
|
|
|
$myurl = $answer;
|
2019-05-05 10:40:18 +02:00
|
|
|
}
|
2019-05-04 16:03:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-05 12:14:27 +02:00
|
|
|
##ACT3: Result
|
|
|
|
if ( $cfg_printurl == 1 ) {
|
|
|
|
Irssi::print("URL: $mytype$myurl");
|
2019-05-04 16:03:22 +02:00
|
|
|
}
|
2019-05-05 12:14:27 +02:00
|
|
|
|
|
|
|
if ( $cfg_donotsendurl != 1 ) {
|
|
|
|
if ( $cfg_send2chan == 1 ) {
|
|
|
|
$server->command("msg $target $myurl");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$server->print( "$target", "$mytype$myurl",
|
|
|
|
MSGLEVEL_CLIENTCRAP );
|
|
|
|
}
|
2019-05-04 16:03:22 +02:00
|
|
|
}
|
2019-05-05 12:14:27 +02:00
|
|
|
|
2019-05-04 16:03:22 +02:00
|
|
|
## End of Act
|
2019-05-03 05:38:02 +02:00
|
|
|
}
|
2019-05-04 16:03:22 +02:00
|
|
|
deb("$target process done for input $myurl");
|
2019-05-03 05:38:02 +02:00
|
|
|
}
|
2019-05-04 16:03:22 +02:00
|
|
|
|
2019-05-04 16:28:09 +02:00
|
|
|
## Cleanup cache
|
2019-05-04 16:03:22 +02:00
|
|
|
if ( $#cached > 500 ) {
|
|
|
|
@cached = ();
|
2019-05-03 05:38:02 +02:00
|
|
|
}
|
2019-05-04 16:03:22 +02:00
|
|
|
|
|
|
|
return;
|
2019-05-03 05:38:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub deb($) {
|
2019-05-04 16:03:22 +02:00
|
|
|
Irssi::print(shift) if ( $cfg_isdebug == 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub goodchan {
|
|
|
|
my $chan = shift;
|
|
|
|
return ("OK") if ( !$cfg_chanlist );
|
|
|
|
foreach ( split( /\,/, $cfg_chanlist ) ) {
|
|
|
|
return ("$_") if ( $_ =~ /$chan/i );
|
|
|
|
}
|
|
|
|
return undef;
|
2019-05-03 05:38:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setuphandler();
|
|
|
|
Irssi::signal_add( "setup changed", "setuphandler" );
|
|
|
|
Irssi::signal_add_last( "message public", "GotUrl" );
|
|
|
|
Irssi::signal_add_last( "ctcp action", "GotUrl" );
|