mirror of
https://gitgud.io/fatchan/haproxy-protection.git
synced 2025-05-09 02:05:37 +00:00
Fully convert to data plane api
Change global ACL to a map to realtime update with data plane api Change how on setartup servers are registered in lua
This commit is contained in:
@@ -56,35 +56,6 @@ else
|
||||
captcha_backend_name = "recaptcha"
|
||||
end
|
||||
|
||||
-- setup initial server backends based on hosts.map into backends.map
|
||||
function _M.setup_servers()
|
||||
if pow_difficulty < 8 then
|
||||
error("POW_DIFFICULTY must be > 8. Around 16-32 is better")
|
||||
end
|
||||
local backend_name = os.getenv("BACKEND_NAME")
|
||||
local server_prefix = os.getenv("SERVER_PREFIX")
|
||||
if backend_name == nil or server_prefix == nil then
|
||||
return;
|
||||
end
|
||||
local hosts_map = Map.new("/etc/haproxy/map/hosts.map", Map._str);
|
||||
local handle = io.open("/etc/haproxy/map/hosts.map", "r")
|
||||
local line = handle:read("*line")
|
||||
local counter = 1
|
||||
while line do
|
||||
local domain, backend_host = line:match("([^%s]+)%s+([^%s]+)")
|
||||
local port_index = backend_host:match'^.*():'
|
||||
local backend_hostname = backend_host:sub(0, port_index-1)
|
||||
local backend_port = backend_host:sub(port_index + 1)
|
||||
core.set_map("/etc/haproxy/map/backends.map", domain, server_prefix..counter)
|
||||
local proxy = core.proxies[backend_name].servers[server_prefix..counter]
|
||||
proxy:set_addr(backend_hostname, backend_port)
|
||||
proxy:set_ready()
|
||||
line = handle:read("*line")
|
||||
counter = counter + 1
|
||||
end
|
||||
handle:close()
|
||||
end
|
||||
|
||||
-- kill a tor circuit
|
||||
function _M.kill_tor_circuit(txn)
|
||||
local ip = txn.sf:src()
|
||||
|
@@ -7,4 +7,3 @@ core.register_action("captcha-check", { 'http-req', }, bot_check.check_captcha_s
|
||||
core.register_action("pow-check", { 'http-req', }, bot_check.check_pow_status)
|
||||
core.register_action("decide-checks-necessary", { 'http-req', }, bot_check.decide_checks_necessary)
|
||||
core.register_action("kill-tor-circuit", { 'http-req', }, bot_check.kill_tor_circuit)
|
||||
core.register_init(bot_check.setup_servers)
|
41
src/lua/scripts/register-servers.lua
Normal file
41
src/lua/scripts/register-servers.lua
Normal file
@@ -0,0 +1,41 @@
|
||||
package.path = package.path .. "./?.lua;/etc/haproxy/scripts/?.lua;/etc/haproxy/libs/?.lua"
|
||||
|
||||
local pow_difficulty = tonumber(os.getenv("POW_DIFFICULTY") or 18)
|
||||
|
||||
-- setup initial server backends based on hosts.map
|
||||
function setup_servers()
|
||||
if pow_difficulty < 8 then
|
||||
error("POW_DIFFICULTY must be > 8. Around 16-32 is better")
|
||||
end
|
||||
local backend_name = os.getenv("BACKEND_NAME")
|
||||
local server_prefix = os.getenv("SERVER_PREFIX")
|
||||
if backend_name == nil or server_prefix == nil then
|
||||
return;
|
||||
end
|
||||
local handle = io.open("/etc/haproxy/map/hosts.map", "r")
|
||||
local line = handle:read("*line")
|
||||
local counter = 1
|
||||
-- NOTE: using tcp socket to interact with runtime API because lua can't add servers
|
||||
local tcp = core.tcp();
|
||||
tcp:settimeout(1);
|
||||
tcp:connect("127.0.0.1", 2000); --TODO: configurable port
|
||||
while line do
|
||||
local domain, backend_host = line:match("([^%s]+)%s+([^%s]+)")
|
||||
-- local host_split = utils.split(backend_host, ":")
|
||||
-- local backend_hostname = host_split[1]
|
||||
-- local backend_port = host_split[2]
|
||||
core.set_map("/etc/haproxy/map/backends.map", domain, server_prefix..counter)
|
||||
-- local proxy = core.proxies[backend_name].servers[server_prefix..counter]
|
||||
-- proxy:set_addr(backend_hostname, backend_port)
|
||||
-- proxy:set_ready()
|
||||
local server_name = "servers/websrv"..counter
|
||||
tcp:send(string.format("add server %s %s check\n", server_name, backend_host))
|
||||
tcp:send(string.format("enable server %s\n", server_name))
|
||||
line = handle:read("*line")
|
||||
counter = counter + 1
|
||||
end
|
||||
handle:close()
|
||||
tcp:close()
|
||||
end
|
||||
|
||||
core.register_task(setup_servers)
|
@@ -13,7 +13,7 @@ _M.body = [[
|
||||
.h-captcha,.g-recaptcha{min-height:85px;display:block}
|
||||
.red{color:red;font-weight:bold}
|
||||
.left{text-align:left}
|
||||
.powstatus{color:green;font-weight:bold}
|
||||
.powstatus{color:green;font-size:small;}
|
||||
a,a:visited{color:var(--text-color)}
|
||||
body,html{height:100%%;text-align:center;}
|
||||
body{display:flex;flex-direction:column;background-color:var(--bg-color);color:var(--text-color);font-family:Helvetica,Arial,sans-serif;max-width:60em;margin:0 auto;padding: 0 20px}
|
||||
|
Reference in New Issue
Block a user