From 0d991770cf450946e6c831b5b5271bf93b29acad Mon Sep 17 00:00:00 2001 From: Thomas Lynch Date: Mon, 12 Sep 2022 23:37:21 +1000 Subject: [PATCH] google recaptcha v2 support --- README.MD | 2 ++ docker-compose.yml | 6 ++-- haproxy/haproxy.cfg | 4 +++ src/scripts/hcaptcha.lua | 64 +++++++++++++++++++++++++--------------- 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/README.MD b/README.MD index 05c93e7..89e66ce 100644 --- a/README.MD +++ b/README.MD @@ -28,6 +28,8 @@ Add some env vars to docker-compose file: - HCAPTCHA_SITEKEY - your hcaptcha site key - HCAPTCHA_SECRET - your hcaptcha secret key +- RECAPTCHA_SITEKEY - your recaptcha site key +- RECAPTCHA_SECRET - your recaptcha secret key - CAPTCHA_COOKIE_SECRET - random string, a salt for captcha cookies - POW_COOKIE_SECRET - different random string, a salt for pow cookies - RAY_ID - string to identify the HAProxy node by diff --git a/docker-compose.yml b/docker-compose.yml index 45c6fee..a788979 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,14 +18,16 @@ services: - ./haproxy/hosts.map:/etc/haproxy/hosts.map - ./haproxy/backends.map:/etc/haproxy/backends.map - ./haproxy/blocked.map:/etc/haproxy/blocked.map + - ./haproxy/whitelist.map:/etc/haproxy/whitelist.map + - ./haproxy/maintenance.map:/etc/haproxy/maintenance.map - ./haproxy/dataplaneapi.hcl:/etc/haproxy/dataplaneapi.hcl - ./haproxy/trace.txt:/etc/haproxy/trace.txt - ./src/scripts/:/etc/haproxy/scripts/ - ./src/libs/:/etc/haproxy/libs/ - ./haproxy/js/:/var/www/js/ environment: - - HCAPTCHA_SECRET= - - HCAPTCHA_SITEKEY= + - RECAPTCHA_SECRET=6LdOmvEhAAAAAB9zq2oSnhpUmw4wZ4uvfzixoxNY + - RECAPTCHA_SITEKEY=6LdOmvEhAAAAAJjwpaaREKW9lYJ8arpZWydWDeXg - CAPTCHA_COOKIE_SECRET=changeme - POW_COOKIE_SECRET=changeme - RAY_ID=docker diff --git a/haproxy/haproxy.cfg b/haproxy/haproxy.cfg index 521e71e..f625e9e 100644 --- a/haproxy/haproxy.cfg +++ b/haproxy/haproxy.cfg @@ -91,3 +91,7 @@ backend servers backend hcaptcha mode http server hcaptcha hcaptcha.com:443 + +backend recaptcha + mode http + server recaptcha www.google.com:443 diff --git a/src/scripts/hcaptcha.lua b/src/scripts/hcaptcha.lua index 692f3cd..a3d4304 100644 --- a/src/scripts/hcaptcha.lua +++ b/src/scripts/hcaptcha.lua @@ -6,14 +6,31 @@ local cookie = require("cookie") local json = require("json") local sha = require("sha") -local captcha_secret = os.getenv("HCAPTCHA_SECRET") -local captcha_sitekey = os.getenv("HCAPTCHA_SITEKEY") -local hcaptcha_cookie_secret = os.getenv("CAPTCHA_COOKIE_SECRET") +local captcha_secret = os.getenv("HCAPTCHA_SECRET") or os.getenv("RECAPTCHA_SECRET") +local captcha_sitekey = os.getenv("HCAPTCHA_SITEKEY") or os.getenv("RECAPTCHA_SITEKEY") +local captcha_cookie_secret = os.getenv("CAPTCHA_COOKIE_SECRET") local pow_cookie_secret = os.getenv("POW_COOKIE_SECRET") local ray_id = os.getenv("RAY_ID") -local captcha_provider_domain = "hcaptcha.com" local captcha_map = Map.new("/etc/haproxy/ddos.map", Map._str); +local captcha_provider_domain = "" +local captcha_classname = "" +local captcha_script_src = "" +local captcha_siteverify_path = "" +local captcha_backend_name = "" +if os.getenv("HCAPTCHA_SITEKEY") then + captcha_provider_domain = "hcaptcha.com" + captcha_classname = "h-captcha" + captcha_script_src = "https://hcaptcha.com/1/api.js" + captcha_siteverify_path = "/siteverify" + captcha_backend_name = "hcaptcha" +else + captcha_provider_domain = "www.google.com" + captcha_classname = "g-recaptcha" + captcha_script_src = "https://www.google.com/recaptcha/api.js" + captcha_siteverify_path = "/recaptcha/api/siteverify" + captcha_backend_name = "recaptcha" +end function _M.setup_servers() local backend_name = os.getenv("BACKEND_NAME") @@ -50,7 +67,7 @@ local body_template = [[