mirror of
https://gitgud.io/fatchan/haproxy-protection.git
synced 2025-05-09 02:05:37 +00:00
refactor: remove ratelimiting functionality,
add on-demand global / per-domain ddos protection enabling add automatic redirect from captcha page back to the requested source prettify the captcha page
This commit is contained in:
1559
src/libs/JSON.lua
Normal file
1559
src/libs/JSON.lua
Normal file
File diff suppressed because it is too large
Load Diff
82
src/libs/cookie.lua
Normal file
82
src/libs/cookie.lua
Normal file
@ -0,0 +1,82 @@
|
||||
-- Copyright (C) 2013-2016 Jiale Zhi (calio), CloudFlare Inc.
|
||||
-- See RFC6265 http://tools.ietf.org/search/rfc6265
|
||||
-- require "luacov"
|
||||
|
||||
local type = type
|
||||
local byte = string.byte
|
||||
local sub = string.sub
|
||||
|
||||
local EQUAL = byte("=")
|
||||
local SEMICOLON = byte(";")
|
||||
local SPACE = byte(" ")
|
||||
local HTAB = byte("\t")
|
||||
|
||||
|
||||
local _M = {}
|
||||
_M._VERSION = '0.01'
|
||||
|
||||
|
||||
function _M.get_cookie_table(text_cookie)
|
||||
if type(text_cookie) ~= "string" then
|
||||
return {}
|
||||
end
|
||||
|
||||
local EXPECT_KEY = 1
|
||||
local EXPECT_VALUE = 2
|
||||
local EXPECT_SP = 3
|
||||
|
||||
local n = 0
|
||||
local len = #text_cookie
|
||||
|
||||
for i=1, len do
|
||||
if byte(text_cookie, i) == SEMICOLON then
|
||||
n = n + 1
|
||||
end
|
||||
end
|
||||
|
||||
local cookie_table = {}
|
||||
|
||||
local state = EXPECT_SP
|
||||
local i = 1
|
||||
local j = 1
|
||||
local key, value
|
||||
|
||||
while j <= len do
|
||||
if state == EXPECT_KEY then
|
||||
if byte(text_cookie, j) == EQUAL then
|
||||
key = sub(text_cookie, i, j - 1)
|
||||
state = EXPECT_VALUE
|
||||
i = j + 1
|
||||
end
|
||||
elseif state == EXPECT_VALUE then
|
||||
if byte(text_cookie, j) == SEMICOLON
|
||||
or byte(text_cookie, j) == SPACE
|
||||
or byte(text_cookie, j) == HTAB
|
||||
then
|
||||
value = sub(text_cookie, i, j - 1)
|
||||
cookie_table[key] = value
|
||||
|
||||
key, value = nil, nil
|
||||
state = EXPECT_SP
|
||||
i = j + 1
|
||||
end
|
||||
elseif state == EXPECT_SP then
|
||||
if byte(text_cookie, j) ~= SPACE
|
||||
and byte(text_cookie, j) ~= HTAB
|
||||
then
|
||||
state = EXPECT_KEY
|
||||
i = j
|
||||
j = j - 1
|
||||
end
|
||||
end
|
||||
j = j + 1
|
||||
end
|
||||
|
||||
if key ~= nil and value == nil then
|
||||
cookie_table[key] = sub(text_cookie, i)
|
||||
end
|
||||
|
||||
return cookie_table
|
||||
end
|
||||
|
||||
return _M
|
96
src/libs/print_r.lua
Normal file
96
src/libs/print_r.lua
Normal file
@ -0,0 +1,96 @@
|
||||
-- Copyright 2016 Thierry Fournier
|
||||
|
||||
function color(index, str)
|
||||
return "\x1b[" .. index .. "m" .. str .. "\x1b[00m"
|
||||
end
|
||||
|
||||
function nocolor(index, str)
|
||||
return str
|
||||
end
|
||||
|
||||
function sp(count)
|
||||
local spaces = ""
|
||||
while count > 0 do
|
||||
spaces = spaces .. " "
|
||||
count = count - 1
|
||||
end
|
||||
return spaces
|
||||
end
|
||||
|
||||
function escape(str)
|
||||
local s = ""
|
||||
for i = 1, #str do
|
||||
local c = str:sub(i,i)
|
||||
ascii = string.byte(c, 1)
|
||||
if ascii > 126 or ascii < 20 then
|
||||
s = s .. string.format("\\x%02x", ascii)
|
||||
else
|
||||
s = s .. c
|
||||
end
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
function print_rr(p, indent, c, wr, hist)
|
||||
local i = 0
|
||||
local nl = ""
|
||||
|
||||
if type(p) == "table" then
|
||||
wr(c("33", "(table)") .. " " .. c("36", tostring(p)) .. " [")
|
||||
|
||||
for idx, value in ipairs(hist) do
|
||||
if value == p then
|
||||
wr(" " .. c("35", "/* recursion */") .. " ]")
|
||||
return
|
||||
end
|
||||
end
|
||||
hist[indent + 1] = p
|
||||
|
||||
mt = getmetatable(p)
|
||||
if mt ~= nil then
|
||||
wr("\n" .. sp(indent+1) .. c("31", "METATABLE") .. ": ")
|
||||
print_rr(mt, indent+1, c, wr, hist)
|
||||
end
|
||||
|
||||
for k,v in pairs(p) do
|
||||
if i > 0 then
|
||||
nl = "\n"
|
||||
else
|
||||
wr("\n")
|
||||
end
|
||||
wr(nl .. sp(indent+1))
|
||||
if type(k) == "number" then
|
||||
wr(c("32", tostring(k)))
|
||||
else
|
||||
wr("\"" .. c("32", escape(tostring(k))) .. "\"")
|
||||
end
|
||||
wr(": ")
|
||||
print_rr(v, indent+1, c, wr, hist)
|
||||
i = i + 1
|
||||
end
|
||||
if i == 0 then
|
||||
wr(" " .. c("35", "/* empty */") .. " ]")
|
||||
else
|
||||
wr("\n" .. sp(indent) .. "]")
|
||||
end
|
||||
|
||||
hist[indent + 1] = nil
|
||||
|
||||
elseif type(p) == "string" then
|
||||
wr(c("33", "(string)") .. " \"" .. c("36", escape(p)) .. "\"")
|
||||
else
|
||||
wr(c("33", "(" .. type(p) .. ")") .. " " .. c("36", tostring(p)))
|
||||
end
|
||||
end
|
||||
|
||||
function print_r(p, col, wr)
|
||||
if col == nil then col = true end
|
||||
if wr == nil then wr = function(msg) io.stdout:write(msg) end end
|
||||
local hist = {}
|
||||
if col == true then
|
||||
print_rr(p, 0, color, wr, hist)
|
||||
else
|
||||
print_rr(p, 0, nocolor, wr, hist)
|
||||
end
|
||||
wr("\n")
|
||||
end
|
18
src/libs/utils.lua
Normal file
18
src/libs/utils.lua
Normal file
@ -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
|
||||
|
Reference in New Issue
Block a user