File: //etc/copper.lua
-- vim: set ts=2 expandtab:
require 'apache2'
require 'os'
-- We use socket.http rather than lua-http because lua-http is completely broken on Bionic :'(
local http = require "socket.http"
http.TIMEOUT = 1
function query_access_controller(client_ip)
-- We expect this call to return either a 200 OK or 429
local req, code, headers = http.request("http://localhost:1997/access-check?client_ip=" .. client_ip)
return code, headers
end
function distributed_traffic(r)
-- Let bots do *some* botty things
if r.uri == "/robots.txt" then
return apache2.DECLINED
end
-- `success` denotes whether the call to query_access_controller() succeeded or not
-- `response` is the return code from query_access_controller() if it did succeed
local success, response, headers = pcall(query_access_controller, r.useragent_ip)
if success == false then
-- Tell Apache that we've decided to not handle the request
return apache2.DECLINED
elseif response == 429 then
-- return `Too Many Requests`
r.err_headers_out['copper-block-reason'] = headers['block-reason']
return 429
end
-- Fallback
return apache2.DECLINED
end