aboutsummaryrefslogtreecommitdiff
path: root/contrib/checksocks.pl
blob: 3fcc0df14a45179a2cddfecf2cae835b5c95d76a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/perl -w

require 5.005;
use strict;
use IO::Socket;
use Getopt::Std;

# Checks routers for open socks-ports and socks5
# Successful connects go to STDOUT, failed ones to STDERR.
# We only do one check per loop in -d mode, so it takes some time.

# Contributed by Peter Kornherr <peter at wuschelpuschel dot org>, and
# cleaned up by Peter Palfrader <peter at palfrader dot org>.

our($opt_i,$opt_p,$opt_d,$opt_h,$opt_l);
getopts('i:p:dhl:');

if ($opt_h || !($opt_d||$opt_i||$opt_l)) {
    print "Usage: $0 -d < file_with_routers_in_it\n";
    print "or:    $0 -i IP -p Port\n";
    print "or:    $0 -l IP:Port\n";
    exit;
}

if ($opt_d) {
    open (IN,"<-") or die $!;
    while (<IN>) {
        next unless /^router /;
        (my $routername,my $checkip,my $checkport) = (split(" "))[1,2,4];
        &do_check($checkip,$checkport,$routername);
    }
} elsif ($opt_i && $opt_p) {
    &do_check($opt_i,$opt_p);
} elsif ($opt_l) {
    &do_check(split(":",$opt_l));
}

sub do_check {
    (my $checkip, my $checkport,my $routername) = @_;
    # as socksports may not be published (therefore "0") here,
    # let's try 9050, the default port:
    if ($checkport == 0) { $checkport = 9050; }
    # print "Checking $checkip:$checkport\n";
    my $s5socket = IO::Socket::INET->new(PeerAddr => $checkip,
        PeerPort => $checkport, Proto => "tcp", Type => SOCK_STREAM,
        Timeout => "20");
    if ($s5socket) {
        my @got;
        print $s5socket pack("CCC",'5','1','0');
        eval {
            local $SIG{ALRM} = sub { die "alarm\n" };
            alarm 10;
            read ($s5socket,$got[0],1);
            read ($s5socket,$got[1],1);
            alarm 0;
        };
        if ($@) {
            return; # die unless $@ eq "alarm\n";
        }
        if ($got[0] eq pack('C','5')) {
            if(defined($routername)) {
                print "Found SOCKS5 at $routername ($checkip:$checkport)\n";
            } else {
                print "Found SOCKS5 at $checkip:$checkport\n";
            }
        } else {
            if(defined($routername)) {
                print "$routername ($checkip:$checkport) answers - " .
                      "but not SOCKS5.\n";
            } else {
                print "$checkip:$checkport answers - but not SOCKS5.\n";
            }
        }
    } else {
        if(defined($routername)) {
            print STDERR "Can't connect to $routername " .
                         "($checkip:$checkport) ($!)\n";
        } else {
            print STDERR "Can't connect to $checkip:$checkport ($!)\n";
        }
    }
}