forked from pymumu/smartdns
-
Notifications
You must be signed in to change notification settings - Fork 11
a simple php script to display audit log on a webpage
PikuZheng edited this page Mar 28, 2024
·
3 revisions
<?php
require_once ('china-ip-ranges-ipv4.php');
$domains=array();
$log_file = fopen('smartdns/log/audit.log', 'r') or die('Unable to open file!');
while(!feof($log_file)){
$line=fgets($log_file);
preg_match("/query (.*?),/", $line, $domain);
preg_match("/type (\d+),/", $line, $type);
preg_match("/time (\d+ms),/", $line, $time);
preg_match("/speed: (-?\d+\.\d+ms),/", $line, $speed);
preg_match("/group (.*?),/", $line, $group);
preg_match("/result (.*)/", $line, $result);
$domains[$domain[1]] = array(
'type' => $type[1],
'time' => $time[1],
'speed' => $speed[1],
'group' => $group[1],
'result' => $result[1]
);
}
$html_table = "<table border='1'>
<tr>
<th>Query</th>
<th>Type</th>
<th>Time</th>
<th>Speed</th>
<th>Group</th>
<th>Result</th>
</tr>";
foreach ($domains as $query => $info) {
$html_table .= "<tr>
<td>{$query}</td>
<td>{$info['type']}</td>
<td>{$info['time']}</td>
<td>{$info['speed']}</td>
<td>{$info['group']}</td>
<td>".ischinaip($info['result'])."</td>
</tr>";
}
$html_table .= "</table>";
echo $html_table;
function ischinaip($target){
global $ip_range;
$ip=sprintf('%u',ip2long(explode(",", $target)[0]));
$low=0;
$high=count($ip_range)-1;
while ($low<=$high){
$mid=floor(($low+$high)/2);
if($ip>=$ip_range[$mid]['left'] && $ip<=$ip_range[$mid]['right']){
return '<span style="color:green">'.$target.'</span>';
}
if($ip<$ip_range[$mid]['left']){
$high=$mid-1;
}
if($ip>$ip_range[$mid]['left']){
$low=$mid+1;
}
}
return '<span style="color:red">'.$target.'</span>';
}
?>
其中 china-ip-ranges-ipv4.php 为记录了国内 ip (转为数值后)的有序数组,形如
$ip_range=array(
0 =>array("left"=> 16844800, "right"=> 16845055),
1 =>array("left"=> 16909312, "right"=> 16909567),
2 =>array("left"=> 17301760, "right"=> 17302015),
3 =>array("left"=> 17303552, "right"=> 17303807),
4 =>array("left"=> 17563648, "right"=> 17825791),
5 =>array("left"=> 18350080, "right"=> 18874367),