script/telnet_over_socks_chain.pl untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 176 CC 15 SCAR 52.0
CC
15
Cov
0%
main
240.0
BEGIN
2.0
BEGIN
2.0
CRAP 180.5
1 #!/usr/local/bin/perl -w
2 ########################################################################
3 #
4 # $Id: telnet_over_socks_chain.pl,v 1.10 2004/03/27 12:31:04 gosha Exp $
5 #
6 # Copyright (C) Okunev Igor gosha@prv.mts-nn.ru 2002-2004
7 #
8 ########################################################################
9 use strict;
10  
11 use Getopt::Long;
12 use IO::Socket;
13 use Net::SC 1.11;
14 use Symbol;
15  
16 sub main {
17 my ( $rc, $sh, $socks_c, $socket, $sh1, $child );
18 my ( $local_port, $local_host, $port, $host, %opt, $sym );
19  
20 GetOptions( \%opt, 'rnd!' , # Random
21 'as!' , # Auto_save
22 'l=i' , # Chain_len
23 'd:i' , # Debug
24 'to=i' , # Time out
25 'lf=s' , # Log_file
26 't=s' , # Target
27 'cd=i' , # Check_delay
28 'asc!' , # Asc command name for exec
29 'if=s' , # Read command name for exec from file
30 'cmd=s', # Command name for exec
31 'cfg=s', # Configuration file
32 'lo=i', # Loop connection flag
33 );
34
35 $socket = IO::Socket::INET->new(
36 Listen => 3,
37 Proto => 'tcp',
38 Timeout => $opt{ 'to' } || 30,
39 Type => IO::Socket::SOCK_STREAM
40 ) || die $!;
41  
42 $socks_c = Net::SC->new(
43 Timeout => ( $opt{'to'} || 10 ),
44 Chain_Len => ( $opt{'l'} || 2 ),
45 Debug => ( $opt{'d'} || 0x09 ),
46 Log_File => ( $opt{'lf'} || undef ),
47 Check_Delay => ( $opt{'cd'} || 3600 * 24),
48 Random_Chain => ( $opt{'rnd'} || 0 ),
49 Auto_Save => ( $opt{'as'} ? 0:1 ),
50 Loop_Connection => ( $opt{'lo'} || 0x03 ),
51 ) || die;
52  
53 die unless ref $socks_c;
54  
55 if ( exists $opt{'cfg'} and defined $opt{'cfg'} ) {
56 $socks_c->configure( Chain_file => $opt{'cfg'} );
57 } elsif ( exists $ENV{SC_CONF} and defined $ENV{SC_CONF} ) {
58 $socks_c->configure( Chain_file => $ENV{SC_CONF} );
59 }
60  
61 if ( $opt{'t'} =~ m#^([^:\s/]+)(?::(\d+))?$# ) {
62 ( $host, $port ) = ( $1, ( $2 || 23) );
63 } else {
64 ( $host, $port ) = ( 'localhost', 23 );
65 }
66  
67 $local_port = $socket->sockport();
68 $local_host = $socket->sockhost();
69  
70 if ( fork ) {
71 alarm 30;
72 LOOP: {
73 $sh = $socket->accept();
74 unless ( defined $sh ) {
75 die $!
76 }
77 if ( $sh->peerhost() ne $local_host and $sh->peerhost() ne '127.0.0.1' ) {
78 print $sh "Accsess deny\n";
79 print STDERR "Accsess deny [ ". $sh->peerhost ." ]\n";
80 $sh->close;
81 redo LOOP;
82 }
83 }
84 alarm 0;
85 print STDERR "\nConnect to $host:$port - please wait....\n";
86 unless ( ( $rc = $socks_c->connect( $host, $port ) ) == SOCKS_OKAY ) {
87 print STDERR "\nCan't connect to $host:$port [".( socks_error($rc) )."]\n";
88 shutdown $sh, 2;
89 exit;
90 }
91 print STDERR "\nConnect to $host:$port - Ok\n";
92 $sh1 = $socks_c->sh;
93 if ( fork ) {
94 select((select($sh1), $| = 1)[0]);
95 while ( read( $sh, $_, 1 ) ) { print $sh1 $_ }
96 } else {
97 select((select($sh), $| = 1)[0]);
98 while ( read( $sh1, $_, 1 ) ) { print $sh $_ }
99 }
100 shutdown $sh, 2;
101 shutdown $sh1, 2;
102 } else {
103 sleep(1);
104 if ( $opt{'asc'} ) {
105 if ( -t STDIN ) {
106 print STDERR "Enter command : ";
107 }
108 undef $/; $_ = <STDIN>; $/ = "\n";
109 s#\%PORT\%#$local_port#gs;
110 exec( $_ );
111 } elsif ( $opt{'cmd'} ) {
112 ( $_ = $opt{'cmd'} ) =~ s#\%PORT\%#$local_port#gs;
113 exec( $_ );
114 } elsif ( $opt{'if'} && -e $opt{'if'} ) {
115 $sym = gensym;
116 open ( $sym, '<' . $opt{'if'} ) || die "Can't open data file: $!\n";
117 undef $/; $_ = <$sym>; $/ = "\n";
118 s#\%PORT\%#$local_port#gs;
119 exec( $_ );
120 } else {
121 exec( '/usr/bin/telnet', 'localhost', $local_port );
122 }
123 }
124 }
125  
126 unless ( scalar @ARGV ) {
127 print <<END_HELP;
128  
129 Usage $0 \%OPTIONS\%
130  
131 OPTIONS :
132 -rnd - Create random chain
133 -asc - ASC Command name for exec
134 -as - No auto save socks data structure
135 -cmd XX - Command name for exec
136 -cfg XX - Configuration file
137 -lo XX - Loop connection flag
138 -l XX - Create chain of XX length
139 -d XX - Debug on
140 -to XX - Set timeout to XX for create chain
141 -lf XX - Dump log to XX file
142 -if XX - Read command for exec from file XX
143 -cd XX - Delay for new checing Socks server ( in seconds )
144 -t XX - Destination host ( host[:port] )
145 END_HELP
146 exit;
147 }
148  
149 main();
150