|
|
@@ -21,15 +21,13 @@ class IpFilter
|
|
|
$request = Yii::$app->request;
|
|
|
$getParams = Yii::$app->request->get();
|
|
|
$postParams = Yii::$app->request->post();
|
|
|
- $remoteAddr = $_SERVER['REMOTE_ADDR']; // 获取用户 IP 地址
|
|
|
+ $ip = $_SERVER['REMOTE_ADDR']; // 获取用户 IP 地址
|
|
|
|
|
|
//如果IP不在指定范围内
|
|
|
- if (!self::checkIpInAllowRange($remoteAddr)) {
|
|
|
+ if (!self::checkIpInAllowRange($ip)) {
|
|
|
$logPreix = $isLogin ? 'nc_ip_filter_login' : 'nc_ip_filter_other';
|
|
|
- $getLog = sprintf('%s_%s: remote_ip%s: url(%s): param%s', $source, $logPreix, $remoteAddr, $request->getAbsoluteUrl(), (is_array($getParams) ? json_encode($getParams) : $getParams));
|
|
|
- $postLog = sprintf('%s_%s: remote_ip%s: url(%s): param%s', $source, $logPreix, $remoteAddr, $request->getAbsoluteUrl(), (is_array($postParams) ? json_encode($postParams) : $postParams));
|
|
|
- //$logPreix . ':remote_ip' . $remoteAddr . ':' . $request->getAbsoluteUrl() . ':' . (is_array($getParams) ? json_encode($getParams) : $getParams);
|
|
|
- //$logPreix . ':remote_ip' . $remoteAddr . ':' . $request->getAbsoluteUrl() . ':' . (is_array($postParams) ? json_encode($postParams) : $postParams);
|
|
|
+ $getLog = sprintf('%s_%s: remote_ip%s: url(%s): param%s', $source, $logPreix, $ip, $request->getAbsoluteUrl(), (is_array($getParams) ? json_encode($getParams) : $getParams));
|
|
|
+ $postLog = sprintf('%s_%s: remote_ip%s: url(%s): param%s', $source, $logPreix, $ip, $request->getAbsoluteUrl(), (is_array($postParams) ? json_encode($postParams) : $postParams));
|
|
|
LoggerTool::error($getLog);
|
|
|
LoggerTool::error($postLog);
|
|
|
return false;
|
|
|
@@ -43,19 +41,22 @@ class IpFilter
|
|
|
* @throws AddressNotFoundException
|
|
|
* @throws InvalidDatabaseException
|
|
|
*/
|
|
|
- public static function checkIpInAllowRange($remoteAddr): bool
|
|
|
+ public static function checkIpInAllowRange($ip): bool
|
|
|
{
|
|
|
// 是否有效的IP
|
|
|
- if (!filter_var($remoteAddr, FILTER_VALIDATE_IP)) {
|
|
|
+ if (!filter_var($ip, FILTER_VALIDATE_IP)) {
|
|
|
return false;
|
|
|
}
|
|
|
-
|
|
|
+ // 开放跳板机IP,后台快速登录使用
|
|
|
+ if (in_array($ip, ['8.219.172.88'])) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
// 替换为 GeoLite2 数据库文件的实际路径
|
|
|
$dbPath = \Yii::getAlias('@common/runtime/geoLite//GeoLite2-Country.mmdb');
|
|
|
// 初始化 MaxMind 数据库读取器
|
|
|
$reader = new \GeoIp2\Database\Reader($dbPath);
|
|
|
// 查询 IP 地址的地理位置
|
|
|
- $record = $reader->country($remoteAddr);
|
|
|
+ $record = $reader->country($ip);
|
|
|
// 返回国家名称
|
|
|
$countryName = $record->country->name;
|
|
|
if (!in_array($countryName, ['China', 'Malaysia'])) {
|