tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

checksocks.pl (2587B)


      1 #!/usr/bin/perl -w
      2 
      3 require 5.005;
      4 use strict;
      5 use IO::Socket;
      6 use Getopt::Std;
      7 
      8 # Checks routers for open socks-ports and socks5
      9 # Successful connects go to STDOUT, failed ones to STDERR.
     10 # We only do one check per loop in -d mode, so it takes some time.
     11 
     12 # Contributed by Peter Kornherr <peter at wuschelpuschel dot org>, and
     13 # cleaned up by Peter Palfrader <peter at palfrader dot org>.
     14 
     15 our($opt_i,$opt_p,$opt_d,$opt_h,$opt_l);
     16 getopts('i:p:dhl:');
     17 
     18 if ($opt_h || !($opt_d||$opt_i||$opt_l)) {
     19    print "Usage: $0 -d < file_with_routers_in_it\n";
     20    print "or:    $0 -i IP -p Port\n";
     21    print "or:    $0 -l IP:Port\n";
     22    exit;
     23 }
     24 
     25 if ($opt_d) {
     26    open (IN,"<-") or die $!;
     27    while (<IN>) {
     28        next unless /^router /;
     29        (my $routername,my $checkip,my $checkport) = (split(" "))[1,2,4];
     30        &do_check($checkip,$checkport,$routername);
     31    }
     32 } elsif ($opt_i && $opt_p) {
     33    &do_check($opt_i,$opt_p);
     34 } elsif ($opt_l) {
     35    &do_check(split(":",$opt_l));
     36 }
     37 
     38 sub do_check {
     39    (my $checkip, my $checkport,my $routername) = @_;
     40    # as socksports may not be published (therefore "0") here,
     41    # let's try 9050, the default port:
     42    if ($checkport == 0) { $checkport = 9050; }
     43    # print "Checking $checkip:$checkport\n";
     44    my $s5socket = IO::Socket::INET->new(PeerAddr => $checkip,
     45        PeerPort => $checkport, Proto => "tcp", Type => SOCK_STREAM,
     46        Timeout => "20");
     47    if ($s5socket) {
     48        my @got;
     49        print $s5socket pack("CCC",'5','1','0');
     50        eval {
     51            local $SIG{ALRM} = sub { die "alarm\n" };
     52            alarm 10;
     53            read ($s5socket,$got[0],1);
     54            read ($s5socket,$got[1],1);
     55            alarm 0;
     56        };
     57        if ($@) {
     58            return; # die unless $@ eq "alarm\n";
     59        }
     60        if ($got[0] eq pack('C','5')) {
     61            if(defined($routername)) {
     62                print "Found SOCKS5 at $routername ($checkip:$checkport)\n";
     63            } else {
     64                print "Found SOCKS5 at $checkip:$checkport\n";
     65            }
     66        } else {
     67            if(defined($routername)) {
     68                print "$routername ($checkip:$checkport) answers - " .
     69                      "but not SOCKS5.\n";
     70            } else {
     71                print "$checkip:$checkport answers - but not SOCKS5.\n";
     72            }
     73        }
     74    } else {
     75        if(defined($routername)) {
     76            print STDERR "Can't connect to $routername " .
     77                         "($checkip:$checkport) ($!)\n";
     78        } else {
     79            print STDERR "Can't connect to $checkip:$checkport ($!)\n";
     80        }
     81    }
     82 }