minor tweaks of the v2-conn-protocol text
[tor.git] / contrib / checksocks.pl
blob3fcc0df14a45179a2cddfecf2cae835b5c95d76a
1 #!/usr/bin/perl -w
3 require 5.005;
4 use strict;
5 use IO::Socket;
6 use Getopt::Std;
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.
12 # Contributed by Peter Kornherr <peter at wuschelpuschel dot org>, and
13 # cleaned up by Peter Palfrader <peter at palfrader dot org>.
15 our($opt_i,$opt_p,$opt_d,$opt_h,$opt_l);
16 getopts('i:p:dhl:');
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;
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);
32 } elsif ($opt_i && $opt_p) {
33 &do_check($opt_i,$opt_p);
34 } elsif ($opt_l) {
35 &do_check(split(":",$opt_l));
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;
57 if ($@) {
58 return; # die unless $@ eq "alarm\n";
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";
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";
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";