Skip to content

Commit ebdc61f

Browse files
committed
添加文件黑名单和记录蜘蛛
1 parent 07aa000 commit ebdc61f

File tree

6 files changed

+57
-2
lines changed

6 files changed

+57
-2
lines changed

config.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ local Config = {
6666
whiteTime = 600,
6767

6868
-- 用于生成token密码的key过期时间
69-
keyExpire = 600,
69+
keyExpire = 0,
7070

7171
-- 匹配url模式,可选值requestUri,uri
7272
-- 值requestUri时,url-protect目录下的正则匹配的是浏览器最初请求的地址且没有被decode,带参数的链接
@@ -80,7 +80,10 @@ local Config = {
8080
reCaptchaPage = baseDir.."html/reCatchaPage.html",
8181

8282
-- 白名单ip文件,文件内容为正则表达式。
83-
whiteIpModules = { state = "Off", ipList = baseDir.."url-protect/white_ip_list.txt" },
83+
whiteIpModules = { state = "On", ipList = baseDir.."url-protect/white_ip_list.txt" },
84+
85+
-- 黑名单ip文件,文件内容为正则表达式。
86+
blackIpModules = { state = "On", ipList = baseDir.."url-protect/black_ip_list.txt" },
8487

8588
-- 如果需要从请求头获取真实ip,此值就需要设置,如x-forwarded-for
8689
-- 当state为on时,此设置才有效

guard.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,32 @@ function Guard:ipInWhiteList(ip)
4949
end
5050
end
5151

52+
function Guard:ipInFileBlackList(ip)
53+
if _Conf.fileBlackIpModulesIsOn then
54+
self:debug("[IpInFileBlackList] fileBlackIpModules is on.",ip,"")
55+
56+
if ngx.re.match(ip, _Conf.fileBlackIpList) then --匹配黑名单列表
57+
self:debug("[ipInFileBlackList] ip "..ip.. " match black list ".._Conf.fileBlackIpList,ip,"")
58+
return true
59+
else
60+
return false
61+
end
62+
end
63+
end
64+
65+
66+
--收集不在白名单中的蜘蛛ip
67+
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"])
70+
if ngx.re.match(userAgent, spiderPattern) then
71+
local filename = _Conf.logPath.."/spider_ip.log"
72+
local file = io.open(filename, "a+")
73+
file:write(os.date('%Y-%m-%d %H:%M:%S').." IP "..ip.." UA "..userAgent.."\n")
74+
file:close()
75+
end
76+
end
77+
5278
--黑名单模块
5379
function Guard:blackListModules(ip,reqUri)
5480
local blackKey = ip.."black"

init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ _Conf = {
139139
--解析开关设置
140140
limitReqModulesIsOn = optionIsOn(Config.limitReqModules.state),
141141
whiteIpModulesIsOn = optionIsOn(Config.whiteIpModules.state),
142+
fileBlackIpModulesIsOn = optionIsOn(Config.blackIpModules.state)
142143
realIpFromHeaderIsOn = optionIsOn(Config.realIpFromHeader.state),
143144
autoEnableIsOn = optionIsOn(Config.autoEnable.state),
144145
redirectModulesIsOn = optionIsOn(Config.redirectModules.state),
@@ -151,6 +152,7 @@ _Conf = {
151152
limitUrlProtect = parseRuleFile(Config.limitReqModules.urlProtect),
152153
cookieUrlProtect = parseRuleFile(Config.cookieModules.urlProtect),
153154
whiteIpList = parseRuleFile(Config.whiteIpModules.ipList),
155+
fileBlackIpList = parseRuleFile(Config.blackIpModules.ipList),
154156

155157
--读取文件到内存
156158
captchaPage = readFile2Mem(Config.captchaPage),

runtime.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,18 @@ else
2828
if _Conf.autoEnableIsOn then
2929
ngx.timer.at(0,Guard.autoSwitch)
3030
end
31+
32+
--永久黑名单
33+
if Guard:ipInFileBlackList(ip) then
34+
ngx.exit(404)
35+
end
36+
3137

3238
--白名单模块
3339
if not Guard:ipInWhiteList(ip) then
40+
--收集不在白名单库里面的蜘蛛
41+
Guard:collectSpiderIp(ip, headers)
42+
3443
--黑名单模块
3544
Guard:blackListModules(ip,reqUri)
3645

url-protect/black_ip_list.txt

Whitespace-only changes.

url-protect/white_ip_list.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
61.135.186.*
2+
61.155.149.*
3+
61.182.137.*
4+
117.27.149.*
5+
117.34.28.*
6+
119.188.132.*
7+
119.188.14.*
8+
119.63.193.*
9+
123.125.71.*
10+
180.76.5.*
11+
180.76.6.*
12+
183.60.235.*
13+
185.10.104.*
14+
220.181.108.*
15+
222.216.190.*

0 commit comments

Comments
 (0)