completata condes
This commit is contained in:
@@ -39,3 +39,5 @@ CREATE TABLE `condes_logs` (
|
|||||||
PRIMARY KEY (`id`)
|
PRIMARY KEY (`id`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8
|
) 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'@'%';
|
||||||
|
|||||||
@@ -3,9 +3,6 @@ $|=1;
|
|||||||
|
|
||||||
use DBI;
|
use DBI;
|
||||||
use FindBin qw($Bin);
|
use FindBin qw($Bin);
|
||||||
use threads;
|
|
||||||
use threads::shared;
|
|
||||||
use Thread::Semaphore;
|
|
||||||
|
|
||||||
my $param = shift @ARGV;
|
my $param = shift @ARGV;
|
||||||
|
|
||||||
@@ -23,267 +20,77 @@ $sts = $dbmysql->prepare("use $DBname");
|
|||||||
$sts->execute ();
|
$sts->execute ();
|
||||||
|
|
||||||
$query = " SELECT
|
$query = " SELECT
|
||||||
|
condes_regole.id,
|
||||||
condes_regole.id_condes_macchine,
|
condes_regole.id_condes_macchine,
|
||||||
condes_regole.id_proxy_acl_list,
|
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
|
FROM
|
||||||
condes_regole
|
condes_regole
|
||||||
JOIN
|
JOIN
|
||||||
condes_macchine
|
condes_macchine
|
||||||
ON
|
ON
|
||||||
condes_regole.id_condes_macchine = condes_macchine.id
|
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
|
WHERE
|
||||||
condes_regole.stato = 1
|
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 = $dbmysql->prepare($query);
|
||||||
$sts->execute ();
|
$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 ) {
|
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
|
printf "Rimosso %-15s (%-10s) con lista %s\n", $proxy_pool_ip, $proxy_pool_nome, $acl if ($verbose);
|
||||||
id,
|
|
||||||
ip,
|
$query = " UPDATE
|
||||||
nome,
|
proxy_acl_ip
|
||||||
port
|
SET
|
||||||
FROM
|
stato = 0,
|
||||||
proxy_conn
|
data = NOW()
|
||||||
WHERE
|
WHERE
|
||||||
attivo = 1
|
idacllist = $proxy_acl_list
|
||||||
|
AND
|
||||||
|
idpool = $proxy_pool
|
||||||
";
|
";
|
||||||
$sts = $dbmysql->prepare($query);
|
my $sts = $dbmysql->prepare($query); $sts->execute ();
|
||||||
$sts->execute ();
|
|
||||||
|
|
||||||
$queryDB = "";
|
$query = " INSERT INTO
|
||||||
share ($queryDB);
|
condes_logs
|
||||||
$sem1 = Thread::Semaphore->new(1);
|
SET
|
||||||
$sem2 = Thread::Semaphore->new(0);
|
id_utenti = 0,
|
||||||
|
id_condes_regole = $id,
|
||||||
|
stato = 0,
|
||||||
|
data = NOW()
|
||||||
|
";
|
||||||
|
$sts = $dbmysql->prepare($query); $sts->execute ();
|
||||||
|
|
||||||
my %proc;
|
$query = " UPDATE
|
||||||
# Generazione figli per DB
|
condes_regole
|
||||||
$proc{'database'} = threads->create(\&database);
|
SET
|
||||||
# Generazione figli per cattura pacchetti
|
stato = 0,
|
||||||
while ( $ref = $sts->fetchrow_hashref ) {
|
modifica = NOW()
|
||||||
my $id = $$ref{'id'};
|
WHERE
|
||||||
my $ip = $$ref{'ip'};
|
id_condes_macchine = $condes_macchine
|
||||||
my $port = $$ref{'port'};
|
AND
|
||||||
my $nome = $$ref{'nome'};
|
id_proxy_acl_list = $proxy_acl_list
|
||||||
$proc{$nome} = threads->create(\&conntrack, $id, $ip, $port);
|
";
|
||||||
print "Attivato processo $nome\n" if ($verbose);
|
$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;
|
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 (<CT>) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user