6565
6666-- 收集不在白名单中的蜘蛛ip
6767function Guard :collectSpiderIp (ip , headers )
68- spiderPattern = " baiduspider|360spider|sogou web spider|sogou inst spider|mediapartners|adsbot-google|googlebot"
69- userAgent = string.lower (headers [" user-agent" ])
68+ local spiderPattern = " baiduspider|360spider|sogou web spider|sogou inst spider|mediapartners|adsbot-google|googlebot"
69+ local userAgent = string.lower (headers [" user-agent" ])
7070 if ngx .re .match (userAgent , spiderPattern ) then
7171 local filename = _Conf .logPath .. " /spider_ip.log"
7272 local file = io.open (filename , " a+" )
@@ -76,15 +76,65 @@ function Guard:collectSpiderIp(ip, headers)
7676end
7777
7878-- 黑名单模块
79- function Guard :blackListModules (ip ,reqUri )
79+ function Guard :blackListModules (ip , reqUri , headers )
8080 local blackKey = ip .. " black"
8181 if _Conf .dict :get (blackKey ) then -- 判断ip是否存在黑名单字典
8282 self :debug (" [blackListModules] ip " .. ip .. " in blacklist" ,ip ,reqUri )
8383 self :takeAction (ip ,reqUri ) -- 存在则执行相应动作
84- end
84+ end
85+
86+ if _Conf .limitUaModulesIsOn then
87+ local userAgent = headers [" user-agent" ]
88+ -- 不存在UA直接抛验证码
89+ if not userAgent then
90+ self :debug (" [limitUaModules] ip " .. ip .. " not have ua" , ip , reqUri )
91+ self :takeAction (ip ,reqUri ) -- 存在则执行相应动作
92+ end
93+
94+ local blackUaKey = uaMd5 .. ' BlackUAKey'
95+ if _Conf .dict :get (blackUaKey ) then -- 判断ua是否存在黑名单字典
96+ self :debug (" [blackListModules] ip " .. ip .. " in ua blacklist" .. " " .. userAgent , ip , reqUri )
97+ self :takeAction (ip ,reqUri ) -- 存在则执行相应动作
98+ end
99+ end
85100end
86101
87- -- 限制请求速率模块
102+ -- 限制UA请求速率模块
103+ function Guard :limitUaModules (ip , reqUri , address , headers )
104+ local userAgent = headers [" user-agent" ]
105+ -- 不存在UA直接抛验证码
106+ if not userAgent then
107+ self :debug (" [limitUaModules] ip " .. ip .. " not have ua" , ip , reqUri )
108+ self :takeAction (ip ,reqUri ) -- 存在则执行相应动作
109+ end
110+
111+ local uaMd5 = ngx .md5 (userAgent )
112+ local blackUaKey = uaMd5 .. ' BlackUAKey'
113+ local limitUaKey = uaMd5 .. ' LimitUaKey'
114+ local uaTimes = _Conf .dict :get (limitUaKey ) -- 获取此ua请求的次数
115+
116+ -- 增加一次请求记录
117+ if uaTimes then
118+ _Conf .dict :incr (limitUaKey , 1 )
119+ else
120+ _Conf .dict :set (limitUaKey , 1 , _Conf .limitUaModules .amongTime )
121+ uaTimes = 0
122+ end
123+
124+ local newUaTimes = uaTimes + 1
125+ self :debug (" [limitUaModules] newUaTimes " .. newUaTimes .. " " .. userAgent , ip , reqUri )
126+
127+ -- 判断请求数是否大于阀值,大于则添加黑名单
128+ if newUaTimes > _Conf .limitUaModules .maxReqs then -- 判断是否请求数大于阀值
129+ self :debug (" [limitUaModules] ip " .. ip .. " request exceed " .. _Conf .limitUaModules .maxReqs .. " " .. userAgent , ip , reqUri )
130+ _Conf .dict :set (blackUaKey , 0 , _Conf .blockTime ) -- 添加此ip到黑名单
131+ self :log (" [limitUaModules] IP " .. ip .. " visit " .. newReqTimes .. " times,block it. " .. userAgent )
132+ end
133+
134+ end
135+
136+
137+ -- 限制IP请求速率模块
88138function Guard :limitReqModules (ip ,reqUri ,address )
89139 if ngx .re .match (address ,_Conf .limitUrlProtect ," i" ) then
90140 self :debug (" [limitReqModules] address " .. address .. " match reg " .. _Conf .limitUrlProtect ,ip ,reqUri )
0 commit comments