Browse Source

fix #757 优化采集时收费域名的逻辑

magicblack 3 years ago
parent
commit
12bfbae69a
2 changed files with 31 additions and 65 deletions
  1. 26 65
      application/api/controller/Provide.php
  2. 5 0
      application/common.php

+ 26 - 65
application/api/controller/Provide.php

@@ -37,19 +37,7 @@ class Provide extends Base
             }
             else {
                 $auth = $GLOBALS['config']['api']['vod']['auth'];
-                $auths = array();
-                if(!empty($auth)){
-                    $auths = explode('#',$auth);
-                    foreach($auths as $k=>$v){
-                        $auths[$k] = gethostbyname(trim($v));
-                    }
-                }
-                if($h != 'localhost' && $h != '127.0.0.1') {
-                    if(!in_array($h, $auths)){
-                        echo lang('api/auth_err');
-                        exit;
-                    }
-                }
+                $this->checkDomainAuth($auth);
             }
         }
 
@@ -296,19 +284,7 @@ class Provide extends Base
             }
             else {
                 $auth = $GLOBALS['config']['api']['art']['auth'];
-                $auths = array();
-                if(!empty($auth)){
-                    $auths = explode('#',$auth);
-                    foreach($auths as $k=>$v){
-                        $auths[$k] = gethostbyname(trim($v));
-                    }
-                }
-                if($h != 'localhost' && $h != '127.0.0.1') {
-                    if(!in_array($h, $auths)){
-                        echo lang('api/auth_err');
-                        exit;
-                    }
-                }
+                $this->checkDomainAuth($auth);
             }
         }
 
@@ -420,19 +396,7 @@ class Provide extends Base
             }
             else {
                 $auth = $GLOBALS['config']['api']['actor']['auth'];
-                $auths = array();
-                if(!empty($auth)){
-                    $auths = explode('#',$auth);
-                    foreach($auths as $k=>$v){
-                        $auths[$k] = gethostbyname(trim($v));
-                    }
-                }
-                if($h != 'localhost' && $h != '127.0.0.1') {
-                    if(!in_array($h, $auths)){
-                        echo lang('api/auth_err');
-                        exit;
-                    }
-                }
+                $this->checkDomainAuth($auth);
             }
         }
 
@@ -544,19 +508,7 @@ class Provide extends Base
             }
             else {
                 $auth = $GLOBALS['config']['api']['role']['auth'];
-                $auths = array();
-                if(!empty($auth)){
-                    $auths = explode('#',$auth);
-                    foreach($auths as $k=>$v){
-                        $auths[$k] = gethostbyname(trim($v));
-                    }
-                }
-                if($h != 'localhost' && $h != '127.0.0.1') {
-                    if(!in_array($h, $auths)){
-                        echo lang('api/auth_err');
-                        exit;
-                    }
-                }
+                $this->checkDomainAuth($auth);
             }
         }
 
@@ -656,19 +608,7 @@ class Provide extends Base
             }
             else {
                 $auth = $GLOBALS['config']['api']['website']['auth'];
-                $auths = array();
-                if(!empty($auth)){
-                    $auths = explode('#',$auth);
-                    foreach($auths as $k=>$v){
-                        $auths[$k] = gethostbyname(trim($v));
-                    }
-                }
-                if($h != 'localhost' && $h != '127.0.0.1') {
-                    if(!in_array($h, $auths)){
-                        echo lang('api/auth_err');
-                        exit;
-                    }
-                }
+                $this->checkDomainAuth($auth);
             }
         }
 
@@ -770,4 +710,25 @@ class Provide extends Base
     {
 
     }
+
+    private function checkDomainAuth($auth)
+    {
+        $ip = mac_get_client_ip();
+        $auth_list = ['127.0.0.1'];
+        if (!empty($auth)) {
+            foreach (explode('#', $auth) as $domain) {
+                $domain = trim($domain);
+                $auth_list[] = $domain;
+                if (!mac_string_is_ip($domain)) {
+                    $auth_list[] = gethostbyname($domain);
+                }
+            }
+            $auth_list = array_unique($auth_list);
+            $auth_list = array_filter($auth_list);
+        }
+        if (!in_array($ip, $auth_list)) {
+            echo lang('api/auth_err');
+            exit;
+        }
+    }
 }

+ 5 - 0
application/common.php

@@ -27,6 +27,11 @@ function slog($logs)
 //$log = $_SERVER['PHP_SELF'] . '---get:' .$getData .'---post:' . $postData .'---'. json_encode($_POST).'---cookie:' . $cookieData ;
 //slog($log);
 
+// 是否IP
+function mac_string_is_ip($string) {
+    return preg_match('/^(\d{1,3}\.){3}\d{1,3}(:\d{1,5})?$/', $string) === 1;
+}
+
 // 应用公共文件
 function mac_return($msg,$code=1,$data=''){
     if(is_array($msg)){