Added regexps check before adding an inst to “Instances” and “Peers” tables

This commit is contained in:
pezcurrel 2023-12-27 21:55:33 +01:00
parent 738fa8c864
commit 943c65b2ba

View file

@ -289,38 +289,42 @@ function crawl(&$list,$id) {
} }
} }
} }
$responded=false; if (!ckexarr($inst)) {
lecho(1,'trying to load «'.$inst.s instance info...'.N); $responded=false;
$instinfo=gurl('https://'.$inst.'/api/v1/instance/peers',$opts['timeout'],$opts['curltimeout']); lecho(1,'trying to load «'.$inst.s instance info...'.N);
if ($instinfo['cont']===false) { $instinfo=gurl('https://'.$inst.'/api/v1/instance',$opts['timeout'],$opts['curltimeout']);
lecho(2,'could not load «'.$inst.s instance info: '.$instinfo['emsg'].'.'.N); if ($instinfo['cont']===false) {
} else { lecho(2,'could not load «'.$inst.s instance info: '.$instinfo['emsg'].'.'.N);
$instinfo=@json_decode($instinfo['cont'],true); } else {
if (is_array($instinfo)) $instinfo=@json_decode($instinfo['cont'],true);
$responded=true; if (is_array($instinfo))
else $responded=true;
lecho(2,'loading «'.$inst.s instance info, got bad JSON.'.N); else
} lecho(2,'loading «'.$inst.s instance info, got bad JSON.'.N);
if ($responded && !in_array($inst,$insts)) { }
lecho(1,'instance «'.$inst.'» responded :-)'.N); if ($responded && !in_array($inst,$insts)) {
$insts[]=$inst; lecho(1,'instance «'.$inst.'» responded :-)'.N);
$res=myq($link,'SELECT ID FROM Instances WHERE URI=\''.myesc($link,$inst).'\''); $insts[]=$inst;
if (mysqli_num_rows($res)==0) { $res=myq($link,'SELECT ID FROM Instances WHERE URI=\''.myesc($link,$inst).'\'');
lecho(1,'instance «'.$inst.'» is new :-)'.N); if (mysqli_num_rows($res)==0) {
myq($link,'INSERT INTO Instances SET URI=\''.myesc($link,$inst).'\', InsertTS='.$now); lecho(1,'instance «'.$inst.'» is new :-)'.N);
$newc++; myq($link,'INSERT INTO Instances SET URI=\''.myesc($link,$inst).'\', InsertTS='.$now);
$newc++;
}
}
$res=myq($link,'SELECT * FROM Peers WHERE Hostname=\''.myesc($link,$inst).'\'');
$nrows=mysqli_num_rows($res);
if ($nrows>0) {
if ($nrows>1) lecho(2,'«'.$inst.'» has '.$nrows.' records in “Peers” table! :-('.N);
$row=mysqli_fetch_assoc($res);
if ($responded) myq($link,'UPDATE Peers SET LastOkCheckTS='.$now.' WHERE ID='.$row['ID']);
} else {
$query='INSERT INTO Peers SET Hostname=\''.myesc($link,$inst).'\', FirstCheckTS='.$now;
if ($responded) $query.=', LastOkCheckTS='.$now;
myq($link,$query);
} }
}
$res=myq($link,'SELECT * FROM Peers WHERE Hostname=\''.myesc($link,$inst).'\'');
$nrows=mysqli_num_rows($res);
if ($nrows>0) {
if ($nrows>1) lecho(2,'«'.$inst.'» has '.$nrows.' records in “Peers” table! :-('.N);
$row=mysqli_fetch_assoc($res);
if ($responded) myq($link,'UPDATE Peers SET LastOkCheckTS='.$now.' WHERE ID='.$row['ID']);
} else { } else {
$query='INSERT INTO Peers SET Hostname=\''.myesc($link,$inst).'\', FirstCheckTS='.$now; lecho(2,'ignoring instance «'.$inst.'» because it matches an exclusion regexp: wont add it to «Instances» and «Peers» tables.'.N);
if ($responded) $query.=', LastOkCheckTS='.$now;
myq($link,$query);
} }
} }
unset($list); unset($list);