Browse Source

🔧 Reduce IP Error reports

BrettonYe 2 years ago
parent
commit
68fbba3704

+ 1 - 1
app/Exceptions/Handler.php

@@ -20,7 +20,7 @@ use Throwable;
 class Handler extends ExceptionHandler
 {
     protected $dontReport = [
-        //        HttpException::class,
+        ConnectionException::class,
         ValidationException::class,
     ];
 

+ 1 - 1
app/Http/Controllers/Admin/UserController.php

@@ -94,7 +94,7 @@ class UserController extends Controller
         Arr::forget($data, 'uuid');
         $data['transfer_enable'] *= GB;
         $data['expired_at'] = $data['expired_at'] ?? date('Y-m-d', strtotime('next year'));
-        $data['remark'] = str_replace(['atob', 'eval'], '', $data['remark']);
+        $data['remark'] = str_replace(['atob', 'eval'], '', $data['remark'] ?? '');
         $data['reg_ip'] = IP::getClientIp();
         $data['reset_time'] = $data['reset_time'] > date('Y-m-d') ? $data['reset_time'] : null;
         $user = User::create($data);

+ 3 - 3
app/Http/Middleware/isForbidden.php

@@ -33,11 +33,11 @@ class isForbidden
         }
 
         $ip = IP::getClientIP();
-        if ($ip !== '::1') {
-            $ipLocation = IP::getIPInfo($ip);
+        $ipLocation = IP::getIPInfo($ip);
 
+        if ($ipLocation !== false) {
             // 拒绝无IP请求
-            if (! $ipLocation || empty(array_filter($ipLocation))) {
+            if (empty($ipLocation) || empty(array_filter($ipLocation))) {
                 return Response::view('auth.error', ['message' => trans('errors.forbidden.access')], 403);
             }
 

+ 1 - 0
app/Models/User.php

@@ -222,6 +222,7 @@ class User extends Authenticatable
         } else {
             // $url = 'https://gravatar.loli.net/avatar/'.md5(strtolower(trim($this->username)))."?&d=identicon";
             // $url = 'https://robohash.org/'.md5(strtolower(trim($this->username))).'?set=set4&bgset=bg2&size=400x400';
+            // $url = 'https://api.dicebear.com/6.x/thumbs/svg?seed='.$this->username.'&radius=50';
             $url = 'https://api.btstu.cn/sjtx/api.php?lx=c1&format=images&method=zsy';
         }
 

+ 31 - 21
app/Utils/IP.php

@@ -8,6 +8,7 @@ use Exception;
 use GeoIp2\Database\Reader;
 use GeoIp2\Exception\AddressNotFoundException;
 use Http;
+use Illuminate\Http\Client\PendingRequest;
 use IP2Location\Database;
 use ipip\db\City;
 use Log;
@@ -23,10 +24,14 @@ class IP
         return request()?->ip();
     }
 
-    public static function getIPInfo(string $ip): ?array
+    public static function getIPInfo(string $ip): array|false|null
     {// 获取IP地址信息
         $info = Cache::tags('IP_INFO')->get($ip);
 
+        if (in_array($ip, ['::1', '127.0.0.1'], true)) {
+            return false;
+        }
+
         if ($info) {
             return $info;
         }
@@ -96,12 +101,12 @@ class IP
     { // 开发依据: https://ip-api.com/docs/api:json
         $key = config('services.ip.ip-api_key');
         if ($key) {
-            $response = Http::timeout(10)->withHeaders(['Origin' => 'https://members.ip-api.com'])->acceptJson()->get("https://pro.ip-api.com/json/$ip?fields=49881&key=$key&lang=".str_replace('_', '-', app()->getLocale()));
+            $response = self::setBasicHttp()->withHeaders(['Origin' => 'https://members.ip-api.com'])->acceptJson()->get("https://pro.ip-api.com/json/$ip?fields=49881&key=$key&lang=".str_replace('_', '-', app()->getLocale()));
             if (! $response->ok()) {
-                $response = Http::timeout(10)->acceptJson()->get("http://ip-api.com/json/$ip?fields=49881&lang=".str_replace('_', '-', app()->getLocale()));
+                $response = self::setBasicHttp()->acceptJson()->get("http://ip-api.com/json/$ip?fields=49881&lang=".str_replace('_', '-', app()->getLocale()));
             }
         } else {
-            $response = Http::timeout(10)->acceptJson()->get("http://ip-api.com/json/$ip?fields=49881&lang=".str_replace('_', '-', app()->getLocale()));
+            $response = self::setBasicHttp()->acceptJson()->get("http://ip-api.com/json/$ip?fields=49881&lang=".str_replace('_', '-', app()->getLocale()));
         }
 
         if ($response->ok()) {
@@ -126,6 +131,11 @@ class IP
         return null;
     }
 
+    private static function setBasicHttp(): PendingRequest
+    {
+        return Http::timeout(10)->withOptions(['http_errors' => false])->withUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36');
+    }
+
     private static function baiduBce(string $ip): ?array
     {
         if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
@@ -133,7 +143,7 @@ class IP
         } else {
             $url = "https://qifu-api.baidubce.com/ip/geo/v1/district?ip=$ip";
         }
-        $response = Http::timeout(10)->get($url);
+        $response = self::setBasicHttp()->get($url);
         $data = $response->json();
         if ($response->ok()) {
             if ($data['code'] === 'Success') {
@@ -158,7 +168,7 @@ class IP
 
     private static function TenAPI(string $ip): ?array
     { // 开发依据: https://docs.tenapi.cn/utility/getip.html
-        $response = Http::timeout(10)->asForm()->post('https://tenapi.cn/v2/getip', ['ip' => $ip]);
+        $response = self::setBasicHttp()->asForm()->post('https://tenapi.cn/v2/getip', ['ip' => $ip]);
         if ($response->ok()) {
             $data = $response->json();
 
@@ -181,7 +191,7 @@ class IP
         $key = config('services.ip.baidu_ak');
         if ($key) {
             // 依据 http://lbsyun.baidu.com/index.php?title=webapi/ip-api 开发
-            $response = Http::timeout(15)->get("https://api.map.baidu.com/location/ip?ak=$key&ip=$ip&coor=gcj02");
+            $response = self::setBasicHttp()->get("https://api.map.baidu.com/location/ip?ak=$key&ip=$ip&coor=gcj02");
 
             if ($response->ok()) {
                 $message = $response->json();
@@ -210,7 +220,7 @@ class IP
 
     private static function ipGeoLocation(string $ip): ?array
     { // 开发依据: https://ipgeolocation.io/documentation.html
-        $response = Http::timeout(15)->withHeaders(['Origin' => 'https://ipgeolocation.io'])
+        $response = self::setBasicHttp()->withHeaders(['Origin' => 'https://ipgeolocation.io'])
             ->get("https://api.ipgeolocation.io/ipgeo?ip=$ip&fields=country_name,state_prov,district,city,isp,latitude,longitude&lang=".config('common.language.'.app()->getLocale().'.1'));
         if ($response->ok()) {
             $data = $response->json();
@@ -255,7 +265,7 @@ class IP
 
     private static function TaoBao(string $ip): ?array
     { // 通过ip.taobao.com查询IP地址的详细信息 依据 https://ip.taobao.com/instructions 开发
-        $response = Http::timeout(15)->post("https://ip.taobao.com/outGetIpInfo?ip=$ip&accessKey=alibaba-inc");
+        $response = self::setBasicHttp()->post("https://ip.taobao.com/outGetIpInfo?ip=$ip&accessKey=alibaba-inc");
 
         if ($response->ok()) {
             $message = $response->json();
@@ -281,7 +291,7 @@ class IP
 
     private static function fkcoder(string $ip): ?array
     { // 开发依据: https://www.fkcoder.com/
-        $response = Http::timeout(15)->acceptJson()->get("https://www.fkcoder.com/ip?ip=$ip");
+        $response = self::setBasicHttp()->acceptJson()->get("https://www.fkcoder.com/ip?ip=$ip");
         if ($response->ok()) {
             $data = $response->json();
 
@@ -299,7 +309,7 @@ class IP
 
     private static function juHe(string $ip): ?array
     { // 开发依据: https://www.juhe.cn/docs/api/id/1
-        $response = Http::timeout(15)->asForm()->post('https://apis.juhe.cn/ip/Example/query.php', ['IP' => $ip]);
+        $response = self::setBasicHttp()->asForm()->post('https://apis.juhe.cn/ip/Example/query.php', ['IP' => $ip]);
         if ($response->ok()) {
             $data = $response->json();
             if ($data['resultcode'] === '200' && $data['error_code'] === 0) {
@@ -333,7 +343,7 @@ class IP
     private static function IPSB(string $ip): ?array
     { // 通过api.ip.sb查询IP地址的详细信息
         try {
-            $response = Http::withUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36')->timeout(15)->post("https://api.ip.sb/geoip/$ip");
+            $response = self::setBasicHttp()->post("https://api.ip.sb/geoip/$ip");
 
             if ($response->ok()) {
                 $data = $response->json();
@@ -357,9 +367,9 @@ class IP
     { // 开发依据: https://ipinfo.io/account/home
         $key = config('services.ip.ipinfo_token');
         if ($key) {
-            $response = Http::timeout(15)->acceptJson()->get("https://ipinfo.io/$ip?token=$key");
+            $response = self::setBasicHttp()->acceptJson()->get("https://ipinfo.io/$ip?token=$key");
         } else {
-            $response = Http::timeout(15)->acceptJson()->withHeaders(['Referer' => 'https://ipinfo.io/'])->get("https://ipinfo.io/widget/demo/$ip");
+            $response = self::setBasicHttp()->acceptJson()->withHeaders(['Referer' => 'https://ipinfo.io/'])->get("https://ipinfo.io/widget/demo/$ip");
         }
 
         if ($response->ok()) {
@@ -383,7 +393,7 @@ class IP
 
     private static function dbIP(string $ip): ?array
     { // 开发依据: https://db-ip.com/api/doc.php
-        $response = Http::timeout(15)->acceptJson()->get("https://api.db-ip.com/v2/free/$ip");
+        $response = self::setBasicHttp()->acceptJson()->get("https://api.db-ip.com/v2/free/$ip");
         if ($response->ok()) {
             $data = $response->json();
 
@@ -403,7 +413,7 @@ class IP
     { // 开发依据: https://www.ip2location.io/ip2location-documentation
         $key = config('services.ip.IP2Location_key');
         if ($key) {
-            $response = Http::timeout(15)->acceptJson()->get("https://api.ip2location.io/?key=$key&ip=$ip");
+            $response = self::setBasicHttp()->acceptJson()->get("https://api.ip2location.io/?key=$key&ip=$ip");
             if ($response->ok()) {
                 $data = $response->json();
 
@@ -426,7 +436,7 @@ class IP
     { // 开发依据: https://docs.ipdata.co/docs
         $key = config('services.ip.ipdata_key');
         if ($key) {
-            $response = Http::timeout(15)->get("https://api.ipdata.co/$ip?api-key=$key&fields=ip,city,region,country_name,latitude,longitude,asn");
+            $response = self::setBasicHttp()->get("https://api.ipdata.co/$ip?api-key=$key&fields=ip,city,region,country_name,latitude,longitude,asn");
             if ($response->ok()) {
                 $data = $response->json();
 
@@ -447,7 +457,7 @@ class IP
 
     private static function ipApiCo(string $ip): ?array
     { // 开发依据: https://ipapi.co/api/
-        $response = Http::timeout(15)->get("https://ipapi.co/$ip/json/");
+        $response = self::setBasicHttp()->get("https://ipapi.co/$ip/json/");
         if ($response->ok()) {
             $data = $response->json();
 
@@ -512,7 +522,7 @@ class IP
 
     private static function ipApiCom(string $ip): ?array
     { // 开发依据: https://docs.ipdata.co/docs
-        $response = Http::timeout(15)->get("https://ipapi.com/ip_api.php?ip=$ip");
+        $response = self::setBasicHttp()->get("https://ipapi.com/ip_api.php?ip=$ip");
         if ($response->ok()) {
             $data = $response->json();
 
@@ -557,9 +567,9 @@ class IP
     private static function userAgentInfo(string $ip): ?array
     { // 开发依据: https://ip.useragentinfo.com/api
         if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
-            $response = Http::timeout(15)->get("https://ip.useragentinfo.com/ipv6/$ip");
+            $response = self::setBasicHttp()->get("https://ip.useragentinfo.com/ipv6/$ip");
         } else {
-            $response = Http::timeout(15)->withBody("ip:$ip")->get('https://ip.useragentinfo.com/json');
+            $response = self::setBasicHttp()->withBody("ip:$ip")->get('https://ip.useragentinfo.com/json');
         }
 
         if ($response->ok()) {

+ 1 - 1
resources/lang/en/admin.php

@@ -699,7 +699,7 @@ return [
             'website_name' => 'Show on email and etc.',
             'website_security_code' => 'When not empty, must be accessed via <a href=":url" target="_blank">Secure Portal</a> to access the website',
             'website_url' => 'Main domain that this panel will be using for everything links related as default',
-            'web_api_url' => 'Example: '.config('app.url'),
+            'web_api_url' => 'For example: '.config('app.url'),
             'wechat_aid' => 'Create an app in <a href=https://work.weixin.qq.com/wework_admin/frame#apps arget=_blank>app management page</a>, get AgentId',
             'wechat_cid' => 'Get the CID in<a href=https://work.weixin.qq.com/wework_admin/frame#profile target=_blank>My Enterprise page</a>',
             'wechat_encodingAESKey' => 'App Management ⇒ Application ⇒ Set API ⇒ EncodingAESKey',

+ 2 - 0
resources/lang/en/pagination.php

@@ -1,5 +1,7 @@
 <?php
 
+declare(strict_types=1);
+
 return [
     'next' => 'Next &raquo;',
     'previous' => '&laquo; Previous',

+ 2 - 0
resources/lang/ja/pagination.php

@@ -1,5 +1,7 @@
 <?php
 
+declare(strict_types=1);
+
 return [
     'next' => '次へ &raquo;',
     'previous' => '&laquo; 前へ',

+ 1 - 1
resources/lang/ko.json

@@ -37,7 +37,7 @@
     "Password": "비밀번호",
     "Payment for #:sn has been received! Total amount: :amount.": "您成功支付了订单#:sn, 总金额为 :amount.",
     "Payment Received": "주문서 결제 성공",
-    "Payment Required": "Payment Required",
+    "Payment Required": "결제 필요",
     "Please click the button below to verify your email address.": "이메일 주소를 확인하려면 아래 버튼을 클릭하십시오.",
     "Regards": "안부",
     "Register": "회원가입",

+ 2 - 0
resources/lang/ko/pagination.php

@@ -1,5 +1,7 @@
 <?php
 
+declare(strict_types=1);
+
 return [
     'next' => '다음 &raquo;',
     'previous' => '&laquo; 이전',

+ 2 - 0
resources/lang/vi/pagination.php

@@ -1,5 +1,7 @@
 <?php
 
+declare(strict_types=1);
+
 return [
     'next' => 'Trang trước &raquo;',
     'previous' => '&laquo; Trang sau',