فهرست منبع

1.修正套了cloudflare CDN后无法正确获取用户登录IP
2.废了了通过淘宝API获取IP地址详情,改用纯真IP库

admin 7 سال پیش
والد
کامیت
2e52e6a598

+ 22 - 0
app/Components/QQWry.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Components;
+
+use itbdw\Ip\IpLocation;
+
+class QQWry
+{
+    /**
+     * 查询IP地址的详细信息
+     *
+     * @param string $ip IPv4
+     *
+     * @return array
+     */
+    public function ip($ip)
+    {
+        $filePath = storage_path('qqwry.dat');
+
+        return IpLocation::getLocation($ip, $filePath);
+    }
+}

+ 3 - 3
app/Http/Controllers/AdminController.php

@@ -210,7 +210,7 @@ class AdminController extends Controller
             $user->remark = clean($request->get('remark', ''));
             $user->level = $request->get('level', 1);
             $user->is_admin = 0;
-            $user->reg_ip = $request->getClientIp();
+            $user->reg_ip = getClientIp();
             $user->referral_uid = 1;
             $user->traffic_reset_day = 0;
             $user->status = 1;
@@ -268,7 +268,7 @@ class AdminController extends Controller
                 $user->transfer_enable = toGB(1000);
                 $user->enable_time = date('Y-m-d');
                 $user->expire_time = date('Y-m-d', strtotime("+365 days"));
-                $user->reg_ip = $request->getClientIp();
+                $user->reg_ip = getClientIp();
                 $user->referral_uid = 1;
                 $user->traffic_reset_day = 0;
                 $user->status = 1;
@@ -1212,7 +1212,7 @@ class AdminController extends Controller
                     $obj->expire_time = '2099-01-01';
                     $obj->remark = '';
                     $obj->is_admin = 0;
-                    $obj->reg_ip = $request->getClientIp();
+                    $obj->reg_ip = getClientIp();
                     $obj->created_at = date('Y-m-d H:i:s');
                     $obj->updated_at = date('Y-m-d H:i:s');
                     $obj->save();

+ 2 - 2
app/Http/Controllers/Api/LoginController.php

@@ -24,7 +24,7 @@ class LoginController extends Controller
     {
         $username = trim($request->get('username'));
         $password = trim($request->get('password'));
-        $cacheKey = 'request_times_' . md5($request->getClientIp());
+        $cacheKey = 'request_times_' . md5(getClientIp());
 
         // 连续请求失败10次,则封IP一小时
         if (Cache::has($cacheKey)) {
@@ -68,7 +68,7 @@ class LoginController extends Controller
             $subscribe->increment('times', 1);
 
             // 记录每次请求
-            $this->log($subscribe->id, $request->getClientIp(), 'API访问');
+            $this->log($subscribe->id, getClientIp(), 'API访问');
 
             // 处理用户信息
             unset($user->password, $user->remember_token);

+ 3 - 3
app/Http/Controllers/Api/YzyController.php

@@ -27,7 +27,7 @@ class YzyController extends Controller
     // 接收GET请求
     public function index(Request $request)
     {
-        \Log::info("YZY-GET:" . var_export($request->all()) . '[' . $request->getClientIp() . ']');
+        \Log::info("YZY-GET:" . var_export($request->all()) . '[' . getClientIp() . ']');
     }
 
     // 接收POST请求
@@ -38,7 +38,7 @@ class YzyController extends Controller
         $json = file_get_contents('php://input');
         $data = json_decode($json, true);
         if (!$data) {
-            Log::info('YZY-POST:回调数据无法解析,可能是非法请求[' . $request->getClientIp() . ']');
+            Log::info('YZY-POST:回调数据无法解析,可能是非法请求[' . getClientIp() . ']');
             exit();
         }
 
@@ -47,7 +47,7 @@ class YzyController extends Controller
         $sign_string = $this->systemConfig['youzan_client_id'] . "" . $msg . "" . $this->systemConfig['youzan_client_secret'];
         $sign = md5($sign_string);
         if ($sign != $data['sign']) {
-            Log::info('YZY-POST:回调数据签名错误,可能是非法请求[' . $request->getClientIp() . ']');
+            Log::info('YZY-POST:回调数据签名错误,可能是非法请求[' . getClientIp() . ']');
             exit();
         } else {
             // 返回请求成功标识给有赞

+ 14 - 13
app/Http/Controllers/LoginController.php

@@ -2,6 +2,7 @@
 
 namespace App\Http\Controllers;
 
+use App\Components\QQWry;
 use App\Http\Models\User;
 use App\Http\Models\UserLoginLog;
 use Illuminate\Http\Request;
@@ -86,7 +87,7 @@ class LoginController extends Controller
             }
 
             // 写入登录日志
-            $this->addUserLoginLog($user->id, $request->getClientIp());
+            $this->addUserLoginLog($user->id, getClientIp());
 
             // 重新取出用户信息
             $userInfo = User::query()->where('id', $user->id)->first();
@@ -134,22 +135,22 @@ class LoginController extends Controller
     // 添加用户登录日志
     private function addUserLoginLog($userId, $ip)
     {
-        // 调用淘宝IP接口查询IP信息
-        $ipInfo = $this->getIPInfo($ip);
+        // 解析IP信息
+        $qqwry = new QQWry();
+        $ipInfo = $qqwry->ip($ip);
+        if (!$ipInfo || is_array($ipInfo)) {
+            \Log::warning("获取IP地址信息异常:" . $ip);
+        }
 
         $log = new UserLoginLog();
         $log->user_id = $userId;
         $log->ip = $ip;
-        $log->country = $ipInfo->country;
-        $log->country_id = $ipInfo->country_id;
-        $log->region = $ipInfo->region;
-        $log->region_id = $ipInfo->region_id;
-        $log->city = $ipInfo->city;
-        $log->city_id = $ipInfo->city_id;
-        $log->county = $ipInfo->county;
-        $log->county_id = $ipInfo->county_id;
-        $log->isp = $ipInfo->isp;
-        $log->isp_id = $ipInfo->isp_id;
+        $log->country = $ipInfo['country'];
+        $log->province = $ipInfo['province'];
+        $log->city = $ipInfo['city'];
+        $log->county = $ipInfo['county'];
+        $log->isp = $ipInfo['isp'];
+        $log->area = $ipInfo['area'];
         $log->save();
     }
 

+ 2 - 2
app/Http/Controllers/RegisterController.php

@@ -26,7 +26,7 @@ class RegisterController extends Controller
     // 注册页
     public function index(Request $request)
     {
-        $cacheKey = 'register_times_' . md5($request->getClientIp()); // 注册限制缓存key
+        $cacheKey = 'register_times_' . md5(getClientIp()); // 注册限制缓存key
 
         if ($request->method() == 'POST') {
             $username = trim($request->get('username'));
@@ -172,7 +172,7 @@ class RegisterController extends Controller
             $user->obfs = $this->getDefaultObfs();
             $user->enable_time = date('Y-m-d H:i:s');
             $user->expire_time = date('Y-m-d H:i:s', strtotime("+" . $this->systemConfig['default_days'] . " days"));
-            $user->reg_ip = $request->getClientIp();
+            $user->reg_ip = getClientIp();
             $user->referral_uid = $referral_uid;
             $user->save();
 

+ 1 - 1
app/Http/Controllers/SubscribeController.php

@@ -42,7 +42,7 @@ class SubscribeController extends Controller
         $subscribe->increment('times', 1);
 
         // 记录每次请求
-        $this->log($subscribe->id, $request->getClientIp(), $request->headers);
+        $this->log($subscribe->id, getClientIp(), $request->headers);
 
         // 获取这个账号可用节点
         $userLabelIds = UserLabel::query()->where('user_id', $user->id)->pluck('label_id');

+ 6 - 5
app/Http/Controllers/UserController.php

@@ -984,9 +984,6 @@ class UserController extends Controller
                         Order::query()->where('oid', $vo->oid)->update(['is_expire' => 1]);
                         User::query()->where('id', $user->id)->decrement('transfer_enable', $vo->goods->traffic * 1048576);
                     }
-
-                    // 重置已用流量
-                    User::query()->where('id', $user->id)->update(['u' => 0, 'd' => 0]);
                 }
 
                 // 把商品的流量加到账号上
@@ -999,8 +996,12 @@ class UserController extends Controller
                     $expireTime = date('Y-m-d', strtotime("+" . $goods->days . " days", strtotime($user->expire_time)));
                 }
 
-                // 更新账号过期时间
-                User::query()->where('id', $user->id)->update(['expire_time' => $expireTime, 'traffic_reset_day' => 1, 'enable' => 1]);
+                // 更新账号过期时间:套餐改流量重置日,重置已用流量
+                if ($goods->type == 2) {
+                    User::query()->where('id', $order->user_id)->update(['u' => 0, 'd' => 0, 'traffic_reset_day' => 1, 'expire_time' => $expireTime, 'enable' => 1]);
+                } else {
+                    User::query()->where('id', $order->user_id)->update(['expire_time' => $expireTime, 'enable' => 1]);
+                }
 
                 // 写入用户标签
                 if ($goods->label) {

+ 1 - 1
app/Http/Middleware/Muv2.php

@@ -33,7 +33,7 @@ class Muv2
         }
 
         // 验证IP是否在节点IP列表当中
-        $ip = $request->getClientIp();
+        $ip = getClientIp();
         $node = SsNode::query()->where('ip', $ip)->orWhere('ipv6', $ip)->first();
         if (!$node && $ip != '127.0.0.1') {
             return Response::json([

+ 40 - 0
app/helpers.php

@@ -100,4 +100,44 @@ if (!function_exists('formatBytes')) {
 
         return round($bytes, $precision) . ' ' . $units[$pow];
     }
+}
+
+// 获取访客真实IP
+if (!function_exists('getClientIP')) {
+    function getClientIP()
+    {
+        /*
+         * 访问时用localhost访问的,读出来的是“::1”是正常情况
+         * ::1说明开启了IPv6支持,这是IPv6下的本地回环地址的表示
+         * 使用IPv4地址访问或者关闭IPv6支持都可以不显示这个
+         */
+        if (isset($_SERVER)) {
+            if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
+                $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
+                $ip = $_SERVER['REMOTE_ADDR'];
+            } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
+                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
+            } elseif (isset($_SERVER['HTTP_CLIENT_ip'])) {
+                $ip = $_SERVER['HTTP_CLIENT_ip'];
+            } elseif (isset($_SERVER['REMOTE_ADDR'])) {
+                $ip = $_SERVER['REMOTE_ADDR'];
+            } else {
+                $ip = 'unknown';
+            }
+        } else {
+            if (getenv('HTTP_X_FORWARDED_FOR')) {
+                $ip = getenv('HTTP_X_FORWARDED_FOR');
+            } elseif (getenv('HTTP_CLIENT_ip')) {
+                $ip = getenv('HTTP_CLIENT_ip');
+            } else {
+                $ip = getenv('REMOTE_ADDR');
+            }
+        }
+
+        if (trim($ip) == '::1') {
+            $ip = '127.0.0.1';
+        }
+
+        return $ip;
+    }
 }

+ 1 - 0
composer.json

@@ -8,6 +8,7 @@
         "php": ">=5.6.4",
         "barryvdh/laravel-ide-helper": "^2.4",
         "guzzlehttp/guzzle": "^6.3",
+        "itbdw/ip-database": "^2.0",
         "jenssegers/agent": "^2.5",
         "laravel/framework": "5.4.*",
         "laravel/tinker": "~1.0",

+ 59 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "4ccf41f17df4f7e9b6217b36d7d0a8a1",
+    "content-hash": "e9bd00a2f0ae43e05d3039d3261bc76b",
     "packages": [
         {
             "name": "barryvdh/laravel-ide-helper",
@@ -680,6 +680,64 @@
             ],
             "time": "2018-05-29T14:19:03+00:00"
         },
+        {
+            "name": "itbdw/ip-database",
+            "version": "v2.0.5",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/itbdw/ip-database.git",
+                "reference": "1b074df1e8092eb0a19abef288b8a47abdba0ae3"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/itbdw/ip-database/zipball/1b074df1e8092eb0a19abef288b8a47abdba0ae3",
+                "reference": "1b074df1e8092eb0a19abef288b8a47abdba0ae3",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "php": ">=5.3.3"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "itbdw\\Ip\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "itbdw",
+                    "email": "[email protected]",
+                    "homepage": "http://github.com/itbdw",
+                    "role": "Developer"
+                },
+                {
+                    "name": "马秉尧"
+                }
+            ],
+            "description": "IP位置数据库(解析为国家、省、市、县、运营商)",
+            "keywords": [
+                "China",
+                "Chinese",
+                "IP",
+                "database",
+                "location",
+                "位置",
+                "地区",
+                "数据库",
+                "纯真"
+            ],
+            "time": "2018-03-25T06:41:36+00:00"
+        },
         {
             "name": "jakub-onderka/php-console-color",
             "version": "0.1",

+ 8 - 2
readme.md

@@ -157,6 +157,11 @@ ln -S ssserver.log /root/shadowsocksr/ssserver.log
 chown www:www ssserver.log
 ````
 
+## IP库
+```
+本项目使用的是纯真IP库,如果需要更新IP库文件,请上纯真官网把qqwry.dat下载并覆盖至 storage/qqwrt.dat 文件
+```
+
 ## SSR(R)部署
 ###### 手动部署(基于SSRR 3.2.2,推荐)
 ````
@@ -287,6 +292,7 @@ ntpdate cn.pool.ntp.org
 - [@91yun](https://github.com/91yun)
 - [@Akkariiin](https://github.com/shadowsocksrr)
 - [@tonychanczm](https://github.com/tonychanczm)
-- [ipcheck](https://ipcheck.need.sh)
-- [check-host](https://www.check-host.net)
+- [@ipcheck](https://ipcheck.need.sh)
+- [@check-host](https://www.check-host.net)
+- [@cz88](http://www.cz88.net/index.shtml)
 

+ 27 - 0
sql/update/20180903.sql

@@ -0,0 +1,27 @@
+-- 更改用户登录IP日志表结构
+ALTER TABLE `user_login_log`
+	ALTER `region` DROP DEFAULT,
+	ALTER `isp_id` DROP DEFAULT;
+
+ALTER TABLE `user_login_log`
+	CHANGE COLUMN `region` `province` CHAR(20) NOT NULL AFTER `country`,
+	CHANGE COLUMN `isp_id` `area` CHAR(20) NOT NULL AFTER `isp`,
+	DROP COLUMN `country_id`,
+	DROP COLUMN `region_id`,
+	DROP COLUMN `city_id`,
+	DROP COLUMN `county_id`;
+
+ALTER TABLE `user_login_log`
+	ALTER `country` DROP DEFAULT,
+	ALTER `province` DROP DEFAULT,
+	ALTER `city` DROP DEFAULT,
+	ALTER `county` DROP DEFAULT,
+	ALTER `isp` DROP DEFAULT,
+	ALTER `area` DROP DEFAULT;
+ALTER TABLE `user_login_log`
+	CHANGE COLUMN `country` `country` VARCHAR(80) NOT NULL AFTER `ip`,
+	CHANGE COLUMN `province` `province` VARCHAR(80) NOT NULL AFTER `country`,
+	CHANGE COLUMN `city` `city` VARCHAR(80) NOT NULL AFTER `province`,
+	CHANGE COLUMN `county` `county` VARCHAR(80) NOT NULL AFTER `city`,
+	CHANGE COLUMN `isp` `isp` VARCHAR(50) NOT NULL AFTER `county`,
+	CHANGE COLUMN `area` `area` VARCHAR(200) NOT NULL AFTER `isp`;

BIN
storage/qqwry.dat