From 107a469d5d1cc07c2b4b4ba3562cd09f695b433c Mon Sep 17 00:00:00 2001 From: cmaffio Date: Mon, 30 May 2016 15:34:58 +0200 Subject: [PATCH] completata condes --- DBDiff/condex.sql | 2 + condes/script/condes.pl | 297 +++++++--------------------------------- 2 files changed, 54 insertions(+), 245 deletions(-) diff --git a/DBDiff/condex.sql b/DBDiff/condex.sql index 1df925a..1a2af9a 100644 --- a/DBDiff/condex.sql +++ b/DBDiff/condex.sql @@ -39,3 +39,5 @@ CREATE TABLE `condes_logs` ( PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 +GRANT SELECT, UPDATE (`stato`) ON `pannello_vbc`.`condes_regole` TO 'pannello_proxy'@'%'; +GRANT SELECT ON `pannello_vbc`.`condes_macchine` TO 'pannello_proxy'@'%'; diff --git a/condes/script/condes.pl b/condes/script/condes.pl index 72304f9..f033a69 100755 --- a/condes/script/condes.pl +++ b/condes/script/condes.pl @@ -3,9 +3,6 @@ $|=1; use DBI; use FindBin qw($Bin); -use threads; -use threads::shared; -use Thread::Semaphore; my $param = shift @ARGV; @@ -23,267 +20,77 @@ $sts = $dbmysql->prepare("use $DBname"); $sts->execute (); $query = " SELECT + condes_regole.id, condes_regole.id_condes_macchine, condes_regole.id_proxy_acl_list, - condes_macchine.id_proxy_pool + condes_macchine.id_proxy_pool, + proxy_pool.ip, + proxy_pool.nome, + proxy_acl_list.nome AS acl FROM condes_regole JOIN condes_macchine ON condes_regole.id_condes_macchine = condes_macchine.id + JOIN + proxy_pool + ON + condes_macchine.id_proxy_pool = proxy_pool.id + JOIN + proxy_acl_list + ON + condes_regole.id_proxy_acl_list = proxy_acl_list.id WHERE condes_regole.stato = 1 "; -$query = " UPDATE - condes_regole - SET - stato = $valore, - modifica = NOW() - WHERE - id_condes_macchine = ".$dato['id_condes_macchine']." - AND - id_proxy_acl_list = ".$dato['id_proxy_acl_list']; - -$query = " INSERT INTO - proxy_acl_ip - SET - idacllist = ".$dato['id_proxy_acl_list'].", - idpool = ".$dato['id_proxy_pool'].", - stato = $stato, - data = NOW() - ON DUPLICATE KEY UPDATE - stato = $stato, - data = NOW() -"; - -$query = " INSERT INTO - condes_logs - SET - id_utenti = 000, - id_condes_regole = $id, - stato = $valore, - data = NOW() -"; - - - - - - - - - - - - - - - - - -$query = " SELECT - - - - - -"; - - - - - - - $sts = $dbmysql->prepare($query); $sts->execute (); -$ref = $sts->fetchrow_hashref; -our $limit_thread = $$ref{'valore'}; - -$query = "SELECT ip FROM proxy_pool GROUP BY ip"; -$sts = $dbmysql->prepare($query); -$sts->execute (); - -our @listaip = (); while ( $ref = $sts->fetchrow_hashref ) { - push @listaip, $$ref{'ip'}; -} + $id = $$ref{'id'}; + $condes_macchine = $$ref{'id_condes_macchine'}; + $proxy_acl_list = $$ref{'id_proxy_acl_list'}; + $proxy_pool = $$ref{'id_proxy_pool'}; + $proxy_pool_ip = $$ref{'ip'}; + $proxy_pool_nome = $$ref{'nome'}; + $acl = $$ref{'acl'}; -$query = " SELECT - id, - ip, - nome, - port - FROM - proxy_conn - WHERE - attivo = 1 + printf "Rimosso %-15s (%-10s) con lista %s\n", $proxy_pool_ip, $proxy_pool_nome, $acl if ($verbose); + + $query = " UPDATE + proxy_acl_ip + SET + stato = 0, + data = NOW() + WHERE + idacllist = $proxy_acl_list + AND + idpool = $proxy_pool "; -$sts = $dbmysql->prepare($query); -$sts->execute (); + my $sts = $dbmysql->prepare($query); $sts->execute (); -$queryDB = ""; -share ($queryDB); -$sem1 = Thread::Semaphore->new(1); -$sem2 = Thread::Semaphore->new(0); + $query = " INSERT INTO + condes_logs + SET + id_utenti = 0, + id_condes_regole = $id, + stato = 0, + data = NOW() + "; + $sts = $dbmysql->prepare($query); $sts->execute (); -my %proc; -# Generazione figli per DB -$proc{'database'} = threads->create(\&database); -# Generazione figli per cattura pacchetti -while ( $ref = $sts->fetchrow_hashref ) { - my $id = $$ref{'id'}; - my $ip = $$ref{'ip'}; - my $port = $$ref{'port'}; - my $nome = $$ref{'nome'}; - $proc{$nome} = threads->create(\&conntrack, $id, $ip, $port); - print "Attivato processo $nome\n" if ($verbose); + $query = " UPDATE + condes_regole + SET + stato = 0, + modifica = NOW() + WHERE + id_condes_macchine = $condes_macchine + AND + id_proxy_acl_list = $proxy_acl_list + "; + $sts = $dbmysql->prepare($query); $sts->execute (); } -while (1) { - sleep 30; - - foreach my $variabile (keys %proc) { - $puntatore = $proc{$variabile}; - if(!$puntatore or !$puntatore->is_running ) { - print "Rilevato termine processo $variabile\n" if ($verbose); - delete $proc{$variabile}; - #$puntatore->join(); - - if ($variabile eq "database") { - $proc{'database'} = threads->create(\&database); - } else { - $query = "SELECT id, ip, port FROM proxy_conn WHERE nome = '$variabile'"; - $sts = $dbmysql->prepare($query); - $sts->execute (); - $ref = $sts->fetchrow_hashref; - my $id = $$ref{'id'}; - my $ip = $$ref{'ip'}; - my $port = $$ref{'port'}; - $proc{$variabile} = threads->create(\&conntrack, $id, $ip, $port); - } - } - } -} exit; - -sub conntrack { - my $sess_id = shift; - my $proxy_IP = shift; - my $proxy_PORT = shift; - - $SIG{INT} = \&end; - - my $conta = 0; - - print "Attivato agente $sess_id su $proxy_IP:$proxy_PORT\n" if ($verbose); - - my $proc_id = open CT, "/usr/sbin/conntrack -E -eNEW,DESTROY -otimestamp,id -p tcp -d $proxy_IP --dport $proxy_PORT 2>/dev/null |" or die "non va\n"; - - while () { - my $riga = $_; - - if ( $riga =~ /^\s*\[(\d+)\.\d+\]\s+\[NEW\] tcp\s+\d+ \d+ \S*\s*src=(\S+) dst=(\S+) sport=\d+ dport=(\d+)\s+\[\w+\] src=\S+ dst=\S+ sport=\d+ dport=\d+ id=(\d+)/ ) { - my $remote_START = $1; - my $remote_IP = $2; - my $local_IP = $3; - my $local_PORT = $4; - my $thread_ID = $5; - - my $query = " INSERT INTO - proxy_dati - (ip, thread, data_new) - VALUES - ('$remote_IP', $thread_ID, FROM_UNIXTIME($remote_START)) - "; - - $sem1->down(); - $queryDB = $query; - $sem2->up(); - print "Invio query INSERT\n" if ($verbose > 1); - - if (!grep( /^$remote_IP$/, @listaip )) { - $sem1->down(); - $queryDB = "INSERT INTO proxy_pool (ip,pool,attivo,ins) VALUE ('$remote_IP', 1, 0,now())"; - $sem2->up(); - push @listaip, $remote_IP; - print "Invio query INSERT in proxy_pool\n" if ($verbose > 1); - } - } - - 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+)/) { - my $remote_DESTROY = $1; - my $remote_IP = $2; - my $local_IP = $3; - my $local_PORT = $4; - my $remote_SEND = $5; - my $remote_RECEIVE = $6; - my $thread_ID = $7; - - $conta++; - printf ("$conta/$limit_thread - $sess_id - IP: %-15s - ID: %-12s - Inviati: %10d - Ricevuti: %10d\n", $remote_IP, $thread_ID, $remote_SEND, $remote_RECEIVE) if ($verbose); - - my $query = " UPDATE - proxy_dati - SET - data_destroy = FROM_UNIXTIME($remote_DESTROY), - inviati = $remote_SEND, - ricevuti = $remote_RECEIVE, - tempo = TIMESTAMPDIFF(SECOND, data_new, FROM_UNIXTIME($remote_DESTROY)), - vel_inv = $remote_SEND / TIMESTAMPDIFF(SECOND, data_new, FROM_UNIXTIME($remote_DESTROY)), - vel_ric = $remote_RECEIVE / TIMESTAMPDIFF(SECOND, data_new, FROM_UNIXTIME($remote_DESTROY)) - WHERE - thread = $thread_ID - AND - ip = '$remote_IP' - AND - data_destroy = '1970-01-01 00:00:00' - "; - - $sem1->down(); - $queryDB = $query; - $sem2->up(); - print "Invio query UPDATE\n" if ($verbose > 1); - } - if ($conta >= $limit_thread) { - last; -# kill 9, $proc_id; -# return - } - } - kill 9, $proc_id; - return; -} - -sub database { - print "Attivato thread DB\n" if ($verbose); - - $dbmysql = DBConn (); - - while (1) { - print "DB in attesa\n" if ($verbose > 1); - $sem2->down(); - print "Query arrivata\n" if ($verbose > 1); - - if ( not $dbmysql->ping ) { - $dbmysql = DBConn (); - } - - my $sts = $dbmysql->prepare($queryDB); - $sts->execute (); - - $sts->finish; - $sem1->up(); - } -} - -sub DBConn { - print "Connessione DB\n" if ($verbose > 1); - my $dbmysql = DBI->connect("DBI:mysql:;host=$DBhost", $DBuser, $DBpass, {PrintError => 0, RaiseError => 0, AutoCommit =>1, mysql_auto_reconnect=>1} ) or die ($DBI::errstr); - $sts = $dbmysql->prepare("use $DBname"); - $sts->execute (); - - return $dbmysql; -}