Authored by jie

修改redis pub 添加恶意IP&删除恶意IP

@@ -178,11 +178,19 @@ local subscribe_mal_ips=function() @@ -178,11 +178,19 @@ local subscribe_mal_ips=function()
178 if res[3] then 178 if res[3] then
179 local t=cjson.decode(res[3]) 179 local t=cjson.decode(res[3])
180 local ips=t.ips 180 local ips=t.ips
181 - local expire=(not t.expire) and 86400 or t.expire 181 + local expire=(not t.expire) and 43200 or t.expire
  182 + if t.type == "add" then
182 for ip in string.gmatch(ips,"[^',']+") do 183 for ip in string.gmatch(ips,"[^',']+") do
183 cache:set("yh:mip:" .. ip,"1",expire) 184 cache:set("yh:mip:" .. ip,"1",expire)
184 - ngx.log(ngx.INFO,"nginx subscribe mal ip:" .. tostring(ip) .. ":" .. tostring(expire)) 185 + ngx.log(ngx.INFO,"nginx subscribe add mal ip:" .. tostring(ip) .. ":" .. tostring(expire))
185 end 186 end
  187 + elseif t.type == "del" then
  188 + for ip in string.gmatch(ips,"[^',']+") do
  189 + cache:delete("yh:mip:" .. ip)
  190 + ngx.log(ngx.INFO,"nginx subscribe del mal ip:" .. tostring(ip) .. ":" .. tostring(expire))
  191 + end
  192 + end
  193 +
186 end 194 end
187 elseif err ~= "timeout" then 195 elseif err ~= "timeout" then
188 connect:close() 196 connect:close()
@@ -260,6 +268,7 @@ function limit_ip_access_conf_to_worker() @@ -260,6 +268,7 @@ function limit_ip_access_conf_to_worker()
260 if t then 268 if t then
261 local r=cjson.decode(t) 269 local r=cjson.decode(t)
262 if r then 270 if r then
  271 + r["white_method"]={"app.graphic.img","app.graphic.verify"}
263 lua_context.configs["limit_ip_access"]=r 272 lua_context.configs["limit_ip_access"]=r
264 --ngx.log(ngx.INFO,"++++++++++++++" .. cjson.encode(lua_context.configs["limit_ip_access"])) 273 --ngx.log(ngx.INFO,"++++++++++++++" .. cjson.encode(lua_context.configs["limit_ip_access"]))
265 end 274 end
@@ -3,7 +3,7 @@ local lrucache = require "resty.lrucache" @@ -3,7 +3,7 @@ local lrucache = require "resty.lrucache"
3 -- init redis twemproxy config 3 -- init redis twemproxy config
4 local redis_config1={host="127.0.0.1",port="6379",auth=nil,timeout=20,max_idle_timeout=60000,pool_size=200} 4 local redis_config1={host="127.0.0.1",port="6379",auth=nil,timeout=20,max_idle_timeout=60000,pool_size=200}
5 5
6 -local ip_limit_redis_config={host="127.0.0.1",port="6379",auth=nil,timeout=20,max_idle_timeout=60000,pool_size=100} 6 +local ip_limit_redis_config={host="redis.nginx.yohoops.org",port="6379",auth=redis9646,timeout=20,max_idle_timeout=60000,pool_size=100}
7 7
8 8
9 local redis_util=require("redisutil") 9 local redis_util=require("redisutil")
@@ -134,16 +134,22 @@ function M:mal_ip() @@ -134,16 +134,22 @@ function M:mal_ip()
134 ngx.say('{"code": 400, "msg": "params error!"}') 134 ngx.say('{"code": 400, "msg": "params error!"}')
135 ngx.exit(ngx.HTTP_OK) 135 ngx.exit(ngx.HTTP_OK)
136 end 136 end
137 - if method == 'publish' then 137 + local exists={}
  138 + if method == 'pubAdd' then
138 local t={} 139 local t={}
139 t.ips=ips 140 t.ips=ips
140 t.expire=expire 141 t.expire=expire
  142 + t.type="add"
141 redis_limit_ip:cmd("publish","mal_ips",cjson.encode(t)) 143 redis_limit_ip:cmd("publish","mal_ips",cjson.encode(t))
142 - end  
143 - local exists={} 144 + elseif method == 'pubDel' then
  145 + local t={}
  146 + t.ips=ips
  147 + t.type="del"
  148 + redis_limit_ip:cmd("publish","mal_ips",cjson.encode(t))
  149 + else
144 for ip in string.gmatch(ips,"[^',']+") do 150 for ip in string.gmatch(ips,"[^',']+") do
145 if method == 'add' then 151 if method == 'add' then
146 - local expire= (not expire) and 86400 or expire 152 + local expire= (not expire) and 43200 or expire
147 cache:set("yh:mip:" .. ip,"1",expire) 153 cache:set("yh:mip:" .. ip,"1",expire)
148 elseif method == 'del' then 154 elseif method == 'del' then
149 cache:delete("yh:mip:" .. ip) 155 cache:delete("yh:mip:" .. ip)
@@ -153,6 +159,7 @@ function M:mal_ip() @@ -153,6 +159,7 @@ function M:mal_ip()
153 exists[#exists+1]=tostring(res) 159 exists[#exists+1]=tostring(res)
154 end 160 end
155 end 161 end
  162 + end
156 local body=table.concat(exists,",") 163 local body=table.concat(exists,",")
157 ngx.say('{"code": 200, "msg": "'.. body ..'"}') 164 ngx.say('{"code": 200, "msg": "'.. body ..'"}')
158 ngx.exit(ngx.HTTP_OK) 165 ngx.exit(ngx.HTTP_OK)