Browse Source

增加配置项:允许同一IP记录多条测速结果

badapple9 4 years ago
parent
commit
e2abfacd09
3 changed files with 21 additions and 7 deletions
  1. 2 2
      backend/config.php
  2. 16 4
      backend/report.php
  3. 3 1
      index.html

+ 2 - 2
backend/config.php

@@ -11,6 +11,6 @@ const MAX_LOG_COUNT = 100;
 const IP_SERVICE = 'ip.sb';
 
 /**
- * todo 是否允许同一IP记录多次测速结果
+ * 允许同一IP记录多条测速结果
  */
-//const SAME_IP_MULTI_LOGS = false;
+const SAME_IP_MULTI_LOGS = false;

+ 16 - 4
backend/report.php

@@ -22,6 +22,7 @@ $store = \SleekDB\SleekDB::store('speedlogs', './',[
 ]);
 
 $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),
@@ -34,7 +35,11 @@ $reportData = [
 
 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);
@@ -42,7 +47,14 @@ if (is_array($oldLog) && empty($oldLog)) {
          $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);
+    }
 }

+ 3 - 1
index.html

@@ -13,6 +13,7 @@ var s=new Speedtest(); //create speedtest object
 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;
@@ -26,9 +27,10 @@ s.onupdate=function(data){ //callback to update data in UI
 	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) {