epicvinny has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl package PerlSvc; use warnings; use strict; #nome do servico e display name #$PerlSvc::Name = 'Proxy'; #$PerlSvc::DisplayName = 'SQL Proxy'; use IO::Socket::INET; use IO::Select; use File::Slurp; #sub PerlSvc::Startup(){ #while (ContinueRun()) { our @allowed_ips = ('all', '127.0.0.1'); our @hostwhitelist = read_file("C:\\proxy\\HostsWhitelist.txt", chomp => 1); our @payloadblacklist = read_file("C:\\proxy\\PayloadBlacklist.txt", chomp => 1); our @payloadwhitelist = read_file("C:\\proxy\\PayloadWhitelist.txt", chomp => 1); #sub Pause { } #sub Continue { } #sub Interactive { } #sub Help { } #sub Stop { } our $ioset = IO::Select->new; our %socket_map; my $debug = 1; local $| = 1; sub new_conn { #my ($host, $port) = @_; my $host = "127.0.0.1"; my $port = "1434"; return IO::Socket::INET->new( PeerAddr => $host, PeerPort => $port ) || die "Unable to connect to $host:$port: $!"; } sub new_server { #my ($host, $port) = @_; my $host = ""; my $port = "1433"; my $server = IO::Socket::INET->new( LocalAddr => $host, LocalPort => $port, ReuseAddr => 1, Listen => 9999 ) || die "Unable to listen on $host:$port: $!"; } sub new_connection { my $server = shift; my $remote_host = shift; my $remote_port = shift; my $client = $server->accept; my $client_ip = client_ip($client); my $peerip = $server->peerhost(); my $ip_peer = $client->peerhost; unless (client_allowed($ip_peer)) { #print "Connection from $ip_peer denied.\n" if $debug; $client->close; return; } #print "Connection from $ip_peer accepted.\n" if $debug; my $remote = new_conn($remote_host, $remote_port); $ioset->add($client); $ioset->add($remote); $socket_map{$client} = $remote; $socket_map{$remote} = $client; } sub close_connection { my $client = shift; my $client_ip = client_ip($client); my $remote = $socket_map{$client}; $ioset->remove($client); $ioset->remove($remote); delete $socket_map{$client}; delete $socket_map{$remote}; $client->close; $remote->close; #print "Connection from $client_ip closed.\n" if $debug; } sub client_ip { my $client = shift; return inet_ntoa($client->sockaddr); } sub liberado { my $client_ip = shift; return grep { $_ eq $client_ip } @hostwhitelist; } sub client_allowed { my $client_ip = shift; return grep { $_ eq $client_ip || $_ eq 'all' } @allowed_ips; } #die "Usage: $0 <local port> <remote_host:remote_port>" unless @ARGV == 2; my $local_port = "1433"; #my ($remote_host, $remote_port) = split ':', shift(); my $remote_host = "127.0.0.1"; my $remote_port = "1434"; print "Starting proxy at localhost:$local_port\n"; my $server = new_server('0.0.0.0', $local_port); $ioset->add($server); #while (ContinueRun(1)) { while (1) { for my $socket ($ioset->can_read) { if ($socket == $server) { new_connection($server, $remote_host, $remote_port); } else { next unless exists $socket_map{$socket}; my $remote = $socket_map{$socket}; my $buffer; my $read = $socket->sysread($buffer, 10*1024); my $ip_client = $remote->peerhost; my $port_client = $remote->peerport; my $buffer_tmp = $buffer; my $ip_dests = $socket->peerhost; #my $ip_dests = $socket->sockhost; #print "-------------------------------------------------- +----------------\n"; #print "Log: IP_Client: $ip_client Remote_Host: $ip_dests +Port_client: $port_client: $buffer\n"; if (!liberado($ip_dests) && ($remote_port eq $port_client) + && ($ip_client eq $remote_host)) { #print "Buffer recebido de $ip_client na porta $por +t_client. Analisando...\n"; #print "O comando IP $ip_client deve ser filtrado.. +.\n"; foreach (@payloadblacklist) { #print "Blocking ($_)\n"; $buffer =~ s/$_/BLOCKED/gi; } } else { #print "Buffer recebido de $ip_client é liberado... +\n"; } #print "-------------------------------------------------- +----------------\n"; if ($read) { #print "Read...\n"; $remote->syswrite($buffer); } else { close_connection($socket); } } } } #} #} #close (log_file);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Multithread/fork TCP Proxy?
by karlgoethebier (Abbot) on Jan 11, 2015 at 18:03 UTC | |
|
Re: Multithread/fork TCP Proxy?
by locked_user sundialsvc4 (Abbot) on Jan 12, 2015 at 01:37 UTC |