Added “checkspam” action

This commit is contained in:
pezcurrel 2023-12-27 21:53:44 +01:00
parent e94e286b01
commit 61a621e4d2

View file

@ -21,6 +21,8 @@ require __DIR__.'/../lib/delinstbyid.php';
$levs=['Debug', 'Info', 'Warning', 'Error'];
$opts=array(
'checkspam'=>false,
'minent'=>10,
'revive'=>false,
'shuffle'=>false,
'updstats'=>false,
@ -37,6 +39,10 @@ $help='mustool.php
SYNOPSIS
mustool.php [options] <action> [parameters] ...
ACTIONS
checkspam
Tries to detect spam instances, i.e. those with the same TLD, same second
level domain, variable higher domains, both in «Instances» and «Peers»
tables.
deleteinstswhere <condition[s]>
First, return a list of Instances records matching “condition”, then let
you choose whether you want to delete them and all records referencing them
@ -75,6 +81,9 @@ for ($i=1; $i<$argc; $i++) {
$dosome=true;
$opts['deleteinstswhere']=true;
$opts['deleteinstswhereconds']=$argv[$i];
} elseif ($argv[$i]=='checkspam') {
$dosome=true;
$opts['checkspam']=true;
} elseif ($argv[$i]=='shuffle') {
$dosome=true;
$opts['shuffle']=true;
@ -111,6 +120,58 @@ catch (Exception $error) { mexit('could not set «utf8mb4» charset for MySQL: '
// for php versions < 8
if ($res===false) mexit('could not set MySQL charset: '.mysqli_error($link).' ['.mysqli_errno($link).'].'.N,1,true);
if ($opts['checkspam']) {
$res=myq($link,'SELECT ID, URI FROM Instances');
checkspam($res,'ID','URI','Instances');
$res=myq($link,'SELECT ID, Hostname FROM Peers');
checkspam($res,'ID','Hostname','Peers');
}
function checkspam(&$res,$idcol,$domcol,$tabnam) {
global $opts;
$buf=[];
while ($row=mysqli_fetch_assoc($res)) $buf[]=$row;
unset($res);
$cbuf=count($buf);
if ($cbuf>0) {
$doms=[];
foreach ($buf as $row)
if (preg_match('#[^.]+\.[^.]+$#',$row[$domcol],$matches)===1)
$doms[$matches[0]][]=['dom'=>$row[$domcol], 'id'=>$row[$idcol]];
/* echo('Do you really want to delete those '.$cbuf.' record(s)? Enter «YES» to do it, anything else to not do it: ');
$inp=rtrim(fgets(STDIN));
if ($inp=='YES') {
$i=0;
foreach ($buf as $row) {
$i++;
eecho('deleting Instances record with ID = '.$row['ID'].' and URI = «'.$row['URI'].'», and all references to it ('.$i.'/'.$cbuf.', '.round(100/$cbuf*$i,2).'%)'.N,1);
$res=delinstbyid($link,$row['ID'],'eecho',N);
if (!$res) mexit('error trying to delete Instances record with ID='.$row['ID'].'; see the log above for more info.'.N,2);
}
}*/
} else {
eecho('no '.$tabnam.' records found.'.N,2);
}
uasort($doms,'cmp');
foreach ($doms as $key=>$arr) {
$carr=count($arr);
if ($carr>$opts['minent']) {
echo $tabnam.': '.$key.': '.$carr.' entries'.N;
foreach ($arr as $entry)
echo ' '.$domcol.': '.$entry['dom'].'; '.$idcol.': '.$entry['id'].N;
echo N;
}
}
}
function cmp($a,$b) {
$a=count($a);
$b=count($b);
if ($a==$b)
return 0;
return ($a<$b) ? 1 : -1;// reverse :-)
}
if ($opts['deleteinstswhere']) {
$res=myq($link,'SELECT ID, URI FROM Instances WHERE '.$opts['deleteinstswhereconds']);
$buf=[];