|
@@ -1,10 +1,8 @@
|
|
|
require "apache2"
|
|
|
--- local redis = require "redis"
|
|
|
-local hiredis = require "hiredis"
|
|
|
+local redis = require "redis"
|
|
|
local regex = require "rex_pcre"
|
|
|
local mime = require "mime"
|
|
|
-local cipher = require "openssl".cipher
|
|
|
-local pretty = require 'pl.pretty'
|
|
|
+local cipher = require "openssl.cipher"
|
|
|
|
|
|
|
|
|
-- --------------------------------------------------------------------------
|
|
@@ -40,7 +38,8 @@ function scw(r)
|
|
|
r:err("SCW_IGNORE is not set!")
|
|
|
end
|
|
|
|
|
|
- local uri = string.match(r.the_request, "^%w+%s+(.+)%s+HTTP")
|
|
|
+
|
|
|
+ uri = string.match(r.the_request, "^%w+%s+(.+)%s+HTTP")
|
|
|
if ignore ~= nil and regex.match(uri, ignore) then
|
|
|
return apache2.DECLINED
|
|
|
end
|
|
@@ -53,7 +52,6 @@ function scw(r)
|
|
|
r:err("SCW_COOKIE is not set!")
|
|
|
end
|
|
|
|
|
|
-
|
|
|
local cookie_key = r:base64_decode(os.getenv("SCW_KEY"))
|
|
|
if cookie_key == nil or cookie_key == "" then
|
|
|
r:err("SCW_KEY is not set!")
|
|
@@ -62,12 +60,12 @@ function scw(r)
|
|
|
local xff = tostring(r.headers_in["X-Forwarded-For"])
|
|
|
local rip = tostring(r.headers_in["X-Real-IP"])
|
|
|
|
|
|
+
|
|
|
if human_cookie then
|
|
|
human_cookie = r:base64_decode(r:unescape(human_cookie))
|
|
|
end
|
|
|
local is_human = false
|
|
|
|
|
|
-
|
|
|
if human_cookie ~= nil and cookie_key:len() == 32 then
|
|
|
local cookie_data = _decrypt(human_cookie, cookie_key)
|
|
|
|
|
@@ -86,20 +84,7 @@ function scw(r)
|
|
|
-- --------------------------------------------------------------------------
|
|
|
-- check for blacklist status
|
|
|
--
|
|
|
-
|
|
|
-
|
|
|
- local sucess = false
|
|
|
- local err = ""
|
|
|
-
|
|
|
- if redis_conn ~= nil then
|
|
|
- success, err = pcall(function() redis_conn:command("PING") end)
|
|
|
- if redis_conn == nil then
|
|
|
- r:info("redis_conn turned nil after ping")
|
|
|
- end
|
|
|
- end
|
|
|
-
|
|
|
- if success then
|
|
|
- r:info("redis ping was successful")
|
|
|
+ if pcall(function() redis_conn:ping() end) then
|
|
|
has_redis = true
|
|
|
else
|
|
|
-- io.stderr:write("reconnecting to redis\n")
|
|
@@ -111,10 +96,7 @@ function scw(r)
|
|
|
if redis_port == nil or redis_port == "" then
|
|
|
r:err("SCW_REDIS_PORT is not set!")
|
|
|
end
|
|
|
- r:info(string.format("connecting to redis %s:%s", redis_host, redis_port))
|
|
|
- success, err = pcall(function() redis_conn = hiredis.connect(redis_host, redis_port) end)
|
|
|
- if success then
|
|
|
- r:info("created redis connection to " .. redis_host .. ":" .. redis_port)
|
|
|
+ if pcall(function() redis_conn = redis.connect(redis_host, redis_port) end) then
|
|
|
has_redis = true
|
|
|
end
|
|
|
end
|
|
@@ -126,25 +108,19 @@ function scw(r)
|
|
|
|
|
|
if has_redis and captcha_url ~= "" then
|
|
|
-- the client ip
|
|
|
- if redis_conn == nil then
|
|
|
- r:info("redis is suddenly nil!")
|
|
|
- end
|
|
|
- local v = redis_conn:command("GET", "bl:" .. r.useragent_ip)
|
|
|
+ local v = redis_conn:get("bl:" .. r.useragent_ip)
|
|
|
|
|
|
- -- if v == nil then
|
|
|
- -- r:info("v is nil!")
|
|
|
- -- end
|
|
|
-- the X-Forwarded-For IP
|
|
|
- if v == nil and xff ~= "" then
|
|
|
- v = redis_conn:command("GET", "bl:" .. xff)
|
|
|
+ if v == nil then
|
|
|
+ v = redis_conn:get("bl:" .. xff)
|
|
|
end
|
|
|
|
|
|
-- the X-Real-IP IP
|
|
|
- if v == nil and rip ~= "" then
|
|
|
- v = redis_conn:command("GET", "bl:" .. rip)
|
|
|
+ if v == nil then
|
|
|
+ v = redis_conn:get("bl:" .. rip)
|
|
|
end
|
|
|
|
|
|
- if tostring(v) ~= "" and tostring(v) ~= "NIL" then -- and h == nil then
|
|
|
+ if v ~= nil then -- and h == nil then
|
|
|
local rprotocol = "http"
|
|
|
if r.is_https then
|
|
|
rprotocol = "https"
|
|
@@ -169,3 +145,4 @@ function scw(r)
|
|
|
|
|
|
return apache2.DECLINED
|
|
|
end
|
|
|
+
|