]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add ip reputation setter logic
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 14 Oct 2017 14:44:46 +0000 (15:44 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 14 Oct 2017 14:44:46 +0000 (15:44 +0100)
src/plugins/lua/reputation.lua

index e8900e027b245e38571286a49261d26882501c53..aa3406a26838ae648fb264554e1e58d058b5a026 100644 (file)
@@ -175,7 +175,54 @@ end
 
 -- Used to set scores
 local function ip_reputation_idempotent(task, rule)
+  if not rule.backend.set_token then return end -- Read only backend
+  local ip = task:get_from_ip()
+
+  if not ip or not ip:is_valid() then return end
+  if lua_util.is_rspamc_or_controller(task) then return end
+
+  local cfg = rule.selector.config
+
+  local pool = task:get_mempool()
+  local asn = pool:get_variable("asn")
+  local country = pool:get_variable("country")
+  local ipnet = pool:get_variable("ipnet")
+
+  if country and cfg.asn_cc_whitelist then
+    if cfg.asn_cc_whitelist:get_key(country) then
+      return
+    end
+    if asn and cfg.asn_cc_whitelist:get_key(asn) then
+      return
+    end
+  end
+
+  local action = task:get_metric_action()
+  local token = {
+  }
+  local need_set = false
+
+  -- TODO: take metric score into consideration
+  local k = cfg.keys_map[action]
 
+  if k then
+    token[k] = 1.0
+    need_set = true
+  end
+
+  if need_set then
+    if asn then
+      rule.backend.set_token(task, rule, cfg.asn_prefix .. asn, token)
+    end
+    if country then
+      rule.backend.set_token(task, rule, cfg.country_prefix .. country, token)
+    end
+    if ipnet then
+      rule.backend.set_token(task, rule, cfg.ipnet_prefix .. ipnet, token)
+    end
+
+    rule.backend.set_token(task, rule, cfg.ip_prefix .. tostring(ip), token)
+  end
 end
 
 -- Selectors are used to extract reputation tokens