Skip to content

Commit

Permalink
增加配置项:允许同一IP记录多条测速结果
Browse files Browse the repository at this point in the history
  • Loading branch information
BadApple9 committed Dec 9, 2020
1 parent 26f9906 commit ef89531
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
4 changes: 2 additions & 2 deletions backend/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
const IP_SERVICE = 'ip.sb';

/**
* todo 是否允许同一IP记录多次测速结果
* 允许同一IP记录多条测速结果
*/
//const SAME_IP_MULTI_LOGS = false;
const SAME_IP_MULTI_LOGS = false;
20 changes: 16 additions & 4 deletions backend/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function maskLastSegment($ip) {
]);

$reportData = [
"key" => sha1(filter_var($_POST['key'], FILTER_SANITIZE_STRING)),
"ip" => maskLastSegment(filter_var($_POST['ip'], FILTER_SANITIZE_STRING)),
"isp" => filter_var($_POST['isp'], FILTER_SANITIZE_STRING),
"addr" => filter_var($_POST['addr'], FILTER_SANITIZE_STRING),
Expand All @@ -34,15 +35,26 @@ function maskLastSegment($ip) {

if (empty($reportData['ip'])) exit;

$oldLog = $store->where('ip', '=', $reportData['ip'])->fetch();
if (SAME_IP_MULTI_LOGS) {
$oldLog = $store->where('key', '=', $reportData['key'])->fetch();
} else {
$oldLog = $store->where('ip', '=', $reportData['ip'])->orderBy( 'desc', '_id' )->fetch();
}

if (is_array($oldLog) && empty($oldLog)) {
$results = $store->insert($reportData);
if ($results['_id'] > MAX_LOG_COUNT) {
$store->where('_id', '=', $results['_id'] - MAX_LOG_COUNT)->delete();
}
} else {
$ip = $reportData['ip'];
unset($reportData['ip']);
$store->where('ip', '=', $ip)->update($reportData);
$id = $oldLog[0]['_id'];
if (SAME_IP_MULTI_LOGS) {
$key = $reportData['key'];
unset($reportData['key']);
$store->where('_id', '=', $id)->update($reportData);
} else {
$ip = $reportData['ip'];
unset($reportData['ip']);
$store->where('_id', '=', $id)->update($reportData);
}
}
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
var xhr=new XMLHttpRequest();
var url_report='./backend/report.php';
var milestone=0;
var key_prefix=Date.parse(new Date());
s.onupdate=function(data){ //callback to update data in UI
I("ip").textContent=data.clientIp;
I("dlText").textContent=(data.testState==1&&data.dlStatus==0)?"...":data.dlStatus;
Expand All @@ -26,9 +27,10 @@
var isp = ipIspArr[1];
var addr = ipIspArr[2] === undefined? '' :ipIspArr[2];
var progress = Math.floor(100*prog);
var key = key_prefix + "_" + ip;
if (progress > 20 && (progress % 10 == 0) && progress != milestone) {
console.log(progress);
var params = 'ip='+ip+'&isp='+isp+'&addr='+addr+'&dspeed='+I("dlText").textContent+'&uspeed='+I("ulText").textContent+'&ping='+I("pingText").textContent
var params = 'key='+key+'&ip='+ip+'&isp='+isp+'&addr='+addr+'&dspeed='+I("dlText").textContent+'&uspeed='+I("ulText").textContent+'&ping='+I("pingText").textContent
+'&jitter='+I("jitText").textContent;
xhr.timeout = 3000;
xhr.ontimeout = function (e) {
Expand Down

0 comments on commit ef89531

Please sign in to comment.