jerry 1 год назад
Родитель
Сommit
18e1d8105d
2 измененных файлов с 62 добавлено и 1 удалено
  1. 58 0
      common/libs/IpFilter.php
  2. 4 1
      frontendApi/config/main.php

+ 58 - 0
common/libs/IpFilter.php

@@ -0,0 +1,58 @@
+<?php
+
+namespace common\libs;
+
+use Yii;
+use yii\base\Component;
+use yii\web\BadRequestHttpException;
+use yii\web\Application;
+use MaxMind\Db\Reader;
+use MaxMind\Db\InvalidDatabaseException;
+use MaxMind\Db\AddressNotFoundException;
+class IpFilter extends Component
+{
+    public function init()
+    {
+        parent::init();
+        Yii::$app->on(Application::EVENT_BEFORE_REQUEST, [$this, 'checkIp']);
+    }
+
+    /**
+     * @throws BadRequestHttpException
+     */
+    public function checkIp()
+    {
+        $request = Yii::$app->getRequest();
+        $remoteAddr = $request->getUserIP(); // 获取用户 IP 地址
+
+        if (!self::remoteAddrCall($remoteAddr)) {
+            throw new BadRequestHttpException('非法 IP 地址');
+        }
+    }
+
+    /**
+     * @throws AddressNotFoundException
+     * @throws InvalidDatabaseException
+     */
+    public static function remoteAddrCall($remoteAddr): bool
+    {
+        // 是否有效的IP
+        if (!filter_var($remoteAddr, FILTER_VALIDATE_IP)) {
+            return false;
+        }
+
+        // 替换为 GeoLite2 数据库文件的实际路径
+        $dbPath = \Yii::getAlias('@common/runtime/geoLite//GeoLite2-Country.mmdb');
+        // 初始化 MaxMind 数据库读取器
+        $reader = new \GeoIp2\Database\Reader($dbPath);
+        // 查询 IP 地址的地理位置
+        $record = $reader->country($remoteAddr);
+        // 返回国家名称
+        $countryName = $record->country->name;
+        if (!in_array($countryName, ['China'])) {
+            return false;
+        }
+
+        return true;
+    }
+}

+ 4 - 1
frontendApi/config/main.php

@@ -8,7 +8,7 @@ $urlManagerRules = require __DIR__ . '/urlManagerRules.php';
 return [
     'id' => 'app-frontendApi',
     'basePath' => dirname(__DIR__),
-    'bootstrap' => ['log'],
+    'bootstrap' => ['log', 'ipFilter'],
     'modules' => [
         'v1' => [
             'basePath' => '@frontendApi/modules/v1',
@@ -16,6 +16,9 @@ return [
         ],
     ],
     'components' => [
+        'ipFilter' => [
+            'class' => 'common\libs\IpFilter',
+        ],
         'request' => [
             //'csrfParam' => '_csrf-frontendApi',
             //'cookieValidationKey' => '98bS8sqf3iRmSy24ZGDug2e36pLmj2wN',