checksocks.pl 2.5 KB

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