modulo condes

This commit is contained in:
cmaffio
2016-05-27 17:20:28 +02:00
parent 691a64e13a
commit 75adb3e6a1
5 changed files with 516 additions and 45 deletions

View File

@@ -25,23 +25,16 @@ CREATE TABLE `condes_regole` (
`permanente` BOOLEAN NOT NULL ,
`data` DATETIME NOT NULL ,
`attivo` BOOLEAN NOT NULL ,
`stato` int(11) DEFAULT 0 ,
`modifica` DATETIME NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
CREATE TABLE `condes_stato` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`id_condes_regole` bigint(20) NOT NULL,
`id_proxy_acl_ip` bigint(20) NOT NULL,
`permanente` tinyint(1) NOT NULL,
`data` datetime NOT NULL,
`attivo` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `condes_logs` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`id_utenti` bigint(20) NOT NULL,
`id_condes_regole` bigint(20) NOT NULL,
`stato` int(11) NOT NULL,
`data` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

162
condes/condes_logs.php Executable file
View File

@@ -0,0 +1,162 @@
<?php
include_once ("../core/config.php");
$UTENTE = login();
$DIRITTI = diritti('Admin');
view_top();
if (isset($_POST['tempo'])) {
$tempo = $_POST['tempo'];
} else {
$tempo = "DAY";
}
if (isset($_POST['idutente']) && $_POST['idutente'] > 0) {
$idutente = $_POST['idutente'];
$queryutente = "AND utenti.id = $idutente";
} else {
$idutente = 0;
$queryutente = "";
}
if (isset($_POST['idip']) && $_POST['idip'] > 0) {
$idip = $_POST['idip'];
$queryip = "AND proxy_pool.id = $idip";
} else {
$idip = 0;
$queryip = "";
}
$stato = array ("Disattivo", "Attivo", "Permanente");
$query_user = " SELECT
utenti.id,
utenti.utente
FROM
utenti
JOIN
permessi
ON
permessi.id_utenti = utenti.id
WHERE
permessi.id_moduli = ".$MODULO['id']."
ORDER BY
utente
";
$res_user = mysql_query( $query_user, $DB_ID );
$query_ip = " SELECT
condes_macchine.id_proxy_pool AS id,
proxy_pool.nome,
proxy_pool.ip
FROM
condes_macchine
JOIN
proxy_pool
ON
condes_macchine.id_proxy_pool = proxy_pool.id
ORDER BY
-proxy_pool.nome DESC,
INET_ATON(proxy_pool.ip) ASC
";
$res_ip = mysql_query( $query_ip, $DB_ID );
?>
<form name="" method="post">
<?php $tabella = new html (0,"90%", array (3,15,2,10,2,20,2,10,2,10,0)); ?>
<tr>
<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
<td class="spazioh">
<select name="tempo" onchange="this.form.submit()">
<option value="HOUR" <?php if ($tempo == "HOUR") {print "selected=\"selected\""; } ?>>Ultima Ora</option>
<option value="DAY" <?php if ($tempo == "DAY") {print "selected=\"selected\""; } ?>>Ultimo Giorno</option>
<option value="WEEK" <?php if ($tempo == "WEEK") {print "selected=\"selected\""; } ?>>Ultima Settimana</option>
<option value="MONTH" <?php if ($tempo == "MONTH") {print "selected=\"selected\""; } ?>>Ultimo Mese</option>
<option value="YEAR" <?php if ($tempo == "YEAR") {print "selected=\"selected\""; } ?>>Ultimo Anno</option>
</select>
</td>
<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
<td class="spazioh">
<select name="idutente" onchange="this.form.submit()">
<option value=0 <?php if ($idutente == 0) {print "selected=\"selected\""; } ?>>----</option>
<?php while ($dato = mysql_fetch_array ( $res_user )) { ?>
<option value="<?php print $dato['id'] ?>" <?php if ($idutente == $dato['id']) {print "selected=\"selected\""; } ?>><?php print $dato['utente'] ?></option>
<?php } ?>
</select>
</td>
<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
<td class="spazioh">
<select name="idip" onchange="this.form.submit()">
<option value=0 <?php if ($idip == 0) {print "selected=\"selected\""; } ?>>----</option>
<?php while ($dato = mysql_fetch_array ( $res_ip )) { ?>
<option value="<?php print $dato['id'] ?>" <?php if ($idip == $dato['id']) {print "selected=\"selected\""; } ?>><?php print $dato['ip']; if ($dato['nome'] != "") print " (".$dato['nome'].")" ?></option>
<?php } ?>
</select>
</td>
<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
</tr>
<?php
$tabella->riga();
$tabella->intestazione (array("", "Data", "", "Utente", "", "Macchina", "", "ACL", "", "Stato", ""));
$query = " SELECT
condes_logs.id,
condes_logs.stato,
DATE_FORMAT(condes_logs.data, '%d.%m.%Y %H:%i:%s') AS data,
utenti.utente,
proxy_pool.nome AS nome_pool,
proxy_pool.ip,
proxy_acl_list.nome
FROM
condes_logs
JOIN
utenti
ON
condes_logs.id_utenti = utenti.id
$queryutente
JOIN
condes_regole
ON
condes_logs.id_condes_regole = condes_regole.id
JOIN
proxy_acl_list
ON
condes_regole.id_proxy_acl_list = proxy_acl_list.id
JOIN
condes_macchine
ON
condes_regole.id_condes_macchine = condes_macchine.id
JOIN
proxy_pool
ON
condes_macchine.id_proxy_pool = proxy_pool.id
$queryip
WHERE
condes_logs.data >= DATE_SUB(NOW(), INTERVAL 1 $tempo)
ORDER BY
condes_logs.data DESC
";
$res = mysql_query( $query, $DB_ID );
while ($dato = mysql_fetch_array ( $res )) {
?>
<tr>
<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
<td class="colip"><?php print $dato['data'] ?></td>
<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
<td class="colip"><?php print $dato['utente'] ?></td>
<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
<td class="colip"><?php print $dato['ip']; if ($dato['nome_pool'] != "") print " (".$dato['nome_pool'].")" ?></td>
<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
<td class="colip"><?php print $dato['nome'] ?></td>
<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
<td class="colip"><?php print $stato[$dato['stato']] ?></td>
<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
</td>
</tr>
<?php
}
$tabella->close();
?>
</form>
<?php view_footer(); ?>

View File

@@ -7,50 +7,70 @@ view_top();
if (isset($_POST['utente'])) {
$utente = $_POST['utente'];
print "-- $utente --<br>";
$old = $new = array();
foreach ($_POST as $id => $valore) {
if ($id == "utente") continue;
print "---- $id => $valore<br>";
if (preg_match ("/^new-(\d+)/", $id, $ris)) $new[$ris[1]] = $valore;
if (preg_match ("/^old-(\d+)/", $id, $ris)) $old[$ris[1]] = $valore;
}
foreach ($new as $id => $valore) {
if ($new[$id] == $old[$id]) continue;
if ($valore >1)
$stato = 1;
else
$stato = $valore;
$query = " SELECT
proxy_acl_ip.id
condes_regole.id_condes_macchine,
condes_regole.id_proxy_acl_list,
condes_macchine.id_proxy_pool
FROM
condes_regole
JOIN
condes_macchine
ON
condes_regole.id_condes_macchine = condes_macchine.id
JOIN
proxy_acl_ip
ON
proxy_acl_ip.idacllist = condes_regole.id_proxy_acl_list
AND
proxy_acl_ip.idpool = condes_macchine.id_proxy_pool
condes_regole.id_condes_macchine = condes_macchine.id
WHERE
condes_regole.id = $id
";
$res = mysql_query( $query, $DB_ID );
$dato = mysql_fetch_array ( $res );
print_r ($dato);
print "<br>";
$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'];
$res = mysql_query( $query, $DB_ID );
$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()
";
$res = mysql_query( $query, $DB_ID );
$query = " INSERT INTO
condes_logs
SET
id_utenti = ".$UTENTE['id'].",
id_condes_regole = $id,
stato = $valore,
data = NOW()
";
$res = mysql_query( $query, $DB_ID );
}
$query = " INSERT INTO
condes_regole
SET
id_utenti = $id_utenti,
id_condes_macchine = $id_macchine,
id_proxy_acl_list = $id_acl,
permanente = $perm,
data = NOW(),
attivo = 1
";
// $res = mysql_query( $query, $DB_ID );
}
$tabella = new html (0,"90%",array(15,2,20,2,15,2,5,0));
@@ -67,6 +87,7 @@ $tabella->riga ();
$query = " SELECT
condes_regole.id,
condes_regole.stato,
proxy_pool.nome,
proxy_pool.ip,
proxy_acl_list.nome AS acl,
@@ -103,11 +124,12 @@ while ($dato = mysql_fetch_array ( $res )) {
?>
<tr>
<td rowspan=2 class="textl">
<select name="<?php print $dato['id'] ?>">
<option value="0">Disattivo</option>
<option value="1">Attivo</option>
<input type="hidden" name="old-<?php print $dato['id'] ?>" value="<?php print $dato['stato'] ?>">
<select name="new-<?php print $dato['id'] ?>">
<option value="0" <?php if ($dato['stato'] == 0 ) print "selected" ?>>Disattivo</option>
<option value="1" <?php if ($dato['stato'] == 1 ) print "selected" ?>>Attivo</option>
<?php if ($dato['permanente']) { ?>
<option value="2">Permanente</option>
<option value="2" <?php if ($dato['stato'] == 2 ) print "selected" ?>>Permanente</option>
<?php } ?>
</select>
</td>
@@ -118,7 +140,6 @@ while ($dato = mysql_fetch_array ( $res )) {
<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
<td class="radio">Perm</td>
<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
</td>
</tr>
<tr>
<td class="textl"><?php print $dato['ip']; if ($dato['nome'] != "") print "(".$dato['nome'].")" ?></td>

View File

@@ -0,0 +1,6 @@
our $DBhost = '127.0.0.1';
our $DBname = 'pannello_vbc';
our $DBuser = 'pannello_proxy';
our $DBpass = 'CuQlM1lfF4VZDCIP';
return 1;

289
condes/script/condes.pl Executable file
View File

@@ -0,0 +1,289 @@
#!/usr/bin/perl
$|=1;
use DBI;
use FindBin qw($Bin);
use threads;
use threads::shared;
use Thread::Semaphore;
my $param = shift @ARGV;
our $verbose = 0;
if ($param eq '-v') {
$verbose = 1;
} elsif ($param eq '-vv') {
$verbose = 2;
}
require "$Bin/condes.conf";
my $dbmysql = DBI->connect("DBI:mysql:;host=$DBhost", $DBuser, $DBpass) or die ($DBI::errstr);
$sts = $dbmysql->prepare("use $DBname");
$sts->execute ();
$query = " SELECT
condes_regole.id_condes_macchine,
condes_regole.id_proxy_acl_list,
condes_macchine.id_proxy_pool
FROM
condes_regole
JOIN
condes_macchine
ON
condes_regole.id_condes_macchine = condes_macchine.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'};
}
$query = " SELECT
id,
ip,
nome,
port
FROM
proxy_conn
WHERE
attivo = 1
";
$sts = $dbmysql->prepare($query);
$sts->execute ();
$queryDB = "";
share ($queryDB);
$sem1 = Thread::Semaphore->new(1);
$sem2 = Thread::Semaphore->new(0);
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);
}
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 (<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;
}