comparison mod_firewall/conditions.lib.lua @ 5704:ad5c77793750

mod_firewall: Add FROM COUNTRY condition based on GeoIP DB
author Kim Alvefur <zash@zash.se>
date Sun, 12 Nov 2023 16:37:47 +0100
parents eeccec0955a1
children 14e17927c0ec
comparison
equal deleted inserted replaced
5703:0ac4545cb4f9 5704:ad5c77793750
379 "it_count", 379 "it_count",
380 "search:"..search_name, "pattern:"..pattern_name 380 "search:"..search_name, "pattern:"..pattern_name
381 }; 381 };
382 end 382 end
383 383
384 -- FROM COUNTRY: SE
385 -- FROM COUNTRY: code=SE
386 -- FROM COUNTRY: SWE
387 -- FROM COUNTRY: code3=SWE
388 -- FROM COUNTRY: continent=EU
389 -- FROM COUNTRY? --> NOT FROM COUNTRY: -- (for unknown/invalid)
390 -- TODO list support?
391 function condition_handlers.FROM_COUNTRY(geoip_spec)
392 local condition = "==";
393 if not geoip_spec then
394 geoip_spec = "--";
395 condition = "~=";
396 end
397 local field, country = geoip_spec:match("(%w+)=(%w+)");
398 if not field then
399 if #geoip_spec == 3 then
400 field, country = "code3", geoip_spec;
401 elseif #geoip_spec == 2 then
402 field, country = "code", geoip_spec;
403 else
404 error("Unknown country code type");
405 end
406 end
407 return ("get_geoip(session.ip, %q) %s %q"):format(field:lower(), condition, country:upper()), { "geoip_country" };
408 end
409
384 return condition_handlers; 410 return condition_handlers;