Gestione conntrack multiprocesso con watchguard

This commit is contained in:
cmaffio
2016-03-23 12:01:23 +01:00
parent 0e421a3956
commit 43e9c95d9a
3 changed files with 112 additions and 66 deletions

BIN
proxy/.conntrack.pl.swp Normal file

Binary file not shown.

View File

@@ -6,7 +6,7 @@ use FindBin qw($Bin);
my $param = shift @ARGV;
$verbose = 0;
our $verbose = 0;
if ($param eq '-v') {
$verbose = 1;
}
@@ -19,7 +19,56 @@ my $dbmysql = DBI->connect("DBI:mysql:;host=$DBhost", $DBuser, $DBpass) or die (
$sts = $dbmysql->prepare("use $DBname");
$sts->execute ();
open CT, "/usr/sbin/conntrack -E -eNEW,DESTROY -otimestamp,id -p tcp --dport $proxy_PORT |" or die "non va\n";
$query = " SELECT
id,
ip,
port
FROM
proxy_conn
WHERE
attivo = 1
";
$sts = $dbmysql->prepare($query);
$sts->execute ();
while ( $ref = $sts->fetchrow_hashref ) {
my $id = $$ref{'id'};
my $ip = $$ref{'ip'};
my $port = $$ref{'port'};
if (!fork) {
while (1) {
my $pidi = fork;
if (!$pidi) {
conntrack ($id, $ip, $port);
exit;
} else {
waitpid $newpidi, WNOHANG;
}
}
}
}
if ($verbose) {
while (1) { sleep 1000; }
}
exit 1;
sub conntrack {
my $sess_id = shift;
my $proxy_IP = shift;
my $proxy_PORT = shift;
print "Attivato agente $sess_id su $proxy_IP:$proxy_PORT\n" if ($verbose);
my $dbmysql = DBI->connect("DBI:mysql:;host=$DBhost", $DBuser, $DBpass) or die ($DBI::errstr);
$sts = $dbmysql->prepare("use $DBname");
$sts->execute ();
open CT, "/usr/sbin/conntrack -E -eNEW,DESTROY -otimestamp,id -p tcp -d $proxy_IP --dport $proxy_PORT |" or die "non va\n";
while (<CT>) {
my $riga = $_;
@@ -38,6 +87,7 @@ while (<CT>) {
VALUES
('$remote_IP', $thread_ID, FROM_UNIXTIME($remote_START))
";
# print "$query\n";
$sts = $dbmysql->prepare($query);
$sts->execute ();
@@ -46,9 +96,6 @@ while (<CT>) {
}
if ( $riga =~ /^\s*\[(\d+)\.\d+\]\s+\[DESTROY\] tcp\s+\d+ src=(\S+) dst=(\S+) sport=\d+ dport=(\d+) packets=\d+ bytes=(\d+) src=\S+ dst=\S+ sport=\d+ dport=\d+ packets=\d+ bytes=(\d+) \[\w+\] id=(\d+)/) {
# next if ($2 != $proxy_IP);
# next if ($3 != $proxy_PORT);
my $remote_DESTROY = $1;
my $remote_IP = $2;
my $local_IP = $3;
@@ -57,7 +104,7 @@ while (<CT>) {
my $remote_RECEIVE = $6;
my $thread_ID = $7;
print "IP: $remote_IP - ID: $thread_ID - Invati: $remote_SEND - Ricevuti: $remote_RECEIVE\n" if ($verbose);
print "$sess_id - IP: $remote_IP - ID: $thread_ID - Invati: $remote_SEND - Ricevuti: $remote_RECEIVE\n" if ($verbose);
my $query = " UPDATE
proxy_dati
@@ -83,3 +130,5 @@ while (<CT>) {
}
}
close CT;
}

View File

@@ -1,9 +1,6 @@
$proxy_IP = '';
$proxy_PORT = 3128;
$DBhost = '';
$DBname = '';
$DBuser = '';
$DBpass = '';
our $DBhost = '';
our $DBname = '';
our $DBuser = '';
our $DBpass = '';
return 1;