Mercurial > prosody-modules
comparison mod_firewall/conditions.lib.lua @ 6502:b15601203d0d
mod_firewall: Support 'FROM IP' and 'FROM SUBNET' conditions
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 26 Mar 2026 13:33:44 +0000 |
| parents | cc665f343690 |
| children | f61564e11d3b |
comparison
equal
deleted
inserted
replaced
| 6501:f7158c0e2202 | 6502:b15601203d0d |
|---|---|
| 1 --luacheck: globals meta idsafe | 1 --luacheck: globals meta idsafe |
| 2 local condition_handlers = {}; | 2 local condition_handlers = {}; |
| 3 | 3 |
| 4 local jid = require "util.jid"; | 4 local jid = require "util.jid"; |
| 5 local iplib = require "util.ip"; | |
| 6 local sha256 = require "util.hashes".sha256; | |
| 5 local unpack = table.unpack or unpack; | 7 local unpack = table.unpack or unpack; |
| 6 | 8 |
| 7 -- Helper to convert user-input strings (yes/true//no/false) to a bool | 9 -- Helper to convert user-input strings (yes/true//no/false) to a bool |
| 8 local function string_to_boolean(s) | 10 local function string_to_boolean(s) |
| 9 s = s:lower(); | 11 s = s:lower(); |
| 379 "it_count", | 381 "it_count", |
| 380 "search:"..search_name, "pattern:"..pattern_name | 382 "search:"..search_name, "pattern:"..pattern_name |
| 381 }; | 383 }; |
| 382 end | 384 end |
| 383 | 385 |
| 386 function condition_handlers.FROM_IP(ip_spec) | |
| 387 local parsed_ip, err = iplib.new_ip(ip_spec); | |
| 388 if not parsed_ip then | |
| 389 error("Cannot parse IP address: "..(err or "unknown error")); | |
| 390 end | |
| 391 local normal_ip = tostring(parsed_ip); | |
| 392 return ("session.ip == %q"):format(normal_ip); | |
| 393 end | |
| 394 | |
| 395 function condition_handlers.FROM_SUBNET(subnet_spec) | |
| 396 local subnet, bits = iplib.parse_cidr(subnet_spec); | |
| 397 if not subnet then | |
| 398 error("Cannot parse subnet CIDR"); | |
| 399 end | |
| 400 local spec_hash = sha256(subnet_spec, true):sub(1, 8); | |
| 401 return ("iplib.match(session_parsed_ip, parsed_ip_%s, %d)"):format( | |
| 402 spec_hash, bits | |
| 403 ), { | |
| 404 "iplib", "session_parsed_ip", "parsed_ip:"..spec_hash..":"..tostring(subnet) | |
| 405 }; | |
| 406 end | |
| 407 | |
| 384 -- FROM COUNTRY: SE | 408 -- FROM COUNTRY: SE |
| 385 -- FROM COUNTRY: code=SE | 409 -- FROM COUNTRY: code=SE |
| 386 -- FROM COUNTRY: SWE | 410 -- FROM COUNTRY: SWE |
| 387 -- FROM COUNTRY: code3=SWE | 411 -- FROM COUNTRY: code3=SWE |
| 388 -- FROM COUNTRY: continent=EU | 412 -- FROM COUNTRY: continent=EU |
