How about something already done?! ;-P
#!/usr/bin/perl
$|=1;
use IO::Socket ;
use IO::Select ;
my $redirect_host = 'smtp.fln.terra.com.br' ;
my $redirect_port = 25 ;
my $local_port = $redirect_port ;
my $accept_externals = 0 ;
my $log_file = 'log.txt' ;
######################################################################
+##########
my $server = IO::Socket::INET->new(
Listen => 5,
( !$accept_externals ? (LocalAddr => 'localhost') : () ) ,
LocalPort => $local_port ,
Proto => 'tcp' ,
Blocking => 1 ,
) ;
my $client = $server->accept;
my $remote = new IO::Socket::INET(
PeerAddr => $redirect_host ,
PeerPort => $redirect_port ,
Proto => 'tcp',
Timeout => 30) ;
my %socks = (
$remote => 'SRV' ,
$client => 'CLT' ,
) ;
my %redir = (
$remote => $client ,
$client => $remote ,
) ;
open (LOG,">$log_file") ;
my $sel = select(LOG) ; $|=1; select($sel);
print "Waiting client...\n" ;
my $can_read = IO::Select->new( $remote , $client );
my @has_buf ;
while( @has_buf = $can_read->can_read(1) ) {
foreach my $has_buf_i ( @has_buf ) {
my $redir = $redir{$has_buf_i} ;
my $buffer ;
my $sel = IO::Select->new($has_buf_i) ;
while( $sel->can_read(0.1) ) {
read($has_buf_i, $buffer , 1 , length($buffer) ) ;
if ($buffer =~ /\n$/s ) { last ;}
}
if ($buffer ne '') {
print $redir $buffer ;
print "$socks{$has_buf_i}>> $buffer" ;
print LOG "$socks{$has_buf_i}>> $buffer" ;
}
}
}
This script will listen and redirect to some port, and log the data to a file. Just edit the variables in the top and run. I used that to check the security of the SMTP server of my ISP.
Graciliano M. P.
"Creativity is the expression of the liberty".
|