@@ -24,6 +24,13 @@ class RateLimiter
2424 public static function traffic ()
2525 {
2626 $ config = config ('plugin.tinywan.limit-traffic.app.limit ' );
27+ // 新增:判断当前请求方法是否在配置的限流方法内
28+ if (isset ($ config ['limit_method ' ]) && is_array ($ config ['limit_method ' ])) {
29+ if (!in_array (strtoupper (request ()->method ()), $ config ['limit_method ' ])) {
30+ // 当前请求方法不需要限流,直接返回 false 表示不过限
31+ return false ;
32+ }
33+ }
2734 $ scriptSha = Redis::get (self ::LIMIT_TRAFFIC_SCRIPT_SHA );
2835 if (!$ scriptSha ) {
2936 $ script = <<<luascript
@@ -41,7 +48,7 @@ public static function traffic()
4148 $ scriptSha = Redis::script ('load ' , $ script );
4249 Redis::set (self ::LIMIT_TRAFFIC_SCRIPT_SHA , $ scriptSha );
4350 }
44- $ limitKey = self ::LIMIT_TRAFFIC_PRE . request ()-> getRealIp ( );
51+ $ limitKey = self ::getLimitKey ( $ config );
4552 $ result = Redis::rawCommand ('evalsha ' , $ scriptSha , 1 , $ limitKey , $ config ['limit ' ], $ config ['window_time ' ]);
4653 if ($ result === 0 ) {
4754 return [
@@ -64,4 +71,35 @@ public static function getRateLimit(): array
6471 $ config = config ('plugin.tinywan.limit-traffic.app.limit ' );
6572 return [$ config ['limit ' ], $ config ['window_time ' ]];
6673 }
74+
75+ /**
76+ * @desc: 获取限流key
77+ * @author kylin987
78+ */
79+ private static function getLimitKey ($ config ): string
80+ {
81+ $ limitKey = $ config ['limit_key ' ] ?? '' ;
82+ // 默认按 IP 限流
83+ if (empty ($ limitKey ) || $ limitKey === 'none ' ) {
84+ return self ::LIMIT_TRAFFIC_PRE . md5 (request ()->getRealIp ());
85+ }
86+ // 按 "来源.字段名" 拆分
87+ [$ source , $ key ] = explode ('. ' , $ limitKey . '. ' , 2 );
88+
89+ switch (strtolower ($ source )) {
90+ case 'get ' :
91+ $ keyValue = request ()->get ($ key , 'none ' );
92+ break ;
93+ case 'post ' :
94+ $ keyValue = request ()->post ($ key , 'none ' );
95+ break ;
96+ case 'header ' :
97+ $ keyValue = request ()->header ($ key , 'none ' );
98+ break ;
99+ default :
100+ // fallback:IP
101+ $ keyValue = request ()->getRealIp ();
102+ }
103+ return self ::LIMIT_TRAFFIC_PRE . md5 ($ keyValue );
104+ }
67105}
0 commit comments