Changeset 40
- Timestamp:
- 11/05/08 22:10:06 (2 months ago)
- Files:
-
- 1 modified
-
network/forward.pl (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
network/forward.pl
r3 r40 1 1 #!/usr/bin/perl 2 2 3 #=========== 4 # forward.pl 5 #=========== 3 =head1 NAME 4 5 forward.pl - forward a socket to remote server 6 7 =head1 SYNOPSIS 8 9 $ forward.pl --local-port <i> --peer-addr <s> --peer-port <i> 10 11 =head1 DESCRIPTION 12 13 Will listen to C<--local-port>, and will forward packages to C<--peer-addr> 14 at port C<--peer-port>. 15 16 =cut 6 17 7 18 use strict; 8 19 use IO::Socket::INET; 9 use vars qw/$ENABLED @VALID_PEERS $LOCAL_PORT $PEER_ADDR $PEER_PORT/;20 use Getopt::Long qw/:config auto_help auto_version/; 10 21 11 12 #== USER SETTINGS ============================================================ 13 $ENABLED = 1; 14 $LOCAL_PORT = shift @ARGV or usage(); 15 $PEER_ADDR = shift @ARGV or usage(); 16 $PEER_PORT = shift @ARGV or usage(); 17 @VALID_PEERS = @ARGV; 18 #============================================================================= 19 20 my $server = IO::Socket::INET->new( 21 LocalPort => $LOCAL_PORT, 22 Proto => 'tcp', 23 Listen => 5, 24 Timeout => 5, 25 ) or die "Could not create socket: $!\n"; 22 our $VERSION = 0.3; 23 our $NAME = q(forward.pl); 24 our $ARG = { 'enable' => 1, 'accept' => [] }; 26 25 $|++; 27 26 28 print "Port forwarding has started:\n"; 29 print "From port $LOCAL_PORT to $PEER_ADDR\:$PEER_PORT\n"; 30 print "Valid peers: @VALID_PEERS\n"; 27 GetOptions($ARG, qw/ 28 local-port=i accept=s@ 29 peer-addr=s peer-port=i 30 enable 31 /); 31 32 32 LISTEN: 33 while(1) { 33 my @missing = grep { !defined $ARG->{$_} } qw/local-port peer-addr peer-port/; 34 34 35 ### init 36 my $client = $server->accept or next LISTEN; 35 if(@missing) { 36 warn "Missing: @missing\n"; 37 Getopt::Long::HelpMessage(); 38 } 37 39 38 ### debug 39 print "Checking peer host: " .$client->peerhost() ."... "; 40 printf("Forward port %i to %s:%i for %s\n", 41 $ARG->{'local-port'}, 42 $ARG->{'peer-addr'}, 43 $ARG->{'peer-port'}, 44 join(", ", @{ $ARG->{'accept'} }) || 'all clients', 45 ); 40 46 41 ### check peer 42 grep { $_ eq $client->peerhost } @VALID_PEERS or next LISTEN; 43 print "OK"; 47 my $server = create_socket(); 48 handle_connection() while(1); 44 49 45 ### connect to printer 46 my $printer = IO::Socket::INET->new( 47 PeerAddr => $PEER_ADDR, 48 PeerPort => $PEER_PORT, 49 ) or die "Could not connect to printer: $!\n"; 50 sub handle_connection { 51 my $client = $server->accept or return; 52 my($peer, $buf); 50 53 51 ### print 52 if($ENABLED) { 53 print $printer $_ while(<$client>); 54 accept_connection($client) or return; 55 56 $peer = IO::Socket::INET->new( 57 PeerAddr => $ARG->{'peer-addr'}, 58 PeerPort => $ARG->{'peer-port'}, 59 ) or die "Could not connect to peer port: $!\n"; 60 61 if($ARG->{'enable'}) { 62 print $peer $_ or last while(read $client, $buf, 1024); 54 63 } 55 64 else { 56 print "." while(<$client>);65 print $buf or last while(read $client, $buf, 1024); 57 66 } 67 68 printf "%s disconnected\n", $client->peerhost; 58 69 } 59 70 60 ### THE END 61 exit 0; 71 sub accept_connection { 72 my $client = shift; 73 my $host = $client->peerhost; 74 my $accept = $ARG->{'accept'}; 62 75 76 unless(@$accept) { 77 print "Connection accepted from $host\n"; 78 return 1; 79 } 63 80 64 sub usage { #================================================================= 81 printf("Checking %s against %s...", 82 $host, join(", ", @{ $ARG->{'accept'} }), 83 ); 65 84 66 print <<'USAGE'; 67 Usage: 68 $ forward.pl <LISTEN_PORT> <PEER_ADDR> <PEER_PORT> <ALLOW_CLIENTS> 85 unless(grep { $_ eq $host } @$accept) { 86 print "Not allowed\n"; 87 return; 88 } 69 89 70 USAGE 90 print "Allowed\n"; 91 return 1; 92 } 71 93 72 exit 255; 94 sub create_socket { 95 IO::Socket::INET->new( 96 LocalPort => $ARG->{'local-port'}, 97 Proto => 'tcp', 98 Listen => 5, 99 Timeout => 5, 100 ) or die "Could not create socket: $!\n"; 73 101 } 102 103 =head1 AUTHOR 104 105 Jan Henning Thorsen - pm at flodhest.net 106 107 =cut 108
