"ddos challenge" style script #4

Open
luna wants to merge 20 commits from claude/ddos-protection-challenge-01CMAtrK6Dt24x9Q3v6Gz9fS into mistress
2 changed files with 39 additions and 63 deletions
Showing only changes of commit 4795879278 - Show all commits

View file

@ -10,6 +10,32 @@ package.preload['resty.string'] = function()
return require('tests.mock_resty_string') return require('tests.mock_resty_string')
end end
-- Create a mock shared dictionary
local function createMockSharedDict()
local storage = {}
return {
get = function(self, key)
local item = storage[key]
if not item then return nil end
if item.expiry and item.expiry < ngx.time() then
storage[key] = nil
return nil
end
return item.value
end,
set = function(self, key, value, exptime)
storage[key] = {
value = value,
expiry = exptime and (ngx.time() + exptime) or nil
}
return true, nil
end,
delete = function(self, key)
storage[key] = nil
end
}
end
function createNgx() function createNgx()
local ngx = { local ngx = {
status = nil status = nil
@ -53,6 +79,15 @@ function createNgx()
ngx.re.match = rex.match ngx.re.match = rex.match
ngx.re.search = rex.find ngx.re.search = rex.find
-- shared memory dictionaries for testing
ngx.shared = {
aproxy_bans = createMockSharedDict(),
aproxy_tokens = createMockSharedDict()
}
-- time function for testing
ngx.time = function() return 1000000 end
return ngx return ngx
end end

View file

@ -1,44 +1,9 @@
TestDDoSProtectionChallenge = {} TestDDoSProtectionChallenge = {}
-- Mock shared dictionary
local function createMockSharedDict()
local storage = {}
return {
get = function(self, key)
local item = storage[key]
if not item then return nil end
if item.expiry and item.expiry < ngx.time() then
storage[key] = nil
return nil
end
return item.value
end,
set = function(self, key, value, exptime)
storage[key] = {
value = value,
expiry = exptime and (ngx.time() + exptime) or nil
}
return true, nil
end,
delete = function(self, key)
storage[key] = nil
end
}
end
function TestDDoSProtectionChallenge:setup() function TestDDoSProtectionChallenge:setup()
-- Reset ngx -- Reset ngx (includes shared dicts and time function)
resetNgx() resetNgx()
-- Create mock shared dictionaries AFTER resetNgx
ngx.shared = {
aproxy_bans = createMockSharedDict(),
aproxy_tokens = createMockSharedDict()
}
-- Mock ngx.time for consistent testing
ngx.time = function() return 1000000 end
-- Add missing ngx.req functions -- Add missing ngx.req functions
ngx.req.read_body = function() end ngx.req.read_body = function() end
ngx.req.get_post_args = function() ngx.req.get_post_args = function()
@ -215,17 +180,9 @@ end
TestDDoSProtectionChallengePaths = {} TestDDoSProtectionChallengePaths = {}
function TestDDoSProtectionChallengePaths:setup() function TestDDoSProtectionChallengePaths:setup()
-- Reset ngx -- Reset ngx (includes shared dicts and time function)
resetNgx() resetNgx()
-- Create mock shared dictionaries AFTER resetNgx
ngx.shared = {
aproxy_bans = createMockSharedDict(),
aproxy_tokens = createMockSharedDict()
}
ngx.time = function() return 1000000 end
-- Add missing ngx.req functions -- Add missing ngx.req functions
ngx.req.read_body = function() end ngx.req.read_body = function() end
ngx.req.get_post_args = function() ngx.req.get_post_args = function()
@ -382,17 +339,9 @@ end
TestDDoSProtectionChallengeQuestion = {} TestDDoSProtectionChallengeQuestion = {}
function TestDDoSProtectionChallengeQuestion:setup() function TestDDoSProtectionChallengeQuestion:setup()
-- Reset ngx -- Reset ngx (includes shared dicts and time function)
resetNgx() resetNgx()
-- Create mock shared dictionaries AFTER resetNgx
ngx.shared = {
aproxy_bans = createMockSharedDict(),
aproxy_tokens = createMockSharedDict()
}
ngx.time = function() return 1000000 end
-- Add missing ngx.req functions -- Add missing ngx.req functions
ngx.req.read_body = function() end ngx.req.read_body = function() end
ngx.req.get_post_args = function() ngx.req.get_post_args = function()
@ -517,17 +466,9 @@ end
TestDDoSProtectionChallengePow = {} TestDDoSProtectionChallengePow = {}
function TestDDoSProtectionChallengePow:setup() function TestDDoSProtectionChallengePow:setup()
-- Reset ngx -- Reset ngx (includes shared dicts and time function)
resetNgx() resetNgx()
-- Create mock shared dictionaries AFTER resetNgx
ngx.shared = {
aproxy_bans = createMockSharedDict(),
aproxy_tokens = createMockSharedDict()
}
ngx.time = function() return 1000000 end
-- Add missing ngx.req functions -- Add missing ngx.req functions
ngx.req.read_body = function() end ngx.req.read_body = function() end
ngx.req.get_post_args = function() ngx.req.get_post_args = function()