diff --git a/app/Console/Commands/TaskHourly.php b/app/Console/Commands/TaskHourly.php index fe3abf28..01be872a 100644 --- a/app/Console/Commands/TaskHourly.php +++ b/app/Console/Commands/TaskHourly.php @@ -33,7 +33,7 @@ class TaskHourly extends Command $end = strtotime($created_at); $start = $end - 3599; $data_anomaly_notification = sysConfig('data_anomaly_notification'); - $traffic_ban_value = sysConfig('traffic_ban_value') * GB; + $traffic_ban_value = (int) sysConfig('traffic_ban_value') * GB; User::activeUser()->with('dataFlowLogs')->WhereHas('dataFlowLogs')->whereHas('dataFlowLogs', function (Builder $query) use ($start, $end) { $query->whereBetween('log_time', [$start, $end]); })->chunk(config('tasks.chunk'), function ($users) use ($traffic_ban_value, $start, $end, $created_at, $data_anomaly_notification) { diff --git a/app/Http/Middleware/TrustProxies.php b/app/Http/Middleware/TrustProxies.php index 3391630e..6d2a91f6 100644 --- a/app/Http/Middleware/TrustProxies.php +++ b/app/Http/Middleware/TrustProxies.php @@ -12,7 +12,9 @@ class TrustProxies extends Middleware * * @var array|string|null */ - protected $proxies; + protected $proxies = [ + '173.245.48.0/20', '103.21.244.0/22', '103.22.200.0/22', '103.31.4.0/22', '141.101.64.0/18', '108.162.192.0/18', '190.93.240.0/20', '188.114.96.0/20', '197.234.240.0/22', '198.41.128.0/17', '162.158.0.0/15', '104.16.0.0/13', '104.24.0.0/14', '172.64.0.0/13', '131.0.72.0/22', '2400:cb00::/32', '2606:4700::/32', '2803:f800::/32', '2405:b500::/32', '2405:8100::/32', '2a06:98c0::/29', '2c0f:f248::/32', + ]; // 以上为 https://www.cloudflare.com/ips-v4 https://www.cloudflare.com/ips-v6 获取的IP 用于在CloudFlare 开启后正确获取用户真实IP /** * The headers that should be used to detect proxies. diff --git a/app/Models/User.php b/app/Models/User.php index e5f2e339..e3420687 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -306,7 +306,7 @@ class User extends Authenticatable public function isTrafficWarning(): bool { // 流量异常警告 - return (sysConfig('traffic_ban_value') * GB) <= $this->recentTrafficUsed(); + return ((int) sysConfig('traffic_ban_value') * GB) <= $this->recentTrafficUsed(); } public function recentTrafficUsed() diff --git a/app/Models/UserHourlyDataFlow.php b/app/Models/UserHourlyDataFlow.php index c945151a..df5e85bf 100644 --- a/app/Models/UserHourlyDataFlow.php +++ b/app/Models/UserHourlyDataFlow.php @@ -47,7 +47,7 @@ class UserHourlyDataFlow extends Model ->selectRaw('user_id, sum(total) as totalTraffic')->pluck('totalTraffic', 'user_id') ->toArray(); // 只统计50M以上的记录,加快速度 foreach ($userTotalTrafficList as $user => $traffic) { - if ($traffic > sysConfig('traffic_ban_value') * GB) { + if ($traffic > (int) sysConfig('traffic_ban_value') * GB) { $result[] = $user; } } diff --git a/app/Utils/CurrencyExchange.php b/app/Utils/CurrencyExchange.php index d52e1638..ffb5d0c1 100644 --- a/app/Utils/CurrencyExchange.php +++ b/app/Utils/CurrencyExchange.php @@ -17,7 +17,7 @@ class CurrencyExchange public static function convert(string $target, float|int $amount, string $base = 'default'): ?float { if ($base === 'default') { - $base = sysConfig('standard_currency', 'CNY'); + $base = sysConfig('standard_currency'); } $cacheKey = "Currency_{$base}_{$target}_ExRate"; $isStored = Cache::has($cacheKey); diff --git a/app/Utils/Helpers.php b/app/Utils/Helpers.php index be0705f4..6d503d19 100644 --- a/app/Utils/Helpers.php +++ b/app/Utils/Helpers.php @@ -245,7 +245,7 @@ class Helpers { $ipLocation = IP::getIPInfo($ip); - if (empty($ipLocation) || empty($ipLocation['country'])) { + if (empty($ipLocation)) { Log::warning(trans('errors.get_ip').':'.$ip); } @@ -255,8 +255,8 @@ class Helpers $log->country = $ipLocation['country'] ?? ''; $log->province = $ipLocation['region'] ?? ''; $log->city = $ipLocation['city'] ?? ''; - $log->county = $ipLocation['county'] ?? ''; - $log->isp = $ipLocation['isp'] ?? ($ipLocation['organization'] ?? ''); + $log->county = ''; + $log->isp = $ipLocation['isp'] ?? ''; $log->area = $ipLocation['area'] ?? ''; $log->save(); @@ -266,7 +266,7 @@ class Helpers public static function getPriceTag(int|float $amount): string { // Get price with money symbol in the user's preferred currency. $currentCurrency = session('currency'); - $standard = sysConfig('standard_currency', 'CNY'); + $standard = sysConfig('standard_currency'); $currencyLib = array_column(config('common.currency'), 'symbol', 'code'); if (! empty($currentCurrency) && isset($currencyLib[$currentCurrency]) && $currentCurrency !== $standard) { $convert = CurrencyExchange::convert($currentCurrency, $amount); diff --git a/app/Utils/IP.php b/app/Utils/IP.php index 0c5a0310..2ca75a1a 100644 --- a/app/Utils/IP.php +++ b/app/Utils/IP.php @@ -3,6 +3,7 @@ namespace App\Utils; use Arr; +use Cache; use Exception; use GeoIp2\Database\Reader; use GeoIp2\Exception\AddressNotFoundException; @@ -24,8 +25,15 @@ class IP public static function getIPInfo(string $ip): ?array {// 获取IP地址信息 + $info = Cache::tags('IP_INFO')->get($ip); + + if ($info) { + return $info; + } + $ret = null; $source = 0; + if (app()->getLocale() === 'zh_CN') { if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { // 中文ipv4 while ($source <= 5 && ($ret === null || (is_array($ret) && empty(array_filter($ret))))) { @@ -77,7 +85,8 @@ class IP } if ($ret !== null) { - $ret['address'] = implode(' ', Arr::except(array_filter($ret), ['isp'])); + $ret['address'] = implode(' ', Arr::except(array_filter($ret), ['isp', 'latitude', 'longitude'])); + Cache::tags('IP_INFO')->put($ip, $ret, Day); // Store information for reduce API Calls } return $ret; @@ -244,34 +253,6 @@ class IP return null; } - 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"); - } else { - $response = Http::timeout(15)->withBody("ip:$ip")->get('https://ip.useragentinfo.com/json'); - } - - if ($response->ok()) { - $data = $response->json(); - if ($data['code'] === 200 && $data['ip'] === $ip) { - return [ - 'country' => $data['country'], - 'region' => $data['province'], - 'city' => $data['city'], - 'isp' => $data['isp'], - 'area' => $data['area'], - ]; - } - - Log::error('【userAgentInfo】IP查询失败:'.$data ?? ''); - } else { - Log::error('【userAgentInfo】查询无效:'.$ip); - } - - return null; - } - 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"); @@ -572,4 +553,32 @@ class IP return Arr::only($ret, ['latitude', 'longitude']); } + + 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"); + } else { + $response = Http::timeout(15)->withBody("ip:$ip")->get('https://ip.useragentinfo.com/json'); + } + + if ($response->ok()) { + $data = $response->json(); + if ($data['code'] === 200 && $data['ip'] === $ip) { + return [ + 'country' => $data['country'], + 'region' => $data['province'], + 'city' => $data['city'], + 'isp' => $data['isp'], + 'area' => $data['area'], + ]; + } + + Log::error('【userAgentInfo】IP查询失败:'.$data ?? ''); + } else { + Log::error('【userAgentInfo】查询无效:'.$ip); + } + + return null; + } } diff --git a/resources/views/user/index.blade.php b/resources/views/user/index.blade.php index 24ab1bda..f331a40c 100644 --- a/resources/views/user/index.blade.php +++ b/resources/views/user/index.blade.php @@ -125,7 +125,7 @@
  • - {{trans('user.attribute.address')}}:{{$userLoginLog->area ?: $userLoginLog->country.' '.$userLoginLog->province.' '.$userLoginLog->city}} + {{trans('user.attribute.address')}}:{{$userLoginLog->country.' '.$userLoginLog->province.' '.$userLoginLog->city.' '.$userLoginLog->area}}