Skip to content

Commit 8f0c20a

Browse files
committed
完成专门针对火车头的单UA的拦截
1 parent 76e7dd1 commit 8f0c20a

File tree

5 files changed

+71
-9
lines changed

5 files changed

+71
-9
lines changed

config.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ local Config = {
55
-- key是否动态生成,可选static,dynamic,如果选dynamic,下面所有的keySecret不需要更改,如果选static,修改手动修改下面的keySecret
66
keyDefine = "dynamic",
77

8+
-- 被动防御,限制UA请求模块。根据在一定时间内统计到的单个UA请求次数作限制(专门针对火车头采集工具)
9+
-- state : 为此模块的状态,表示开启或关闭,可选值为On或Off;
10+
-- maxReqs,amongTime : 在amongTime秒内允许请求的最大次数maxReqs,如默认的是在10s内最大允许请求50次。
11+
limitUaModules = { state = "On" , maxReqs = 5 , amongTime = 5},
12+
813
-- 被动防御,限制请求模块。根据在一定时间内统计到的请求次数作限制,建议始终开启
914
-- state : 为此模块的状态,表示开启或关闭,可选值为On或Off;
1015
-- maxReqs,amongTime : 在amongTime秒内允许请求的最大次数maxReqs,如默认的是在10s内最大允许请求50次。

guard.lua

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ end
6565

6666
--收集不在白名单中的蜘蛛ip
6767
function 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)
7676
end
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
85100
end
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请求速率模块
88138
function 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)

init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ end
121121
_Conf = {
122122

123123
--引入原始设置
124+
limitUaModules = Config.limitUaModules,
124125
limitReqModules = Config.limitReqModules,
125126
redirectModules = Config.redirectModules,
126127
JsJumpModules = Config.JsJumpModules,
@@ -137,6 +138,7 @@ _Conf = {
137138
captchaKey = Config.captchaKey,
138139

139140
--解析开关设置
141+
limitUaModulesIsOn = optionIsOn(Config.limitUaModules.state),
140142
limitReqModulesIsOn = optionIsOn(Config.limitReqModules.state),
141143
whiteIpModulesIsOn = optionIsOn(Config.whiteIpModules.state),
142144
fileBlackIpModulesIsOn = optionIsOn(Config.blackIpModules.state),

runtime.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,15 @@ else
4040
Guard:collectSpiderIp(ip, headers)
4141

4242
--黑名单模块
43-
Guard:blackListModules(ip,reqUri)
43+
Guard:blackListModules(ip, reqUri, headers)
4444

45-
--限制请求速率模块
45+
--限制UA请求速率模块
46+
if _Conf.limitUaModulesIsOn then
47+
Guard:debug("[limitUaModules] limitUaModules is on.",ip,reqUri)
48+
Guard:limitUaModules(ip, reqUri, address, headers)
49+
end
50+
51+
--限制IP请求速率模块
4652
if _Conf.limitReqModulesIsOn then --limitReq模块是否开启
4753
Guard:debug("[limitReqModules] limitReqModules is on.",ip,reqUri)
4854
Guard:limitReqModules(ip,reqUri,address)

url-protect/white_ip_list.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
61.155.149.*
33
61.182.137.*
44
117.27.149.*
5-
117.34.28.*
5+
117.34.28.1.*
66
119.188.132.*
77
119.188.14.*
88
119.63.193.*
99
123.125.71.*
1010
180.76.5.*
1111
180.76.6.*
1212
183.60.235.*
13-
185.10.104.*
1413
220.181.108.*
1514
222.216.190.*

0 commit comments

Comments
 (0)