scw.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. require "apache2"
  2. local redis = require "redis"
  3. local regex = require "rex_pcre"
  4. local mime = require "mime"
  5. local cipher = require "openssl.cipher"
  6. -- --------------------------------------------------------------------------
  7. -- try to connect to redis
  8. --
  9. local redis_conn = nil
  10. function _decrypt(message_and_iv, key)
  11. if message_and_iv:len() < 32 then
  12. return ""
  13. end
  14. iv, _ = message_and_iv:sub(1, 16)
  15. message, _ = message_and_iv:sub(17, -1)
  16. local c = cipher.new("aes-256-cbc")
  17. c:decrypt(key, iv)
  18. cleartext, err = c:final(message)
  19. if err ~= nil then
  20. return ""
  21. end
  22. return cleartext
  23. end
  24. function scw(r)
  25. local start_time = r:clock()
  26. local has_redis = false
  27. local ignore = os.getenv("SCW_IGNORE")
  28. if ignore ~= nil and regex.match(r.uri, ignore) then
  29. return apache2.DECLINED
  30. end
  31. -- --------------------------------------------------------------------------
  32. -- Does the user have an encrypted scw cookie that proves he is human?
  33. --
  34. local cookie_name = os.getenv("SCW_COOKIE")
  35. local cookie_key = r:base64_decode(os.getenv("SCW_KEY"))
  36. local human_cookie = r:getcookie(cookie_name)
  37. if human_cookie then
  38. human_cookie = r:base64_decode(r:unescape(human_cookie))
  39. end
  40. local is_human = false
  41. if human_cookie ~= nil and cookie_key:len() == 32 then
  42. local cookie_data = _decrypt(human_cookie, cookie_key)
  43. is_human = string.gsub(cookie_data, "scw|(.-)|(%d+)$", function (ip, exp)
  44. if ip == r.useragent_ip and r:clock() <= tonumber(exp) then
  45. return true
  46. end
  47. return false
  48. end)
  49. if is_human then
  50. return apache2.DECLINED
  51. end
  52. end
  53. -- --------------------------------------------------------------------------
  54. -- check for blacklist status
  55. --
  56. if pcall(function() redis_conn:ping() end) then
  57. has_redis = true
  58. else
  59. io.stderr:write("reconnecting to redis\n")
  60. local redis_host = os.getenv("SCW_REDIS_HOST")
  61. local redis_port = os.getenv("SCW_REDIS_PORT")
  62. io.stderr:write(string.format("redis host: %s\n", redis_host))
  63. if pcall(function() redis_conn = redis.connect(redis_host, 6379) end) then
  64. has_redis = true
  65. end
  66. end
  67. local captcha_url = os.getenv("SCW_CAPTCHA_URL")
  68. if has_redis and captcha_url ~= "" then
  69. local v = redis_conn:get("bl:" .. r.useragent_ip)
  70. if v ~= nil then -- and h == nil then
  71. local rprotocol = "http"
  72. if r.is_https then
  73. rprotocol = "https"
  74. end
  75. local rport = ""
  76. if (r.is_https and r.port ~= 443) or (r.is_https ~= true and r.port ~= 80) then
  77. rport = string.format(":%d", r.port)
  78. end
  79. local referer = string.format("%s://%s%s%s", rprotocol, r.hostname, rport, r.unparsed_uri)
  80. r.headers_out["location"] = string.format(captcha_url, r.useragent_ip, r:escape(referer))
  81. --[[
  82. r.headers_in["X-SCW-IP"] = v
  83. r.handler = "proxy-server"
  84. r.proxyreq = apache2.PROXYREQ_REVERSE
  85. r.filename = string.format("proxy:http://captcha:8080/?src=%s&r=%s", r.useragent_ip, r.unparsed_uri)
  86. --]]
  87. return apache2.HTTP_MOVED_TEMPORARILY
  88. end
  89. end
  90. return apache2.DECLINED
  91. end