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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
#!/usr/bin/perl
# proxytee.pl, proxy between a set of dancer servers, dumping the traffic
# between them to stdout
# Based on example 17-6 (recipe 17.13) in the perl cookbook (bighorn sheep)
#
# This file is copyright (C) 2001 Andrew Suffield
# <asuffield@users.sourceforge.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
use warnings;
use strict;
use Carp;
use POSIX;
use IO::Socket;
use IO::Select;
use Tie::RefHash;
my $port = 6667;
my %servers = ();
my %skip = ();
# Command filter
# (These commands will never be displayed)
foreach my $command (qw/PING PONG SPINGTIME/)
{
$skip{uc $command} = 1;
}
read_config();
my (%inbuffer, %outbuffer, %ready, %peer, %preconnect_buffer, %name) = ((), (), (), (), (), ());
my (%client_name, %last_wallops, %last_wallops_to) = ((), (), ());
tie %ready, 'Tie::RefHash';
tie %preconnect_buffer, 'Tie::RefHash';
my $listener = IO::Socket::INET->new(LocalPort => $port,
Listen => SOMAXCONN,
ReuseAddr => 1,
Type => SOCK_STREAM)
or croak "Couldn't listen on $port: $@\n";
nonblock($listener);
my $select = IO::Select->new();
$select->add($listener);
# Main polling loop
while (1)
{
foreach my $client ($select->can_read(1))
{
if ($client == $listener)
{
# We've got a new incoming connection. Accept it.
my $new_client = $listener->accept();
# OK, did that succeed?
if (defined $new_client)
{
$select->add($new_client);
nonblock($client);
}
}
else
{
# Normal connection, incoming data
my $data = '';
my $rv = $client->recv($data, POSIX::BUFSIZ, 0);
unless (defined $rv and length $data)
{
# Either EOF or some network error, clean up
close_socket($peer{$client}) if exists $peer{$client};
close_socket($client);
next;
}
$inbuffer{$client} .= $data;
# OK, do we have a complete line?
while ($inbuffer{$client} =~ s/(.*\n)//)
{
# Queue 'em up
push @{$ready{$client}}, $1;
}
}
}
# Parse the processing queues
foreach my $client (keys %ready)
{
handle($client);
}
# And flush outgoing buffers
foreach my $client ($select->can_write(1))
{
next unless exists $outbuffer{$client};
my $rv = $client->send($outbuffer{$client}, 0);
unless (defined $rv)
{
warn "select said I could write, but send failed\n";
next;
}
if ($rv == length $outbuffer{$client} || $! == POSIX::EWOULDBLOCK)
{
# We wrote something. Remove it from the buffer
substr($outbuffer{$client}, 0, $rv) = '';
delete $outbuffer{$client} if 0 == length $outbuffer{$client};
}
else
{
# Something failed during write. Kill the connection.
close_socket($peer{$client}) if exists $peer{$client};
close_socket($client);
next
}
}
}
# NOT REACHED
exit;
sub handle
{
my $client = shift;
foreach my $request (@{$ready{$client}})
{
$request =~ s/\r?\n$//;
my ($prefix, $command, @args) = parse($request);
if (exists $peer{$client})
{
unless (exists $skip{$command} or ($command eq 'NOTICE' and not defined $prefix))
{
my $sargs = '';
$sargs = join ' ', (@args) if (scalar @args);
if ($command eq "WALLOPS")
{
# Eliminate forwarded WALLOPS
if ($name{$client} eq $prefix)
{
# Eliminate repeated WALLOPS
unless (defined $last_wallops{$name{$client}}
and $last_wallops{$name{$client}} eq $sargs
and $last_wallops_to{$name{$client}} ne $name{$peer{$client}})
{
print "$name{$client} WALLOPS $sargs\n" if $name{$client} eq $prefix;
$last_wallops{$name{$client}} = $sargs;
$last_wallops_to{$name{$client}} = $name{$peer{$client}};
}
}
}
else
{
my $output = "$name{$client} -> $name{$peer{$client}} ";
$output .= ":$prefix " if defined $prefix;
$output .= $command;
$output .= " " . $sargs if length $sargs;
print "$output\n";
}
}
$outbuffer{$peer{$client}} .= "$request\r\n";
}
else
{
push @{$preconnect_buffer{$client}}, $request;
if ($command eq 'CHALL' and defined $args[1])
{
my ($from, $to) = ($args[0], $args[1]);
print "Server connected, claiming to be $from, asking for $to\n";
my $target = $servers{$to} if exists $servers{$to}
or do
{
print "Don't have a server record for $to\n";
close_socket($client);
return;
};
my $target_socket = IO::Socket::INET->new(PeerAddr => $target->{addr},
PeerPort => $target->{port})
or do
{
print "Couldn't connect to ".$target->{addr}.':'.$target->{port}.": $@\n";
close_socket($client);
return;
};
nonblock($target_socket);
print "Connected to ".$target->{addr}.':'.$target->{port}.", repeating buffer\n";
$peer{$client} = $target_socket;
$peer{$target_socket} = $client;
$name{$client} = $from;
$client_name{$from} = $client;
$name{$target_socket} = $to;
$client_name{$to} = $target_socket;
$select->add($target_socket);
foreach my $line (@{$preconnect_buffer{$client}})
{
$outbuffer{$target_socket} .= "$line\r\n";
}
delete $preconnect_buffer{$client};
}
}
}
delete $ready{$client};
}
sub parse
{
my $line = shift;
my ($prefix, $command, $args);
if ($line =~ /^:(\S*) (.*)$/)
{
$prefix = $1;
my $rest = $2;
if ($rest =~ /(\S*) (.*)/)
{
$command = $1;
$args = $2;
}
else
{
# Grmph. No arguments.
$command = $rest;
}
}
else
{
if ($line =~ /(\S*) (.*)/)
{
$command = $1;
$args = $2;
}
else
{
# Grmph. No arguments.
$command = $line;
}
}
my @args = ();
@args = split ' ', $args if defined $args;
return ($prefix, $command, @args);
}
sub nonblock
{
my $socket = shift;
use Fcntl;
my $flags = fcntl($socket, F_GETFL, 0)
or die "Can't get flags for socket: $!\n";
fcntl($socket, F_SETFL, $flags | O_NONBLOCK)
or die "Can't set O_NONBLOCK on socket: $!\n";
}
sub close_socket
{
my $client = shift;
delete $inbuffer{$client};
delete $outbuffer{$client};
delete $ready{$client};
if (exists($name{$client}))
{
delete $client_name{$name{$client}};
delete $name{$client};
}
delete $peer{$client} if exists $peer{$client};
$select->remove($client);
close $client;
}
sub read_config
{
open(CONF, "proxy.rc")
or die "Can't open proxy.rc: $!\n";
while(my $line = <CONF>)
{
next if $line =~ /^\s*(\#.*)?$/; # Comments and lines containing only whitespace
my ($name, $addr, $port) = $line =~ /(.*),(.*),(.*)/;
$servers{$name} = {addr => $addr,
port => $port,
};
}
close CONF;
}
|