diff --git a/haproxy/Dockerfile b/haproxy/Dockerfile index 46197e5..dad6069 100644 --- a/haproxy/Dockerfile +++ b/haproxy/Dockerfile @@ -113,7 +113,8 @@ RUN wget "https://luarocks.org/releases/luarocks-3.3.1.tar.gz" &&\ RUN /usr/local/bin/luarocks install luasocket &&\ /usr/local/bin/luarocks install luasec &&\ - /usr/local/bin/luarocks install net-url + /usr/local/bin/luarocks install net-url &&\ + /usr/local/bin/luarocks install md5 ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] diff --git a/scripts/hcaptcha.lua b/scripts/hcaptcha.lua index 90076fe..7dcb501 100644 --- a/scripts/hcaptcha.lua +++ b/scripts/hcaptcha.lua @@ -3,6 +3,9 @@ hcaptcha = {} local url = require("net.url") local https = require("ssl.https") local json = require("json") +local utils = require("utils") + +local floating_hash = utils.get_floating_hash() function hcaptcha.view(applet) local hcaptcha_secret = os.getenv("HCAPTCHA_SECRET") @@ -34,7 +37,7 @@ function hcaptcha.view(applet) if api_response.success == true then print("HCAPTCHA SUCCESSFULLY PASSED") - print("... success captcha flow goes here ...") + applet:add_header("set-cookie", string.format("z_ddos_protection=%s; Max-Age=14400", floating_hash)) else print("HCAPTCHA FAILED", body) end diff --git a/scripts/utils.lua b/scripts/utils.lua new file mode 100644 index 0000000..e70268a --- /dev/null +++ b/scripts/utils.lua @@ -0,0 +1,18 @@ +local _M = {} +local md5 = require("md5") + +function _M.get_hostname() + local f = io.popen ("/bin/hostname") + local hostname = f:read("*a") or "" + f:close() + hostname =string.gsub(hostname, "\n$", "") + return hostname +end + +function _M.get_floating_hash() + -- This ensures that a cookie is rotated every day + return md5.sumhexa(_M.get_hostname() .. os.date("%d")) +end + +return _M +