Browse Source

增加配置项:可选择使用的ip解析服务(如果ip.sb不可用,可配置切换为ipinfo.io)

Bad Apple!! 4 years ago
parent
commit
6c304d74c2
3 changed files with 47 additions and 18 deletions
  1. 11 1
      backend/config.php
  2. 33 16
      backend/getIP.php
  3. 3 1
      backend/report.php

+ 11 - 1
backend/config.php

@@ -3,4 +3,14 @@
 /**
  * 最多保存多少条测试记录
  */
-const MAX_LOG_COUNT = 100;
+const MAX_LOG_COUNT = 100;
+
+/**
+ * IP运营商解析服务:(1) ip.sb | (2) ipinfo.io (如果1解析ip异常,请切换成2)
+ */
+const IP_SERVICE = 'ip.sb';
+
+/**
+ * todo 是否允许同一IP记录多次测速结果
+ */
+//const SAME_IP_MULTI_LOGS = false;

+ 33 - 16
backend/getIP.php

@@ -5,6 +5,7 @@
  * Output from this script is a JSON string composed of 2 objects: a string called processedString which contains the combined IP, ISP, Contry and distance as it can be presented to the user; and an object called rawIspInfo which contains the raw data from ipinfo.io (will be empty if isp detection is disabled).
  * Client side, the output of this script can be treated as JSON or as regular text. If the output is regular text, it will be shown to the user as is.
  */
+require_once "./config.php";
 
 error_reporting(0);
 
@@ -98,10 +99,14 @@ function getIpInfoTokenString()
  *
  * @return array|null
  */
-function getIspInfo($ip)
+function getIspInfo($ip, $ipService)
 {
-//    $json = file_get_contents('https://ipinfo.io/'.$ip.'/json'.getIpInfoTokenString());
-    $json = file_get_contents('https://api.ip.sb/geoip/' . $ip);
+    $json = '';
+    if ($ipService == 'ip.sb') {
+        $json = file_get_contents('https://api.ip.sb/geoip/' . $ip);
+    } elseif ($ipService == 'ipinfo.io') {
+        $json = file_get_contents('https://ipinfo.io/'.$ip.'/json'.getIpInfoTokenString());
+    }
     if (!is_string($json)) {
         return null;
     }
@@ -119,21 +124,33 @@ function getIspInfo($ip)
  *
  * @return string
  */
-function getIsp($rawIspInfo)
+function getIsp($rawIspInfo, $ipService)
 {
-    if (
-        !is_array($rawIspInfo)
-        || !array_key_exists('organization', $rawIspInfo)
-        || !is_string($rawIspInfo['organization'])
-        || empty($rawIspInfo['organization'])
-    ) {
-        return 'Unknown';
+    if ($ipService == 'ip.sb') {
+        if (
+            !is_array($rawIspInfo)
+            || !array_key_exists('organization', $rawIspInfo)
+            || !is_string($rawIspInfo['organization'])
+            || empty($rawIspInfo['organization'])
+        ) {
+            return 'Unknown';
+        }
+        return $rawIspInfo['organization'];
+    } elseif ($ipService == 'ipinfo.io') {
+        if (
+            !is_array($rawIspInfo)
+            || !array_key_exists('org', $rawIspInfo)
+            || !is_string($rawIspInfo['org'])
+            || empty($rawIspInfo['org'])
+        ) {
+            return 'Unknown';
+        }
+        return preg_replace('/AS\\d+\\s/', '', $rawIspInfo['org']);
     }
-
-    // Remove AS##### from ISP name, if present
-    return $rawIspInfo['organization'];
+    return 'Unknown';
 }
 
+
 /**
  * @return string|null
  */
@@ -329,8 +346,8 @@ if (!isset($_GET['isp'])) {
     exit;
 }
 
-$rawIspInfo = getIspInfo($ip);
-$isp = getIsp($rawIspInfo);
+$rawIspInfo = getIspInfo($ip, IP_SERVICE);
+$isp = getIsp($rawIspInfo, IP_SERVICE);
 //$distance = getDistance($rawIspInfo);
 
 sendResponse($ip, $isp, $rawIspInfo);

+ 3 - 1
backend/report.php

@@ -7,9 +7,11 @@ function maskLastSegment($ip) {
     $ipaddr = inet_pton($ip);
     if (strlen($ipaddr) == 4) {
         $ipaddr[3] = chr(0);
-    } else {
+    } elseif (strlen($ipaddr) == 16) {
         $ipaddr[14] = chr(0);
         $ipaddr[15] = chr(0);
+    } else {
+        return "";
     }
     return rtrim(inet_ntop($ipaddr),"0")."*";
 }