diff options
author | Soha Jin <soha@lohu.info> | 2023-07-11 20:28:07 +0800 |
---|---|---|
committer | Soha Jin <soha@lohu.info> | 2023-07-11 22:44:54 +0800 |
commit | d16171495fbb469b9cf4ccd171b92f5a9f331d9b (patch) | |
tree | c9ee1cfaf25ea558100a76da5e50f5c772c9bc67 /src | |
parent | 7cca8ad4b07c3994294621fc87c138827c934e4f (diff) | |
download | rspamd-d16171495fbb469b9cf4ccd171b92f5a9f331d9b.tar.gz rspamd-d16171495fbb469b9cf4ccd171b92f5a9f331d9b.zip |
[Feature] external_relay: add ip_map strategy
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/lua/external_relay.lua | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/plugins/lua/external_relay.lua b/src/plugins/lua/external_relay.lua index 9f65490e9..9c0f06ff8 100644 --- a/src/plugins/lua/external_relay.lua +++ b/src/plugins/lua/external_relay.lua @@ -61,6 +61,12 @@ local config_schema = ts.shape{ strategy = 'hostname_map', symbol = ts.string:is_optional(), }, + ts.shape{ + ip_map = lua_maps.map_schema, + priority = ts.number:is_optional(), + strategy = 'ip_map', + symbol = ts.string:is_optional(), + }, } ), } @@ -182,6 +188,42 @@ strategies.hostname_map = function(rule) end end +strategies.ip_map = function(rule) + local ip_map = lua_maps.map_add_from_ucl(rule.ip_map, 'radix', 'external relay IPs') + if not ip_map then + rspamd_logger.errx(rspamd_config, "couldn't add map %s; won't register symbol %s", + rule.ip_map, rule.symbol) + return + end + + return function(task) + local from_ip = task:get_from_ip() + if not (from_ip and from_ip:is_valid()) then + lua_util.debugm(N, task, 'sender\'s IP is missing') + return + end + + if not ip_map:get_key(from_ip) then + lua_util.debugm(N, task, 'sender\'s ip (%s) is not a relay', from_ip) + return + end + + local rcvd_hdrs = task:get_received_headers() + local num_rcvd = #rcvd_hdrs + -- Try find sending IP in Received headers + for i, rcvd in ipairs(rcvd_hdrs) do + if rcvd.real_ip then + local rcvd_ip = rcvd.real_ip + if rcvd_ip:is_valid() and (not ip_map:get_key(rcvd_ip) or i == num_rcvd) then + return set_from_rcvd(task, rcvd) + end + end + end + + rspamd_logger.errx(task, 'found nothing useful in Received headers') + end +end + strategies['local'] = function(rule) return function(task) local from_ip = task:get_from_ip() |