diff --git a/application/http/middleware/Initialize.php b/application/http/middleware/Initialize.php index 94913026..85c832fb 100644 --- a/application/http/middleware/Initialize.php +++ b/application/http/middleware/Initialize.php @@ -24,11 +24,47 @@ class Initialize $banIp = $this->getConfig('ban_ip'); if ($banIp) { $ips = explode(',', str_replace(',', ',', $banIp)); - if (in_array($request->ip(), $ips)) { + if ($this->banIp($request->ip(), $ips)) { throw new HttpResponseException(Response::code(403)); } } return $next($request); } + + /** + * 拦截某个IP, 支持通配符 + * + * @param string $ip + * @param array $ips + * @return bool + */ + private function banIp(string $ip, array $ips) + { + $parts = explode('.', $ip); // 分段当前 IP + $ban = false; + foreach ($ips as $item) { + // 如果直接匹配到 + if ($item === $ip) { + $ban = true; + continue; + } + + if (strpos($item, '*') !== false) { + $array = explode('.', $item); + $check = true; + for ($i = 0; $i < count($array); $i++) { + if ($array[$i] !== '*' && $array[$i] !== $parts[$i]) { + $check = false; + break; + } + } + if ($check) { + $ban = true; + break; + } + } + } + return $ban; + } } diff --git a/extend/Upgrade.php b/extend/Upgrade.php index 9bf85480..ff1994c1 100644 --- a/extend/Upgrade.php +++ b/extend/Upgrade.php @@ -52,6 +52,10 @@ class Upgrade throw new \Exception('无法继续执行, 请确保 ZipArchive 正确安装'); } + set_time_limit(0); + ini_set('max_execution_time', 0); + ini_set('memory_limit', '256M'); + ob_clean(); } @@ -64,10 +68,6 @@ class Upgrade */ public function download($url) { - set_time_limit(0); - ini_set('max_execution_time', 0); - ini_set('memory_limit', '256M'); - $ip = $this->getRandIp(); $headers = [ 'Accept-Encoding: gzip, deflate',